/** * Returns products for special filtration query * Used in ProductsWidget and ProductController * * @param $category_group_id * @param array $values_by_property_id * @param null|integer|string $selected_category_id * @param bool|string $force_sorting If false - use UserPreferences, if string - use supplied orderBy condition * @param null|integer $limit limit query results * @param bool $apply_filterquery Should we apply filter query(filters based on query params ie. price_min/max) * @param bool $force_limit False to use Pagination, true to use $limit and ignore pagination * @param array $additional_filters Array of callables that will apply additional filters to query */ public static function filteredProducts($category_group_id, array $values_by_property_id = [], $selected_category_id = null, $force_sorting = false, $limit = null, $apply_filterquery = true, $force_limit = false, array $additional_filters = []) { Yii::beginProfile("FilteredProducts"); if (null === ($object = Object::getForClass(static::className()))) { throw new \yii\web\ServerErrorHttpException('Object not found.'); } /** @var \app\modules\shop\ShopModule $module */ $module = Yii::$app->getModule('shop'); $onlyParents = $module->filterOnlyByParentProduct; $query = static::find()->with('images'); if (true === $onlyParents) { $query->andWhere([static::tableName() . '.parent_id' => 0, static::tableName() . '.active' => 1]); } else { $query->andWhere(['!=', static::tableName() . '.parent_id', 0]); $query->andWhere([static::tableName() . '.active' => 1]); } if (null !== $selected_category_id) { $query->innerJoin($object->categories_table_name . ' ocats', 'ocats.category_id = :catid AND ocats.object_model_id = ' . static::tableName() . '.id', [':catid' => $selected_category_id]); } else { $query->innerJoin($object->categories_table_name . ' ocats', 'ocats.object_model_id = ' . static::tableName() . '.id'); } $query->innerJoin(Category::tableName() . ' ocatt', 'ocatt.id = ocats.category_id AND ocatt.category_group_id = :gcatid AND ocatt.active = 1', [':gcatid' => $category_group_id]); $query->addGroupBy(static::tableName() . ".id"); $userSelectedSortingId = UserPreferences::preferences()->getAttributes()['productListingSortId']; $allSorts = []; if ($force_sorting === false) { $allSorts = ProductListingSort::enabledSorts(); if (isset($allSorts[$userSelectedSortingId])) { $query->addOrderBy($allSorts[$userSelectedSortingId]['sort_field'] . ' ' . $allSorts[$userSelectedSortingId]['asc_desc']); } else { $query->addOrderBy(static::tableName() . '.sort_order'); } } elseif (empty($force_sorting) === false || is_array($force_sorting) === true) { $query->addOrderBy($force_sorting); } $productsPerPage = $limit === null ? UserPreferences::preferences()->getAttributes()['productsPerPage'] : $limit; \app\properties\PropertiesHelper::appendPropertiesFilters($object, $query, $values_by_property_id, Yii::$app->request->get('p', [])); // apply additional filters $cacheKeyAppend = ""; if ($apply_filterquery) { $query = Yii::$app->filterquery->filter($query, $cacheKeyAppend); } foreach ($additional_filters as $filter) { if (is_callable($filter)) { call_user_func_array($filter, [&$query, &$cacheKeyAppend]); } } $cacheKey = 'ProductsCount:' . implode('_', [md5($query->createCommand()->getRawSql()), $limit ? '1' : '0', $force_limit ? '1' : '0', $productsPerPage]) . $cacheKeyAppend; $pages = null; if ($force_limit === true) { $query->limit($limit); } else { $products_query = clone $query; $products_query->limit(null); if (false === ($pages = Yii::$app->cache->get($cacheKey))) { $pages = new Pagination(['defaultPageSize' => !is_null($query->limit) ? $query->limit : $productsPerPage, 'pageSizeLimit' => [], 'forcePageParam' => false, 'totalCount' => $products_query->count()]); Yii::$app->cache->set($cacheKey, $pages, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(Category::className()), ActiveRecordHelper::getCommonTag(static::className()), ActiveRecordHelper::getCommonTag($module->className())]])); } $query->offset($pages->offset)->limit($pages->limit); } $cacheKey .= '-' . Yii::$app->request->get('page', 1); if (false === ($products = Yii::$app->cache->get($cacheKey))) { $products = $query->all(); Yii::$app->cache->set($cacheKey, $products, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(Category::className()), ActiveRecordHelper::getCommonTag(static::className()), ActiveRecordHelper::getCommonTag($module->className())]])); } Yii::endProfile("FilteredProducts"); return ['products' => $products, 'pages' => $pages, 'allSorts' => $allSorts]; }
/** * @inheritdoc */ public function rules() { return [[['productListingSortId'], 'integer'], ['productListingSortId', 'default', 'value' => 1], ['productListingSortId', 'in', 'range' => array_keys(ProductListingSort::enabledSorts())], ['listViewType', 'default', 'value' => Yii::$app->getModule('shop')->listViewType], ['listViewType', 'in', 'range' => ['listView', 'blockView'], 'strict' => true], ['productsPerPage', 'default', 'value' => Yii::$app->getModule('shop')->productsPerPage], ['productsPerPage', 'integer', 'max' => 50]]; }
/** * @inheritdoc */ public function rules() { return [[['productListingSortId'], 'integer'], ['productListingSortId', 'default', 'value' => 1], ['productListingSortId', 'in', 'range' => array_keys(ProductListingSort::enabledSorts())], ['listViewType', 'default', 'value' => Yii::$app->getModule('shop')->listViewType], ['listViewType', 'in', 'range' => ['listView', 'blockView'], 'strict' => true], ['productsPerPage', 'default', 'value' => Yii::$app->getModule('shop')->productsPerPage], ['productsPerPage', 'integer', 'max' => 50], ['userCurrency', 'default', 'value' => CurrencyHelper::getMainCurrency()->iso_code], ['userCurrency', 'in', 'range' => Currency::getIsoCodes(), 'strict' => true]]; }
<?php use yii\helpers\Html; use app\modules\shop\models\UserPreferences; // @deprecated, need to be rewritten ?> <form class="form-horizontal span6"> <div class="control-group"> <?php echo Html::activeLabel(UserPreferences::preferences(), 'productListingSortId', ['class' => 'control-label alignL']); ?> <?php echo Html::activeDropDownList(UserPreferences::preferences(), 'productListingSortId', \yii\helpers\ArrayHelper::map(\app\modules\shop\models\ProductListingSort::enabledSorts(), 'id', 'name'), ['data-userpreference' => 'productListingSortId']); ?> </div> <div class="control-group"> <?php echo Html::activeLabel(UserPreferences::preferences(), 'productsPerPage', ['class' => 'control-label alignL']); ?> <?php echo Html::activeDropDownList(UserPreferences::preferences(), 'productsPerPage', [9 => 9, 18 => 18, 30 => 30], ['data-userpreference' => 'productsPerPage']); ?> </div> </form> <div class="pull-right"> <a href="#" data-dotplant-listViewType="listView"><span class="btn btn-large"><i class="fa fa-list"></i></span></a> <a href="#" data-dotplant-listViewType="blockView"><span class="btn btn-large btn-primary"><i class="fa fa-th-large"></i></span></a> </div>