setTemplateName('block.tabs'); return true; } /** * Возвращаем префикс класса блока * @param string $plus * @return string */ public function cssClass($plus = '') { return parent::cssClass( ($this->list !== null ? '-list' : '').'-tabs' . ($plus !== '' ? '-' . $plus : '') ); } /** * Устанавливаем объект блока списка * @param BlockList $list */ public function setList(BlockList $list) { $this->list = $list; } /** * Устанавливаем активный таб * @param mixed $id ID активного таба * @return $this */ public function active($id) { if (array_key_exists($id, $this->tabs)) { $this->tabs[$id]['active'] = true; $this->activeId = $id; } return $this; } /** * Получаем активный таб * @return mixed ID активного таба */ public function getActive() { if ($this->activeId === null) { $this->fillActive(); } return $this->activeId; } /** * Включена ли у активного таба ротация * @return boolean */ public function isActiveRotation() { $activeId = $this->getActive(); return ( ! empty($this->tabs[$activeId]['rotate'])); } /** * Определение скрывать табы или нет * @return bool */ public function isHidden() { if (count($this->tabs) == 1) { $tab = reset($this->tabs); if (empty($tab['title'])) return true; } return false; } /** * Устанавливаем настройки параметра передающего активный таб * @param string $key название параметра * @param integer $validation настройки валидации TYPE_ * @param string $method источник данных */ public function setActiveSettings($key, $validation = TYPE_NOTAGS, $method = 'postget') { $this->activeKey = $key; $this->activeValidation = $validation; $this->activeMethod = $method; } /** * Получаем название параметра передающего активный таб * @return string */ public function getActiveKey() { return $this->activeKey; } /** * Получаем настройки валидации параметра передающего активный таб * @return string */ public function getActiveValidation() { return $this->activeKey; } /** * Получаем активный таб * @param string|null $method источник данных: 'get','post','postget','getpost',null(указанный ранее) * @return mixed ID активированного таба или false */ protected function fillActive($method = null) { if (empty($this->tabs)) { return false; } $value = false; if ($method === null) { $method = $this->activeMethod; } switch ($method) { case 'postget': $value = $this->input->postget($this->activeKey, $this->activeValidation); break; case 'getpost': $value = $this->input->getpost($this->activeKey, $this->activeValidation); break; case 'post': $value = $this->input->post($this->activeKey, $this->activeValidation); break; case 'get': $value = $this->input->get($this->activeKey, $this->activeValidation); break; } if (array_key_exists($value, $this->tabs)) { $this->tabs[$value]['active'] = true; $this->activeId = $value; } else if (array_key_exists($this->defaultId, $this->tabs)) { $this->tabs[$this->defaultId]['active'] = true; $this->activeId = $this->defaultId; } else { $first = reset($this->tabs); if ( ! empty($first)) { $this->tabs[$first['id']]['active'] = true; $this->activeId = $first['id']; } } return $this->activeId; } /** * Добавляем таб * @param integer|string $id уникальный ID таба * @param string $title название таба * @param boolean $default помечаем по умолчанию * @param array $extra аттрибуты * @return $this * @throws AdminFormsException */ public function add($id, $title, $default = false, array $extra = []) { if ( ! is_scalar($id)) { throw new AdminFormsException(_t('@html', '[class]: Tab identifier of the wrong type, valid: integer, float, string', ['class'=>static::class])); } else if (is_bool($id)) { $id = ($id ? 1 : 0); } $this->tabs[$id] = array_merge(array( 'id' => $id, 'title' => $title, 'attr' => [], 'default' => !empty($default), 'rotate' => false, 'counter' => null, 'style' => 'default', 'collapsed' => false, 'icon' => false, 'link' => false, 'ajax' => false, ), $extra); if (!empty($default)) { $this->defaultId = $id; } $this->currentId = $id; return $this; } /** * Действия выполняемые перед отрисовкой блока */ protected function beforeRender() { parent::beforeRender(); if ($this->activeId === null) { $this->fillActive(); } } /** * Сделать таб текущим (для изменения параметров) * @param mixed $id ID таба * @return $this */ public function _current($id) { if ($id && isset($this->tabs[$id])) { $this->currentId = $id; } return $this; } /** * Установить название * @param string $title * @return $this */ public function title($title) { if ($this->currentId && isset($this->tabs[$this->currentId])) { $this->tabs[$this->currentId]['title'] = $title; } return $this; } /** * Добавить атрибут * @param string $key ключ атрибута * @param mixed $value значение * @return $this */ public function attr($key, $value) { if ($this->currentId && isset($this->tabs[$this->currentId])) { static::attrAdd($this->tabs[$this->currentId]['attr'], $key, $value); } return $this; } /** * Устновить флаг перетаскивания записей * @param bool $rotate * @return $this */ public function rotate($rotate) { if ($this->currentId && isset($this->tabs[$this->currentId])) { $this->tabs[$this->currentId]['rotate'] = $rotate; } return $this; } /** * Указать значение для счетчика * @param integer|string|callable|array $counter * @return $this */ public function counter($counter) { if ($this->currentId && isset($this->tabs[$this->currentId])) { $this->tabs[$this->currentId]['counter'] = $counter; } return $this; } /** * Указать стиль для счетчика * @param string $style * @return $this */ public function style($style) { if ($this->currentId && isset($this->tabs[$this->currentId])) { $this->tabs[$this->currentId]['style'] = $style; } return $this; } /** * Указать флаг сворачивания для счетчика * @param bool $collapsed * @return $this */ public function collapsed($collapsed) { if ($this->currentId && isset($this->tabs[$this->currentId])) { $this->tabs[$this->currentId]['collapsed'] = $collapsed; } return $this; } /** * Получить значение счетчика для таба * @param integer $id ID таба или null - для текущего таба * @return string */ public function counterValue($id = null) { do { if (is_null($id)) { $id = $this->currentId; } if (is_null($id)) { break; } if ( ! isset($this->tabs[$id])) { break; } return tpl::counterValue($this->tabs[$id]['counter']); } while(false); return ''; } }