Skip to content

Commit

Permalink
Version 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshoerrmann committed Jun 25, 2014
1 parent 3c9dee4 commit 190d657
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 161 deletions.
5 changes: 0 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions assets/publish.xmlfield.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.field-xml textarea {
font: 1.2rem/1.7 Monaco, Consolas, 'Andale Mono', monospace;
}
59 changes: 41 additions & 18 deletions extension.driver.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
<?php

class extension_XMLField extends Extension {

public function uninstall() {
Symphony::Database()->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`");
}
}
8 changes: 6 additions & 2 deletions extension.meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension id="xmlfield" status="released" xmlns="http://symphony-cms.com/schemas/extension/1.0">
<extension id="xmlfield" status="released" xmlns="http://getsymphony.com/schemas/extension/1.0">
<name>XML Field</name>
<description>A textarea field that only accepts valid XML</description>
<repo type="github">https://github.com/symphonists/xmlfield</repo>
Expand All @@ -13,8 +13,12 @@
</author>
</authors>
<releases>
<release version="1.1" date="2011-03-22" min="2.2" max="2.2.5" />
<release version="1.2.1" date="2014-06-25" min="2.4">
- Clean-up
- Set monospace font
</release>
<release version="1.2.0" date="2012-03-16" min="2.3" />
<release version="1.2.1" date="2012-05-16" min="2.3" />
<release version="1.1.0" date="2011-03-22" min="2.2" max="2.2.5" />
</releases>
</extension>
277 changes: 141 additions & 136 deletions fields/field.xml.php
Original file line number Diff line number Diff line change
@@ -1,137 +1,142 @@
<?php

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

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: <br/><code>%2$s</code>', 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('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
}

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: <br/><code>%2$s</code>', 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);
}
}

0 comments on commit 190d657

Please sign in to comment.