denverkurt

IndexController.php

Jan 18th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.34 KB | None | 0 0
  1. <?php
  2.  
  3. require_once('GlobalController.php');
  4. class IndexController extends GlobalController {
  5.     public $modules_dir = 'controllers/';
  6.     public $ext_modules_dir = 'modules/';
  7.    
  8.     public function __construct() {
  9.         parent::__construct();
  10.     }
  11.    
  12.     private function recursive_categories_tree(&$tree, $steps) {
  13.         if ($steps <= 0) return;
  14.         foreach($tree as &$tree_item) {
  15.             $tree_item['subcategories'] = $this->categories->get_categories_lazy_load_filter(array('parent_id'=>$tree_item['id'], 'visible'=>1));
  16.             $this->recursive_categories_tree($tree_item['subcategories'], $steps-1);
  17.         }
  18.     }
  19.    
  20.     private function check_visible_menu_items(&$items) {
  21.         $result = false;
  22.         if (!$items) return $result;
  23.         foreach($items as $item) {
  24.             $item->show_item = false;
  25.             if ($item->visible && in_array($item->module_name, $this->global_group_permissions)){
  26.                 $item->show_item = true;
  27.                 $result = true;
  28.             } else if ($item->visible && !empty($item->subitems)) {
  29.                 if ($this->check_visible_menu_items($item->subitems)) {
  30.                     $item->show_item = true;
  31.                     $result = true;
  32.                 }
  33.             }
  34.         }
  35.         return $result;
  36.     }
  37.    
  38.     private function make_url_menu_items(&$items) {
  39.         $result = "";
  40.         if (!$items) return $result;
  41.         foreach($items as $item) {
  42.             $item->active_url = "";
  43.             if (in_array($item->module_name, $this->global_group_permissions)) {
  44.                 if (!empty($item->url)) $item->active_url = $item->url;
  45.                 else if (!empty($item->sub_url)) $item->active_url = $item->sub_url;
  46.                 if (empty($result)) $result = $item->active_url;
  47.             } else if ($item->visible && !empty($item->subitems)) {
  48.                 $item->active_url = $this->make_url_menu_items($item->subitems);
  49.                 if (empty($result)) $result = $item->active_url;
  50.             }
  51.             if ($item->active_url == "/") $item->active_url = "";
  52.         }
  53.         if ($result == "/") $result = "";
  54.         return $result;
  55.     }
  56.    
  57.     function fetch() {
  58.         $url = $_SERVER['REQUEST_URI'];
  59.         $test_301_url = trim($url, '/ ');
  60.         if ($this->settings->seo_use_url_old && !empty($test_301_url)) {
  61.             $this->db->query("SELECT url FROM __categories WHERE url_old=?", $test_301_url);
  62.             $tmp_category = $this->db->result();
  63.             if (!empty($tmp_category)) {
  64.                 $products_module = $this->furl->get_module_by_name('ProductsController');
  65.                 header("HTTP/1.1 301 Moved Permanently");
  66.                 header("Location: ".$this->config->root_url . $products_module->url . $tmp_category->url . $this->settings->postfix_category_url);
  67.                 exit();
  68.             }
  69.             $this->db->query("SELECT url FROM __products WHERE url_old=?", $test_301_url);
  70.             $tmp_product = $this->db->result();
  71.             if (!empty($tmp_product)) {
  72.                 $product_module = $this->furl->get_module_by_name('ProductController');
  73.                 header("HTTP/1.1 301 Moved Permanently");
  74.                 header("Location: ".$this->config->root_url . $product_module->url . $tmp_product->url . $this->settings->postfix_product_url);
  75.                 exit();
  76.             }
  77.             $this->db->query("SELECT url FROM __brands WHERE url_old=?", $test_301_url);
  78.             $tmp_brand = $this->db->result();
  79.             if (!empty($tmp_brand)) {
  80.                 $brand_module = $this->furl->get_module_by_name('BrandController');
  81.                 header("HTTP/1.1 301 Moved Permanently");
  82.                 header("Location: ".$this->config->root_url . $brand_module->url . $tmp_brand->url . $this->settings->postfix_brand_url);
  83.                 exit();
  84.             }
  85.             $this->db->query("SELECT url FROM __materials WHERE url_old=?", $test_301_url);
  86.             $tmp_material = $this->db->result();
  87.             if (!empty($tmp_material)) {
  88.                 $pages_module = $this->furl->get_module_by_name('PagesController');
  89.                 header("HTTP/1.1 301 Moved Permanently");
  90.                 header("Location: ".$this->config->root_url . $pages_module->url . $tmp_material->url . '.html');
  91.                 exit();
  92.             }
  93.         }
  94.         $module = $this->furl->get_module($url);
  95.         if ($module === false) return false;
  96.         $url = mb_substr($url, mb_strlen($module->url, 'utf-8'), mb_strlen($url, 'utf-8'), 'utf-8');
  97.         if (array_key_exists('show_success_registration', $_SESSION) && $_SESSION['show_success_registration']) {
  98.             $this->design->assign('show_success_registration', true);
  99.             unset($_SESSION['show_success_registration']);
  100.         }
  101.         if (is_file($this->config->root_dir.$this->modules_dir.$module->branch.'/'.$module->module.".php")) {
  102.             if ($module->branch == 'admin' && !in_array($module->module, $this->global_group_permissions)) {
  103.                 if(!$this->request->check_session()) {
  104.                     unset($_POST);
  105.                     die('Session expired');
  106.                 }
  107.                 $this->db->query("SELECT * FROM __system_furl WHERE branch=? and module in (?@) ORDER BY url LIMIT 0,1", $module->branch, $this->global_group_permissions);
  108.                 $module = $this->db->result();
  109.                 if ($module === false || !in_array($module->module, $this->global_group_permissions)) return false;
  110.                 if ($module->module == "LoginControllerAdmin" && ($_SERVER['REQUEST_URI'] != $module->url)) header("Location: " . $this->config->root_url . $module->url . $this->design->url_modifier(array('add'=>array('return_url' => base64_encode($this->config->root_url . $_SERVER['REQUEST_URI'])))) );
  111.             }
  112.             include_once($this->modules_dir.$module->branch.'/'.$module->module.".php");
  113.             if (class_exists($module->module)) {
  114.                 $this->main = new $module->module($this);
  115.                 $this->main->set_params($url, (array)json_decode($module->options));
  116.             } else {
  117.                 return $this->page404();
  118.             }
  119.         } else if ($module->is_external) {
  120.             $this->db->query("SELECT * FROM __external_modules WHERE controller=?", $module->module);
  121.             $ext_module = $this->db->result();
  122.             if (!$ext_module || !$ext_module->enabled) return false;
  123.             if (!is_file($this->ext_modules_dir.$ext_module->path.'/controller/'.$ext_module->filename)) return false;
  124.             include_once($this->ext_modules_dir.$ext_module->path.'/controller/'.$ext_module->filename);
  125.             if (class_exists($module->module)) {
  126.                 $this->main = new $module->module($this);
  127.                 $this->main->set_params($url, (array)json_decode($module->options));
  128.             } else {
  129.                 return $this->page404();
  130.             }
  131.         } else {
  132.             return $this->page404();
  133.         }
  134.        
  135.         $this->design->assign('module', $module);
  136.         if ($module->branch == 'admin') {
  137.             $menu_tree = $this->menu->get_menu_tree(0);
  138.             $this->check_visible_menu_items($menu_tree->subitems);
  139.             $this->make_url_menu_items($menu_tree->subitems);
  140.             $menu_tree_up = $this->menu->get_menu_tree(1);
  141.             if ($module->is_external)
  142.                 $active_menu_item = $this->menu->get_selected_menu_item('ModulesControllerAdmin');
  143.             else
  144.                 $active_menu_item = $this->menu->get_selected_menu_item($module->module);
  145.             if (!$active_menu_item)
  146.                 $active_menu_item = reset($menu_tree->subitems)->id;
  147.             $active_menu = $this->menu->get_item($active_menu_item);
  148.             $this->design->assign('menu_tree', $menu_tree);
  149.             $this->design->assign('menu_tree_up', $menu_tree_up);
  150.             $this->design->assign('active_menu_item', $active_menu_item);
  151.             $this->design->assign('active_menu', $active_menu);
  152.         }
  153.        
  154.         if ($module->branch == 'frontend') {
  155.             $this->design->assign('main_module', $this->furl->get_module_by_name('MainController'));
  156.             $this->design->assign('brand_module', $this->furl->get_module_by_name('BrandController'));
  157.             $this->design->assign('brand_module_admin', $this->furl->get_module_by_name('BrandControllerAdmin'));
  158.             $this->design->assign('product_module', $this->furl->get_module_by_name('ProductController'));
  159.             $this->design->assign('product_module_admin', $this->furl->get_module_by_name('ProductControllerAdmin'));
  160.             $this->design->assign('category_module_admin', $this->furl->get_module_by_name('CategoryControllerAdmin'));
  161.             $this->design->assign('material_module_admin', $this->furl->get_module_by_name('MaterialControllerAdmin'));
  162.             $this->design->assign('products_module', $this->furl->get_module_by_name('ProductsController'));
  163.             $this->design->assign('pages_module', $this->furl->get_module_by_name('PagesController'));
  164.             $this->design->assign('reviews_module', $this->furl->get_module_by_name('ReviewsController'));
  165.             $this->design->assign('cart_module', $this->furl->get_module_by_name('CartController'));
  166.             $compare_module = $this->furl->get_module_by_name('CompareController');
  167.             $this->design->assign('compare_module', $compare_module);
  168.             $this->design->assign('register_module', $this->furl->get_module_by_name('RegisterController'));
  169.             $this->design->assign('account_module', $this->furl->get_module_by_name('AccountController'));
  170.             $this->design->assign('order_module', $this->furl->get_module_by_name('OrderController'));
  171.             $this->design->assign('login_module', $this->furl->get_module_by_name('LoginController'));
  172.             $this->design->assign('cart', $this->cart->get_cart());
  173.             if (!empty($this->user))
  174.                 $this->design->assign('favorites_products_count', $this->products->count_favorites_products($this->user->id));
  175.             $categories_frontend = $this->categories->get_categories_lazy_load_filter(array('parent_id'=>0, 'visible'=>1));
  176.             $this->recursive_categories_tree($categories_frontend, $this->settings->catalog_count_opened_level);
  177.             $this->design->assign('categories_frontend', $categories_frontend);
  178.             $this->design->assign('categories_frontend_all', $this->categories->get_categories_tree());
  179.             $brands_popular = $this->brands->get_brands(array('visible'=>1, 'is_popular'=>1, 'sort'=>'name'));
  180.             $brands_all = $this->brands->get_brands(array('visible'=>1, 'sort'=>'name'));
  181.             foreach($brands_popular as $index=>$brand)
  182.                 $brands_popular[$index]->products_count = $this->products->count_products(array('brand_id'=>$brand->id, 'visible'=>1));
  183.             foreach($brands_all as $index=>$brand)
  184.                 $brands_all[$index]->products_count = $this->products->count_products(array('brand_id'=>$brand->id, 'visible'=>1));
  185.             $this->design->assign('brands_popular', $brands_popular);
  186.             $this->design->assign('brands_all', $brands_all);
  187.             $sort_methods = array();
  188.             $sort_methods_name = array();
  189.             $sort_methods_biger = array();
  190.             $sort_methods_lower = array();
  191.             $sort_methods_icon_asc = array();
  192.             $sort_methods_icon_desc = array();
  193.             $sort_methods_direction = array();
  194.             if ($this->settings->settings_sort_position) {
  195.                 $sort_methods[] = 'position';
  196.                 $sort_methods_name[] = 'РџРѕСЂСЏРґРєСѓ';
  197.                 $sort_methods_biger[] = '';
  198.                 $sort_methods_lower[] = '';
  199.                 $sort_methods_icon_asc[] = '';
  200.                 $sort_methods_icon_desc[] = '';
  201.                 $sort_methods_direction[] = 'asc';
  202.             }
  203.             if ($this->settings->settings_sort_price) {
  204.                 $sort_methods[] = 'price';
  205.                 $sort_methods_name[] = 'Цене';
  206.                 $sort_methods_biger[] = 'Сначала РґРѕСЂРѕРіРёРµ';
  207.                 $sort_methods_lower[] = 'Сначала дешевые';
  208.                 $sort_methods_icon_asc[] = 'fa fa-sort-amount-asc';
  209.                 $sort_methods_icon_desc[] = 'fa fa-sort-amount-desc';
  210.                 $sort_methods_direction[] = 'asc';
  211.             }
  212.             if ($this->settings->settings_sort_popular) {
  213.                 $sort_methods[] = 'popular';
  214.                 $sort_methods_name[] = 'Популярности';
  215.                 $sort_methods_biger[] = 'Сначала РЅРµ популярные';
  216.                 $sort_methods_lower[] = 'Сначала популярные';
  217.                 $sort_methods_icon_asc[] = 'fa fa-sort-amount-asc';
  218.                 $sort_methods_icon_desc[] = 'fa fa-sort-amount-desc';
  219.                 $sort_methods_direction[] = 'asc';
  220.             }
  221.             if ($this->settings->settings_sort_newest) {
  222.                 $sort_methods[] = 'newest';
  223.                 $sort_methods_name[] = 'РќРѕРІРёР·РЅРµ';
  224.                 $sort_methods_biger[] = 'Сначала новые';
  225.                 $sort_methods_lower[] = 'Сначала старые';
  226.                 $sort_methods_icon_asc[] = 'fa fa-sort-numeric-asc';
  227.                 $sort_methods_icon_desc[] = 'fa fa-sort-numeric-desc';
  228.                 $sort_methods_direction[] = 'desc';
  229.             }
  230.             if ($this->settings->settings_sort_name) {
  231.                 $sort_methods[] = 'name';
  232.                 $sort_methods_name[] = 'Алфавиту';
  233.                 $sort_methods_biger[] = '';
  234.                 $sort_methods_lower[] = '';
  235.                 $sort_methods_icon_asc[] = 'fa fa-sort-alpha-asc';
  236.                 $sort_methods_icon_desc[] = 'fa fa-sort-alpha-desc';
  237.                 $sort_methods_direction[] = 'asc';
  238.             }
  239.             $this->design->assign('sort_methods', $sort_methods);
  240.             $this->design->assign('sort_methods_name', $sort_methods_name);
  241.             $this->design->assign('sort_methods_biger', $sort_methods_biger);
  242.             $this->design->assign('sort_methods_lower', $sort_methods_lower);
  243.             $this->design->assign('sort_methods_icon_asc', $sort_methods_icon_asc);
  244.             $this->design->assign('sort_methods_icon_desc', $sort_methods_icon_desc);
  245.             $this->design->assign('sort_methods_direction', $sort_methods_direction);
  246.             $history_products = array();
  247.             if(!empty($_COOKIE['history_products'])) {
  248.                 $history_products_ids = explode(',', $_COOKIE['history_products']);
  249.                 $history_products_ids = array_reverse($history_products_ids);
  250.                 foreach($history_products_ids as $history_id) {
  251.                     $tmp_product = $this->products->get_product($history_id);
  252.                     if ($tmp_product && $tmp_product->visible)
  253.                         $history_products[] = $tmp_product;
  254.                 }
  255.                 foreach($history_products as &$history_product) {
  256.                     $history_product->images = $this->image->get_images('products', $history_product->id);
  257.                     $history_product->image = reset($history_product->images);
  258.                     $history_product->variants = $this->variants->get_variants(array('product_id'=>$history_product->id, 'visible'=>1, 'sort'=>'abs(stock)', 'sort_type'=>'desc'));
  259.                     $history_product->variant = reset($history_product->variants);
  260.                 }
  261.             }
  262.            
  263.             $this->design->assign('history_products', $history_products);
  264.             $compare_items = $this->compare->get_compare();
  265.             $ids_str = join(",", $compare_items);
  266.             $compare_href = $this->config->root_url . $compare_module->url . (empty($ids_str) ? "" : "?id=".$ids_str);
  267.             $this->design->assign('compare_items', $compare_items);
  268.             $this->design->assign('compare_href', $compare_href);
  269.             $this->design->assign('compare_href_show', !empty($ids_str));
  270.         }
  271.        
  272.         $new_order_status = $this->orders->get_status('Новые');
  273.         if ($new_order_status) {
  274.             $count_new_orders = $this->orders->count_orders(array('status_id'=>$new_order_status->id, 'moderated'=>0));
  275.             $this->design->assign('count_new_orders', $count_new_orders);
  276.         }
  277.        
  278.         $count_new_reviews = $this->reviews->count_reviews(array('moderated'=>0));
  279.         $this->design->assign('count_new_reviews', $count_new_reviews);
  280.         $count_shop_new_reviews = $this->reviews->count_shop_reviews(array('moderated'=>0));
  281.         $this->design->assign('count_shop_new_reviews', $count_shop_new_reviews);
  282.         $reviews_filter = array('limit' => 3, 'sort' => 'date', 'sort_type' => 'desc', 'visible' => 1);
  283.         if ($this->settings->reviews_shop_premoderate)
  284.             $reviews_filter['moderated'] = 1;
  285.         $last_shop_reviews = $this->reviews->get_shop_reviews($reviews_filter);
  286.         $this->design->assign('last_shop_reviews', $last_shop_reviews);
  287.         $count_all_shop_reviews_filter = array('visible'=>1);
  288.         if ($this->settings->reviews_shop_premoderate)
  289.             $count_all_shop_reviews_filter['moderated'] = 1;
  290.         $count_all_shop_reviews = $this->reviews->count_shop_reviews($count_all_shop_reviews_filter);
  291.         $this->design->assign('count_all_shop_reviews', $count_all_shop_reviews);
  292.         $callbacks_count = $this->callbacks->count_callbacks(array('moderated'=>0));
  293.         $this->design->assign('count_callbacks', $callbacks_count);
  294.         if (!$content = $this->main->fetch())
  295.             return false;
  296.         $this->design->assign('content', $content);
  297.         $wrapper = $this->design->smarty->getTemplateVars('wrapper');
  298.         if(!isset($wrapper))
  299.             $wrapper = $this->design->getTemplateDir($module->branch).'index.tpl';
  300.         $this->body = $this->design->fetch($wrapper);
  301.         if ($module->branch == 'frontend') {
  302.             $whitelist = array( '127.0.0.1', '::1' );
  303.             if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist) && ((intval(base64_decode($this->settings->last_check_rc)) < 1) || (strval(base64_decode($this->settings->last_check_rh)) != $_SERVER['SERVER_NAME']))) {
  304.                 $this->body .= '<div style="background: #c20000; color: #FFF; padding: 12px; position: absolute; top: 0px; text-align: center; width: 100%;"><strong>Ваша лицензия недействительна!</strong> Приобрести лицензию РІС‹ можете РЅР° сайте <a href="http://minicart.ru" style="color: #FFF; font-weight:500;" target="_blank">minicart.ru</a></div>';
  305.             }
  306.         }
  307.         return $this->body;
  308.     }
  309. }
Add Comment
Please, Sign In to add comment