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) { |
|
|
|
|
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(); |
|
|
|
|
74
|
|
|
} elseif ($modelObj->ParentID) { |
|
|
|
|
75
|
|
|
$parent = $modelObj->Parent(); |
|
|
|
|
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
|
|
|
|