Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: development
Fetching contributors…

Cannot retrieve contributors at this time

file 590 lines (483 sloc) 20.363 kb
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
<?php

/**
* @package dynamictextgroup
*/

/**
* This field provides a method to dynamically add a text field or text field groups to a section entry
*/

if(!defined('__IN_SYMPHONY__')) die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');

require_once(EXTENSIONS . '/dynamictextgroup/lib/class.textgroup.php');
require_once FACE . '/interface.exportablefield.php';
require_once FACE . '/interface.importablefield.php';

Class fielddynamictextgroup extends Field implements ImportableField, ExportableField {

/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#__construct * * */
function __construct() {
parent::__construct();
$this->_name = __('Dynamic Text Group');
$this->_required = true;
$this->set('required', 'no');
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#canFilter * * */
function canFilter() {
return true;
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#isSortable * * */
function isSortable() {
return true;
}

/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#canPrePopulate * * */
function canPrePopulate() {
return false;
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#allowDatasourceOutputGrouping * * */
function allowDatasourceOutputGrouping() {
return false;
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#allowDatasourceParamOutput * * */
function allowDatasourceParamOutput() {
return false;
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#displaySettingsPanel * * */
function displaySettingsPanel(&$wrapper, $errors=NULL) {

// Initialize field settings based on class defaults (name, placement)
parent::displaySettingsPanel($wrapper, $errors);

// Field Editor
if ($this->get('id')) {
Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/jquery-ui-1.10.3.custom.min.js', 101, false);
Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/json2.js', 102, false);
Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.fieldeditor.js', 103, false);
Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.fieldeditor.css', 'screen', 104, false);

$tblocks = '<input type="hidden" class="dtg-fieldschema" name="fields['.$this->get('sortorder').'][schema]" value=\''.$this->get('schema').'\' />';
//$tblocks .= '<span>'.$this->get('schema').'</span>';
$tblocks .= '<input type="hidden" class="dtg-addfields" name="fields['.$this->get('sortorder').'][addfields]" value="" />';
$tblocks .= '<input type="hidden" class="dtg-delfields" name="fields['.$this->get('sortorder').'][delfields]" value="" />';
$tblocks .= '<input type="hidden" class="dtg-renfields" name="fields['.$this->get('sortorder').'][renfields]" value="" />';

$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);
$wrapper->appendChild($fieldset);
} else {
$fieldset = new XMLElement('fieldset', '<legend>Field Editor</legend>Please save the section to enable the Field Editor.<br />');
$wrapper->appendChild($fieldset);
}

// Options
$fieldset = new XMLElement('fieldset', '<legend>Options</legend>');
$checkbox = Widget::Input('fields[' . $this->get('sortorder') . '][allow_multiple]', 'yes', 'checkbox');
if($this->get('allow_multiple') == 1) $checkbox->setAttribute('checked', 'checked');
$setting = new XMLElement('label', __('%s Allow creation of new items', array($checkbox->generate())), array('class' => 'column'));
$fieldset->appendChild($setting);
$wrapper->appendChild($fieldset);

// General
$fieldset = new XMLElement('fieldset');
$group = new XMLElement('div', NULL, array('class' => 'group'));
$this->appendRequiredCheckbox($group);
$this->appendShowColumnCheckbox($group);
$fieldset->appendChild($group);
$wrapper->appendChild($fieldset);
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#checkFields * * */
function checkFields(&$errors, $checkForDuplicates=true) {
parent::checkFields($errors, $checkForDuplicates);
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#commit * * */
function commit() {
// Prepare commit
if(!parent::commit()) return false;
$id = $this->get('id');
if($id === false) return false;

// Set up fields
$fields = array();
$fields['field_id'] = $id;

// Set up options
$fields['allow_multiple'] = ($this->get('allow_multiple') ? 1 : 0);

// Parse schema
if ($this->get('schema') != '') {
$schema = json_decode($this->get('schema'));
$ct = count($schema);
$totalW = 100;
foreach ($schema as $i=>&$field) {
$field->handle = Lang::createHandle($field->label);
$totalW -= $field->width;
}
$schema[$ct-1]->width += $totalW;

$fields['fieldcount'] = $ct;
$fields['schema'] = json_encode($schema);
}

// Parse rename data
if ($this->get('renfields') != '') {
$renfields = json_decode($this->get('renfields'));
foreach ($renfields->handles as $key=>$rename) self::__alterTable(2, $rename, Lang::createHandle($renfields->labels[$key]));
}
// Parse delete data
if ($this->get('delfields') != '') {
$delfields = json_decode($this->get('delfields'));
foreach ($delfields->handles as $deletion) self::__alterTable(0, $deletion);
}
// Parse add data
if ($this->get('addfields') != '') {
$addfields = json_decode($this->get('addfields'));
foreach ($addfields->labels as $addition) self::__alterTable(1, Lang::createHandle($addition));
}

// Delete old field settings
Symphony::Database()->query(
"DELETE FROM `tbl_fields_" . $this->handle() . "` WHERE `field_id` = '$id' LIMIT 1"
);

// Save new field setting
return Symphony::Database()->insert($fields, 'tbl_fields_' . $this->handle());

}

function __alterTable($mode, $col, $rename=NULL) {
// Function $mode options:
// 0 = Delete column; e.g. __alterTable(0, 'badcolumn');
// 1 = Add column; e.g. __alterTable(1, 'newcolumn');
// 2 = Rename column; e.g. __alterTable(2, 'newcolumnname', 'oldcolumnname');
switch ($mode) {
case 0:
// Delete column
Symphony::Database()->query("ALTER TABLE `tbl_entries_data_" . $this->get('id') . "` DROP COLUMN `". $col ."`");
break;
case 1:
// Add column
Symphony::Database()->query("ALTER TABLE `tbl_entries_data_" . $this->get('id') . "` ADD COLUMN `". $col ."` varchar(255) null");
break;
case 2:
// Rename column
Symphony::Database()->query("ALTER TABLE `tbl_entries_data_" . $this->get('id') . "` CHANGE `". $col ."` `". $rename ."` varchar(255) null");
break;
default:
return false;
}
}

/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#displayPublishPanel * * */
function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) {

// Append assets
if(class_exists('Administration')) {
Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/select2/select2.min.js', 101, false);
Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/dynamictextgroup/assets/select2/select2.css', 'screen', 102, false);
Administration::instance()->Page->addScriptToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.publish.js', 103, false);
Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/dynamictextgroup/assets/dynamictextgroup.publish.css', 'screen', 104, false);
}

// Get settings
$settings = array('frame');
if($this->get('allow_multiple') == 1) {
$settings[] = 'multiple';
} else {
$settings[] = 'single';
}

$schema = json_decode($this->get('schema'));
$fieldCount = $this->get('fieldcount');

// Create duplicator
$duplicator = new XMLElement('div', null, array(
'class' => implode(' ', $settings)
));
$list = new XMLElement('ol', null, array(
'data-add' => __('Create New'),
'data-remove' => __('Remove')
));

// Populate existing entries
if ($schema != NULL) {
$content = array();
if(is_array($data)) {
$entryCount = 1;
foreach ($data as &$row) {
if (!is_array($row)) $row = array($row);
if (count($row) > $entryCount) $entryCount = count($row);
}

for($i=0; $i<$entryCount; $i++) {
foreach ($schema as $field) {
$entryValues[$i][] = $data[$field->handle][$i];
}
$list->appendChild(
Textgroup::createNewTextGroup($this->get('element_name'), $fieldCount, $entryValues[$i], 'dtg', $schema)
);
}
}
// Blank entry
else {
$list->appendChild(
Textgroup::createNewTextGroup($this->get('element_name'), $fieldCount, NULL, 'dtg empty', $schema)
);
}

// Add template
$template = Textgroup::createNewTextGroup($this->get('element_name'), $fieldCount, NULL, 'template empty create', $schema);
$template->setAttribute('data-name', 'dynamictextgroup');
$template->setAttribute('data-type', 'dynamictextgroup');
$list->appendChild($template);


// Field label
$holder = new XMLElement('div');
$label = new XMLElement('label', $this->get('label'));
$holder->appendChild($label);

$duplicator->appendChild($list);
$holder->appendChild($duplicator);

if($flagWithError != NULL) $wrapper->appendChild(Widget::Error($holder, $flagWithError));
else $wrapper->appendChild($holder);
} else {
$holder = new XMLElement('div');
$label = new XMLElement('label', $this->get('label'));
$holder->appendChild($label);

$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)"));
}
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#checkPostFieldData * * */
public function checkPostFieldData($data, &$message, $entry_id=NULL){
$message = __("'%s' is a required field.", array($this->get('label')));

$schema = json_decode($this->get('schema'));

$sampling = $schema[0]->handle;
$entryCount = count($data[$sampling]);

$empty = true;

$badValidate = false;
$badRadio = false;
$badCheck = false;

$checkItems = array();
$radioItems = array();

for($i=0; $i<$entryCount; $i++) {
$emptyRow = true;
$emptyReq = false;
foreach ($schema as $f=>$field) {
// Get/set required option
$req = $field->options->required ? true : false;

switch ($field->options->type) {
case 'text':
// Check if field passes any rules
$rule = $field->options->validationRule != '' ? $field->options->validationRule : false;
if ($rule && !General::validateString($data[$field->handle][$i], $rule)){
$badValidate[] = array('handle' => $field->handle.'-holder', 'index' => $i);
}
// Check if required subfield is empty
if ($req && $data[$field->handle][$i] == '') {
$emptyReq = true;
} else if ($data[$field->handle][$i] != '') {
$empty = false;
$emptyRow = false;
}
break;

case 'select':
if ($req && $data[$field->handle][$i] == '') {
$emptyReq = true;
} else if ($data[$field->handle][$i] != '') {
$empty = false;
$emptyRow = false;
}
break;

case 'checkbox':
if ($i == 0) $checkItems[$f] = false;
if ($data[$field->handle][$i] == 'yes') {
$checkItems[$f] = true;
$emptyRow = false;
$empty = false;
}
if ($i == $entryCount-1 && $entryCount > 0 && !$checkItems[$f] && $req && !$empty) {
$badCheck[] = array('handle' => $field->handle.'-holder');
}
break;

case 'radio':
if ($i == 0) $radioItems[$f] = false;
if ($data[$field->handle][$i] == 'yes') {
$radioItems[$f] = true;
$emptyRow = false;
$empty = false;
}
if ($i == $entryCount-1 && $entryCount > 0 && !$radioItems[$f] && $req && !$empty) {
$badRadio[] = array('handle' => $field->handle.'-holder');
}
break;
}
}

if (!$emptyRow && $emptyReq) {
$message = __("'%s' contains required fields that are empty.", array($this->get('label')));
return self::__MISSING_FIELDS__;
}
}

if ($badValidate){
$badValidate = json_encode($badValidate);
$message = __("'%s' contains invalid data. Please check the contents.<input type='hidden' id='badItems' value='%s' />", array($this->get('label'), $badValidate));
return self::__INVALID_FIELDS__;
}
if ($badRadio) {
$badRadio = json_encode($badRadio);
$message = __("'%s' contains required fields that are empty. <input type='hidden' id='badItems' value='%s' />", array($this->get('label'), $badRadio));
return self::__MISSING_FIELDS__;
}
if ($badCheck) {
$badCheck = json_encode($badCheck);
$message = __("'%s' contains required fields that are empty. <input type='hidden' id='badItems' value='%s' />", array($this->get('label'), $badCheck));
return self::__MISSING_FIELDS__;
}

if ($empty && $this->get('required') == 'yes') return self::__MISSING_FIELDS__;

return self::__OK__;
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#processRawFieldData * * */
function processRawFieldData($data, &$status, &$message=null, $simulate=false, $entry_id=null) {
$status = self::__OK__;
if(!is_array($data)) return NULL;

$result = array();
$count = $this->get('fieldcount');

// Check for the field with the most values
$entryCount = 0;
foreach ($data as $row) if (count($row) > $entryCount) $entryCount = count($row);

// Check for empties
$empty = true;

for($i=0; $i < $entryCount; $i++) {
$emptyEntry = true;
foreach ($data as &$field) {
if (!empty($field[$i]) || $field[$i] == '0') {
$empty = false;
$emptyEntry = false;
//$field[$i] = str_replace("\"", "&quot;", $field[$i]);
$field[$i] = str_replace("'", "&apos;", $field[$i]);
} else {
$field[$i] = ' ';
}
}
if ($emptyEntry) {
foreach ($data as &$field) {
unset($field[$i]);
}
}
}

if ($empty) {
return null;
} else {
return $data;
}

}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#createTable * * */
function createTable() {
return Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
`id` int(11) unsigned NOT NULL auto_increment,
`entry_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `entry_id` (`entry_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#prepareTableValue * * */
function prepareTableValue($data, XMLElement $link = null, $entry_id = null) {
if (is_array($data)) {
$keys = array_keys($data);
$key = $keys[0];

if(!is_array($data[$key])) $data[$key] = array($data[$key]);
if ($data[$key][0] != null) {
$strung = count($data[$key]) == 1 ? count($data[$key]) . ' item' : count($data[$key]) . ' items';
} else {
$strung = null;
}
} else {
$strung = null;
}
return $strung;
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#buildSortingSQL * * */
/*
function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC') {
}
*/


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#buildDSRetrivalSQL * * */
/*
** Accepted filter:
** handle:value (e.g. first-name:Brock)
** 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.
*/
public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false) {
$field_id = $this->get('id');

if (preg_match('/.*:.*/', $data[0])) {
$this->_key++;
$joins .= "
LEFT JOIN
`tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
ON
(e.id = t{$field_id}_{$this->_key}.entry_id)
";

$data[0] = explode(':', trim($this->cleanValue($data[0])));
$handle = $data[0][0];
$value = $data[0][1];

$where .= "
AND (
`t{$field_id}_{$this->_key}`.`{$handle}` IN ('{$value}')
)
";
}

return true;
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#groupRecords * * */
/*
public function groupRecords($records) {
}
*/


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#appendFormattedElement * * */
public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null) {
// Get field properties and decode schema
$fieldCount = $this->get('fieldcount');
$schema = json_decode($this->get('schema'));
$sampling = $schema[0]->handle;
$entryCount = count($data[$sampling]);

// Parse data
$textgroup = new XMLElement($this->get('element_name'));
if(is_array($data)) {
foreach ($data as &$row) { if (!is_array($row)) $row = array($row); }
}
for($i=0; $i<$entryCount; $i++) {
$item = new XMLElement('item');
$empty = true;
foreach ($schema as $field) {
$node = new XMLElement($field->handle);

// If field data is JSON, iterate through the keys and append each as its own node
$tryjson = json_decode($data[$field->handle][$i]);
if (!json_last_error() && strpos($data[$field->handle][$i],'{') !== false) {
foreach ($tryjson as $key => $obj) {
$node->appendChild(new XMLElement($key, $obj));
}
} else {
$val = $data[$field->handle][$i] != ' ' ? General::sanitize($data[$field->handle][$i]) : '';
$node->setValue($val);
}
$item->appendChild($node);
}
$textgroup->appendChild($item);
}

// Append to data source
$wrapper->appendChild($textgroup);
}


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#getParameterPoolValue * * */
/*
public function getParameterPoolValue($data) {
}
*/


/* * * @see http://symphony-cms.com/learn/api/2.2/toolkit/field/#getExampleFormMarkup * * */
public function getExampleFormMarkup() {
$label = Widget::Label($this->get('label'));
$schema = json_decode($this->get('schema'));
$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!');
foreach ($schema as $field) $label->appendChild(Widget::Input('fields['.$this->get('element_name').']['.$field->handle.'][]'));
return $label;
}

/*-------------------------------------------------------------------------
Import:
-------------------------------------------------------------------------*/

public function getImportModes() {
return array(
'getPostdata' => ImportableField::ARRAY_VALUE
);
}

public function prepareImportValue($data, $mode, $entry_id = null) {
$message = $status = null;
$modes = (object)$this->getImportModes();

if($mode === $modes->getPostdata) {
return $this->processRawFieldData($data, $status, $message, true, $entry_id);
}

return null;
}

/*-------------------------------------------------------------------------
Export:
-------------------------------------------------------------------------*/

public function getExportModes() {
return array(
'getPostdata' => ExportableField::POSTDATA
);
}

public function prepareExportValue($data, $mode, $entry_id = null) {
$modes = (object)$this->getExportModes();

if($mode === $modes->getPostdata) {
return $data;
}

return null;
}
}
Something went wrong with that request. Please try again.