Класс Block
Объект $Block:
Объект для работы с блоками внутри страниц
Исходный код
class Block extends \Cms\Site\Base { … }
Свойства
$confBlock
$Block->confBlock = array(
'menu' => 'Ссылки на подразделы',
'html' => 'Текстовый блок',
'uikit' => 'Визуальные блоки',
'b' => 'Баннеры',
'gallery' => 'Галерея',
'accordion' => 'Аккордеон',
'tab' => 'Вкладки',
'table' => 'Таблица',
'wrapper' => 'Разметка',
'2col' => '2 колонки',
'col' => 'Колонка',
'grid' => 'Сетка',
);
Исходный код
var $confBlock = array( … );
$confBlockDefault
$Block->confBlockDefault = array(
'menu' => 'Y',
'html' => 'Y',
);
Исходный код
var $confBlockDefault = array( … );
$confBlockWrapper
$Block->confBlockWrapper = array(
'wrapper' => 'Y',
'2col' => 'Y',
'col' => 'Y',
'grid' => 'Y',
);
Исходный код
var $confBlockWrapper = array( … );
Методы
getBlock()
$Block->getBlock($Page);
Исходный код
function getBlock($Page) {
$Conf = $this->Conf();
$Main = $this->Main();
$confBlock = $Conf->get('block');
if (is_array($confBlock)) {
foreach ($this->confBlock as $block => $title) if (!array_key_exists($block, $confBlock)) $confBlock[$block] = $title;
} else {
$confBlock = $this->confBlock;
}
if (!$list = $Page->list) if ($Type = $Main->getModule($Page->type)) $list = $Type->list;
if ($list) if ($Type = $Main->getModule($list)) {
$block = 'list.' . $list;
$title = $Type->plural ? $Type->plural : 'Список: ' . ($Type->title ? $Type->title : strtr(mb_convert_case($Type->name, MB_CASE_TITLE), '-_/', ' '));
if (!array_key_exists($block, $confBlock)) $confBlock[$block] = $title;
}
return $confBlock;
}
getBlockDefault()
$Block->getBlockDefault($Page);
Исходный код
function getBlockDefault($Page) {
$Conf = $this->Conf();
$Main = $this->Main();
$confBlockDefault = $Conf->get('block-default');
if (!is_array($confBlockDefault)) $confBlockDefault = $this->confBlockDefault;
if (isset($confBlockDefault['menu']) && $Page->isMain() && !$this->Auth()->admin) unset($confBlockDefault['menu']);
if (!$list = $Page->list) if ($Type = $Main->getModule($Page->type)) $list = $Type->list;
if ($list) if ($Type = $Main->getModule($list)) {
$block = 'list.' . $list;
if (!array_key_exists($block, $confBlockDefault)) $confBlockDefault[$block] = 'Y';
}
return $confBlockDefault;
}
getBlockWrapper()
$Block->getBlockWrapper($Page);
Исходный код
function getBlockWrapper($Page) {
$Conf = $this->Conf();
$confBlockWrapper = $Conf->get('block-wrapper');
if (!is_array($confBlockWrapper)) $confBlockWrapper = $this->confBlockWrapper;
return $confBlockWrapper;
}
getBlockList()
$Block->getBlockList($Page);
Исходный код
function getBlockList($Page) {
$r = array();
$confBlock = $this->getBlock($Page);
$pageBlockList = $Page->getInfo('block-list');
if (!is_array($pageBlockList)) $pageBlockList = array();
$order = 0;
$nList = array();
foreach ($pageBlockList as $blockItem) if (is_string($blockItem) && is_array($blockItem = json_decode($blockItem, true))) {
$n = $blockItem['n'];
$block = $blockItem['block'];
if ($block && $n > $nList[$block]) $nList[$block] = $n;
$blockItem['title'] = $confBlock[$block];
$blockItem['order'] = ++ $order;
$r[] = $blockItem;
}
return $r;
}
getDefaultBlockList()
$Block->getDefaultBlockList($Page);
Исходный код
function getDefaultBlockList($Page) {
$r = array();
$confBlock = $this->getBlock($Page);
$confBlockDefault = $this->getBlockDefault($Page);
$order = 0;
foreach ($confBlockDefault as $block => $active) {
$r[] = array(
'n' => 1,
'block' => $block,
'active' => $active,
'order' => ++ $order,
'title' => isset($confBlock[$block]) ? $confBlock[$block] : '',
'default' => 'Y',
);
}
return $r;
}
getBlockItem()
$Block->getBlockItem($Page, $param);
Исходный код
function getBlockItem($Page, $param) {
$block = strval($param['block']);
$n = intval($param['n']);
if (!$block || !$n) return;
$blockList = $this->getBlockList($Page);
foreach ($blockList as $blockItem) {
if ($block == $blockItem['block'] && $n == $blockItem['n']) {
return $blockItem;
}
}
}
createBlockItem()
$Block->createBlockItem($Page, $param);
Исходный код
function createBlockItem($Page, $param) {
$block = strval($param['block']);
$n = intval($param['n']);
if (!$block || !$n) return;
$blockList = $this->getBlockList($Page);
foreach ($blockList as $blockItem) {
if ($block == $blockItem['block'] && $n == $blockItem['n']) {
return;
}
}
$blockItem = array(
'n' => strval($n),
'block' => strval($block),
'active' => 'Y',
'order' => intval($param['order']),
'data' => array(),
);
$pageBlockList = $Page->getInfo('block-list');
if (!is_array($pageBlockList)) $pageBlockList = array();
$pageBlockList[] = json_encode($blockItem, defined('JSON_UNESCAPED_UNICODE') && defined('JSON_UNESCAPED_SLASHES') ? JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES : 0);
$Page->setInfo('block-list', $pageBlockList);
return $blockItem;
}
saveBlockItem()
$Block->saveBlockItem($Page, $blockItem);
Исходный код
function saveBlockItem($Page, $blockItem) {
if (isset($blockItem['data'])) if (is_array($data = &$blockItem['data'])) {
if (strlen($title = trim(strval($data['title'])))) $blockItem['title'] = $title;
if ($blockItem['block'] == 'menu' && $blockItem['n'] == 1) $this->updateBlockMenu1($Page, $blockItem);
if ($blockItem['block'] == 'html' && $blockItem['n'] == 1) $this->updateBlockHtml1($Page, $blockItem);
foreach ($data as $k => $v) {
if ($v === '') unset($data[$k]);
if (is_string($k) && $k[0] === '_') unset($data[$k]);
}
}
$pageBlockList = $Page->getInfo('block-list');
if (!is_array($pageBlockList)) $pageBlockList = array();
foreach ($pageBlockList as $order => $pageBlockItem) if (is_string($pageBlockItem) && is_array($pageBlockItem = json_decode($pageBlockItem, true))) {
if ($pageBlockItem['block'] == $blockItem['block'] && $pageBlockItem['n'] == $blockItem['n']) {
$pageBlockList[$order] = json_encode($blockItem, defined('JSON_UNESCAPED_UNICODE') && defined('JSON_UNESCAPED_SLASHES') ? JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES : 0);
}
}
$Page->setInfo('block-list', $pageBlockList);
return $Page->saveInfo();
}
getBlockEditHref()
$Block->getBlockEditHref($Page, $blockItem);
Исходный код
function getBlockEditHref($Page, $blockItem) {
return '?a=block&p=' . rawurlencode($Page->id) . '&order=' . rawurlencode($blockItem['order']) . '&block=' . rawurlencode($blockItem['block']) . '&n=' . rawurlencode($blockItem['n']);
}
getBlockEditForm()
$Block->getBlockEditForm($Page, $blockItem);
Исходный код
function getBlockEditForm($Page, $blockItem) {
$name = $blockItem['block'];
if ($name === 'menu') {
if ($blockItem['n'] == 1) $name = 'menu1';
else $name = 'menu2';
}
return 'cms/form/block/' . $name . '.xml';
}
displayBlockPanel()
$Block->displayBlockPanel($Page, $blockItem);
Исходный код
function displayBlockPanel($Page, $blockItem) {
$Main = $this->Main();
$panel = array(
'edit' => array(
'href' => $this->getBlockEditHref($Page, $blockItem),
'title' => $Main->translate('Редактировать блок'),
'image' => 'edit',
'modal' => 'Y',
'can' => 'block|edit',
),
);
return $Main->callTemplate('display', 'panel', $Page, array( 'panel' => $panel, 'redirect' => null ));
}
updateBlockMenu1()
$Block->updateBlockMenu1($Page, $blockItem);
Исходный код
function updateBlockMenu1($Page, $blockItem) {
$Main = $this->Main();
$Data = $this->Data();
$Storage = $Main->Storage();
$data = &$blockItem['data'];
$sortList = array();
foreach ($Storage->loadSortList($Page) as $sortItem) $sortList[$sortItem['id']] = $sortItem;
$orderList = array();
if (is_array($data['menu'])) foreach ($data['menu'] as $menuItem) {
if ($id = $menuItem['id']) if ($item = $sortList[$id]) {
$table = $item['table'];
if (!isset($orderList[$table])) $orderList[$table] = 0;
$order = ++ $orderList[$table];
$menu = $menuItem['menu'];
if (isset($item['menu'])) {
$Data->query("UPDATE `$table` SET `order`={$Data->quote($order)}, `menu`={$Data->quote($menu)} WHERE `id`={$Data->quote($id)}");
} else {
$Data->query("UPDATE `$table` SET `order`={$Data->quote($order)} WHERE `id`={$Data->quote($id)}");
}
}
}
unset($data['menu']);
}
updateBlockHtml1()
$Block->updateBlockHtml1($Page, $blockItem);
Исходный код
function updateBlockHtml1($Page, $blockItem) {
$data = &$blockItem['data'];
$Page->html = strval($data['html']);
$Page->saveHtml();
unset($data['html']);
}