*/ WBClass::load('WBHtml_JS', 'WBUser'); /** * patForms Elemenbt: vfsselect * * Select files from VFS * * @version 0.2.0 * @package Wombat * @subpackage patForms */ class patForms_Element_Vfsselect 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 = 'Vfsfile'; /** * element type to create HTML output * @var array */ public $elementType = array("html" => "input"); /** * session * @var patSession_Storage */ private $sess; /** * session * @var WBVFS_File */ private $file; /** * 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(), ), 'name' => array( 'required' => true, 'format' => 'string', 'outputFormats' => array('html'), 'modifiers' => array('insertSpecials' => array()), ), 'type' => array( 'required' => false, 'format' => 'string', 'default' => 'file', 'outputFormats' => array(), ), 'default' => array( 'required' => false, 'format' => 'string', 'outputFormats' => array(), ), '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(''), ), '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() ), ), '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()), ), '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'), ), 'user' => array( 'required' => false, 'format' => 'string', 'outputFormats' => array(), ), 'chroot' => array( 'required' => false, 'format' => 'int', 'outputFormats' => array(), ), 'multiple' => array( 'required' => false, 'format' => 'string', 'outputFormats' => array( 'html' ), ), 'min' => array( 'required' => false, 'format' => 'int', 'outputFormats' => array(), ), 'max' => array( 'required' => false, 'format' => 'int', 'outputFormats' => array(), ), 'listformat' => array( 'required' => false, 'format' => 'string', 'default' => 'array', 'outputFormats' => array(), ), 'mimetype' => array( 'required' => false, 'format' => 'string', 'default' => '*/*', 'outputFormats' => array(), ), 'thumbnailsize' => array( 'required' => false, 'format' => 'string', 'default' => '50x50', '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.'), // 2 => patI18n::dgettext('wombat', 'The values given for the element does not match any of the possible values.' ), 3 => patI18n::dgettext('wombat', 'You have to select at least [MIN] entries.' ), 4 => patI18n::dgettext('wombat', 'You may not select more than [MAX] entries.' ), ); return true; } /** * element creation method for the 'HTML' format in the 'default' form mode. * * @param mixed value of the element * @return mixed $element The element, or false if failed. */ public function serializeHtmlDefault($value) { $this->startSession(); $value = $this->normalizeValue($value); // editable or not? if (isset($this->attributes['edit']) && $this->attributes['edit'] == 'no') { return $this->serializeHtmlReadonly($value); } $id = $this->attributes['id']; WBHtml_JS::add('WB/Form'); WBHtml_JS::add('WB/Form/Element'); WBHtml_JS::add('WB/Form/Element/Vfsselect'); WBHtml_JS::add(sprintf('$("%s").selector = new WB.Form.Element.Vfsselect("%1$s", "file");', $id), WBHtml_JS::AREA_FOOT); $className = ''; if (isset($this->attributes['class']) && !empty($this->attributes['class'])) { $className = $this->attributes['class'] . ' '; } // control element $atts = $this->getAttributesFor('html'); $atts['onclick'] = "$('$id').selector.select(this, '[[PATH]]', '{CONTENT_PATH}');"; $atts['class'] = $className . 'control'; $control = $this->createTag('div', 'full', $atts, 'Datei(en) auswählen...'); // list element $atts = array(); $atts['class'] = $className . 'list'; $list = $this->createTag('div', 'opening', $atts) . $this->values2HtmlList($value) . $this->createTag('div', 'closing'); // surrounding box element $atts = array(); $atts['class'] = $className . 'vfsselect'; $box = $this->createTag('div', 'opening', $atts) . $control . $list . $this->createHiddenTag(implode(',', $value)) . $this->createTag('div', 'closing'); return $box; } private function values2HtmlList($value, $buttons = true) { $className = ''; if (isset($this->attributes['class']) && !empty($this->attributes['class'])) { $className = $this->attributes['class'] . ' '; } $list = array(); $id = $this->attributes['id']; $ts = '50x50'; if (isset($this->attributes['thumbnailsize']) && !empty($this->attributes['thumbnailsize'])) { $ts = $this->attributes['thumbnailsize']; } WBHtml_JS::add(sprintf('$("%s").selector.setThumbnailSize("%s");', $id, $ts), WBHtml_JS::AREA_FOOT); foreach ($value as $v) { if (empty($v)) { continue; } $file = $this->file->loadById($v); if (!$file->isOK()) { continue; } $o = $file->getObscureId(); $n = $file->getName(); $item = array(); $item[] = '
'; $item[] = ''; $item[] = sprintf('', $o, $ts); $item[] = ''; if ($buttons) { $item[] = sprintf('entfernen', $v, $id, $v); } $item[] = sprintf('%s', $n); $item[] = '
'; $list[] = implode('', $item); } // end tag $atts = array(); $atts['class'] = $className . 'end'; $end = $this->createTag('div', 'full', $atts, ''); $list[] = $end; return implode("\n", $list); } /** * 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 */ public function serializeHtmlReadonly($value) { $this->startSession(); $value = $this->normalizeValue($value); $className = ''; if (isset($this->attributes['class']) && !empty($this->attributes['class'])) { $className = $this->attributes['class'] . ' '; } // list element $atts = array(); $atts['class'] = $className . 'list'; $list = $this->createTag('div', 'opening', $atts) . $this->values2HtmlList($value) . $this->createTag('div', 'closing'); // surrounding box element $atts = array(); $atts['class'] = $className . 'vfsselect'; $box = $this->createTag('div', 'opening', $atts) . $list . $this->createHiddenTag(implode(',', $value)) . $this->createTag('div', 'closing'); return $box; } /** * validates the element. * * Verify file selection * * @param stringd $value of the element * @return bool $isValid True if element could be validated, false otherwise. */ public function validateElement($value) { $this->startSession(); $value = $this->normalizeValue($value); // required & empty if (isset($this->attributes['required']) && $this->attributes['required'] == 'yes' && empty($value)) { $this->addValidationError(1); return false; } // remove unwanted $mimetype = array('*'); if (isset($this->attributes['mimetype']) && !empty($this->attributes['mimetype'])) { $mimetype = explode('/', strtolower($this->attributes['mimetype'])); if (empty($mimetype) || empty($mimetype[0])) { $mimetype = array('*'); } } $validated = array(); foreach($value as $v) { // check for existing file $file = $this->file->loadById($v); if (!$file->isOK()) { continue; } // remove files of unwanted mime type if ($mimetype[0] != '*') { $mime = $file->getMime(WBVFS_File::MIME_MAJOR); if ($mime != $mimetype[0]) { continue; } if (isset($mimetype[1]) && $mimetype[1] != '*') { $mime = $file->getMime(WBVFS_File::MIME_MINOR); if ($mime != $mimetype[1]) { continue; } } } $validated[] = $v; } // set value after validation $this->value = $validated; // count $amountSelected = count($validated); if (isset($this->attributes['min']) && $amountSelected < $this->attributes['min']) { $this->addValidationError(3, array('min' => $this->attributes['min'])); return false; } if (isset($this->attributes['max']) && $amountSelected > $this->attributes['max']) { $this->addValidationError( 4, array('max' => $this->attributes['max'])); return false; } return true; } /** * Redefinition of the main patForms_Element method that manages * converting string values to the internal format. * * @access public * @param string|array A single value as string, or a selected values list * @see patForms_Element::setValue() */ public function setValue($value) { $value = $this->normalizeValue($value); return parent::setValue($value); } /** * implement special behaviour for array * * Format value as configured in attribute: "listformat": * * - string: a comma separated list of ids * - array: an array with ids * * @see patForms_Element::getValue() * @return mixed $value */ public function getValue() { $value = parent::getValue(); if (empty($value)) { $value = array(); } $value = $this->normalizeValue($value); if (isset($this->attributes['listformat']) && $this->attributes['listformat'] == 'array') { return $value; } // "string" otherwise return implode(',', $value); } /** * normalize value array * * Normalize array or string list of comma separated ids to array * * @param mixed $value * @return array $value */ private function normalizeValue($value) { if (empty($value)) { $value = array(); } if (!is_array($value)) { $value = explode(',', $value); } for ($i = 0; $i < count($value); ++$i) { if (empty($value[$i])) { unset($value[$i]); } } $value = array_values(array_unique($value)); return $value; } /** * start session and other object * * this method runs only once per instance */ private function startSession() { if ($this->sess) { return; } $this->sess = WBClass::create('patSession'); // user id for VFS $user = null; if (isset($this->attributes['user']) && !empty($this->attributes['user'])) { $user = $this->attributes['user']; } $params = array(); if ($user) { $this->sess->set('vfsfile_user', $user); $params['user'] = $user; } else { $this->sess->clear('vfsfile_user'); $user = WBUser::getCurrent(); $params['user'] = $user->getId(); } $this->file = WBClass::create('WBVFS_File', $params); // chroot if (isset($this->attributes['chroot']) && !empty($this->attributes['chroot'])) { $this->sess->set('vfsfile_chroot', $this->attributes['chroot']); } else { $this->sess->clear('vfsfile_chroot'); } } }