From 190d657dc48330b28fa2b9645d123409175ea6cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Ho=CC=88rrmann?= Date: Wed, 25 Jun 2014 18:58:07 +0200 Subject: [PATCH] Version 1.2.1 --- README.markdown | 5 - assets/publish.xmlfield.css | 3 + extension.driver.php | 59 +++++--- extension.meta.xml | 8 +- fields/field.xml.php | 277 ++++++++++++++++++------------------ 5 files changed, 191 insertions(+), 161 deletions(-) create mode 100644 assets/publish.xmlfield.css diff --git a/README.markdown b/README.markdown index 4fa90d2..90118eb 100644 --- a/README.markdown +++ b/README.markdown @@ -1,8 +1,3 @@ # XML Field -* Version: 1.1 -* Author: Symphony Community (https://github.com/symphonists) -* Build Date: 2011-03-22 -* Requirements: Symphony 2.2 - A textarea field that only accepts valid XML. \ No newline at end of file diff --git a/assets/publish.xmlfield.css b/assets/publish.xmlfield.css new file mode 100644 index 0000000..973c9e6 --- /dev/null +++ b/assets/publish.xmlfield.css @@ -0,0 +1,3 @@ +.field-xml textarea { + font: 1.2rem/1.7 Monaco, Consolas, 'Andale Mono', monospace; +} \ No newline at end of file diff --git a/extension.driver.php b/extension.driver.php index 0b09910..5d39516 100755 --- a/extension.driver.php +++ b/extension.driver.php @@ -1,20 +1,43 @@ query("DROP TABLE `tbl_fields_xml`"); - } - - public function install() { - return Symphony::Database()->query("CREATE TABLE IF NOT EXISTS `tbl_fields_xml` ( - `id` int(11) unsigned NOT NULL auto_increment, - `field_id` int(11) unsigned NOT NULL, - `size` int(3) unsigned NOT NULL, - PRIMARY KEY (`id`), - KEY `field_id` (`field_id`) - ) TYPE=MyISAM;" - ); - } - } +class extension_XMLField extends Extension +{ + + public function getSubscribedDelegates() + { + return array( + array( + 'page' => '/backend/', + 'delegate' => 'InitaliseAdminPageHead', + 'callback' => 'initaliseAdminPageHead' + ) + ); + } + + public function initaliseAdminPageHead($context) + { + $callback = Symphony::Engine()->getPageCallback(); + + if($callback['driver'] == 'publish' && $callback['context']['page'] != 'index') { + Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/xmlfield/assets/publish.xmlfield.css'); + } + } + + public function install() + { + return Symphony::Database()->query( + "CREATE TABLE IF NOT EXISTS `tbl_fields_xml` ( + `id` int(11) unsigned NOT NULL auto_increment, + `field_id` int(11) unsigned NOT NULL, + `size` int(3) unsigned NOT NULL, + PRIMARY KEY (`id`), + KEY `field_id` (`field_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" + ); + } + + public function uninstall() + { + Symphony::Database()->query("DROP TABLE `tbl_fields_xml`"); + } +} diff --git a/extension.meta.xml b/extension.meta.xml index acea227..e5b12ef 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -1,5 +1,5 @@ - + XML Field A textarea field that only accepts valid XML https://github.com/symphonists/xmlfield @@ -13,8 +13,12 @@ - + + - Clean-up + - Set monospace font + + diff --git a/fields/field.xml.php b/fields/field.xml.php index b37128a..e6a08fd 100755 --- a/fields/field.xml.php +++ b/fields/field.xml.php @@ -1,137 +1,142 @@ Symphony Error

You cannot directly access this file

