Passed
Push — fix-3519 ( f12fa6 )
by Sam
07:39
created

GridFieldLevelup::getHTMLFragments()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 8
nop 1
dl 0
loc 42
rs 8.9137
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\GridField;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\FieldType\DBField;
8
use SilverStripe\ORM\Hierarchy\Hierarchy;
9
use SilverStripe\View\ArrayData;
10
use SilverStripe\View\HTML;
11
use SilverStripe\View\SSViewer;
12
13
/**
14
 * Adds a "level up" link to a GridField table, which is useful when viewing
15
 * hierarchical data. Requires the managed record to have a "getParent()"
16
 * method or has_one relationship called "Parent".
17
 */
18
class GridFieldLevelup implements GridField_HTMLProvider
19
{
20
    use Injectable;
21
22
    /**
23
     * @var integer - the record id of the level up to
24
     */
25
    protected $currentID = null;
26
27
    /**
28
     * sprintf() spec for link to link to parent.
29
     * Only supports one variable replacement - the parent ID.
30
     * @var string
31
     */
32
    protected $linkSpec = '';
33
34
    /**
35
     * @var array Extra attributes for the link
36
     */
37
    protected $attributes = array();
38
39
    /**
40
     *
41
     * @param integer $currentID - The ID of the current item; this button will find that item's parent
42
     */
43
    public function __construct($currentID)
44
    {
45
        if ($currentID && is_numeric($currentID)) {
46
            $this->currentID = $currentID;
47
        }
48
    }
49
50
    /**
51
     * @param GridField $gridField
52
     * @return array|null
53
     */
54
    public function getHTMLFragments($gridField)
55
    {
56
        $modelClass = $gridField->getModelClass();
57
        $parentID = 0;
58
59
        if (!$this->currentID) {
60
            return null;
61
        }
62
63
        /** @var DataObject|Hierarchy $modelObj */
64
        $modelObj = DataObject::get_by_id($modelClass, $this->currentID);
65
        if (!$modelObj) {
0 ignored issues
show
introduced by
$modelObj is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
66
            throw new \LogicException(
67
                "Can't find object of class $modelClass ID #{$this->currentID} for GridFieldLevelup"
68
            );
69
        }
70
71
        $parent = null;
72
        if ($modelObj->hasMethod('getParent')) {
73
            $parent = $modelObj->getParent();
0 ignored issues
show
Bug introduced by
The method getParent() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            /** @scrutinizer ignore-call */ 
74
            $parent = $modelObj->getParent();
Loading history...
74
        } elseif ($modelObj->ParentID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
            $parent = $modelObj->Parent();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            /** @scrutinizer ignore-call */ 
76
            $parent = $modelObj->Parent();
Loading history...
76
        }
77
78
        if ($parent) {
79
            $parentID = $parent->ID;
80
        }
81
82
        // Attributes
83
        $attrs = array_merge($this->attributes, array(
84
            'href' => sprintf($this->linkSpec, $parentID),
85
            'class' => 'cms-panel-link ss-ui-button font-icon-level-up no-text grid-levelup'
86
        ));
87
        $linkTag = HTML::createTag('a', $attrs);
88
89
        $forTemplate = new ArrayData(array(
90
            'UpLink' => DBField::create_field('HTMLFragment', $linkTag)
91
        ));
92
93
        $template = SSViewer::get_templates_by_class($this, '', __CLASS__);
94
            return array(
95
            'before' => $forTemplate->renderWith($template),
96
            );
97
    }
98
99
    public function setAttributes($attrs)
100
    {
101
        $this->attributes = $attrs;
102
        return $this;
103
    }
104
105
    public function getAttributes()
106
    {
107
        return $this->attributes;
108
    }
109
110
    public function setLinkSpec($link)
111
    {
112
        $this->linkSpec = $link;
113
        return $this;
114
    }
115
116
    public function getLinkSpec()
117
    {
118
        return $this->linkSpec;
119
    }
120
}
121