1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\ElementalSwitchTabs\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
7
|
|
|
use DNADesign\Elemental\Controllers\ElementalAreaController; |
8
|
|
|
use SilverStripe\Forms\LiteralField; |
9
|
|
|
use SilverStripe\Core\Extension; |
10
|
|
|
use SilverStripe\Control\Controller; |
11
|
|
|
use SilverStripe\CMS\Controllers\CMSPageEditController; |
12
|
|
|
use SilverStripe\Forms\DropdownField; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class \Sunnysideup\ElementalSwitchTabs\Extensions\ElementalSwitchTabsExtension |
16
|
|
|
* |
17
|
|
|
* @property BaseElement|ElementalSwitchTabsExtension $owner |
18
|
|
|
*/ |
19
|
|
|
class ElementalSwitchTabsExtension extends Extension |
20
|
|
|
{ |
21
|
|
|
private static $show_change_type = true; |
22
|
|
|
|
23
|
|
|
public function updateCMSFields(FieldList $fields) |
24
|
|
|
{ |
25
|
|
|
$owner = $this->getOwner(); |
26
|
|
|
$controller = Controller::curr(); |
27
|
|
|
if (($controller && $controller instanceof ElementalAreaController)) { |
28
|
|
|
$fields->addFieldsToTab( |
29
|
|
|
'Root.Main', |
30
|
|
|
[ |
31
|
|
|
LiteralField::create( |
32
|
|
|
'AllSettings', |
33
|
|
|
'<a |
34
|
|
|
href="' . $owner->MyCMSEditLink() . '" |
35
|
|
|
style="float: right; display: block; width: auto;" |
36
|
|
|
>Edit All Settings</a>' |
37
|
|
|
), |
38
|
|
|
], |
39
|
|
|
'Title' |
40
|
|
|
); |
41
|
|
|
} elseif ($controller && ! ($controller instanceof CMSPageEditController)) { |
42
|
|
|
$page = $owner->getPage(); |
43
|
|
|
$pageTitle = 'Page not found'; |
44
|
|
|
if ($page) { |
45
|
|
|
$pageTitle = $page->MenuTitle; |
46
|
|
|
} |
47
|
|
|
$fields->addFieldsToTab( |
48
|
|
|
'Root.Main', |
49
|
|
|
[ |
50
|
|
|
LiteralField::create( |
51
|
|
|
'AllSettings', |
52
|
|
|
'<a |
53
|
|
|
href="' . $owner->CMSEditLink(false) . '" |
54
|
|
|
style="text-align: right; display: block; padding-bottom: 20px;" |
55
|
|
|
>Edit on the "' . $pageTitle . '" page</a>' |
56
|
|
|
), |
57
|
|
|
], |
58
|
|
|
'Title' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
if ($owner->Config()->get('show_change_type')) { |
62
|
|
|
$this->addChangeTypeField($fields); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function addChangeTypeField(FieldList $fields) |
67
|
|
|
{ |
68
|
|
|
$fields->addFieldsToTab( |
69
|
|
|
'Root.Settings', |
70
|
|
|
[ |
71
|
|
|
DropdownField::create( |
72
|
|
|
'ClassName', |
73
|
|
|
'Change type of block', |
74
|
|
|
$this->getClassDropdown() |
75
|
|
|
) |
76
|
|
|
->setDescription('Use with care! Changing the type of block can lead to loss of data for this block.'), |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
|
83
|
|
|
protected function getClassDropdown(): array |
84
|
|
|
{ |
85
|
|
|
$owner = $this->getOwner(); |
86
|
|
|
$page = $owner->getPage(); |
87
|
|
|
if ($page) { |
88
|
|
|
$list = $page->getElementalTypes(); |
89
|
|
|
if (isset($list[$owner->ClassName])) { |
90
|
|
|
$list[$owner->ClassName] = $list[$owner->ClassName] . ' (current type)'; |
91
|
|
|
} else { |
92
|
|
|
$list[$owner->ClassName] = $owner->singular_name() . ' (current type) - ERROR!'; |
93
|
|
|
} |
94
|
|
|
return $list; |
95
|
|
|
} |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getLinksField(string $nameOfTab, string $label) |
100
|
|
|
{ |
101
|
|
|
return LiteralField::create( |
102
|
|
|
'LinkToLink' . $nameOfTab, |
103
|
|
|
'<a href="#" onclick="' . $this->getJsFoTabSwitch($nameOfTab) . '">' . $label . '</a>' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return BaseElement|null |
109
|
|
|
*/ |
110
|
|
|
public function PreviousBlock() |
111
|
|
|
{ |
112
|
|
|
$owner = $this->getOwner(); |
113
|
|
|
if ($owner->exists()) { |
114
|
|
|
$parent = $owner->Parent(); |
115
|
|
|
if ($parent) { |
116
|
|
|
return $parent->Elements() |
117
|
|
|
->filter(['Sort:LessThanOrEqual' => $owner->Sort]) |
118
|
|
|
->exclude(['ID' => $owner->ID]) |
119
|
|
|
->sort(['Sort' => 'ASC']) |
120
|
|
|
->last() |
121
|
|
|
; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function MyCMSEditLink(): string |
128
|
|
|
{ |
129
|
|
|
$owner = $this->getOwner(); |
130
|
|
|
return (string) $owner->CMSEditLink(true); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return BaseElement|null |
135
|
|
|
*/ |
136
|
|
|
public function NextBlock() |
137
|
|
|
{ |
138
|
|
|
$owner = $this->getOwner(); |
139
|
|
|
if ($owner->exists()) { |
140
|
|
|
$parent = $owner->Parent(); |
141
|
|
|
if ($parent) { |
142
|
|
|
return $parent->Elements() |
143
|
|
|
->filter(['Sort:GreaterThanOrEqual' => $owner->Sort]) |
144
|
|
|
->exclude(['ID' => $owner->ID]) |
145
|
|
|
->sort(['Sort' => 'ASC']) |
146
|
|
|
->first() |
147
|
|
|
; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function getJsFoTabSwitch(string $nameOfTab): string |
154
|
|
|
{ |
155
|
|
|
return <<<js |
156
|
|
|
if(jQuery(this).closest('div.element-editor__element').length > 0) { |
157
|
|
|
jQuery(this) |
158
|
|
|
.closest('div.element-editor__element') |
159
|
|
|
.find('button[name=\\'{$nameOfTab}\\']') |
160
|
|
|
.click(); |
161
|
|
|
} else { |
162
|
|
|
jQuery('li[aria-controls=\\'Root_{$nameOfTab}\\'] a').click(); |
163
|
|
|
} |
164
|
|
|
return false; |
165
|
|
|
js; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|