'Empty List' ]; protected $_orientation = self::ORIENTATION_HORIZONTAL; protected $_itemName; protected $_itemsName; protected $_itemClass; protected $_required; protected $_resources = [ 'scripts' => [ 'js/qs/form/subform/table.js' ] ]; public function setText($name, $text) { $this->_text[$name] = $text; return $this; } public function getText($name) { return Qs_Array::get($this->_text, $name); } public function getOrientation() { return $this->_orientation; } public function setOrientation($orientation) { $this->_orientation = $orientation; return $this; } public function getItemName() { return $this->_itemName; } public function setItemName($itemName) { $this->_itemName = $itemName; return $this; } public function getItemsName() { return $this->_itemsName; } public function setItemsName($itemsName) { $this->_itemsName = $itemsName; return $this; } public function getItemClass() { return $this->_itemClass; } public function setItemClass($itemClass) { $this->_itemClass = $itemClass; return $this; } public function isRequired() { return $this->_required; } public function setRequired($required = true) { $this->_required = $required; return $this; } protected function _initElements() { foreach ($this->getRows() as $index => $defaults) { $name = '' . $index; $options = [ 'name' => $name, 'orientation' => $this->getOrientation(), 'defaults' => $defaults, ]; $this->addSubForm(new $this->_itemClass($options), $name); } $this->addAddNewButton(); return parent::_initElements(); } protected function getRows() { return $this->_defaults; } protected function addAddNewButton() { $decorators = [ 'ViewHelper', 'Errors', [ ['td' => 'HtmlTag'], [ 'tag' => 'td', 'class' => 'bottom-row', 'colspan' => $this->getColumnCount(), 'id' => ['callback' => ['Zend_Form_Element', 'resolveElementId']] ], ], [['tr' => 'HtmlTag'], ['tag' => 'tr']] ]; $this->addElement( 'button', 'btnAddNew', [ 'label' => 'Add New ' . $this->getItemName(), 'data-action-add' => '', 'class' => 'btn btn-default btn-small', 'decorators' => $decorators, ] ); return $this; } protected function getColumnCount() { static $count; if (null === $count) { $count = ($form = $this->getItemSubForm()) ? $form->count() : 1; } return $count; } /** * @return Table\Row * @throws \Exception */ public function getItemSubForm() { static $item; if (null === $item) { $item = current($this->getSubForms()); if (!$item) { if (null === $this->_itemClass) { throw new Exception('itemClass is not deifned'); } $item = new $this->_itemClass(); if (!$item instanceof Table\Row) { throw new Exception('itemClass should be instance of ' . Table\Row::CLASS); } } } return $item; } public function loadDefaultDecorators() { if ($this->loadDefaultDecoratorsIsDisabled()) { return $this; } if (($decorators = $this->getDecorators())) { return $this; } $labelAttribs = [ 'class' => $this->isRequired() ? 'required' : 'optional', ]; if ($this->getOrientation() == self::ORIENTATION_HORIZONTAL) { $this->addDecorator('TableHeader'); $this->addDecorator(['theadTr' => 'HtmlTag'], ['tag' => 'tr']); $this->addDecorator(['thead' => 'HtmlTag'], ['tag' => 'thead']); } $this->addDecorator(['tbodyOpen' => 'HtmlTag'], ['tag' => 'tbody', 'openOnly' => true, 'placement' => 'append']); $this->addDecorator('FormElements'); $this->addDecorator(['tbodyClose' => 'HtmlTag'], ['tag' => 'tbody', 'closeOnly' => true, 'placement' => 'append']); $tableAttribs = [ 'tag' => 'table', 'id' => $this->getId(), 'class' => 'table table-bordered table-striped table-form', ]; if (empty($this->getRows())) { $tableAttribs['class'] .= ' table-empty'; } $this->addDecorator(['table' => 'HtmlTag'], $tableAttribs); $this->addDecorator('FormErrorMessages'); $this->addDecorator('DtDdWrapper', ['label' => $this->getItemsName(), 'labelAttribs' => $labelAttribs]); return $this; } protected function _addResources() { parent::_addResources(); $options = [ 'id' => $this->getId(), 'name' => $this->getElementsBelongTo(), 'itemName' => $this->getItemName(), 'sortable' => false, 'numerable' => false, 'removable' => false, 'nodes' => [ 'items' => '>tbody>[data-collection-item]', 'add' => '>tbody>tr>td>[data-action-add]', ] ]; $item = $this->getItemSubForm(); if ($item instanceof OptionsInterface) { if (($options['sortable'] = $item->isSortable())) { $options['nodes']['moveUp'] = '>td>[data-action-up]'; $options['nodes']['moveDown'] = '>td>[data-action-down]'; } if (($options['removable'] = $item->isRemovable())) { $options['nodes']['delete'] = '>td>[data-action-delete]'; } if (($options['numerable'] = $item->isNumerable())) { $options['nodes']['rowNumber'] = '>[data-row-number]'; } } $this->getDoc()->addInitObject('qs.form.subform.Table', [$options]); return $this; } }