Пример #1
0
 public function getCategoriesList()
 {
     jimport('joomla.html.pagination');
     $db = JFactory::getDBO();
     $app = JFactory::getApplication();
     $option = $app->input->get('option');
     $view = $app->input->get('view');
     $context = JeproshopContext::getContext();
     $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'), 'int');
     $limitstart = $app->getUserStateFromRequest($option . $view . '.limit_start', 'limit_start', 0, 'int');
     $lang_id = $app->getUserStateFromRequest($option . $view . '.lang_id', 'lang_id', $context->language->lang_id, 'int');
     $shop_id = $app->getUserStateFromRequest($option . $view . '.shop_id', 'shop_id', $context->shop->shop_id, 'int');
     $shop_group_id = $app->getUserStateFromRequest($option . $view . '.shop_group_id', 'shop_group_id', $context->shop->shop_group_id, 'int');
     $category_id = $app->getUserStateFromRequest($option . $view . '.category_id', 'category_id', 0, 'int');
     $order_by = $app->getUserStateFromRequest($option . $view . '.order_by', 'order_by', 'date_add', 'string');
     $order_way = $app->getUserStateFromRequest($option . $view . '.order_way', 'order_way', 'ASC', 'string');
     $published = $app->getUserStateFromRequest($option . $view . '.published', 'published', 0, 'string');
     $count_categories_without_parent = count(JeproshopCategoryModelCategory::getCategoriesWithoutParent());
     $top_category = JeproshopCategoryModelCategory::getTopCategory();
     $parent_id = 0;
     if ($category_id) {
         $category = new JeproshopCategoryModelCategory($category_id);
         $parent_id = $category->category_id;
     } elseif (!JeproshopShopModelShop::isFeaturePublished() && $count_categories_without_parent > 1) {
         $parent_id = $top_category->category_id;
     } elseif (JeproshopShopModelShop::isFeaturePublished() && $count_categories_without_parent == 1) {
         $parent_id = JeproshopSettingModelSetting::getValue('root_category');
     } elseif (JeproshopShopModelShop::isFeaturePublished() && $count_categories_without_parent > 1 && JeproshopShopModelShop::getShopContext() != JeproshopShopModelShop::CONTEXT_SHOP) {
         if (JeproshopSettingModelSetting::getValue('multishop_feature_active') && count(JeproshopShopModelShop::getShops(true, null, true)) == 1) {
             $parent_id = $context->shop->category_id;
         } else {
             $parent_id = $top_category->category_id;
         }
     }
     $explicitSelect = true;
     /* Manage default params values */
     $use_limit = true;
     if ($limit === false) {
         $use_limit = false;
     }
     $join = " LEFT JOIN " . $db->quoteName('#__jeproshop_category_shop') . " AS category_shop ON (category." . $db->quoteName('category_id') . " = category_shop." . $db->quoteName('category_id') . " AND ";
     if (JeproshopShopModelShop::getShopContext() == JeproshopShopModelShop::CONTEXT_SHOP) {
         $join .= " category_shop.shop_id = " . (int) $context->shop->shop_id . ") ";
     } else {
         $join .= " category_shop.shop_id = category.default_shop_id)";
     }
     // we add restriction for shop
     if (JeproshopShopModelShop::getShopContext() == JeproshopShopModelShop::CONTEXT_SHOP && JeproshopShopModelShop::isFeaturePublished()) {
         $where = " AND category_shop." . $db->quoteName('shop_id') . " = " . (int) JeproshopContext::getContext()->shop->shop_id;
     }
     /* Check params validity */
     if (!JeproshopTools::isOrderBy($order_by) || !JeproshopTools::isOrderWay($order_way) || !is_numeric($limitstart) || !is_numeric($limit) || !JeproshopTools::isUnsignedInt($lang_id)) {
         echo JError::raiseError(500, 'get list params is not valid');
     }
     /* Cache */
     if (preg_match('/[.!]/', $order_by)) {
         $order_by_split = preg_split('/[.!]/', $order_by);
         $order_by = bqSQL($order_by_split[0]) . '.`' . bqSQL($order_by_split[1]) . '`';
     } elseif ($order_by) {
         $order_by = $db->quoteName($db->escape($order_by));
     }
     // Add SQL shop restriction
     $shopLinkType = "";
     $select_shop = $join_shop = $where_shop = '';
     /*if ($shopLinkType){
     			$select_shop = ", shop.shop_name as shop_name ";
     			$join_shop = " LEFT JOIN " ._DB_PREFIX_.$this->shopLinkType.' shop
     							ON a.id_'.$this->shopLinkType.' = shop.id_'.$this->shopLinkType;
     			$where_shop = JeproshopShopModelShop::addSqlRestriction('1', 'category');
     		}*/
     if ($context->controller->multishop_context && JeproshopShopModelShop::isTableAssociated('category')) {
         if (JeproshopShopModelShop::getShopContext() != JeproshopShopModelShop::CONTEXT_ALL || !$context->employee->isSuperAdmin()) {
             $test_join = !preg_match('/`?' . preg_quote('#__jeproshop_category_shop') . '`? *category_shop/', $join);
             if (JeproshopShopModelShop::isFeaturePublished() && $test_join && JeproshopShopModelShop::isTableAssociated('category')) {
                 $where .= " AND category.category_id IN ( SELECT category_shop.category_id FROM ";
                 $where .= $db->quoteName('#__jeproshop_category__shop') . " AS category_shop WHERE category_shop.";
                 $where .= "shop_id IN (" . implode(', ', JeproshopShopModelShop::getContextListShopIds()) . ") )";
             }
         }
     }
     $select = ", category_shop.position AS position ";
     $tmpTableFilter = "";
     /* Query in order to get results with all fields */
     $lang_join = " LEFT JOIN " . $db->quoteName('#__jeproshop_category_lang') . " AS category_lang ON (";
     $lang_join .= "category_lang." . $db->quoteName('category_id') . " = category." . $db->quoteName('category_id');
     $lang_join .= " AND category_lang." . $db->quoteName('lang_id') . " = " . (int) $lang_id;
     if ($context->shop->shop_id) {
         if (!JeproshopShopModelShop::isFeaturePublished()) {
             $lang_join .= " AND category_lang." . $db->quoteName('shop_id') . " = 1";
         } elseif (JeproshopShopModelShop::getShopContext() == JeproshopShopModelShop::CONTEXT_SHOP) {
             $lang_join .= " AND category_lang." . $db->quoteName('shop_id') . " = " . (int) $context->shop->shop_id;
         } else {
             $lang_join .= " AND category_lang." . $db->quoteName('shop_id') . " = category.default_shop_id";
         }
     }
     $lang_join .= ") ";
     $having_clause = '';
     if (isset($this->_filterHaving) || isset($this->_having)) {
         $having_clause = ' HAVING ';
         if (isset($this->_filterHaving)) {
             $having_clause .= ltrim($this->_filterHaving, ' AND ');
         }
         if (isset($this->_having)) {
             $having_clause .= $this->_having . ' ';
         }
     }
     do {
         $query = "SELECT SQL_CALC_FOUND_ROWS " . ($tmpTableFilter ? " * FROM (SELECT " : "");
         if ($explicitSelect) {
             $query .= "category." . $db->quoteName('category_id') . ", category_lang." . $db->quoteName('name') . ", category_lang." . $db->quoteName('description');
             $query .= " , category." . $db->quoteName('position') . " AS category_position, " . $db->quoteName('published');
         } else {
             $query .= ($lang_id ? " category_lang.*," : "") . " category.*";
         }
         $query .= (isset($select) ? rtrim($select, ", ") : "") . $select_shop . " FROM " . $db->quoteName('#__jeproshop_category') . " AS category " . $lang_join . (isset($join) ? $join . " " : "");
         $query .= $join_shop . " WHERE 1 " . (isset($where) ? $where . " " : "") . ($this->deleted_category ? " AND category." . $db->quoteName('deleted') . " = 0 " : "") . "AND " . $db->quoteName('parent_id');
         $query .= "= " . (int) $parent_id . $where_shop . (isset($group) ? $group . " " : "") . $having_clause . " ORDER BY " . (str_replace('`', '', $order_by) == 'category_id' ? "category." : "") . " category.";
         $query .= $order_by . " " . $db->escape($order_way) . ($tmpTableFilter ? ") tmpTable WHERE 1" . $tmpTableFilter : "");
         $db->setQuery($query);
         $total = count($db->loadObjectList());
         $query .= $use_limit === true ? " LIMIT " . (int) $limitstart . ", " . (int) $limit : "";
         $db->setQuery($query);
         $categories = $db->loadObjectList();
         if ($use_limit === true) {
             $limitstart = (int) $limitstart - (int) $limit;
             if ($limitstart < 0) {
                 break;
             }
         } else {
             break;
         }
     } while (empty($categories));
     if (!empty($categories)) {
         foreach ($categories as $item) {
             $category_tree = JeproshopCategoryModelCategory::getChildren((int) $item->category_id, $context->language->lang_id);
             $item->set_view = count($category_tree) ? 1 : 0;
         }
     }
     $this->pagination = new JPagination($total, $limitstart, $limit);
     return $categories;
 }