Completed
Push — master ( f85faa...6214aa )
by Jason
10s
created

AccordionBlock::canCreate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 15.5468

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 2
cts 8
cp 0.25
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 4
nop 1
crap 15.5468
1
<?php
2
3
class AccordionBlock extends Block
4
{
5
    /**
6
     * @var string
7
     */
8
    private static $singular_name = 'Accordion Block';
9
10
    /**
11
     * @var string
12
     */
13
    private static $plural_name = 'Accordion Blocks';
14
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
19
        'Content' => 'HTMLText',
20
        'SortOrder' => 'Int',
21
    );
22
23
    /**
24
     * @var array
25
     */
26
    private static $has_many = array(
27
        'Panels' => 'AccordionPanel',
28
    );
29
30
    /**
31
     * @return FieldList
32
     */
33 1
    public function getCMSFields()
34
    {
35 1
        $fields = parent::getCMSFields();
36
37 1
        $fields->removeByName(array(
38 1
            'SortOrder',
39 1
            'Panels',
40 1
        ));
41
42 1
        $config = GridFieldConfig_RecordEditor::create();
43 1
        if (class_exists('GridFieldOrderableRows')) {
44 1
            $config->addComponent(new GridFieldOrderableRows());
45 1
        }
46 1
        $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
47 1
        $config->removeComponentsByType('GridFieldDeleteAction');
48 1
        $config->addComponent(new GridFieldDeleteAction(false));
49
50 1
        if ($this->ID) {
51 1
            $fields->addFieldsToTab('Root.Panels', array(
52 1
                GridField::create('Panels', 'Accordion Panels', $this->Panels()->sort('Sort'), $config),
0 ignored issues
show
Documentation Bug introduced by
The method Panels does not exist on object<AccordionBlock>? 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...
53 1
            ));
54 1
        }
55
56 1
        return $fields;
57
    }
58
59
    /**
60
     * @param null $member
61
     * @return bool|null
62
     */
63 1
    public function canCreate($member = null)
64
    {
65
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
66 1
            $member = Member::currentUserID();
67
        }
68
69
        // Standard mechanism for accepting permission changes from extensions
70
        $extended = $this->extendedCan('canCreate', $member);
71
        if ($extended !== null) {
72
            return $extended;
73
        }
74
75
        return parent::canCreate($member);
76
    }
77
78
    /**
79
     * @param null $member
80
     * @return bool|null
81
     */
82 1
    public function canView($member = null)
83
    {
84
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
85 1
            $member = Member::currentUserID();
86
        }
87
88
        // Standard mechanism for accepting permission changes from extensions
89
        $extended = $this->extendedCan('canView', $member);
90
        if ($extended !== null) {
91
            return $extended;
92
        }
93
94
        return parent::canView($member);
95
    }
96
}