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