コード例 #1
0
 public function index()
 {
     ClassLoader::import('application.model.presentation.CategoryPresentation');
     $this->getAppliedFilters();
     // presentation
     if ($theme = CategoryPresentation::getThemeByCategory($this->getCategory())) {
         if ($theme->getTheme()) {
             $this->application->setTheme($theme->getTheme());
         }
         if ($layout = $theme->listStyle->get()) {
             $this->request->set('layout', strtolower($layout));
             $this->config->set('LIST_LAYOUT', $layout);
             $this->config->set('ALLOW_SWITCH_LAYOUT', false);
         }
     }
     // pagination
     $currentPage = $this->request->get('page', 1);
     $listLayout = $this->getListLayout();
     $perPage = $this->getProductLimitCount($listLayout);
     $offsetStart = ($currentPage - 1) * $perPage + 1;
     $offsetEnd = $currentPage * $perPage;
     $selectFilter = new ARSelectFilter();
     $selectFilter->setLimit($perPage, $offsetStart - 1);
     $this->application->processInstancePlugins('productFilter', $selectFilter);
     // create new search filter
     $query = $this->request->get('q');
     if ($query) {
         $searchFilter = new SearchFilter($query);
         $this->filters[] = $searchFilter;
         // search by category names
         $f = new ARSelectFilter();
         foreach (array($this->locale->getLocaleCode(), $this->application->getDefaultLanguageCode()) as $handle) {
             $langHandle = MultiLingualObject::getLangSearchHandle(new ARFieldHandle('Category', 'name'), $handle);
             $f->mergeCondition(new LikeCond($langHandle, '%' . $query . '%'));
         }
         $foundCategories = ActiveRecordModel::getRecordSet('Category', $f, Category::LOAD_REFERENCES);
         foreach ($foundCategories as $category) {
             $category->getPathNodeSet();
         }
         $cleanedQuery = $searchFilter->getCleanedQuery($query);
         $this->logSearchQuery($cleanedQuery);
     }
     // root category?
     if ($this->getCategory()->isRoot() && !$this->filters && !$this instanceof IndexController && !$this->request->get('includeSub') && $currentPage > 1) {
         return new ActionRedirectResponse('index', 'index');
     }
     // sorting
     $sort = array();
     $opts = $this->config->get('ALLOWED_SORT_ORDER');
     if ($opts) {
         foreach ($opts as $opt => $status) {
             $sort[strtolower($opt)] = $this->translate($opt);
         }
     }
     foreach ($this->getCategory()->getSpecificationFieldArray() as $field) {
         if ($field['isSortable']) {
             $sortName = $field['dataType'] == SpecField::DATATYPE_NUMBERS ? '_sort_num' : '_sort_text';
             $sort[$field['ID'] . '-' . $field['handle'] . '_asc'] = $this->maketext($sortName . '_asc', $field['name_lang']);
             $sort[$field['ID'] . '-' . $field['handle'] . '_desc'] = $this->maketext($sortName . '_desc', $field['name_lang']);
         }
     }
     $order = $this->request->get('sort');
     $defOrder = strtolower($this->config->get('SORT_ORDER'));
     if (!$order) {
         $order = $defOrder;
     }
     $this->applySortOrder($selectFilter, $order);
     // setup ProductFilter
     $productFilter = new ProductFilter($this->getCategory(), $selectFilter);
     if ($this->config->get('INCLUDE_SUBCATEGORY_PRODUCTS')) {
         $productFilter->includeSubcategories();
     }
     $this->productFilter = $productFilter;
     foreach ($this->filters as $filter) {
         $productFilter->applyFilter($filter);
         if ($filter instanceof SearchFilter) {
             $productFilter->includeSubcategories();
             $searchQuery = $filter->getKeywords();
         }
     }
     if ($this->getCategory()->isRoot() && $this->filters || $this->filters || $this->request->get('includeSub')) {
         $productFilter->includeSubcategories();
     }
     $products = $this->getProductsArray($productFilter);
     $this->hasProducts = count($products) > 0;
     // pagination
     $count = new ProductCount($this->productFilter, $this->application);
     $totalCount = $count->getCategoryProductCount($productFilter);
     $offsetEnd = min($totalCount, $offsetEnd);
     $this->totalCount = $totalCount;
     // narrow by subcategories
     $subCategories = $this->getCategory()->getSubCategoryArray(Category::LOAD_REFERENCES);
     $categoryNarrow = array();
     if ((!empty($searchQuery) || $this->getCategory()->isRoot() || $this->filters) && $products) {
         $categoryNarrow = $this->getSubCategoriesBySearchQuery($productFilter->getSelectFilter(), $subCategories);
     }
     $categoryArray = $this->getCategory()->toArray();
     if (!$this->getCategory()->isRoot()) {
         $this->redirect301($this->request->get('cathandle'), createHandleString($categoryArray['name_lang']));
     }
     // if all the results come from one category, redirect to this category
     if (count($categoryNarrow) == 1 && count($this->filters) == 1 && empty($foundCategories)) {
         $canNarrow = true;
         foreach ($products as $product) {
             if ($product['Category']['ID'] == $this->getCategoryId()) {
                 $canNarrow = false;
             }
         }
         if ($canNarrow) {
             while (count($categoryNarrow) == 1) {
                 if ($categoryNarrow[0]['searchCount'] != $totalCount) {
                     break;
                 }
                 $this->category = Category::getInstanceByID($categoryNarrow[0]['ID'], Category::LOAD_DATA);
                 $subCategories = $this->getCategory()->getSubCategoryArray(Category::LOAD_REFERENCES);
                 if ($subCategories) {
                     $subCategories[] = $categoryArray;
                 }
                 $categoryNarrow = $this->getSubCategoriesBySearchQuery($selectFilter, $subCategories);
             }
             include_once ClassLoader::getRealPath('application.helper.smarty') . '/function.categoryUrl.php';
             if (!$this->getCategory()->isRoot()) {
                 return new RedirectResponse(createCategoryUrl(array('data' => $this->getCategory()->toArray(), 'filters' => $this->filters), $this->application));
             }
         }
     }
     // get subcategory-subcategories
     if ($subCategories && $this->config->get('CAT_MENU_SUBS')) {
         $this->getSubSubCategories($subCategories);
     }
     // get subcategory featured products
     $subCatFeatured = array();
     if ($subCategories && !$products || $this instanceof IndexController) {
         $subCatFeatured = $this->getSubCatFeaturedProducts();
     }
     // if there were no products found, include subcategories in filter counts... except home page
     if (!$products || $this->getCategory()->isRoot()) {
         $selectFilter->removeCondition(new EqualsCond(new ARFieldHandle('Product', 'categoryID'), $this->getCategory()->getID()));
         $this->productFilter->includeSubcategories();
     }
     // search redirects
     // no products found, but found one category name - redirect to this category
     if (isset($foundCategories) && 1 == $foundCategories->size() && !$products) {
         include_once ClassLoader::getRealPath('application.helper.smarty') . '/function.categoryUrl.php';
         return new RedirectResponse(createCategoryUrl(array('data' => $foundCategories->get(0)->toArray()), $this->application));
     }
     $filterArray = array();
     foreach ($this->filters as $filter) {
         $filterArray[] = $filter->toArray();
     }
     if ($this->config->get('DISPLAY_CATEGORY_FEATURED')) {
         $this->getFeaturedMainCategoryProducts($subCategories);
         $this->getFeaturedMainCategoryProducts($categoryNarrow);
     }
     $response = new ActionResponse();
     $response->set('id', $this->getCategoryId());
     $response->set('products', $products);
     $response->set('count', $totalCount);
     $response->set('offsetStart', $offsetStart);
     $response->set('offsetEnd', $offsetEnd);
     $response->set('perPage', $perPage);
     $response->set('currentPage', $currentPage);
     $response->set('category', $categoryArray);
     $response->set('subCategories', $subCategories);
     $response->set('currency', $this->getRequestCurrency());
     $response->set('sortOptions', $sort);
     $response->set('sortForm', $this->buildSortForm($order));
     $response->set('sortField', $order);
     $response->set('categoryNarrow', $categoryNarrow);
     $response->set('subCatFeatured', $subCatFeatured);
     //$response->set('allFilters', $filters);
     //$response->set('showAll', $showAll);
     $response->set('appliedFilters', $filterArray);
     $response->set('layout', $listLayout);
     $response->set('listAttributes', $this->getListAttributes());
     $filterChainHandle = $this->setUpBreadCrumbAndReturnFilterChainHandle($currentPage);
     $response->set('url', $this->getCategoryPageUrl(array('page' => '_000_', 'filters' => $filterChainHandle)));
     $response->set('layoutUrl', $this->getCategoryPageUrl(array('filters' => $filterChainHandle, 'query' => array('layout' => ''))));
     $response->set('filterChainHandle', $filterChainHandle);
     if (isset($searchQuery)) {
         $response->set('searchQuery', $searchQuery);
     }
     if (isset($foundCategories)) {
         $response->set('foundCategories', $foundCategories->toArray());
     }
     // look for manufacturer filter
     foreach ($this->filters as $filter) {
         if ($filter instanceof ManufacturerFilter) {
             $response->set('manufacturerFilter', $filter->toArray());
         }
     }
     if (1 == $currentPage && $query) {
         $searchCon = new SearchController($this->application);
         $response->set('modelSearch', $searchCon->searchAll($cleanedQuery));
     }
     return $response;
 }