Улучшенное определение layout_id

Тема в разделе "OpenCart", создана пользователем MShekhovtsev, 8 фев 2015.

  1. MShekhovtsev

    MShekhovtsev

    Регистрация:
    19 ноя 2013
    Сообщения:
    23
    Симпатии:
    9
    Улучшенное определение layout_id

    В периферийных контроллерах: column_left, column_right, content_top, content_bottom каждый раз подключается модель design/layout и каждый раз происходит запрос к БД для получения layout_id, который один для всей страницы.

    Чтобы избежать этого, можно определить layout_id только один раз, например в файле index.php:
    после строк

    // Router
    if (isset($request->get['route'])) {
    $action = new Action($request->get['route']);
    } else {
    $action = new Action('common/home');
    }

    Добавить

    $loader->model('design/layout');

    В файле catalog/model/design/layout.php нужно добавить конструктор для определения layout_id автоматически, а также добавить методы для определения layout_id категорий, товаров и статей.

    Содержание файла catalog/model/design/layout.php:

    <?php
    class ModelDesignLayout extends Model {
    function __construct($registry) {
    parent::__construct($registry);

    if (isset($this->request->get['route'])) {
    $route = (string)$this->request->get['route'];
    } else {
    $route = 'common/home';
    }

    $layout_id = 0;

    if ($route == 'product/category' && isset($this->request->get['path'])) {
    $path = explode('_', (string)$this->request->get['path']);

    $layout_id = $this->getCategoryLayoutId(end($path));
    }

    if ($route == 'product/product' && isset($this->request->get['product_id'])) {
    $layout_id = $this->getProductLayoutId($this->request->get['product_id']);
    }

    if ($route == 'information/information' && isset($this->request->get['information_id'])) {
    $layout_id = $this->getInformationLayoutId($this->request->get['information_id']);
    }

    if (!$layout_id) {
    $layout_id = $this->getLayout($route);
    }

    if (!$layout_id) {
    $layout_id = $this->config->get('config_layout_id');
    }
    $this->layout_id = $layout_id;
    }

    public function getLayout($route) {
    $query = $this->db->query("SELECT layout_id FROM " . DB_PREFIX . "layout_route WHERE '" . $this->db->escape($route) . "' LIKE route AND store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY route DESC LIMIT 1");

    return ($query->num_rows) ? $query->row['layout_id'] : 0;
    }

    public function getCategoryLayoutId($category_id) {
    $query = $this->db->query("SELECT layout_id FROM " . DB_PREFIX . "category_to_layout WHERE category_id = '" . (int)$category_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");

    return ($query->num_rows) ? $query->row['layout_id'] : 0;
    }

    public function getProductLayoutId($product_id) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_layout WHERE product_id = '" . (int)$product_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");

    return ($query->num_rows) ? $query->row['layout_id'] : 0;
    }

    public function getInformationLayoutId($information_id) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");

    return ($query->num_rows) ? $query->row['layout_id'] : 0;
    }

    public function getLayoutModules($layout_id, $position) {
    $query = $this->db->query("SELECT `code` FROM " . DB_PREFIX . "layout_module WHERE layout_id = '" . (int)$layout_id . "' AND position = '" . $this->db->escape($position) . "' ORDER BY sort_order");

    return $query->rows;
    }
    }

    Далее в периферийных контроллерах
    catalog/controller/common/column_left.php
    catalog/controller/common/column_right.php

    catalog/controller/common/content_top.php
    catalog/controller/common/content_bottom.php

    уже не нужно определять layout_id. Достаточно просто добавить строку:
    $layout_id = $this->model_design_layout->layout_id;
     
    nikfakel нравится это.