/** * Loads UserPreferences model from session or creates new one * @return UserPreferences */ public static function preferences() { if (static::$_cachedPreferences === null) { $model = new UserPreferences(); $model->load([]); $model->validate(); $attributes = Yii::$app->session->get('UserPreferencesModel'); if (isset($attributes) === true) { $model->setAttributes($attributes); } static::$_cachedPreferences = $model; } return static::$_cachedPreferences; }
/** * @return Currency */ public static function getUserCurrency() { if (null === static::$userCurrency) { static::$userCurrency = static::findCurrencyByIso(UserPreferences::preferences()->userCurrency); } return static::$userCurrency; }
public function actionSet($key, $value) { Yii::$app->response->format = Response::FORMAT_JSON; $preferences = UserPreferences::preferences(); $preferences->setAttributes([$key => $value]); return $preferences->validate(); }
public function bootstrap($app) { $app->on(Application::EVENT_AFTER_REQUEST, function () use($app) { $preferences = UserPreferences::preferences(); $defaultPreferences = new UserPreferences(); $defaultPreferences->load([]); $defaultPreferences->validate(); // compare current preferences with default // if user hasn't changed anything - don't flood session variable with default preferences if (count(array_diff($preferences->getAttributes(), $defaultPreferences->getAttributes())) !== 0) { $app->session->set('UserPreferencesModel', $preferences->getAttributes()); } else { $app->session->set('UserPreferencesModel', null); } }); $app->on(Application::EVENT_BEFORE_ACTION, function () use($app) { UserPreferences::preferences(); }); }
/** * @param $key * @param $value * @return mixed */ public function actionSet($key, $value) { $request = Yii::$app->request; $preferences = UserPreferences::preferences(); $preferences->setAttributes([$key => $value]); $result = $preferences->validate(); if (true === $request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return $result; } else { return $this->redirect(\Yii::$app->request->referrer, 301); } }
/** * 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]; }
<?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>
* @var $object \app\models\Object * @var $pages \yii\data\Pagination * @var $products \app\modules\shop\models\Product[] * @var $selected_category \app\modules\shop\models\Category * @var $selected_category_id integer * @var $selected_category_ids integer[] * @var $selections * @var $this app\components\WebView * @var $title_append string * @var $values_by_property_id */ use app\modules\shop\models\UserPreferences; use yii\helpers\Url; use yii\helpers\Html; $this->params['breadcrumbs'] = $breadcrumbs; $listView = UserPreferences::preferences()->getAttributes()['listViewType']; ?> <div id="product-list-block"> <h1> <?php echo $this->blocks['h1']; ?> </h1> <?php if (!empty($this->blocks['announce'])) { ?> <div class="block-announce"> <?php echo $this->blocks['announce']; ?> </div>