WidgetAreaEditorExtended::AvailableWidgets()   D
last analyzed

Complexity

Conditions 9
Paths 76

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 4.909
c 0
b 0
f 0
cc 9
eloc 16
nc 76
nop 0
1
<?php
2
/**
3
 * Get more out of your WidgetAreaEditor
4
 * @package cms
5
 * @subpackage content
6
 * @author nicolaas [at] sunnysideup.co.nz
7
 */
8
class WidgetAreaEditorExtended extends WidgetAreaEditor
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
    protected $availableWidgets = array();
11
12
    protected $blockedWidgets = array();
13
14
    public function addToAvailableWidgets($array)
15
    {
16
        if (!is_array($array)) {
17
            user_error("first and only argument for WidgetAreaEditor::addToAvailableWidgets must be an array", E_USER_ERROR);
18
        }
19
        $this->availableWidgets = $array;
20
    }
21
22
    public function blockFromAvailableWidgets($array)
23
    {
24
        if (!is_array($array)) {
25
            user_error("first and only argument for WidgetAreaEditor::blockFromAvailableWidgets must be an array", E_USER_ERROR);
26
        }
27
        $this->blockedWidgets = $array;
28
    }
29
30
    public function AvailableWidgets()
31
    {
32
        $classes = ClassInfo::subclassesFor('Widget');
33
        array_shift($classes);
34
        $widgets= new ArrayList();
35
        $hasSpecificallyAddedWidgets = count($this->availableWidgets) && is_array($this->availableWidgets);
36
        $hasSpecificallyBlockedWidgets = count($this->blockedWidgets) && is_array($this->blockedWidgets);
37
38
        foreach ($classes as $class) {
39
            if ($hasSpecificallyAddedWidgets) {
40
                if (!in_array($class, $this->availableWidgets)) {
41
                    $class = '';
42
                }
43
            }
44
            if ($hasSpecificallyBlockedWidgets) {
45
                if (in_array($class, $this->blockedWidgets)) {
46
                    $class = '';
47
                }
48
            }
49
            if ($class) {
50
                $widgets->push(singleton($class));
51
            }
52
        }
53
54
        return $widgets;
55
    }
56
}
57