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

ElementList::WidgetControllers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'ListDescription' => 'HTMLText'
13
    );
14
15
    private static $has_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
        'Elements' => 'BaseElement'
17
    );
18
19
    private static $duplicate_relations = array(
0 ignored issues
show
Unused Code introduced by
The property $duplicate_relations is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
        'Elements'
21
    );
22
23
    private static $extensions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
        'ElementPublishChildren'
25
    );
26
27
    private static $title = "Element List Element";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
    private static $description = "Orderable list of elements";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $description is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    protected $enable_title_in_template = true;
32
33
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
34
    {
35
        $elements = $this->Elements();
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...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
                    $list = $allowed;
49
                } else {
50
                    $classes = ClassInfo::subclassesFor('BaseElement');
51
                    $list = array();
52
                    unset($classes['BaseElement']);
53
54
                    foreach ($classes as $class) {
0 ignored issues
show
Bug introduced by
The expression $classes of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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