*/
if (!class_exists('patForms_Element', false)) {
WBClass::load('patForms_Element');
}
/**
* patForms Elemenbt: Keyvaluepair
*
* Set Key-Value-Pair-List
*
* @version 0.2.2
* @package Wombat
* @subpackage patForms
*/
class patForms_Element_Keyvaluepair extends patForms_Element
{
/**
* Stores the name of the element - this is used mainly by the patForms
* error management and should be set in every element class.
* @var string
*/
public $elementName = 'Keyvaluepair';
/**
* element type to create HTML output
* @var array
*/
public $elementType = array('html' => 'input');
/**
* Kev Value Pair
* @var WBDatasource_KeyValuePair
*/
private $kvp;
/**
* List of collected key value pairs
* @var array
*/
private $kvpList = array();
/**
* Template engine
* @var patTemplate
*/
private $tmpl;
/**
* set here which attributes you want to include in the element if you want to use
* the {@link patForms_Element::convertDefinition2Attributes()} method to automatically
* convert the values from your element definition into element attributes.
*
* @see patForms_Element::convertDefinition2Attribute()
* @var array
*/
public $attributeDefinition = array(
'id' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
),
'name' => array( 'required' => true,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array()),
),
'type' => array( 'required' => false,
'format' => 'string',
'default' => 'file',
'outputFormats' => array('html'),
),
'title' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array()),
),
'description' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
'modifiers' => array('insertSpecials' => array()),
),
'label' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'display' => array( 'required' => false,
'format' => 'string',
'default' => 'yes',
'outputFormats' => array(),
),
'edit' => array( 'required' => false,
'format' => 'string',
'default' => 'yes',
'outputFormats' => array(),
),
'required' => array( 'required' => false,
'format' => 'string',
'default' => 'yes',
'outputFormats' => array(),
),
'value' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'default' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'style' => array( 'required' => false,
'outputFormats' => array('html'),
'format' => 'string',
),
'class' => array( 'required' => false,
'outputFormats' => array('html'),
'format' => 'string',
),
'onchange' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array() ),
),
'onclick' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array()),
),
'onfocus' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array()),
),
'onmouseover' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array()),
),
'onmouseout' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array()),
),
'onblur' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
'modifiers' => array('insertSpecials' => array()),
),
'accesskey' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array('html'),
),
'position' => array( 'required' => false,
'format' => 'int',
'outputFormats' => array(),
),
'tabindex' => array( 'required' => false,
'format' => 'int',
'outputFormats' => array('html'),
),
'format' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'disabled' => array( 'required' => false,
'format' => 'string',
'default' => 'no',
'outputFormats' => array('html'),
),
'domain' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'selectkeylabel' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'keylabel' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'valuelabel' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'newkeylabel' => array( 'required' => false,
'format' => 'string',
'outputFormats' => array(),
),
'allowedtags' => array(
'required' => false,
'format' => 'string',
'default' => 'sub,sup,div',
'outputFormats' => array(),
),
'mandator' => array(
'required' => false,
'format' => 'string',
'default' => '',
'outputFormats' => array(),
)
);
/**
* Initialize validation codes using gettext
*
* Overwrite this function to add element's error codes
*
* @return bool $success Always returns true.
* @see $attributeDefaults
*/
public function loadValidatiorErrorCodes()
{
$this->validatorErrorCodes = array(
1 => patI18n::dgettext('wombat', 'This field is required, please complete it.')
);
// translate default attribute values
$this->attributeDefinition['selectkeylabel']['default'] = patI18n::dgettext('wombat', 'Select key');
$this->attributeDefinition['keylabel']['default'] = patI18n::dgettext('wombat', 'Key');
$this->attributeDefinition['valuelabel']['default'] = patI18n::dgettext('wombat', 'Value');
$this->attributeDefinition['newkeylabel']['default'] = patI18n::dgettext('wombat', 'Enter new key');
return true;
}
/**
* element creation method for the 'HTML' format in the 'default' form mode.
*
* @access public
* @param mixed value of the element
* @return mixed $element The element, or false if failed.
*/
public function serializeHtmlDefault($value)
{
$this->attributes['value'] = $value;
// editable or not?
if (isset($this->attributes['edit']) && $this->attributes['edit'] == 'no') {
return $this->serializeHtmlReadonly($value);
}
$keys = $this->getKVPKeys();
if (empty($this->kvpList)) {
$kvlist = $this->getKVPList($value);
} else {
$kvlist = $this->kvpList;
$keys = array_merge($keys, array_keys($kvlist));
}
sort($keys);
$atts = $this->attributes;
$atts['value'] = $value;
WBHtml_JS::add('WB/Form');
WBHtml_JS::add('WB/Form/Element');
WBHtml_JS::add('WB/Form/Element/Keyvaluepair');
WBHtml_JS::add(sprintf('document.getElementById("%s").keyvalue = new WB.Form.Element.Keyvaluepair("%1$s", "%2$s");', $atts['id'], $atts['name']), WBHtml_JS::AREA_ONLOAD);
$this->tmpl = WBClass::create('patTemplate');
$this->tmpl->readTemplatesFromInput('Form/Element/keyvaluepair.tmpl');
$this->tmpl->addGlobalVars($atts, 'root_');
$i = 0;
$list = array();
$this->tmpl->addGlobalVar('keyoption_list_count', count($keys));
foreach ($kvlist as $k => $v) {
$l = array(
'value' => $v,
'selected' => $k,
'no' => $i
);
$this->tmpl->addVars('keyoption', $l);
$this->tmpl->addVar('keyoption_list_entry', 'key', $keys);
$l['keyoption'] = $this->tmpl->getParsedTemplate('keyoption');
$list[] = $l;
++$i;
$this->tmpl->clearTemplate('keyoption');
}
// append prototype
$l = array(
'value' => '',
'selected' => '',
'no' => 'proto'
);
$this->tmpl->addVars('keyoption', $l);
$this->tmpl->addVar('keyoption_list_entry', 'key', $keys);
$l['keyoption'] = $this->tmpl->getParsedTemplate('keyoption');
$list[] = $l;
$this->tmpl->addRows('list_entry', $list);
return $this->tmpl->getParsedTemplate('form-element-snippet');
}
/**
* element creation method for the 'HTML' format in the 'readonly' form mode.
* Very simple; just returns the stored element value.
*
* @access public
* @param mixed value of the element
* @return string $value The element's value
* @todo implement this funktion
*/
public function serializeHtmlReadonly($value)
{
$html = array();
$atts = array(
'class' => $this->attributes['class']
);
$html[] = $this->createTag('div', 'opening', $atts);
$html[] = $this->createHiddenTag($value);
$html[] = $this->createTag('div', 'closing');
return implode("\n", $html);
}
/**
* validates the element.
*
* Verify file upload
*
* @param stringd $value of the element
* @return bool $isValid True if element could be validated, false otherwise.
*/
public function validateElement($value)
{
$required = false;
$name = $this->attributes['name'];
$id = $this->attributes['id'];
// store the required flag for easy access
if (isset($this->attributes['required']) && $this->attributes['required'] == 'yes') {
$required = true;
}
$this->collectDataFromRequest($name);
if (empty($this->kvpList) && $required) {
$this->addValidationError(1);
return false;
}
if (empty($value)) {
$value = '__new';
}
$this->initKVP();
$this->value = $this->kvp->set($this->kvpList, $value);
return true;
}
/**
* Collect values
*
* Walk through request data and collect input acording to selected key
*
* @param string $name
*/
private function collectDataFromRequest($name)
{
$allowed = $this->getAttribute('allowedtags');
$allowed = explode(',', $allowed);
$allowed = '<' . implode('><', $allowed) . '>';
$data = array();
$keyS = 'kvp-' . $name . '-keyselect-';
$keySl = strlen($keyS);
$keyI = 'kvp-' . $name . '-keyinput-';
$valI = 'kvp-' . $name . '-value-';
foreach ($_REQUEST as $k => $v) {
if (0 != strncmp($k, $keyS, $keySl)) {
continue;
}
if ($v == '__empty') {
continue;
}
$no = substr($k, $keySl);
if ('proto' == $no) {
continue;
}
$key = $v;
if ($v == '__input') {
$key = $_REQUEST[$keyI . $no];
}
if (empty($key)) {
continue;
}
$value = $_REQUEST[$valI . $no];
if (empty($value)) {
continue;
}
// normalize line breaks
$value = str_replace("\r\n", "\n", $value);
// newline-style-tags
$value = str_replace('
', '
', $value);
$value = str_replace('
', '', $value);
$value = strip_tags($value, $allowed);
// replace
with nl
$value = str_replace('
', "\n", $value);
// remove duplicate whitespace
$value = preg_replace('/[ \\t]+/', ' ', $value);
$value = preg_replace('/\\s+\\n/', "\n", $value);
$data[$key] = trim($value);
}
$this->kvpList = $data;
}
private function getKVPKeys()
{
$this->initKVP();
return $this->kvp->getKeys();
}
private function getKVPList($id)
{
$this->initKVP();
return $this->kvp->get($id);
}
private function initKVP()
{
if ($this->kvp) {
return;
}
/** @var WBMandator */
$mandator = WBClass::create('WBMandator');
$man = $this->getAttribute('mandator');
if (!empty($man)) {
$mandator->setId($this->getAttribute('mandator'));
}
$parameter = array(
'mandator' => $mandator
);
$this->kvp = WBClass::create('WBDatasource_KeyValuePair', $parameter);
$this->kvp->setDomain($this->attributes['domain']);
$this->kvp->switchTranslation(false);
}
}