icon(); } /** * Заголовок обвертки * @param string|callable $title * @return BlockWrapper */ public function title($title) { $this->title = $title; return $this; } /** * Заголовок обвертки по умолчанию * @param string $title * @return BlockWrapper */ public function titleDefault($title) { $this->titleDefault = $title; return $this; } /** * Добавление атрибута обвертки * @param string $key ключ атрибута * @param string $value значение атрибута * @return BlockWrapper */ public function attr($key, $value) { static::attrAdd($this->attr, $key, $value); return $this; } /** * Добавление ссылки справа * @param string $title заголовок * @param string $href url * @param string $class класс * @param array $opts опции * @return BlockWrapper */ public function link($title, $href = '', $class = '', $opts = []) { if ( ! isset($opts['attr'])) { $opts['attr'] = []; } $opts['title'] = $title; if ($href) { static::attrAdd($opts['attr'], 'href', $href); } if ($class) { static::attrAdd($opts['attr'], 'class', $class); } $this->link = $opts; return $this; } /** * Добавление ссылки перехода на фронтенд * @param string|callable $link адресс ссылки или функция, фозвращающая массив ['link' => string, 'title' => string] * @param string $title заголовок ссылки * @param array $opts доп. параметры * @return self */ public function linkOut($link, $title = '', $opts = []) { if (empty($link)) { $this->linkOut = []; } else { if (is_callable($link)) { $opts['callable'] = $link; } else { $opts = array_merge($opts, [ 'link' => $link, 'title' => $title, ]); } } $this->linkOut = $opts; return $this; } /** * Класс иконки * @param bool|mixed $icon клас иконки или true - список false - форма * @return $this */ public function icon($icon = true) { if (is_bool($icon)) { $this->icon = $icon ? 'icon-align-justify' /* список */ : 'icon-edit' /* форма */; } else { $this->icon = $icon; } return $this; } /** * Скрыть обвертку * @param bool $hide * @return $this */ public function hide($hide = true) { $this->hide = $hide; return $this; } /** * Создать невидимую обвертку * @param bool $invisible * @return $this */ public function invisible($invisible = true) { $this->invisible = $invisible; return $this; } /** * Вывести форму в popup окне * @param bool|array $popup * @return $this */ public function popup($popup = true) { $this->popup = $popup; return $this; } /** * @param string $title заголовок * @param string|\Closure $href ссылка для перехода * @param bool|string $confirm спросить подтверждение (текст подтверждения) * @param string $icon класс иконки * @param array $attr атрибуты ['debug-only'] * @return BlockWrapper */ public function fordev($title, $href, $confirm = false, $icon = '', array $attr = []) { func::array_defaults($attr, [ 'debug-only' => false, # доступно только в режиме отладки ]); do { if (empty($title)) break; if (empty($href)) break; $attr['title'] = $title; $attr['href'] = $href; $attr['confirm'] = $confirm; $attr['icon'] = $icon; $this->fordev[] = $attr; }while(false); return $this; } /** * Вернуть значение свойства или все свойства в виде массива * @param string $name название свойства * @return array */ public function data($name = '') { if ($name && property_exists($this, $name)) { return $this->$name; } return [ 'attr' => $this->attr, 'icon' => $this->icon, 'title' => $this->title, 'titleDefault' => $this->titleDefault, 'fordev' => $this->fordev, 'link' => $this->link, ]; } /** * Начало блока для обворачивания * @return BlockWrapper */ public function start() { ob_start(); ob_implicit_flush(true); return $this; } /** * Конец блока для обварачивания */ public function stop() { $data = ['content' => ob_get_clean()]; if (is_callable($this->_onStop)) { $data = call_user_func($this->_onStop, $data); } echo $this->view($data); } /** * Функция вызываемая в конце блока для обварачивания * @param callable $callable аргументы array $data = ['content'] 'content' - html код блока, должна вернуть массив, с элементом 'content' * @return BlockWrapper */ public function onStop(callable $callable) { $this->_onStop = $callable; return $this; } /** * Функция для добавления кода в заголовок * @param callable $callable аргументы ($this) должна вернуть string HTML * @return BlockWrapper */ public function inHeader(callable $callable) { $this->_inHeader = $callable; return $this; } /** * Отрисовываем блок * @param array $data доп. данные шаблона * @return string HTML */ public function view(array & $data = []) { if ($this->hide) return $data['content']; if ($this->invisible) return 'attr).'>'.$data['content'].''; # помечаем дальнейшее отсутствие необходимости открывать блок в основном шаблоне tplAdmin::adminPageSettings(array('custom' => true)); $data = array_merge($data, $this->data()); if ($this->isAdminFordev()) { foreach ($data['fordev'] as $k => & $v) { foreach ($v as & $vv) { $vv = static::obCallableAuto($vv); } unset($vv); if ( ! empty($v['debug-only']) && ! $this->isDebug()) { unset($data['fordev'][$k]); continue; } if (empty($v['onclick'])) { if ( ! empty($v['confirm'])) { $sure = 'sure'; if (is_string($v['confirm'])) { $sure = HTML::escape($v['confirm']); } $v['attr']['onclick'] = "return bff.confirm('".$sure."', {r:'".$v['href']."'});"; } else { $v['attr']['onclick'] = "return bff.redirect('".$v['href']."');"; } } } unset($v); } else { unset($data['fordev']); } $linkOut = []; if ( ! empty($this->linkOut)) { if (isset($this->linkOut['callable'])) { $linkOut = call_user_func($this->linkOut['callable'], $this); } else { $linkOut = $this->linkOut; } } $data['linkOut'] = $linkOut; $data['title'] = static::obCallableAuto($data['title']); if (empty($data['title'])) { $data['title'] = $data['titleDefault'] ?? ''; } $data['inHeader'] = $this->_inHeader; if ( ! empty($this->popup)) { $data['popup'] = is_array($this->popup) ? $this->popup : []; if (isset($data['popup']['width'])) { static::attrAdd($data['popup'], 'style', 'width:'.$data['popup']['width'].'px;'); unset($data['popup']['width']); } return $this->render($data, 'block.wrapper.popup'); } $html = $this->render($data, 'block.wrapper'); return $html; } } # todo: move to separate file trait BlockWrap { /** @var BlockWrapper */ private $_wrap = null; /** * Инициализация обвертки * @return BlockWrapper */ public function wrapper() { if ( ! $this->_wrap) { $this->_wrap = new BlockWrapper(); //$this->_wrap->setTemplateDir($this->getTemplateDir()); $this->_wrap->setTemplateDir(bff()->corePath('tpl/admin/' . bff::adminTheme())); $this->_wrap->init(); } return $this->_wrap; } }