Ejemplo n.º 1
0
 /**
  * Списочный вывод каталога
  * @param null|string $section символьный код категории каталога
  * @return string
  * @throws \yii\base\InvalidConfigException
  * @throws \yii\web\NotFoundHttpException
  */
 public function actionIndex($section = null)
 {
     $cacheId = $this->getActionCacheId(static::LIST_CACHE_ID);
     $res = Yii::$app->cache->get($cacheId);
     $model = Yii::createObject(['class' => Catalog::className()]);
     $searchModel = Yii::createObject(['class' => CatalogSearch::className(), 'scenario' => ActiveRecord::SCENARIO_SEARCH]);
     $filter = $searchModel->load(Yii::$app->request->get());
     if (empty($res) or $filter) {
         $dependency = Yii::createObject(TagDependency::className());
         $res["sectionModel"] = null;
         if (empty($searchModel->sectionsIds) and $section) {
             $res["sectionModel"] = CatalogSection::find()->published()->andWhere(["code" => $section])->one();
             if (!$res["sectionModel"]) {
                 throw new NotFoundHttpException();
             }
             $dependency->addTag($res["sectionModel"]->setItemTagSafe());
             $searchModel->sectionsIds = $res["sectionModel"]->getFilterIds();
         }
         $dataProvider = $searchModel->publicSearch();
         $dataProvider->getSort()->defaultOrder = $this->orderBy;
         $dataProvider->getPagination()->pageSize = $this->pageSize;
         $dependency->addTag($model->setClassTagSafe());
         $dependency->setTagsFromModels($dataProvider->getModels());
         $res["html"] = $this->renderPartial('_grid', ["dataProvider" => $dataProvider, "previewImageWidth" => $this->previewImageWidth]);
         if (!$filter) {
             Yii::$app->cache->set($cacheId, $res, Yii::$app->params["cacheDuration"], $dependency);
         }
     }
     $this->view->addBreadCrumb(["label" => Yii::t('catalog/app', 'Catalog'), "url" => Url::toRoute(["/catalog/catalog/index"])]);
     if ($res["sectionModel"]) {
         $this->view->registerMetaTags($res["sectionModel"]);
         $crumbs = $res["sectionModel"]->getBreadCrumbsItems($res["sectionModel"]->id, function ($model) {
             return ['/catalog/catalog/index', 'section' => $model->code];
         });
         $this->view->addBreadCrumbs($crumbs);
     }
     return $this->render("index", ["sectionModel" => $res["sectionModel"], "model" => $model, "searchModel" => $searchModel, "html" => $res["html"]]);
 }