Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 590 lines (483 sloc) 20.363 kb
430749b @brockpetrie First commit
authored
1 <?php
2
395451f @brendo Add Importable/Exportable Field support
brendo authored
3 /**
4 * @package dynamictextgroup
5 */
6
7 /**
8 * This field provides a method to dynamically add a text field or text field groups to a section entry
9 */
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
10
430749b @brockpetrie First commit
authored
11 if(!defined('__IN_SYMPHONY__')) die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
12
13 require_once(EXTENSIONS . '/dynamictextgroup/lib/class.textgroup.php');
395451f @brendo Add Importable/Exportable Field support
brendo authored
14 require_once FACE . '/interface.exportablefield.php';
15 require_once FACE . '/interface.importablefield.php';
430749b @brockpetrie First commit
authored
16
395451f @brendo Add Importable/Exportable Field support
brendo authored
17 Class fielddynamictextgroup extends Field implements ImportableField, ExportableField {
430749b @brockpetrie First commit
authored
18
19 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#__construct * * */
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
20 function __construct() {
77074e0 work started on v3
Brock Petrie authored
21 parent::__construct();
430749b @brockpetrie First commit
authored
22 $this->_name = __('Dynamic Text Group');
23 $this->_required = true;
24 $this->set('required', 'no');
25 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
26
27
430749b @brockpetrie First commit
authored
28 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#canFilter * * */
29 function canFilter() {
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
30 return true;
430749b @brockpetrie First commit
authored
31 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
32
33
430749b @brockpetrie First commit
authored
34 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#isSortable * * */
35 function isSortable() {
36 return true;
37 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
38
430749b @brockpetrie First commit
authored
39 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#canPrePopulate * * */
40 function canPrePopulate() {
41 return false;
42 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
43
44
430749b @brockpetrie First commit
authored
45 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#allowDatasourceOutputGrouping * * */
46 function allowDatasourceOutputGrouping() {
47 return false;
48 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
49
50
430749b @brockpetrie First commit
authored
51 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#allowDatasourceParamOutput * * */
52 function allowDatasourceParamOutput() {
53 return false;
54 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
55
56
430749b @brockpetrie First commit
authored
57 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#displaySettingsPanel * * */
58 function displaySettingsPanel(&$wrapper, $errors=NULL) {
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
59
430749b @brockpetrie First commit
authored
60 // Initialize field settings based on class defaults (name, placement)
61 parent::displaySettingsPanel($wrapper, $errors);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
62
524af7d @brockpetrie Output working
authored
63 // Field Editor
64 if ($this->get('id')) {
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
65 Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/jquery-ui-1.10.3.custom.min.js', 101, false);
634b4c0 @brockpetrie 2.0dev2: See change log
authored
66 Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/json2.js', 102, false);
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
67 Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.fieldeditor.js', 103, false);
68 Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.fieldeditor.css', 'screen', 104, false);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
69
5359745 @brendo Support for multiple DTG fields in one section
brendo authored
70 $tblocks = '<input type="hidden" class="dtg-fieldschema" name="fields['.$this->get('sortorder').'][schema]" value=\''.$this->get('schema').'\' />';
ed0cf96 @brockpetrie 3.0 beta 1
authored
71 //$tblocks .= '<span>'.$this->get('schema').'</span>';
5359745 @brendo Support for multiple DTG fields in one section
brendo authored
72 $tblocks .= '<input type="hidden" class="dtg-addfields" name="fields['.$this->get('sortorder').'][addfields]" value="" />';
73 $tblocks .= '<input type="hidden" class="dtg-delfields" name="fields['.$this->get('sortorder').'][delfields]" value="" />';
74 $tblocks .= '<input type="hidden" class="dtg-renfields" name="fields['.$this->get('sortorder').'][renfields]" value="" />';
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
75
5359745 @brendo Support for multiple DTG fields in one section
brendo authored
76 $fieldset = new XMLElement('fieldset', '<legend>Field Editor</legend><div class="dtg-stageholder frame empty"><ol class="dtg-stage"></ol><div class="dtg-messages"></div><button class="dtg-add">Add Field</button><br clear="all" /></div>'.$tblocks);
524af7d @brockpetrie Output working
authored
77 $wrapper->appendChild($fieldset);
78 } else {
5359745 @brendo Support for multiple DTG fields in one section
brendo authored
79 $fieldset = new XMLElement('fieldset', '<legend>Field Editor</legend>Please save the section to enable the Field Editor.<br />');
524af7d @brockpetrie Output working
authored
80 $wrapper->appendChild($fieldset);
81 }
430749b @brockpetrie First commit
authored
82
77074e0 work started on v3
Brock Petrie authored
83 // Options
84 $fieldset = new XMLElement('fieldset', '<legend>Options</legend>');
b407ac8 @brendo Whitespace, fix custom Selects
brendo authored
85 $checkbox = Widget::Input('fields[' . $this->get('sortorder') . '][allow_multiple]', 'yes', 'checkbox');
86 if($this->get('allow_multiple') == 1) $checkbox->setAttribute('checked', 'checked');
87 $setting = new XMLElement('label', __('%s Allow creation of new items', array($checkbox->generate())), array('class' => 'column'));
88 $fieldset->appendChild($setting);
430749b @brockpetrie First commit
authored
89 $wrapper->appendChild($fieldset);
90
91 // General
92 $fieldset = new XMLElement('fieldset');
93 $group = new XMLElement('div', NULL, array('class' => 'group'));
94 $this->appendRequiredCheckbox($group);
95 $this->appendShowColumnCheckbox($group);
96 $fieldset->appendChild($group);
97 $wrapper->appendChild($fieldset);
98 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
99
100
430749b @brockpetrie First commit
authored
101 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#checkFields * * */
102 function checkFields(&$errors, $checkForDuplicates=true) {
103 parent::checkFields($errors, $checkForDuplicates);
104 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
105
106
430749b @brockpetrie First commit
authored
107 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#commit * * */
108 function commit() {
109 // Prepare commit
110 if(!parent::commit()) return false;
111 $id = $this->get('id');
112 if($id === false) return false;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
113
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
114 // Set up fields
430749b @brockpetrie First commit
authored
115 $fields = array();
116 $fields['field_id'] = $id;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
117
77074e0 work started on v3
Brock Petrie authored
118 // Set up options
119 $fields['allow_multiple'] = ($this->get('allow_multiple') ? 1 : 0);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
120
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
121 // Parse schema
524af7d @brockpetrie Output working
authored
122 if ($this->get('schema') != '') {
634b4c0 @brockpetrie 2.0dev2: See change log
authored
123 $schema = json_decode($this->get('schema'));
0871e28 @brockpetrie 2.0dev4
authored
124 $ct = count($schema);
634b4c0 @brockpetrie 2.0dev2: See change log
authored
125 $totalW = 100;
0871e28 @brockpetrie 2.0dev4
authored
126 foreach ($schema as $i=>&$field) {
127 $field->handle = Lang::createHandle($field->label);
128 $totalW -= $field->width;
129 }
130 $schema[$ct-1]->width += $totalW;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
131
634b4c0 @brockpetrie 2.0dev2: See change log
authored
132 $fields['fieldcount'] = $ct;
133 $fields['schema'] = json_encode($schema);
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
134 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
135
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
136 // Parse rename data
137 if ($this->get('renfields') != '') {
634b4c0 @brockpetrie 2.0dev2: See change log
authored
138 $renfields = json_decode($this->get('renfields'));
139 foreach ($renfields->handles as $key=>$rename) self::__alterTable(2, $rename, Lang::createHandle($renfields->labels[$key]));
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
140 }
141 // Parse delete data
142 if ($this->get('delfields') != '') {
634b4c0 @brockpetrie 2.0dev2: See change log
authored
143 $delfields = json_decode($this->get('delfields'));
144 foreach ($delfields->handles as $deletion) self::__alterTable(0, $deletion);
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
145 }
146 // Parse add data
147 if ($this->get('addfields') != '') {
634b4c0 @brockpetrie 2.0dev2: See change log
authored
148 $addfields = json_decode($this->get('addfields'));
149 foreach ($addfields->labels as $addition) self::__alterTable(1, Lang::createHandle($addition));
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
150 }
430749b @brockpetrie First commit
authored
151
152 // Delete old field settings
153 Symphony::Database()->query(
154 "DELETE FROM `tbl_fields_" . $this->handle() . "` WHERE `field_id` = '$id' LIMIT 1"
155 );
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
156
430749b @brockpetrie First commit
authored
157 // Save new field setting
158 return Symphony::Database()->insert($fields, 'tbl_fields_' . $this->handle());
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
159
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
160 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
161
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
162 function __alterTable($mode, $col, $rename=NULL) {
163 // Function $mode options:
b407ac8 @brendo Whitespace, fix custom Selects
brendo authored
164 // 0 = Delete column; e.g. __alterTable(0, 'badcolumn');
165 // 1 = Add column; e.g. __alterTable(1, 'newcolumn');
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
166 // 2 = Rename column; e.g. __alterTable(2, 'newcolumnname', 'oldcolumnname');
167 switch ($mode) {
168 case 0:
169 // Delete column
170 Symphony::Database()->query("ALTER TABLE `tbl_entries_data_" . $this->get('id') . "` DROP COLUMN `". $col ."`");
171 break;
172 case 1:
173 // Add column
174 Symphony::Database()->query("ALTER TABLE `tbl_entries_data_" . $this->get('id') . "` ADD COLUMN `". $col ."` varchar(255) null");
175 break;
176 case 2:
177 // Rename column
178 Symphony::Database()->query("ALTER TABLE `tbl_entries_data_" . $this->get('id') . "` CHANGE `". $col ."` `". $rename ."` varchar(255) null");
179 break;
180 default:
181 return false;
182 }
430749b @brockpetrie First commit
authored
183 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
184
430749b @brockpetrie First commit
authored
185 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#displayPublishPanel * * */
395451f @brendo Add Importable/Exportable Field support
brendo authored
186 function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) {
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
187
430749b @brockpetrie First commit
authored
188 // Append assets
3dbf379 @brendo Section Schemas compatibility...(basic, not with appendFieldSchema)
brendo authored
189 if(class_exists('Administration')) {
190 Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/select2/select2.min.js', 101, false);
191 Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/dynamictextgroup/assets/select2/select2.css', 'screen', 102, false);
192 Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.publish.js', 103, false);
193 Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.publish.css', 'screen', 104, false);
194 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
195
430749b @brockpetrie First commit
authored
196 // Get settings
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
197 $settings = array('frame');
77074e0 work started on v3
Brock Petrie authored
198 if($this->get('allow_multiple') == 1) {
430749b @brockpetrie First commit
authored
199 $settings[] = 'multiple';
200 } else {
201 $settings[] = 'single';
202 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
203
634b4c0 @brockpetrie 2.0dev2: See change log
authored
204 $schema = json_decode($this->get('schema'));
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
205 $fieldCount = $this->get('fieldcount');
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
206
77074e0 work started on v3
Brock Petrie authored
207 // Create duplicator
208 $duplicator = new XMLElement('div', null, array(
209 'class' => implode(' ', $settings)
210 ));
211 $list = new XMLElement('ol', null, array(
212 'data-add' => __('Create New'),
213 'data-remove' => __('Remove')
214 ));
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
215
430749b @brockpetrie First commit
authored
216 // Populate existing entries
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
217 if ($schema != NULL) {
218 $content = array();
219 if(is_array($data)) {
220 $entryCount = 1;
221 foreach ($data as &$row) {
222 if (!is_array($row)) $row = array($row);
223 if (count($row) > $entryCount) $entryCount = count($row);
430749b @brockpetrie First commit
authored
224 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
225
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
226 for($i=0; $i<$entryCount; $i++) {
227 foreach ($schema as $field) {
228 $entryValues[$i][] = $data[$field->handle][$i];
229 }
230 $list->appendChild(
231 Textgroup::createNewTextGroup($this->get('element_name'), $fieldCount, $entryValues[$i], 'dtg', $schema)
232 );
233 }
234 }
235 // Blank entry
236 else {
77074e0 work started on v3
Brock Petrie authored
237 $list->appendChild(
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
238 Textgroup::createNewTextGroup($this->get('element_name'), $fieldCount, NULL, 'dtg empty', $schema)
77074e0 work started on v3
Brock Petrie authored
239 );
430749b @brockpetrie First commit
authored
240 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
241
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
242 // Add template
243 $template = Textgroup::createNewTextGroup($this->get('element_name'), $fieldCount, NULL, 'template empty create', $schema);
244 $template->setAttribute('data-name', 'dynamictextgroup');
245 $template->setAttribute('data-type', 'dynamictextgroup');
246 $list->appendChild($template);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
247
248
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
249 // Field label
250 $holder = new XMLElement('div');
251 $label = new XMLElement('label', $this->get('label'));
252 $holder->appendChild($label);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
253
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
254 $duplicator->appendChild($list);
255 $holder->appendChild($duplicator);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
256
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
257 if($flagWithError != NULL) $wrapper->appendChild(Widget::Error($holder, $flagWithError));
258 else $wrapper->appendChild($holder);
259 } else {
260 $holder = new XMLElement('div');
261 $label = new XMLElement('label', $this->get('label'));
262 $holder->appendChild($label);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
263
997886f @brockpetrie Symphony 2.3.3 compatibility
authored
264 $wrapper->appendChild(Widget::Error($holder, "You must define your text groups via the Field Editor before using this field. (You'll find it in this section's blueprint)"));
430749b @brockpetrie First commit
authored
265 }
266 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
267
268
430749b @brockpetrie First commit
authored
269 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#checkPostFieldData * * */
270 public function checkPostFieldData($data, &$message, $entry_id=NULL){
524af7d @brockpetrie Output working
authored
271 $message = __("'%s' is a required field.", array($this->get('label')));
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
272
b5a92e0 @brockpetrie Required subfields now work as expected
authored
273 $schema = json_decode($this->get('schema'));
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
274
0871e28 @brockpetrie 2.0dev4
authored
275 $sampling = $schema[0]->handle;
b5a92e0 @brockpetrie Required subfields now work as expected
authored
276 $entryCount = count($data[$sampling]);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
277
b5a92e0 @brockpetrie Required subfields now work as expected
authored
278 $empty = true;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
279
0041bf0 @brockpetrie 2.0dev5
authored
280 $badValidate = false;
9354aa2 @brockpetrie 2.0dev6
authored
281 $badRadio = false;
282 $badCheck = false;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
283
9354aa2 @brockpetrie 2.0dev6
authored
284 $checkItems = array();
285 $radioItems = array();
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
286
b5a92e0 @brockpetrie Required subfields now work as expected
authored
287 for($i=0; $i<$entryCount; $i++) {
288 $emptyRow = true;
289 $emptyReq = false;
0871e28 @brockpetrie 2.0dev4
authored
290 foreach ($schema as $f=>$field) {
0041bf0 @brockpetrie 2.0dev5
authored
291 // Get/set required option
9354aa2 @brockpetrie 2.0dev6
authored
292 $req = $field->options->required ? true : false;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
293
9354aa2 @brockpetrie 2.0dev6
authored
294 switch ($field->options->type) {
295 case 'text':
296 // Check if field passes any rules
297 $rule = $field->options->validationRule != '' ? $field->options->validationRule : false;
298 if ($rule && !General::validateString($data[$field->handle][$i], $rule)){
299 $badValidate[] = array('handle' => $field->handle.'-holder', 'index' => $i);
300 }
301 // Check if required subfield is empty
302 if ($req && $data[$field->handle][$i] == '') {
303 $emptyReq = true;
304 } else if ($data[$field->handle][$i] != '') {
305 $empty = false;
306 $emptyRow = false;
307 }
308 break;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
309
9354aa2 @brockpetrie 2.0dev6
authored
310 case 'select':
311 if ($req && $data[$field->handle][$i] == '') {
312 $emptyReq = true;
313 } else if ($data[$field->handle][$i] != '') {
314 $empty = false;
315 $emptyRow = false;
316 }
317 break;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
318
9354aa2 @brockpetrie 2.0dev6
authored
319 case 'checkbox':
320 if ($i == 0) $checkItems[$f] = false;
321 if ($data[$field->handle][$i] == 'yes') {
322 $checkItems[$f] = true;
323 $emptyRow = false;
324 $empty = false;
325 }
b407ac8 @brendo Whitespace, fix custom Selects
brendo authored
326 if ($i == $entryCount-1 && $entryCount > 0 && !$checkItems[$f] && $req && !$empty) {
9354aa2 @brockpetrie 2.0dev6
authored
327 $badCheck[] = array('handle' => $field->handle.'-holder');
328 }
329 break;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
330
9354aa2 @brockpetrie 2.0dev6
authored
331 case 'radio':
332 if ($i == 0) $radioItems[$f] = false;
333 if ($data[$field->handle][$i] == 'yes') {
334 $radioItems[$f] = true;
335 $emptyRow = false;
336 $empty = false;
337 }
b407ac8 @brendo Whitespace, fix custom Selects
brendo authored
338 if ($i == $entryCount-1 && $entryCount > 0 && !$radioItems[$f] && $req && !$empty) {
9354aa2 @brockpetrie 2.0dev6
authored
339 $badRadio[] = array('handle' => $field->handle.'-holder');
340 }
341 break;
430749b @brockpetrie First commit
authored
342 }
343 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
344
b5a92e0 @brockpetrie Required subfields now work as expected
authored
345 if (!$emptyRow && $emptyReq) {
346 $message = __("'%s' contains required fields that are empty.", array($this->get('label')));
347 return self::__MISSING_FIELDS__;
348 }
524af7d @brockpetrie Output working
authored
349 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
350
0041bf0 @brockpetrie 2.0dev5
authored
351 if ($badValidate){
9354aa2 @brockpetrie 2.0dev6
authored
352 $badValidate = json_encode($badValidate);
353 $message = __("'%s' contains invalid data. Please check the contents.<input type='hidden' id='badItems' value='%s' />", array($this->get('label'), $badValidate));
0041bf0 @brockpetrie 2.0dev5
authored
354 return self::__INVALID_FIELDS__;
355 }
9354aa2 @brockpetrie 2.0dev6
authored
356 if ($badRadio) {
357 $badRadio = json_encode($badRadio);
358 $message = __("'%s' contains required fields that are empty. <input type='hidden' id='badItems' value='%s' />", array($this->get('label'), $badRadio));
359 return self::__MISSING_FIELDS__;
360 }
361 if ($badCheck) {
362 $badCheck = json_encode($badCheck);
363 $message = __("'%s' contains required fields that are empty. <input type='hidden' id='badItems' value='%s' />", array($this->get('label'), $badCheck));
364 return self::__MISSING_FIELDS__;
365 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
366
b5a92e0 @brockpetrie Required subfields now work as expected
authored
367 if ($empty && $this->get('required') == 'yes') return self::__MISSING_FIELDS__;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
368
430749b @brockpetrie First commit
authored
369 return self::__OK__;
370 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
371
372
430749b @brockpetrie First commit
authored
373 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#processRawFieldData * * */
395451f @brendo Add Importable/Exportable Field support
brendo authored
374 function processRawFieldData($data, &$status, &$message=null, $simulate=false, $entry_id=null) {
430749b @brockpetrie First commit
authored
375 $status = self::__OK__;
376 if(!is_array($data)) return NULL;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
377
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
378 $result = array();
430749b @brockpetrie First commit
authored
379 $count = $this->get('fieldcount');
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
380
430749b @brockpetrie First commit
authored
381 // Check for the field with the most values
0871e28 @brockpetrie 2.0dev4
authored
382 $entryCount = 0;
383 foreach ($data as $row) if (count($row) > $entryCount) $entryCount = count($row);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
384
524af7d @brockpetrie Output working
authored
385 // Check for empties
430749b @brockpetrie First commit
authored
386 $empty = true;
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
387
430749b @brockpetrie First commit
authored
388 for($i=0; $i < $entryCount; $i++) {
389 $emptyEntry = true;
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
390 foreach ($data as &$field) {
77074e0 work started on v3
Brock Petrie authored
391 if (!empty($field[$i]) || $field[$i] == '0') {
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
392 $empty = false;
ed0cf96 @brockpetrie 3.0 beta 1
authored
393 $emptyEntry = false;
394 //$field[$i] = str_replace("\"", "&quot;", $field[$i]);
395 $field[$i] = str_replace("'", "&apos;", $field[$i]);
430749b @brockpetrie First commit
authored
396 } else {
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
397 $field[$i] = ' ';
430749b @brockpetrie First commit
authored
398 }
399 }
400 if ($emptyEntry) {
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
401 foreach ($data as &$field) {
402 unset($field[$i]);
430749b @brockpetrie First commit
authored
403 }
404 }
405 }
406
407 if ($empty) {
408 return null;
409 } else {
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
410 return $data;
430749b @brockpetrie First commit
authored
411 }
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
412
430749b @brockpetrie First commit
authored
413 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
414
415
430749b @brockpetrie First commit
authored
416 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#createTable * * */
417 function createTable() {
418 return Symphony::Database()->query(
419 "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
420 `id` int(11) unsigned NOT NULL auto_increment,
421 `entry_id` int(11) unsigned NOT NULL,
422 PRIMARY KEY (`id`),
423 KEY `entry_id` (`entry_id`)
395451f @brendo Add Importable/Exportable Field support
brendo authored
424 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
430749b @brockpetrie First commit
authored
425 );
426 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
427
428
430749b @brockpetrie First commit
authored
429 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#prepareTableValue * * */
395451f @brendo Add Importable/Exportable Field support
brendo authored
430 function prepareTableValue($data, XMLElement $link = null, $entry_id = null) {
524af7d @brockpetrie Output working
authored
431 if (is_array($data)) {
432 $keys = array_keys($data);
433 $key = $keys[0];
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
434
524af7d @brockpetrie Output working
authored
435 if(!is_array($data[$key])) $data[$key] = array($data[$key]);
436 if ($data[$key][0] != null) {
437 $strung = count($data[$key]) == 1 ? count($data[$key]) . ' item' : count($data[$key]) . ' items';
438 } else {
439 $strung = null;
440 }
430749b @brockpetrie First commit
authored
441 } else {
442 $strung = null;
443 }
444 return $strung;
445 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
446
447
430749b @brockpetrie First commit
authored
448 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#buildSortingSQL * * */
449 /*
450 function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC') {
451 }
452 */
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
453
454
430749b @brockpetrie First commit
authored
455 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#buildDSRetrivalSQL * * */
456 /*
cff4de4 @brockpetrie 2.0dev3 Changes
authored
457 ** Accepted filter:
458 ** handle:value (e.g. first-name:Brock)
b407ac8 @brendo Whitespace, fix custom Selects
brendo authored
459 ** Where 'handle' is equal to the handle of a subfield, and 'value' is equal to the input of said subfield. All entries with a matching value in this subfield will be returned.
430749b @brockpetrie First commit
authored
460 */
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
461 public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) {
cff4de4 @brockpetrie 2.0dev3 Changes
authored
462 $field_id = $this->get('id');
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
463
cff4de4 @brockpetrie 2.0dev3 Changes
authored
464 if (preg_match('/.*:.*/', $data[0])) {
465 $this->_key++;
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
466 $joins .= "
467 LEFT JOIN
cff4de4 @brockpetrie 2.0dev3 Changes
authored
468 `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
469 ON
cff4de4 @brockpetrie 2.0dev3 Changes
authored
470 (e.id = t{$field_id}_{$this->_key}.entry_id)
471 ";
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
472
cff4de4 @brockpetrie 2.0dev3 Changes
authored
473 $data[0] = explode(':', trim($this->cleanValue($data[0])));
474 $handle = $data[0][0];
475 $value = $data[0][1];
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
476
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
477 $where .= "
cff4de4 @brockpetrie 2.0dev3 Changes
authored
478 AND (
479 `t{$field_id}_{$this->_key}`.`{$handle}` IN ('{$value}')
480 )
481 ";
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
482 }
cff4de4 @brockpetrie 2.0dev3 Changes
authored
483
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
484 return true;
485 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
486
430749b @brockpetrie First commit
authored
487
488 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#groupRecords * * */
489 /*
490 public function groupRecords($records) {
491 }
492 */
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
493
494
430749b @brockpetrie First commit
authored
495 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#appendFormattedElement * * */
395451f @brendo Add Importable/Exportable Field support
brendo authored
496 public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) {
634b4c0 @brockpetrie 2.0dev2: See change log
authored
497 // Get field properties and decode schema
430749b @brockpetrie First commit
authored
498 $fieldCount = $this->get('fieldcount');
634b4c0 @brockpetrie 2.0dev2: See change log
authored
499 $schema = json_decode($this->get('schema'));
0871e28 @brockpetrie 2.0dev4
authored
500 $sampling = $schema[0]->handle;
524af7d @brockpetrie Output working
authored
501 $entryCount = count($data[$sampling]);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
502
634b4c0 @brockpetrie 2.0dev2: See change log
authored
503 // Parse data
430749b @brockpetrie First commit
authored
504 $textgroup = new XMLElement($this->get('element_name'));
b777447 @bauhouse Fix error in data source when fields are empty. Test whether the vari…
bauhouse authored
505 if(is_array($data)) {
506 foreach ($data as &$row) { if (!is_array($row)) $row = array($row); }
507 }
430749b @brockpetrie First commit
authored
508 for($i=0; $i<$entryCount; $i++) {
509 $item = new XMLElement('item');
510 $empty = true;
0871e28 @brockpetrie 2.0dev4
authored
511 foreach ($schema as $field) {
ed0cf96 @brockpetrie 3.0 beta 1
authored
512 $node = new XMLElement($field->handle);
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
513
ed0cf96 @brockpetrie 3.0 beta 1
authored
514 // If field data is JSON, iterate through the keys and append each as its own node
515 $tryjson = json_decode($data[$field->handle][$i]);
516 if (!json_last_error() && strpos($data[$field->handle][$i],'{') !== false) {
517 foreach ($tryjson as $key => $obj) {
518 $node->appendChild(new XMLElement($key, $obj));
519 }
520 } else {
521 $val = $data[$field->handle][$i] != ' ' ? General::sanitize($data[$field->handle][$i]) : '';
522 $node->setValue($val);
523 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
524 $item->appendChild($node);
430749b @brockpetrie First commit
authored
525 }
526 $textgroup->appendChild($item);
527 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
528
524af7d @brockpetrie Output working
authored
529 // Append to data source
430749b @brockpetrie First commit
authored
530 $wrapper->appendChild($textgroup);
531 }
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
532
533
430749b @brockpetrie First commit
authored
534 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#getParameterPoolValue * * */
524af7d @brockpetrie Output working
authored
535 /*
430749b @brockpetrie First commit
authored
536 public function getParameterPoolValue($data) {
537 }
524af7d @brockpetrie Output working
authored
538 */
7eadca1 @brendo Some updates for Symphony 2.4
brendo authored
539
540
430749b @brockpetrie First commit
authored
541 /* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#getExampleFormMarkup * * */
542 public function getExampleFormMarkup() {
543 $label = Widget::Label($this->get('label'));
9354aa2 @brockpetrie 2.0dev6
authored
544 $schema = json_decode($this->get('schema'));
545 $note = new XMLElement('strong', 'IMPORTANT: the event sample code is not updated when you make changes to DynamicTextGroup subfields in the section editor. Remember that your front-end fields must always match the back-end fields!');
546 foreach ($schema as $field) $label->appendChild(Widget::Input('fields['.$this->get('element_name').']['.$field->handle.'][]'));
430749b @brockpetrie First commit
authored
547 return $label;
548 }
395451f @brendo Add Importable/Exportable Field support
brendo authored
549
550 /*-------------------------------------------------------------------------
551 Import:
552 -------------------------------------------------------------------------*/
553
554 public function getImportModes() {
555 return array(
556 'getPostdata' => ImportableField::ARRAY_VALUE
557 );
558 }
559
560 public function prepareImportValue($data, $mode, $entry_id = null) {
561 $message = $status = null;
562 $modes = (object)$this->getImportModes();
563
564 if($mode === $modes->getPostdata) {
565 return $this->processRawFieldData($data, $status, $message, true, $entry_id);
566 }
567
568 return null;
569 }
570
571 /*-------------------------------------------------------------------------
572 Export:
573 -------------------------------------------------------------------------*/
574
575 public function getExportModes() {
576 return array(
577 'getPostdata' => ExportableField::POSTDATA
578 );
579 }
580
581 public function prepareExportValue($data, $mode, $entry_id = null) {
582 $modes = (object)$this->getExportModes();
583
584 if($mode === $modes->getPostdata) {
585 return $data;
586 }
587
588 return null;
589 }
6f9f0d0 @brockpetrie 2.0dev Initial commit
authored
590 }
Something went wrong with that request. Please try again.