Completed
Push — master ( 59c799...149ad0 )
by Will
02:15
created

code/models/ElementList.php (1 issue)

undocumented call capabilities.

Bug Documentation Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * A list contains nested {@link BaseElement} such as a list of related files.
5
 *
6
 * @package elemental
7
 */
8
class ElementList extends BaseElement
9
{
10
11
    private static $db = array(
12
        'ListDescription' => 'HTMLText'
13
    );
14
15
    private static $has_many = array(
16
        'Elements' => 'BaseElement'
17
    );
18
19
    private static $duplicate_relations = array(
20
        'Elements'
21
    );
22
23
    private static $extensions = array(
24
        'ElementPublishChildren'
25
    );
26
27
    private static $title = "Element List Element";
28
29
    private static $description = "Orderable list of elements";
30
31
    protected $enable_title_in_template = true;
32
33
    public function getCMSFields()
34
    {
35
        $elements = $this->Elements();
36
        $isInDb = $this->isInDB();
37
        $allowed = $this->config()->get('allowed_elements');
38
39
        $this->beforeUpdateCMSFields(function ($fields) use ($elements, $isInDb, $allowed) {
40
            $desc = HTMLEditorField::create('ListDescription', 'List Description');
41
            $desc->setRightTitle('Optional');
42
            $fields->addFieldToTab('Root.Main', $desc);
43
44
            if ($isInDb) {
45
                $adder = new GridFieldAddNewMultiClass();
46
47 View Code Duplication
                if (is_array($allowed)) {
48
                    $list = $allowed;
49
                } else {
50
                    $classes = ClassInfo::subclassesFor('BaseElement');
51
                    $list = array();
52
                    unset($classes['BaseElement']);
53
54
                    foreach ($classes as $class) {
55
                        $list[$class] = singleton($class)->i18n_singular_name();
56
                    }
57
                }
58
59
                asort($list);
60
61
                $adder->setClasses($list);
62
63
                $config = GridFieldConfig_RecordEditor::create(100);
64
                $config->addComponent(new GridFieldSortableRows('Sort'));
65
                $config->removeComponentsByType('GridFieldAddNewButton');
66
                $config->addComponent($adder);
67
68
                $config->removeComponentsByType('GridFieldDetailForm');
69
                $config->addComponent(new VersionedDataObjectDetailsForm());
70
71
                $widgetArea = new GridField(
72
                    'Elements',
73
                    Config::inst()->get("ElementPageExtension", 'elements_title'),
74
                    $elements,
75
                    $config
76
                );
77
78
                $fields->addFieldToTab('Root.Main', $widgetArea);
79
            } else {
80
                $fields->addFieldToTab('Root.Main', LiteralField::create('warn', '<p class="message notice">Once you save this object you will be able to add items</p>'));
81
            }
82
83
            $fields->removeByName('Root.Elements');
84
        });
85
86
        return parent::getCMSFields();
87
    }
88
89
    /**
90
     * Used in template instead of {@link Widgets()} to wrap each widget in its
91
     * controller, making it easier to access and process form logic and
92
     * actions stored in {@link WidgetController}.
93
     *
94
     * @return SS_List - Collection of {@link WidgetController} instances.
95
     */
96
    public function WidgetControllers() {
97
        $controllers = new ArrayList();
98
99
        foreach($this->Elements()->filter('Enabled', 1) as $widget) {
0 ignored issues
show
Documentation Bug introduced by
The method Elements does not exist on object<ElementList>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
100
            $controller = $widget->getController();
101
102
            $controller->init();
103
            $controllers->push($controller);
104
        }
105
106
        return $controllers;
107
    }
108
}
109