[ 'tag' => 'button', 'text' => 'Add', 'attribs' => [ 'type' => 'button', 'data-multitext-do' => 'add', ], ], 'delete' => [ 'tag' => 'button', 'text' => 'Delete', 'attribs' => [ 'type' => 'button', 'data-multitext-do' => 'delete', 'data-confirm-msg' => 'Do you really want to delete this row?', 'class' => 'del btn btn-danger btn-small', ], ], 'up' => [ 'tag' => 'button', 'text' => '', 'attribs' => [ 'type' => 'button', 'data-multitext-do' => 'up', 'class' => 'btn btn-default btn-small fa fa-long-arrow-up', ], ], 'down' => [ 'tag' => 'button', 'text' => '', 'attribs' => [ 'type' => 'button', 'data-multitext-do' => 'down', 'class' => 'btn btn-default btn-small fa fa-long-arrow-down', ], ], ]; public function setButton(array $options) { if (empty($this->_buttonOptions)) { $this->_buttonOptions = $this->_buttonDefaults; } $this->_buttonOptions = Qs_Array::mergeRecursive($this->_buttonOptions, $options); vdie($options, $this->_buttonOptions); return $this; } public function getButton($name) { if (empty($this->_buttonOptions)) { $this->_buttonOptions = ($options = $this->getOption('button')) ? Qs_Array::mergeRecursive($this->_buttonDefaults, $options) : $this->_buttonDefaults; } if (!array_key_exists($name, $this->_buttonOptions)) { throw new Exception('Unknown button "' . $name . '"'); } return $this->_buttonOptions[$name]; } protected function _initOptions() { $this->_options = Qs_Array::exclude($this->getElement()->getAttribs(), 'helper', 'options'); } public function getOptions() { if (null === $this->_options) { $this->_initOptions(); } return $this->_options; } public function getOption($key) { if (null === $this->_options) { $this->_initOptions(); } return parent::getOption($key); } 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() { /** @var Qs_Doc $doc */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/lib/form-element-multitext.js') ->addInitObject( 'qs.form.element.MultiText', [ [ 'id' => $this->getElement()->getId(), 'template' => $this->_renderRow('', [], ''), ] ] ); return $this; } public function render($content) { // init options $this->getOptions(); $element = $this->getElement(); $placement = $this->getPlacement(); $noAttribs = $this->getOption('noAttribs'); $this->removeOption('button'); $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(); } $html = $this->_getOpenTag('table', $attribs); $html .= $this->_renderBody(); $html .= $this->_renderFooter(); $html .= $this->_getCloseTag('table'); switch ($placement) { case self::APPEND: $content = $content . $html; break; case self::PREPEND: $content = $html . $content; break; } return $content; } protected function _renderBody() { $element = $this->getElement(); $html = $this->_getOpenTag('tbody'); $index = 0; $values = (array)$element->getValue() ? : ['']; foreach ($values as $value) { $html .= $this->_renderRow((string)$value, [], ++$index); } $html .= $this->_getCloseTag('tbody'); return $html; } protected function _renderRow($value, array $attribs, $number) { $element = $this->getElement(); $view = $element->getView(); $html = $this->_getOpenTag('tr', ['data-multitext-row' => '']); if ($element->isNumering()) { $html .= $this->_getOpenTag('td', ['class' => 'no']); $html .= str_replace( '%number%', '' . $number . '', $element->getNumeringFormat() ); $html .= $this->_getCloseTag('td'); } $html .= $this->_getOpenTag('td', ['class' => 'element']); $html .= $view->formText($element->getFullyQualifiedName(), $value, $attribs); $html .= $this->_getCloseTag('td'); $html .= $this->_getOpenTag('td', ['class' => 'options']); $html .= $this->_renderOptionsCell(); $html .= $this->_getCloseTag('td'); $html .= $this->_getCloseTag('tr'); return $html; } protected function _renderFooter() { $element = $this->getElement(); $html = $this->_getOpenTag('tfoot'); $html .= $this->_getOpenTag('tr'); $html .= $this->_getOpenTag('td', ['colspan' => ($element->isNumering()) ? 3 : 2]); $html .= $this->_renderButton('add'); $html .= $this->_getCloseTag('td'); $html .= $this->_getCloseTag('tr'); $html .= $this->_getCloseTag('tfoot'); return $html; } protected function _getText($text) { if ($translator = $this->getElement()->getTranslator()) { return $translator->translate($text); } return $text; } protected function _renderOptionsCell() { $html = ''; if ($this->getElement()->isSortable()) { $html .= $this->_renderButton('up') . ' '; $html .= $this->_renderButton('down') . ' '; } $html .= $this->_renderButton('delete'); return $html; } protected function _renderButton($name) { $options = $this->getButton($name); $html = $this->_getOpenTag($options['tag'], $options['attribs']); $html .= empty($options['text']) ? '' : $this->_getText($options['text']); $html .= $this->_getCloseTag($options['tag']); return $html; } }