/**
  * @param string $rt
  * @param array $args
  */
 public function __construct($rt, $args = array())
 {
     $this->registry = Registry::getInstance();
     $rt = str_replace('../', '', $rt);
     if (!empty($args)) {
         $this->args = $args;
     }
     ADebug::checkpoint('ADispatch: ' . $rt . ' construct start');
     // We always get full RT (route) to dispatcher. Needs to have pages/ or responses/
     if (!$this->_process_path($rt)) {
         $warning_txt = 'ADispatch: ' . $rt . ' construct FAILED. Missing or incorrect controller route path. Possibly, layout block is enabled for disabled or missing extension! ' . genExecTrace('full');
         $warning = new AWarning($warning_txt);
         $warning->toLog()->toDebug();
     }
     ADebug::checkpoint('ADispatch: ' . $rt . ' construct end. file: class: ' . $this->class . '; method: ' . $this->method);
 }
 /**
  * @param string $controller
  * @return int
  * @throws AException
  */
 public function buildPageData($controller)
 {
     //for Maintenance mode
     if ($this->config->get('config_maintenance')) {
         /** @noinspection PhpIncludeInspection */
         require_once DIR_CORE . "lib/user.php";
         $this->registry->set('user', new AUser($this->registry));
         if (!$this->user->isLogged()) {
             $controller = 'pages/index/maintenance';
         }
     }
     // Locate and set page information. This needs to be called once per page
     $unique_page = array();
     // find page records for given controller
     $key_param = $this->getKeyParamByController($controller);
     $key_value = $key_param ? $this->request->get[$key_param] : null;
     // for nested categories
     if ($key_param == 'path' && $key_value && is_int(strpos($key_value, '_'))) {
         $key_value = (int) substr($key_value, strrpos($key_value, '_') + 1);
     }
     $key_param = is_null($key_value) ? null : $key_param;
     $pages = $this->getPages($controller, $key_param, $key_value);
     if (empty($pages)) {
         //if no specific page found try to get page for group
         $new_path = preg_replace('/\\/\\w*$/', '', $controller);
         $pages = $this->getPages($new_path);
     }
     if (empty($pages)) {
         //if no specific page found load generic
         $pages = $this->getPages('generic');
     } else {
         /* if specific pages with key_param presents...
         		 in any case first row will be that what we need (see sql "order by" in getPages method) */
         $unique_page = $pages[0];
     }
     // look for key_param and key_value in the request
     /*
     Steps to perform
     1. Based on rt (controller) select all rows from pages table where controller = "$controller"  
     2. Based on $key_param = key_param from pages table for given $controller locate this $key_param value from CGI input. 
     You will have $key_param and $key_value pair. 
     NOTE: key_param will be unique per controller. More optimized select can be used to get key_param from pages table.
     3. Locate id from pages table based on $key_param and $key_value pair
      where controller = "$controller" and key_param = $key_param and key_value = $this->request->get[$key_param];
     NOTE: Do select only if value present.
     4. If locate page id use the layout.
     */
     $this->page = !empty($unique_page) ? $unique_page : $pages[0];
     $this->page_id = $this->page['page_id'];
     //if no page found set default page id 1
     if (empty($this->page_id)) {
         $this->page_id = 1;
     }
     //Get the page layout
     $layouts = $this->getLayouts(1);
     if (sizeof($layouts) == 0) {
         //No page specific layout found, load default layout
         $layouts = $this->getDefaultLayout();
         if (sizeof($layouts) == 0) {
             // ????? How to terminate ????
             throw new AException(AC_ERR_LOAD_LAYOUT, 'No layout found for page_id/controller ' . $this->page_id . '::' . $this->page['controller'] . '! ' . genExecTrace('full'));
         }
     }
     $this->layout = $layouts[0];
     $this->layout_id = $this->layout['layout_id'];
     // Get all blacks for the page;
     $blocks = $this->getlayoutBlocks($this->layout_id);
     $this->blocks = $blocks;
     return $this->page_id;
 }
 /**
  *  Layout Manager Class to handle layout in the admin
  *  NOTES: Object can be constructed with specific template, page or layout id provided
  * Possible to create an object with no specifics to access layout methods.
  * @param string $tmpl_id
  * @param string $page_id
  * @param string $layout_id
  * @throws AException
  */
 public function __construct($tmpl_id = '', $page_id = '', $layout_id = '')
 {
     if (!IS_ADMIN) {
         // forbid for non admin calls
         throw new AException(AC_ERR_LOAD, 'Error: permission denied to change page layout');
     }
     $this->registry = Registry::getInstance();
     $this->tmpl_id = !empty($tmpl_id) ? $tmpl_id : $this->config->get('config_storefront_template');
     //do check for existance of storefront template in case when $tmpl_id not set
     if (empty($tmpl_id)) {
         //check is template an extension
         $template = $this->config->get('config_storefront_template');
         $dir = $template . DIR_EXT_STORE . DIR_EXT_TEMPLATE . $template;
         $enabled_extensions = $this->extensions->getEnabledExtensions();
         if (in_array($template, $enabled_extensions) && is_dir(DIR_EXT . $dir)) {
             $is_valid = true;
         } else {
             $is_valid = false;
         }
         //check if this is template from core
         if (!$is_valid && is_dir(DIR_ROOT . '/storefront/view/' . $template)) {
             $is_valid = true;
         }
         if (!$is_valid) {
             $this->tmpl_id = 'default';
         } else {
             $this->tmpl_id = $template;
         }
     } else {
         $this->tmpl_id = $tmpl_id;
     }
     //load all pages specific to set template. No cross template page/layouts
     $this->pages = $this->getPages();
     //set current page for this object instance
     $this->_set_current_page($page_id, $layout_id);
     $this->page_id = $this->page['page_id'];
     //preload all layouts for this page and template
     //NOTE: layout_type: 0 Default, 1 Active layout, 2 draft layout, 3 template layout
     $this->layouts = $this->getLayouts();
     //locate layout for the page instance. If not specified for this instance fist active layout is used
     foreach ($this->layouts as $layout) {
         if (!empty($layout_id)) {
             if ($layout['layout_id'] == $layout_id) {
                 $this->active_layout = $layout;
                 break;
             }
         } else {
             if ($layout['layout_type'] == 1) {
                 $this->active_layout = $layout;
                 break;
             }
         }
     }
     //if not layout set, use default (layout_type=0) layout
     if (count($this->active_layout) == 0) {
         $this->active_layout = $this->getLayouts(0);
         if (count($this->active_layout) == 0) {
             $message_text = 'No template layout found for page_id/controller ' . $this->page_id . '::' . $this->page['controller'] . '!';
             $message_text .= ' Requested data: template: ' . $tmpl_id . ', page_id: ' . $page_id . ', layout_id: ' . $layout_id;
             $message_text .= '  ' . genExecTrace('full');
             throw new AException(AC_ERR_LOAD_LAYOUT, $message_text);
         }
     }
     $this->layout_id = $this->active_layout['layout_id'];
     ADebug::variable('Template id', $this->tmpl_id);
     ADebug::variable('Page id', $this->page_id);
     ADebug::variable('Layout id', $this->layout_id);
     // Get blocks
     $this->all_blocks = $this->getAllBlocks();
     $this->blocks = $this->_getLayoutBlocks();
 }