'); - - require_once(TOOLKIT . '/fields/field.textarea.php'); - - class FieldXML extends fieldTextarea { - - public function __construct(){ - parent::__construct(); - $this->_name = 'XML'; - $this->_required = true; - - // Set defaults - $this->set('show_column', 'no'); - $this->set('required', 'yes'); - } - - function checkPostFieldData($data, &$message, $entry_id=NULL){ - - $message = NULL; - - if($this->get('required') == 'yes' && strlen($data) == 0){ - $message = __("'%s' is a required field.", array($this->get('label'))); - return self::__MISSING_FIELDS__; - } - - if(empty($data)) self::__OK__; - - include_once(TOOLKIT . '/class.xsltprocess.php'); - $xsltProc =& new XsltProcess; - - if(!General::validateXML($data, $errors, false, $xsltProc)){ - - $message = __('"%1$s" contains invalid XML. The following error was returned:
%2$s', array($this->get('label'), $errors[0]['message'])); - return self::__INVALID_FIELDS__; - } - - return self::__OK__; - - } - - public function processRawFieldData($data, &$status, &$message=null, $simulate = false, $entry_id = null) { - $status = self::__OK__; - return array( - 'value' => $data - ); - } - - public function displaySettingsPanel(&$wrapper, $errors = null) { - Field::displaySettingsPanel($wrapper, $errors); - - // Textarea Size - $label = Widget::Label(__('Number of default rows')); - $label->setAttribute('class', 'column'); - $input = Widget::Input('fields['.$this->get('sortorder').'][size]', (string)$this->get('size')); - $label->appendChild($input); - - $div = new XMLElement('div'); - $div->setAttribute('class', 'two columns'); - $div->appendChild($label); - $wrapper->appendChild($div); - - $div = new XMLElement('div', NULL, array('class' => 'two columns')); - $this->appendRequiredCheckbox($div); - $this->appendShowColumnCheckbox($div); - $wrapper->appendChild($div); - } - - 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, - `value` text, - PRIMARY KEY (`id`), - KEY `entry_id` (`entry_id`), - FULLTEXT KEY `value` (`value`) - ) TYPE=MyISAM;" - - ); - } - - public function commit(){ - if(!Field::commit()) return false; - - $id = $this->get('id'); - - if($id === false) return false; - - $fields = array(); - $fields['size'] = $this->get('size'); - - return FieldManager::saveSettings($id, $fields); - } - - public function fetchIncludableElements() { - return array( - $this->get('element_name') - ); - } - - function appendFormattedElement(&$wrapper, $data, $encode=false){ - $value = trim($data['value']); - $wrapper->appendChild(new XMLElement($this->get('element_name'), ($encode ? General::sanitize($value) : $value))); - } - - function checkFields(&$required, $checkForDuplicates=true, $checkForParentSection=true){ - $required = array(); - if($this->get('size') == '' || !is_numeric($this->get('size'))) $required[] = 'size'; - return parent::checkFields($required, $checkForDuplicates, $checkForParentSection); - } - - public function prepareTableValue($data, XMLElement $link = null) { - $max_length = Symphony::Configuration()->get('cell_truncation_length', 'symphony'); - $max_length = ($max_length ? $max_length : 75); - - $value = $data['value']; - - if(function_exists('mb_substr')) { - $value = (strlen($value) <= $max_length ? $value : mb_substr($value, 0, $max_length, 'utf-8') . '...'); - } - else { - $value = (strlen($value) <= $max_length ? $value : substr($value, 0, $max_length) . '...'); - } - - if (strlen($value) == 0) $value = __('None'); - - if ($link) { - $link->setValue(htmlspecialchars($value)); - return $link->generate(); - } - - return htmlspecialchars($value); - } - - } + +if (!defined('__IN_SYMPHONY__')) { + die('

Symphony Error

You cannot directly access this file

'); +} + +require_once(TOOLKIT . '/fields/field.textarea.php'); + +class FieldXML extends fieldTextarea +{ + public function __construct() + { + parent::__construct(); + $this->_name = 'XML'; + $this->_required = true; + + // Set defaults + $this->set('show_column', 'no'); + $this->set('required', 'yes'); + } + + public function checkPostFieldData($data, &$message, $entry_id = null) + { + $message = null; + + if ($this->get('required') == 'yes' && strlen($data) == 0) { + $message = __("'%s' is a required field.", array($this->get('label'))); + + return self::__MISSING_FIELDS__; + } + + if (empty($data)) { + self::__OK__; + } + + include_once(TOOLKIT . '/class.xsltprocess.php'); + $xsltProc =& new XsltProcess; + + if (!General::validateXML($data, $errors, false, $xsltProc)) { + + $message = __('"%1$s" contains invalid XML. The following error was returned:
%2$s', array($this->get('label'), $errors[0]['message'])); + + return self::__INVALID_FIELDS__; + } + + return self::__OK__; + + } + + public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) + { + $status = self::__OK__; + + return array( + 'value' => $data + ); + } + + public function displaySettingsPanel(&$wrapper, $errors = null) + { + Field::displaySettingsPanel($wrapper, $errors); + + // Textarea Size + $label = Widget::Label(__('Number of default rows')); + $label->setAttribute('class', 'column'); + $input = Widget::Input('fields['.$this->get('sortorder').'][size]', (string)$this->get('size')); + $label->appendChild($input); + + $div = new XMLElement('div'); + $div->setAttribute('class', 'two columns'); + $div->appendChild($label); + $wrapper->appendChild($div); + + $div = new XMLElement('div', NULL, array('class' => 'two columns')); + $this->appendRequiredCheckbox($div); + $this->appendShowColumnCheckbox($div); + $wrapper->appendChild($div); + } + + public 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, + `value` text, + PRIMARY KEY (`id`), + KEY `entry_id` (`entry_id`), + FULLTEXT KEY `value` (`value`) + ) TYPE=MyISAM;" + ); + } + + public function fetchIncludableElements() + { + return array( + $this->get('element_name') + ); + } + + public function appendFormattedElement(&$wrapper, $data, $encode = false) + { + $value = trim($data['value']); + $wrapper->appendChild(new XMLElement($this->get('element_name'), ($encode ? General::sanitize($value) : $value))); + } + + public function checkFields(&$required, $checkForDuplicates = true, $checkForParentSection = true) + { + $required = array(); + if ($this->get('size') == '' || !is_numeric($this->get('size'))) { + $required[] = 'size'; + } + + return parent::checkFields($required, $checkForDuplicates, $checkForParentSection); + } + + public function prepareTableValue($data, XMLElement $link = null) + { + $max_length = Symphony::Configuration()->get('cell_truncation_length', 'symphony'); + $max_length = ($max_length ? $max_length : 75); + + $value = $data['value']; + + if (function_exists('mb_substr')) { + $value = (strlen($value) <= $max_length ? $value : mb_substr($value, 0, $max_length, 'utf-8') . '...'); + } else { + $value = (strlen($value) <= $max_length ? $value : substr($value, 0, $max_length) . '...'); + } + + if (strlen($value) == 0) { + $value = __('None'); + } + + if ($link) { + $link->setValue(htmlspecialchars($value)); + + return $link->generate(); + } + + return htmlspecialchars($value); + } +}