$type = $block_data['type']; Tygh::$app['view']->assign('changed_content_stat', Block::instance()->getChangedContentsCount($block_id, true)); } else { $type = isset($_REQUEST['block_data']['type']) ? $_REQUEST['block_data']['type'] : 'html_block'; $block_data = array('type' => $type, 'block_id' => 0); } if (!empty($_REQUEST['block_data']['description']['name'])) { $block_data['name'] = $_REQUEST['block_data']['description']['name']; } if (!empty($_REQUEST['block_data']['properties'])) { $block_data['properties'] = $_REQUEST['block_data']['properties']; } if (!empty($_REQUEST['block_data']['content'])) { $block_data['content'] = $_REQUEST['block_data']['content']; } $block_scheme = SchemesManager::getBlockScheme($type, isset($_REQUEST['block_data']) ? $_REQUEST['block_data'] : $block_data, true); // Set template as first default from scheme if (empty($block_data['properties']['template']) && isset($block_scheme['templates'])) { if (is_array($block_scheme['templates'])) { $block_data['properties']['template'] = current(array_keys($block_scheme['templates'])); } else { $block_data['properties']['template'] = $block_scheme['templates']; } $block_scheme['content'] = SchemesManager::prepareContent($block_scheme, $block_data); } // Set content_type as first default from scheme if (empty($block_data['properties']['content_type']) && !empty($block_scheme['content'])) { $block_data['properties']['content_type'] = current(array_keys($block_scheme['content'])); } // Set filing as first default from scheme if (isset($block_scheme['content']) && is_array($block_scheme['content'])) {
/** * Renders block content * * @static * * @param array $block Block data for rendering content * @param array $params Parameters of rendering: * * use_cache - Whether to use cache * * parse_js - Whether to move inline JS of the block to the bottom of the page * * @return string HTML code of rendered block content */ public static function renderBlockContent($block, $params = array()) { $default_params = array('use_cache' => true, 'parse_js' => true); $params = array_merge($default_params, $params); // Do not render block if it disabled in the frontend if (isset($block['is_disabled']) && $block['is_disabled']) { return ''; } $smarty = \Tygh::$app['view']; $smarty_original_vars = $smarty->getTemplateVars(); $display_this_block = true; self::_assignBlockSettingsToTemplate($block); // Assign block data from DB $smarty->assign('block', $block); $theme_path = self::getCustomerThemePath(); $block_schema = SchemesManager::getBlockScheme($block['type'], array()); $grid_id = empty($block['grid_id']) ? 0 : $block['grid_id']; $cache_key = "block_content_{$block['block_id']}_{$block['snapping_id']}_{$block['type']}_{$grid_id}"; if (!empty($block['object_id']) && !empty($block['object_type'])) { $cache_key .= "_{$block['object_id']}_{$block['object_type']}"; } $cache_this_block = $params['use_cache'] && self::allowCache(); if ($cache_this_block && isset($block['content']['items']['filling']) && isset($block_schema['content']['items']['fillings'][$block['content']['items']['filling']]['disable_cache'])) { $cache_this_block = !$block_schema['content']['items']['fillings'][$block['content']['items']['filling']]['disable_cache']; } /** * Determines flags for Cache * * @param array $block Block data * @param string $cache_key Generated name of cache * @param array $block_schema Block schema * @param bool $cache_this_block Flag to register cache * @param bool $display_this_block Flag to display block */ fn_set_hook('render_block_register_cache', $block, $cache_key, $block_schema, $cache_this_block, $display_this_block); if ($cache_this_block) { // We need an extra data to cache Inline JavaScript $smarty->assign('block_cache_name', $cache_key); // Check whether cache was registered successfully $cache_this_block = self::registerBlockCacheIfNeeded($cache_key, $block_schema, $block); } else { $smarty->clearAssign('block_cache_name'); } $smarty->assign('block_rendering', true); $smarty->assign('block_parse_js', $params['parse_js']); // We should load only when cache record exists $load_block_from_cache = $cache_this_block && Registry::isExist($cache_key); $block_content = ''; // Block content is found at cache and should be loaded out of there if ($load_block_from_cache) { $cached_content = Registry::get($cache_key); $block_content = $cached_content['content']; if (!empty($cached_content['javascript'])) { $repeat = false; $smarty->loadPlugin('smarty_block_inline_script'); smarty_block_inline_script(array(), $cached_content['javascript'], $smarty, $repeat); } Debugger::blockFoundAtCache($block['block_id']); } else { if ($block['type'] == Block::TYPE_MAIN) { $block_content = self::_renderMainContent(); } else { $title = $block['name']; if (Registry::get('runtime.customization_mode.live_editor')) { $le_block_types = fn_get_schema('customization', 'live_editor_block_types'); if (!empty($le_block_types[$block['type']]) && !empty($le_block_types[$block['type']]['name'])) { $title = sprintf('<span data-ca-live-editor-obj="block:name:%s">%s</span>', $block['block_id'], $title); } } $smarty->assign('title', $title); if (!empty($block_schema['content'])) { $all_values_are_empty = true; foreach ($block_schema['content'] as $template_variable => $field) { /** * Actions before render any variable of block content * * @param string $template_variable name of current block content variable * @param array $field Scheme of this content variable from block scheme content section * @param array $block_schema block scheme * @param array $block Block data */ fn_set_hook('render_block_content_pre', $template_variable, $field, $block_schema, $block); $value = self::getValue($template_variable, $field, $block_schema, $block); if ($all_values_are_empty && !empty($value)) { $all_values_are_empty = false; } $smarty->assign($template_variable, $value); } // We shouldn't display block which content variables are all empty $display_this_block = $display_this_block && !$all_values_are_empty; } // Assign block data from scheme $smarty->assign('block_scheme', $block_schema); if ($display_this_block && file_exists($theme_path . $block['properties']['template'])) { $block_content = $smarty->fetch($block['properties']['template']); } } if (!empty($block['wrapper']) && file_exists($theme_path . $block['wrapper']) && $display_this_block) { $smarty->assign('content', $block_content); if ($block['type'] == Block::TYPE_MAIN) { $smarty->assign('title', !empty(\Smarty::$_smarty_vars['capture']['mainbox_title']) ? \Smarty::$_smarty_vars['capture']['mainbox_title'] : '', false); } $block_content = $smarty->fetch($block['wrapper']); } else { $smarty->assign('content', $block_content); $block_content = $smarty->fetch('views/block_manager/render/block.tpl'); } fn_set_hook('render_block_content_after', $block_schema, $block, $block_content); // Save block contents to cache if ($cache_this_block && $display_this_block) { $cached_content = Registry::get($cache_key); $cached_content['content'] = $block_content; Registry::set($cache_key, $cached_content); } } $wrap_id = $smarty->getTemplateVars('block_wrap'); $smarty->clearAllAssign(); $smarty->assign($smarty_original_vars); // restore original vars \Smarty::$_smarty_vars['capture']['title'] = null; if ($display_this_block == true) { if (!empty($wrap_id)) { $block_content = '<div id="' . $wrap_id . '">' . $block_content . '<!--' . $wrap_id . '--></div>'; } return trim($block_content); } else { return ''; } }
/** * Get blocks for the twigmo homepage * @param string $dispatch Dispatch of needed location * @param array $allowed_objects - array of blocks types * @return array blocks */ public static final function getBlocksForLocation($dispatch, $allowed_objects) { $allowed_page_types = array('T', 'L', 'F'); $blocks = array(); $location = Location::instance(fn_twg_get_default_layout_id())->get($dispatch); if (!$location) { return $blocks; } $get_cont_params = array('location_id' => $location['location_id']); $container = Container::getList($get_cont_params); if (!$container or !$container['CONTENT']) { return $blocks; } $grids_params = array('container_ids' => $container['CONTENT']['container_id']); $grids = Grid::getList($grids_params); if (!$grids) { return $blocks; } $block_grids = Block::instance()->getList(array('?:bm_snapping.*', '?:bm_blocks.*', '?:bm_blocks_descriptions.*'), Grid::getIds($grids)); $image_params = TwigmoSettings::get('images.catalog'); foreach ($block_grids as $block_grid) { foreach ($block_grid as $block) { if ($block['status'] != 'A' or !in_array($block['type'], $allowed_objects)) { continue; } $block_data = array('block_id' => $block['block_id'], 'title' => $block['name'], 'hide_header' => isset($block['properties']['hide_header']) ? $block['properties']['hide_header'] : 'N', 'user_class' => $block['user_class']); $block_scheme = SchemesManager::getBlockScheme($block['type'], array()); if ($block['type'] == 'html_block') { // Html block if (isset($block['content']['content']) and fn_string_not_empty($block['content']['content'])) { $block_data['html'] = $block['content']['content']; } } elseif (!empty($block_scheme['content']) and !empty($block_scheme['content']['items'])) { // Products and categories: get items $template_variable = 'items'; $field = $block_scheme['content']['items']; fn_set_hook('render_block_content_pre', $template_variable, $field, $block_scheme, $block); $items = RenderManager::getValue($template_variable, $field, $block_scheme, $block); // Filter pages - only texts, links and forms posible if ($block['type'] == 'pages') { foreach ($items as $item_id => $item) { if (!in_array($item['page_type'], $allowed_page_types)) { unset($items[$item_id]); } } } if (empty($items)) { continue; } $block_data['total_items'] = count($items); // Images if ($block['type'] == 'products' or $block['type'] == 'categories') { $object_type = $block['type'] == 'products' ? 'product' : 'category'; foreach ($items as $items_id => $item) { if (!empty($item['main_pair'])) { $main_pair = $item['main_pair']; } else { $main_pair = fn_get_image_pairs($item[$object_type . '_id'], $object_type, 'M', true, true); } if (!empty($main_pair)) { $items[$items_id]['icon'] = TwigmoImage::getApiImageData($main_pair, $object_type, 'icon', $image_params); } } } // Banners properties if ($block['type'] == 'banners') { $rotation = $block['properties']['template'] == 'addons/banners/blocks/carousel.tpl' ? 'Y' : 'N'; $block_data['delay'] = $rotation == 'Y' ? $block['properties']['delay'] : 0; $block_data['hide_navigation'] = isset($block['properties']['navigation']) && $block['properties']['navigation'] == 'N' ? 'Y' : 'N'; } $block_data[$block['type']] = Api::getAsList($block['type'], $items); } $blocks[$block['block_id']] = $block_data; } } return $blocks; }
function fn_twg_get_product_tabs($params) { $allowed_page_types = array('T', 'L', 'F'); $allowed_templates = array('blocks/product_tabs/features.tpl', 'blocks/product_tabs/description.tpl'); $allowed_block_types = array('html_block', 'pages', 'products', 'categories', 'banners'); $tabs = ProductTabs::instance()->getList('', $params['product_id'], $params['descr_sl']); foreach ($tabs as $k => $tab) { $isAllowedType = $tab['tab_type'] == 'B'; $isAllowedType = ($isAllowedType or $tab['tab_type'] == 'T' and in_array($tab['template'], $allowed_templates)); if (empty($params['all_tabs']) && $tab['status'] != 'A' || !$isAllowedType) { unset($tabs[$k]); continue; } if ($tab['tab_type'] == 'B') { $block = TwigmoBlock::getBlock(array('block_id' => $tab['block_id'], 'area' => 'C')); if (empty($block['type']) || !in_array($block['type'], $allowed_block_types)) { unset($tabs[$k]); continue; } $block_scheme = SchemesManager::getBlockScheme($block['type'], array()); if ($block['type'] == 'html_block') { $tabs[$k]['html'] = $block['content']['content']; } elseif (!empty($block_scheme['content']) && !empty($block_scheme['content']['items'])) { // Products and categories: get items $template_variable = 'items'; $field = $block_scheme['content']['items']; $items = RenderManager::getValue($template_variable, $field, $block_scheme, $block); // Filter pages - only texts, links and forms posible if ($block['type'] == 'pages') { foreach ($items as $item_id => $item) { if (!in_array($item['page_type'], $allowed_page_types)) { unset($items[$item_id]); } } } if (fn_is_empty($items)) { unset($tabs[$k]); continue; } $block_data = array('total_items' => count($items)); // Images $image_params = TwigmoSettings::get('images.cart'); if ($block['type'] == 'products' or $block['type'] == 'categories') { $object_type = $block['type'] == 'products' ? 'product' : 'category'; foreach ($items as $items_id => $item) { $main_pair = fn_get_image_pairs($item[$object_type . '_id'], $object_type, 'M', true, true); if (!empty($main_pair)) { $items[$items_id]['icon'] = TwigmoImage::getApiImageData($main_pair, $object_type, 'icon', $image_params); } } } // Banners properties if ($block['type'] == 'banners') { $rotation = $block['properties']['template'] == 'addons/banners/blocks/carousel.tpl' ? 'Y' : 'N'; $block_data['delay'] = $rotation == 'Y' ? $block['properties']['delay'] : 0; $object_type = 'banner'; } $block_data[$block['type']] = Api::getAsList($block['type'], $items); $tabs[$k] = array_merge($tab, $block_data); } } else { if ($tab['template'] == 'blocks/product_tabs/features.tpl') { $tabs[$k]['type'] = 'features'; } if ($tab['template'] == 'blocks/product_tabs/description.tpl') { $tabs[$k]['type'] = 'description'; } } } return array_values($tabs); // reindex }
/** * Renders block content * @static * @param array $block Block data for rendering content * @return string HTML code of rendered block content */ public static function renderBlockContent($block) { // Do not render block if it disabled in the frontend if (isset($block['is_disabled']) && $block['is_disabled'] == 1) { return ''; } $smarty = Registry::get('view'); $_tpl_vars = $smarty->getTemplateVars(); // save state of original variables // By default block is displayed $display_block = true; self::_assignBlockSettings($block); // Assign block data from DB Registry::get('view')->assign('block', $block); $theme_path = self::getCustomerThemePath(); $block_scheme = SchemesManager::getBlockScheme($block['type'], array()); $cache_name = 'block_content_' . $block['block_id'] . '_' . $block['snapping_id'] . '_' . $block['type'] . '_' . $block['grid_id'] . '_' . $block['object_id'] . '_' . $block['object_type']; $register_cache = true; if (isset($block['content']['items']['filling']) && isset($block_scheme['content']['items']['fillings'][$block['content']['items']['filling']]['disable_cache'])) { $register_cache = !$block_scheme['content']['items']['fillings'][$block['content']['items']['filling']]['disable_cache']; } /** * Determines flags for Cache * * @param array $block Block data * @param string $cache_name Generated name of cache * @param array $block_scheme Block scheme * @param bool $register_cache Flag to register cache * @param bool $display_block Flag to display block */ fn_set_hook('render_block_register_cache', $block, $cache_name, $block_scheme, $register_cache, $display_block); if ($register_cache) { self::_registerBlockCache($cache_name, $block_scheme); } $block_content = ''; if (isset($block_scheme['cache']) && Registry::isExist($cache_name) == true && self::allowCache()) { $block_content = Registry::get($cache_name); } else { if ($block['type'] == 'main') { $block_content = self::_renderMainContent(); } else { Registry::get('view')->assign('title', $block['name']); if (!empty($block_scheme['content'])) { foreach ($block_scheme['content'] as $template_variable => $field) { /** * Actions before render any variable of block content * @param string $template_variable name of current block content variable * @param array $field Scheme of this content variable from block scheme content section * @param array $block_scheme block scheme * @param array $block Block data */ fn_set_hook('render_block_content_pre', $template_variable, $field, $block_scheme, $block); $value = self::getValue($template_variable, $field, $block_scheme, $block); // If block have not empty content - display it if (empty($value)) { $display_block = false; } Registry::get('view')->assign($template_variable, $value); } } // Assign block data from scheme Registry::get('view')->assign('block_scheme', $block_scheme); if ($display_block && file_exists($theme_path . $block['properties']['template'])) { $block_content = Registry::get('view')->fetch($block['properties']['template']); } } if (!empty($block['wrapper']) && file_exists($theme_path . $block['wrapper']) && $display_block) { Registry::get('view')->assign('content', $block_content); if ($block['type'] == 'main') { Registry::get('view')->assign('title', !empty(\Smarty::$_smarty_vars['capture']['mainbox_title']) ? \Smarty::$_smarty_vars['capture']['mainbox_title'] : '', false); } $block_content = Registry::get('view')->fetch($block['wrapper']); } else { Registry::get('view')->assign('content', $block_content); $block_content = Registry::get('view')->fetch('views/block_manager/render/block.tpl'); } fn_set_hook('render_block_content_after', $block_scheme, $block, $block_content); if (isset($block_scheme['cache']) && $display_block == true && self::allowCache()) { Registry::set($cache_name, $block_content); } } $wrap_id = $smarty->getTemplateVars('block_wrap'); $smarty->clearAllAssign(); $smarty->assign($_tpl_vars); // restore original vars \Smarty::$_smarty_vars['capture']['title'] = null; if ($display_block == true) { if (!empty($wrap_id)) { $block_content = '<div id="' . $wrap_id . '">' . $block_content . '<!--' . $wrap_id . '--></div>'; } return trim($block_content); } else { return ''; } }