customOptions as $field) { if (array_key_exists($field, $info['attribs'])) { $info[$field] = $info['attribs'][$field]; unset($info['attribs'][$field]); } } $info['tableAttribs']['id'] = $info['id']; if (empty($info['value'])) { $info['value'] = []; } return $info; } public function getInfo($field = null, $default = null) { return Qs_Array::get($this->info, $field, $default); } public function formCheckboxTreeTable($name, $value = null, $attribs = null, $options = null, $listsep = null) { $this->info = $this->_getInfo($name, $value, $attribs, $options, $listsep); /** @var Qs_Doc $doc */ $doc = Zend_Registry::get('doc'); $doc->addStylesheet('css/thirdpart/jquery.treetable.css'); $doc->addStylesheet('css/thirdpart/jquery.treetable.theme.default.css'); $doc->addScript('js/jquery.treetable.js'); $doc->addScript('js/lib/form/element/checkboxTreeTable.js'); $scriptOptions = [ 'id' => $this->getInfo('id'), 'mainColumnName' => $this->getMainColumnName(), 'treetable' => ['expandable' => true], 'expandChecked' => $this->getInfo('expandChecked', true), 'toggleDialog' => $this->getInfo('toggleDialog', []), ]; $doc->addInitObject('lib.form.element.CheckboxTreeTable', [$scriptOptions]); return $this->renderLinks() . $this->renderTable(); } protected function renderLinks() { $links = [ 'checkAll' => 'Check All', 'uncheckAll' => 'Uncheck All', 'expandAll' => 'Expand All', 'collapseAll' => 'Collapse All', ]; $items = []; foreach ($links as $action => $title) { $items[] = Html::renderContainer(['li', 'a'], $title, [ 'data-tree-action' => $action, 'href' => '#', 'onclick' => 'return false', ]); } return Html::renderContainer('ul', implode($items), ['class' => 'nav nav-pills top-links']); } protected function renderTable() { return Html::renderContainer( 'table', $this->renderTableHeaders() . Html::renderContainer('tbody', $this->renderTableRows($this->getInfo('options'))), $this->getInfo('tableAttribs', []) ); } protected function renderTableRows($options, $parentId = null) { $xhtml = ''; foreach ($options as $index => $option) { $xhtml .= $this->renderTableRow($option, $parentId); if (!empty($option['sub'])) { $xhtml .= $this->renderTableRows($option['sub'], $option['id']); } } return $xhtml; } protected function renderTableHeaders() { $xhtml = ''; $columns = $this->getInfo('columns', []); $hasTitle = false; foreach ($columns as $column) { if (!empty($column['title'])) { $hasTitle = true; break; } } if (!$hasTitle) { return $xhtml; } foreach ($columns as $column) { $attribs = [ 'class' => $column['name'] . '-header' ]; $xhtml .= Html::renderContainer('th', $this->view->escape($column['title']), $attribs); } return Html::renderContainer(['thead', 'tr'], $xhtml); } protected function getValue($id, $field = null, $default = null) { static $rows; if (null == $rows) { $rows = Qs_Array::group($this->getInfo('value', []), $this->getMainColumnName(), []); } if (!array_key_exists($id, $rows)) { if (null === $field) { return []; } return $default; } return Qs_Array::get($rows[$id], $field, $default); } protected function getMainColumnName() { $columns = $this->getInfo('columns', []); foreach ($columns as $column) { if ($column['type'] == CheckboxTreeTableElement::MAIN) { return $column['name']; } } return null; } protected function isRowDisabled($row) { return 'y' == $row['disabled'] || $this->getValue($row['id'], $this->getMainColumnName()) != $row['id']; } protected function renderTableRow($row, $parentId) { $xhtml = ''; $columns = $this->getInfo('columns', []); $prefix = $this->getInfo('name') . "[{$this->rowIndex}]"; foreach ($columns as $column) { $name = $prefix . '[' . $column['name'] . ']'; $value = $this->getValue($row['id'], $column['name'], Qs_Array::get($column, 'value')); switch ($column['type']) { case CheckboxTreeTableElement::MAIN: $elementContent = $this->renderCheckbox( $name, $row['id'], $row['id'] == $value, $row['title'], ['disable' => 'y' == $row['disabled'], 'data-main' => ''] ); break; case 'select': $multiOptions = is_callable($column['multiOptions']) ? call_user_func($column['multiOptions'], $row) : $column['multiOptions']; $elementContent = $this->view->formSelect( $name, $value, ['disable' => $this->isRowDisabled($row), 'data-other' => ''], $multiOptions ); break; default: throw new Exception('Unsupported column type "' . $column['type'] . '""'); } $xhtml .= Html::renderContainer('td', $elementContent); } $trAttribs = ['data-tt-id' => $row['id']]; if ($parentId) { $trAttribs['data-tt-parent-id'] = $parentId; } $this->rowIndex++; return Html::renderContainer('tr', $xhtml, $trAttribs); } protected function renderCheckbox($name, $value, $checked, $label, $attribs = []) { $attribs['disableHidden'] = true; $attribs['checked'] = $checked; $attribs['class'] = 'checkbox'; return $this->view->formCheckbox( $name, $value, $attribs, [] ) . $this->view->formLabel($name, $label); } }