['title' => 'Check all', 'data-tree-action' => 'checkAll'], self::LINK_UNCHECK_ALL => ['title' => 'Uncheck all', 'data-tree-action' => 'uncheckAll'], self::LINK_EXPAND_ALL => ['title' => 'Expand All', 'data-tree-action' => 'expandAll'], self::LINK_COLLAPSE_ALL => ['title' => 'Collapse All', 'data-tree-action' => 'collapseAll'], ]; protected $_defaultLinkAttribs = ['href' => '#']; /** @var array Links */ protected $_links = []; /** @var array Options for element jQuery TreeView @see http://docs.jquery.com/Plugins/Treeview/treeview */ protected $_treeViewOptions = []; protected $_scriptOptions = []; public function init() { parent::init(); $this->_initDefaultLinks(); return $this; } public function loadDefaultDecorators() { parent::loadDefaultDecorators(); if (!$this->loadDefaultDecoratorsIsDisabled()) { $this->addDecorator('JQuery_Treeview'); } if (false !== $decorator = $this->getDecorator('Label')) { $decorator->setOption('disableFor', true); } return $this; } public function isValid($value, $context = null) { if ($this->registerInArrayValidator() && !$this->getValidator('InArray')) { $multiOptions = $this->getMultiOptions(); $options = Qs_Array::treeValues($multiOptions, 'id'); $this->addValidator('InArray', true, [$options]); } return parent::isValid($value, $context); } protected function _initDefaultLinks() { if (!$this->_hasLinks) { return $this; } foreach (array_keys($this->_defaultLinks) as $name) { $this->addLink($name); } return $this; } public function getLinks() { return $this->_links; } public function clearLinks() { $this->_links = []; return $this; } public function setLinks(array $links) { $this->clearLinks(); $this->addLinks($links); return $this; } public function addLinks(array $links) { foreach ($links as $link) { $this->addLink($link); } return $this; } /** * @throws Qs_Form_Element_Exception * @param array|string $link * @return Qs_Form_Element_CheckboxTree */ public function addLink($link) { if (is_array($link)) { $this->_links[] = array_merge($this->_defaultLinkAttribs, $link); } elseif (is_string($link) && isset($this->_defaultLinks[$link])) { $this->_links[] = array_merge($this->_defaultLinkAttribs, $this->_defaultLinks[$link]); } else { throw new Qs_Form_Element_Exception('Incorrect param $link passed to ' . __METHOD__); } return $this; } public function getTreeViewOptions() { return $this->_treeViewOptions; } public function cleanTreeViewOptions() { $this->_treeViewOptions = []; return $this; } public function setTreeViewOptions(array $options) { $this->cleanTreeViewOptions(); $this->addTreeViewOptions($options); return $this; } public function addTreeViewOptions(array $options) { foreach ($options as $name => $value) { $this->addTreeViewOption($name, $value); } return $this; } public function addTreeViewOption($name, $value) { if (null === $value) { unset($this->_treeViewOptions[$name]); } else { $this->_treeViewOptions[$name] = $value; } return $this; } public function getAttribs() { $attribs = parent::getAttribs(); $attribs['links'] = $this->getLinks(); return $attribs; } public function setHasLinks($hasLinks) { $this->_hasLinks = (bool) $hasLinks; return $this; } public function getHasLinks() { return $this->_hasLinks; } public function getScriptOptions() { return $this->_scriptOptions; } public function cleanScriptOptions() { $this->_scriptOptions = []; return $this; } public function setScriptOptions(array $options) { $this->cleanScriptOptions(); $this->addScriptOptions($options); return $this; } public function addScriptOptions(array $options) { foreach ($options as $name => $value) { $this->addScriptOption($name, $value); } return $this; } public function addScriptOption($name, $value) { if (null === $value) { unset($this->_scriptOptions[$name]); } else { $this->_scriptOptions[$name] = $value; } return $this; } }