forked from knupska/static_section
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextension.driver.php
309 lines (250 loc) · 8.49 KB
/
extension.driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
require_once(TOOLKIT . '/class.entrymanager.php');
require_once(TOOLKIT . '/class.sectionmanager.php');
Class extension_static_section extends Extension{
private $_callback;
private $_static;
private $_section;
private $_limit = 1;
private $_count = 0;
public function __construct($args){
$this->_callback = Administration::instance()->getPageCallback();
$this->_section = $this->getSection();
$this->_static = $this->isStaticSection();
$this->_limit = $this->getSectionLimit();
$this->_count = $this->getNumberOfEntriesInSection();
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'initaliseAdminPageHead'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'AddSectionElements',
'callback' => 'addSectionSettings'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'SectionPreCreate',
'callback' => 'saveSectionSettings'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'SectionPreEdit',
'callback' => 'saveSectionSettings'
),
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreGenerate',
'callback' => 'adminPagePreGenerate'
)
);
}
/*-------------------------------------------------------------------------
Delegates
-------------------------------------------------------------------------*/
public function initaliseAdminPageHead($context) {
$this->redirectRules($context);
$this->addJavascriptFile();
}
public function addSectionSettings($context) {
// Get current setting
$setting = array();
if($context['meta']['static'] == 'yes') {
$setting = array('checked' => 'checked');
}
$limit_value = $context['meta']['static_limit'] ? $context['meta']['static_limit'] : '1';
// Prepare setting UI
$label = new XMLElement('label');
$checkbox = Widget::Input('meta[static]',
NULL,
'checkbox',
array_merge($setting, array('value' => 'yes')));
$textbox = Widget::Input('meta[static_limit]', $limit_value, 'text', array('style'=>'width:30px'));
$br = new XMLElement('br');
$br->setSelfClosingTag(true);
$label->appendChild($checkbox);
$label->appendChild(new XMLElement('span', ' ' . __('Make this section static (i.e. a single entry section)')));
$label->appendChild($br);
$label->appendChild(new XMLElement('span', __('You can set a maximum number of entries too')));
$label->appendChild($textbox);
// Find context
$children = $context['form']->getChildren();
foreach( $children as $element ) {
// Only work with Fieldset, improves compatibility with other extensions
if($element->getName() !== 'fieldset') continue;
$group = $element->getChildren();
$column = $group[1]->getChildren();
// Append setting
$column[0]->appendChild($label);
break;
}
}
public function saveSectionSettings($context) {
if(!$context['meta']['static']) {
$context['meta']['static'] = 'no';
}
if (!$context['meta']['static_limit']) {
$context['meta']['static_limit'] = 1;
}
}
public function adminPagePreGenerate($context){
if ( $this->_static && $this->isLimitReached()) {
// add a css class in order to get the js do its job
Administration::instance()->Page->Html->addClass('no-new');
}
}
/*-------------------------------------------------------------------------
Public Helpers
-------------------------------------------------------------------------*/
/**
*
* Method that returns <code>true</code> if we currently are in
* a static section in the Backend and if this section is static; <code>false</code> otherwise
* @return boolean
*/
public function isStaticSection(){
if( $this->_section != null && $this->_callback['driver'] == 'publish' && is_array($this->_callback['context']) ) {
return ($this->_section->get('static') == 'yes');
}
return false;
}
/**
*
* Method that returns true if the maximum number of entries is reach
* @return boolean
*/
public function isLimitReached() {
return $this->_count >= $this->_limit;
}
/**
*
* Method that returns the actual limit
* @return int
*/
public function getLimit() {
return $this->_limit;
}
/*-------------------------------------------------------------------------
Private Helpers
-------------------------------------------------------------------------*/
public function redirectRules($context){
if ($this->_static && $this->_limit == 1) {
// we must redirect than
$section_handle = $this->_section->get('handle');
$entry = $this->getLastPosition();
$params = $this->getConcatenatedParams();
// no entry found... redirect to new
if ($this->_callback['context']['entry_id'] != $entry || $this->_callback['context']['page'] == 'index'){
redirect(SYMPHONY_URL . "/publish/{$section_handle}/edit/{$entry}/{$params}");
}
if (!$entry && $this->_callback['context']['page'] != 'new'){
redirect(SYMPHONY_URL . "/publish/{$section_handle}/new/{$params}");
}
// some entries are there redirect to edit page
if ($this->_callback['context']['entry_id'] != $entry || $this->_callback['context']['page'] == 'index'){
redirect(SYMPHONY_URL . "/publish/{$section_handle}/edit/{$entry}/{$params}");
}
}
}
private function addJavascriptFile() {
Administration::instance()->Page->addScriptToHead(
URL . '/extensions/static_section/assets/publish.static_section.js',
time(),
false
);
}
private function hideCreateNewButton($xmlRoot) {
$children = $xmlRoot->getChildren();
foreach ($children as $child) {
var_dump($child->getName() . ' ' . $child->getNumberOfChildren());
if ($child->getName() == 'a' && $child->getAttribute('class') == 'create button') {
$child->addClass('invisible');
} else if ($child->getNumberOfChildren() > 0) {
$this->hideCreateNewButton($child);
}
}
}
private function getSection(){
$sm = new SectionManager($this->_Parent);
$section_id = $sm->fetchIDFromHandle($this->_callback['context']['section_handle']);
$section = $sm->fetch($section_id);
if( is_object($section) && $section instanceof Section ){
return $section;
}
return null;
}
private function getLastPosition(){
$em = new EntryManager($this->_Parent);
$em->setFetchSortingDirection('DESC');
$entry = $em->fetch(NULL, $this->_section->get('id'), 1);
if (is_array($entry) && !empty($entry)){
$entry = end($entry);
return $entry->get('id');
}
}
private function getConcatenatedParams(){
if (count($_GET) > 2) {
$params = "?";
}
foreach($_GET as $key => $value){
if (in_array($key, array('symphony-page', 'mode'))) continue;
$params .= "{$key}={$value}";
if (next($_GET)) {
$params .= '&';
}
}
return $params;
}
private function isInSection() {
return $this->_callback['driver'] == 'publish' && is_array($this->_callback['context']);
}
private function getSectionLimit() {
if ($this->isInSection()){
return $this->_section->get('static_limit');
}
return -1; // no section found...
}
private function getNumberOfEntriesInSection() {
if ($this->isInSection()){
$em = new EntryManager($this->_Parent);
return $em->fetchCount($this->_section->get('id'));
}
return -1; // no entries
}
/*-------------------------------------------------------------------------
Installation
-------------------------------------------------------------------------*/
public function install(){
return $this->install_Pre1_7_0() && $this->install_1_7_0();
}
private function install_Pre1_7_0() {
return Symphony::Database()->query("
ALTER TABLE `tbl_sections`
ADD `static` enum('yes','no') NOT NULL DEFAULT 'no' AFTER `hidden`
");
}
private function install_1_7_0() {
return Symphony::Database()->query("
ALTER TABLE `tbl_sections`
ADD `static_limit` int(11) NOT NULL DEFAULT 1 AFTER `static`
");
}
public function update($previousVersion) {
if (version_compare($previousVersion, '1.7.0', '<')) {
// install new column that appear in the 1.7.0 version
return $this->install_1_7_0();
}
return true;
}
public function uninstall(){
return Symphony::Database()->query("
ALTER TABLE `tbl_sections`
DROP `static`,
DROP `static_limit`
");
}
}