-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.driver.php
270 lines (218 loc) · 9 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
<?php
require_once(TOOLKIT . "/class.sectionmanager.php");
Class extension_dynamic_section_editor extends Extension{
protected $_section;
public function about(){
return array(
'name' => 'Dynamic section editor',
'version' => '1.0',
'release-date' => '2011-10-01',
'author' => array(
'name' => 'Huib Keemink',
'website' => 'http://www.creativedutchmen.com',
'email' => '[email protected]'
)
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/backend/',
'delegate' => 'AdminPagePostGenerate',
'callback' => 'doMagic'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'FieldPostEdit',
'callback' => 'saveFieldPrefs'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'FieldPostCreate',
'callback' => 'saveFieldPrefs'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPreRender',
'callback' => 'addFilterClass'
),
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'addScriptsAndStyles'
),
);
}
public function doMagic(&$context){
$callback = $context['parent']->getPageCallback();
if ($callback['driver'] == 'blueprintssections' && in_array($callback['context'][0], array('edit', 'new'))){
$context['parent']->Page->addStylesheetToHead(URL . '/extensions/dynamic_section_editor/assets/dse_filter.css');
$sectionManager = new SectionManager($context['parent']);
$this->_section = $sectionManager->fetch($callback['context'][1]);
$this->addPreferences($context);
if(is_object($this->_section)){
$this->addFieldOptions($context);
}
}
}
public function addPreferences(&$context){
$dom = @DOMDocument::loadHTML($context['output']);
$xpath = new DOMXPath($dom);
$meta = $xpath->query("//input[@name='meta[hidden]']")->item(0);
$label = $dom->createElement('label');
$hidden = $dom->createElement('input');
$hidden->setAttribute('type', 'hidden');
$hidden->setAttribute('value', 'no');
$hidden->setAttribute('name', 'meta[dse_dynamic]');
$meta->parentNode->parentNode->appendChild($hidden);
$input = $dom->createElement('input');
$input->setAttribute('type', 'checkbox');
$input->setAttribute('value', 'yes');
$input->setAttribute('name', 'meta[dse_dynamic]');
if(is_object($this->_section)){
if($this->_section->get('dse_dynamic') == 'yes'){
$input->setAttribute('checked', 'yes');
}
}
$label->appendChild($input);
$label->appendChild(new DomText(__(' Make this a dynamic section. This will enable you to hide/show fields based on the value of another field.')));
$meta->parentNode->parentNode->appendChild($label);
$context['output'] = $dom->saveHTML();
}
public function addFieldOptions(&$context){
$dom = DOMDocument::loadHTML($context['output']);
$xpath = new DOMXPath($dom);
$domFields = $xpath->query("//li[substring-before(@class, '-') = 'field']");
$order = 0;
foreach($domFields as $domField){
$field = null;
foreach($this->_section->fetchFields() as $sym_field){
if($sym_field->get('sortorder') == $order){
$field = $sym_field;
}
}
$div = $dom->createElement('div');
$div->setAttribute('class', 'dse_settings');
$label = $dom->createElement('label');
$label->setAttribute('class', 'meta dse_dynamic');
$hidden = $dom->createElement('input');
$hidden->setAttribute('type', 'hidden');
$hidden->setAttribute('value', 'no');
$hidden->setAttribute('name', 'options_' . $field->get('id') . '[dse_dynamic]');
$domField->appendChild($hidden);
$input = $dom->createElement('input');
$input->setAttribute('type', 'checkbox');
$input->setAttribute('value', 'yes');
$input->setAttribute('class', 'dse_watch');
$input->setAttribute('name', 'options_' . $field->get('id') . '[dse_dynamic]');
if($field->get('dse_dynamic') == 'yes'){
$input->setAttribute('checked','true');
}
$label->appendChild($input);
$label->appendChild(new DomText(__(' Show/hide this field based on the value of another field.')));
$domField->appendChild($label);
$label = $dom->createElement('label');
$label->setAttribute('class', 'meta dse_hide');
$select = $dom->createElement('select');
$select->setAttribute('name','options_' . $field->get('id') . '[dse_show]');
$select->setAttribute('class','dse_options_show');
$option = $dom->createElement('option');
$option->setAttribute('value','yes');
$option->appendChild(new DomText(__('show')));
if($field->get('dse_show') != 'no'){
$option->setAttribute('selected','yes');
}
$select->appendChild($option);
$option = $dom->createElement('option');
$option->setAttribute('value','no');
if($field->get('dse_show') == 'no'){
$option->setAttribute('selected','yes');
}
$option->appendChild(new DomText(__('hide')));
$select->appendChild($option);
$label->appendChild($select);
$div->appendChild($label);
$text = $dom->createElement("p");
$text->appendChild(new DomText(__(' if ')));
$div->appendChild($text);
$label = $dom->createElement('label');
$label->setAttribute('class', 'meta dse_link');
$select = $dom->createElement('select');
$select->setAttribute('class','dse_options_link');
$select->setAttribute('name','options_' . $field->get('id') . '[dse_link_id]');
foreach($this->_section->fetchFields() as $sym_field){
//we do not want to hide the field based on its own value. That would be.. wrong.
if($sym_field->get('sortorder') != $order){
$option = $dom->createElement('option');
$option->setAttribute('value', $sym_field->get('id'));
if($sym_field->get('id') == $field->get('dse_link_id')){
$option->setAttribute('selected', 'yes');
}
$option->appendChild(new DomText($sym_field->get('label')));
$select->appendChild($option);
}
}
$label->appendChild($select);
$div->appendChild($label);
$text = $dom->createElement("p");
$text->appendChild(new DomText(__(' is ')));
$div->appendChild($text);
$label = $dom->createElement('label');
$label->setAttribute('class', 'meta dse_filter_value');
$input = $dom->createElement('input');
$input->setAttribute('name','options_' . $field->get('id') . '[dse_filter_value]');
$input->setAttribute('value', $field->get('dse_filter_value'));
$label->appendChild($input);
$div->appendChild($label);
$domField->appendChild($div);
$context['output'] = $dom->saveHTML();
$order++;
}
}
public function saveFieldPrefs(&$context){
$field = $context['field'];
$id = $field->get('id');
if(isset($_POST['options_' . $id])){
Symphony::Database()->update($_POST['options_' . $id], 'tbl_fields', " `id` = $id");
}
}
public function addFilterClass(){
}
public function addScriptsAndStyles(&$context){
$sectionManager = new SectionManager($context['parent']);
$callback = $context['parent']->getPageCallback();
if ($callback['driver'] == 'blueprintssections' && in_array($callback['context'][0], array('edit', 'new'))){
$context['parent']->Page->addStylesheetToHead(URL . '/extensions/dynamic_section_editor/assets/dse_filter.css');
$context['parent']->Page->addScriptToHead(URL . '/extensions/dynamic_section_editor/assets/dse.js', 222);
$context['parent']->Page->addScriptToHead(URL . '/extensions/dynamic_section_editor/assets/dse_dev.js', 222);
}
$section = $sectionManager->fetch($sectionManager->fetchIDFromHandle($callback['context']['section_handle']));
if(is_object($section)){
if($section->get('dse_dynamic')){
$context['parent']->Page->addScriptToHead(URL . '/extensions/dynamic_section_editor/assets/dse.js', 222);
$context['parent']->Page->addScriptToHead(URL . '/extensions/dynamic_section_editor/assets/dse_publish.js', 222);
}
}
}
public function install(){
if(!Symphony::Database()->query("ALTER TABLE `tbl_sections` ADD `dse_dynamic` ENUM( 'yes', 'no' ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no'")) return false;
if(!Symphony::Database()->query(
"ALTER TABLE `tbl_fields` ADD `dse_dynamic` ENUM( 'yes', 'no' ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
ADD `dse_link_id` INT( 11 ) UNSIGNED NULL ,
ADD `dse_show` ENUM( 'yes', 'no' ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL ,
ADD `dse_filter_value` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL"
)) return false;
return true;
}
public function uninstall(){
if(!Symphony::Database()->query("ALTER TABLE `tbl_sections` DROP `dse_dynamic`")) return false;
if(!Symphony::Database()->query(
"ALTER TABLE `tbl_fields`
DROP `dse_dynamic` ,
DROP `dse_link_id` ,
DROP `dse_show` ,
DROP `dse_filter_value`"
)) return false;
return true;
}
}