_options) {
$this->_options = Qs_Array::exclude($this->getElement()->getAttribs(), 'helper', 'options');
}
return $this->_options;
}
public function setOptions(array $options)
{
if (isset($options['options'])) {
unset($options['options']);
}
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (method_exists($this, $method)) {
$this->$method($value);
} else {
$this->_options[$key] = $value;
}
}
return $this;
}
public function initRender()
{
$doc = Zend_Registry::get('doc');
$doc->addScript('js/lib/form-element-multitext.js')
->addInitFunction(
'Qs_Form_Element_MultiText.init',
array($this->getElement()->getId(), $this->_getScriptOptions())
);
return $this;
}
public function render($content)
{
// init options
$this->getOptions();
$tag = $this->getTag();
$element = $this->getElement();
$id = $element->getId();
$view = $element->getView();
$placement = $this->getPlacement();
$noAttribs = $this->getOption('noAttribs');
$this->removeOption('noAttribs');
$this->removeOption('openOnly');
$this->removeOption('closeOnly');
$attribs = null;
if (!$noAttribs) {
$attribs = $this->getOptions();
}
if (!isset($attribs['id'])) {
$attribs['id'] = $element->getId();
}
if (!isset($attribs['class'])) {
$attribs['class'] = $element->getClass();
}
$elementAttribs = array(
'class' => 'text',
'id' => $element->getId() . '-0',
);
$elementContent = $this->_getOpenTag('table', $attribs);
$elementContent .= $this->_getOpenTag('tfoot');
$elementContent .= $this->_getOpenTag('tr');
$elementContent .= $this->_getOpenTag('td', array('colspan' => ($element->isNumering()) ? 3 : 2));
$elementContent .= $this->_renderAddLink();
$elementContent .= $this->_getCloseTag('td');
$elementContent .= $this->_getCloseTag('tr');
$elementContent .= $this->_getCloseTag('tfoot');
$elementContent .= $this->_getOpenTag('tbody');
$values = $element->getValue();
if (empty($values)) {
$values = array('');
}
$index = 1;
foreach ($values as $value) {
$elementContent .= $this->_getOpenTag('tr');
if ($element->isNumering()) {
$elementContent .= $this->_getOpenTag('td', array('class' => 'no'));
$elementContent .= str_replace(
'%number%',
'' . ($index++) . '',
$element->getNumeringFormat()
);
$elementContent .= $this->_getCloseTag('td');
}
$elementContent .= $this->_getOpenTag('td', array('class' => 'element'));
$elementAttribs['id'] = $element->getId() . '-' . $index;
$elementContent .= $view->formText($element->getFullyQualifiedName(), $value, $elementAttribs);
$elementContent .= $this->_getCloseTag('td');
$elementContent .= $this->_getOpenTag('td', array('class' => 'options'));
$elementContent .= $this->_renderOptions();
$elementContent .= $this->_getCloseTag('td');
$elementContent .= $this->_getCloseTag('tr');
}
$elementContent .= $this->_getCloseTag('tbody');
$elementContent .= $this->_getCloseTag('table');
switch ($placement) {
case self::APPEND:
return $content . $elementContent;
case self::PREPEND:
return $elementContent . $content;
}
}
protected function _renderScriptOptions()
{
$options = $this->_getScriptOptions();
return json_encode($options);
}
protected function _getScriptOptions()
{
$options = array(
'name' => $this->getElement()->getFullyQualifiedName(),
'isNumering' => $this->getElement()->isNumering(),
'isSortable' => $this->getElement()->isSortable(),
'numeringFormat' => $this->getElement()->getNumeringFormat(),
);
if (!($itemName = $this->getElement()->getItemName())) {
$itemName = 'row';
}
$options['itemName'] = $this->_getText($itemName);
return $options;
}
protected function _getText($text)
{
if ($translator = $this->getElement()->getTranslator()) {
return $translator->translate($text);
}
return $text;
}
protected function _renderLink($text, $attribs, $separator = '')
{
$content = $this->_getOpenTag('a', $attribs)
. $this->_getText($text)
. $this->_getCloseTag('a')
. '' . $separator . '';
return $content;
}
protected function _renderOptions()
{
$id = $this->getElement()->getId();
$content = '';
if ($this->getElement()->isSortable()) {
$content .= $this->_renderLink(
'up',
array(
'class' => 'up',
'href' => '#',
'onclick' => 'return Qs_Form_Element_MultiText.up(this, \'' . $id . '\');'
),
' | '
);
$content .= $this->_renderLink(
'down',
array(
'class' => 'down',
'href' => '#',
'onclick' => 'return Qs_Form_Element_MultiText.down(this, \'' . $id . '\');'
),
' | '
);
}
$content .= $this->_renderLink(
$this->getElement()->getDeleteText(),
array(
'class' => 'del',
'href' => '#',
'onclick' => 'return Qs_Form_Element_MultiText.del(this, \'' . $id . '\');'
)
);
return $content;
}
protected function _renderAddLink()
{
$id = $this->getElement()->getId();
$content = $this->_getOpenTag(
'a',
array(
'id' => $id . '-add',
'onclick' => "return Qs_Form_Element_MultiText.add('{$id}')",
'href' => '#'
)
);
$content .= $this->_getText($this->getElement()->getAddText());
$content .= $this->_getCloseTag('a');
return $content;
}
}