Пример #1
0
 /**
  * Products listing by category with filtration support.
  *
  * @return string
  * @throws \Exception
  * @throws NotFoundHttpException
  * @throws ServerErrorHttpException
  */
 public function actionList()
 {
     $request = Yii::$app->request;
     if (null === $request->get('category_group_id')) {
         throw new NotFoundHttpException();
     }
     if (null === ($object = Object::getForClass(Product::className()))) {
         throw new ServerErrorHttpException('Object not found.');
     }
     $category_group_id = intval($request->get('category_group_id', 0));
     $title_append = $request->get('title_append', '');
     if (!empty($title_append)) {
         $title_append = is_array($title_append) ? implode(' ', $title_append) : $title_append;
         unset($_GET['title_append']);
     }
     $title_prepend = $request->get("title_prepend", "");
     if (!empty($title_prepend)) {
         $title_prepend = is_array($title_prepend) ? implode(" ", $title_prepend) : $title_prepend;
         unset($_GET["title_prepend"]);
     }
     $values_by_property_id = $request->get('properties', []);
     if (!is_array($values_by_property_id)) {
         $values_by_property_id = [$values_by_property_id];
     }
     if (Yii::$app->request->isPost && isset($_POST['properties'])) {
         if (is_array($_POST['properties'])) {
             foreach ($_POST['properties'] as $key => $value) {
                 if (isset($values_by_property_id[$key])) {
                     $values_by_property_id[$key] = array_unique(ArrayHelper::merge($values_by_property_id[$key], $value));
                 } else {
                     $values_by_property_id[$key] = array_unique($value);
                 }
             }
         }
     }
     $selected_category_ids = $request->get('categories', []);
     if (!is_array($selected_category_ids)) {
         $selected_category_ids = [$selected_category_ids];
     }
     if (null !== ($selected_category_id = $request->get('last_category_id'))) {
         $selected_category_id = intval($selected_category_id);
     }
     $result = Product::filteredProducts($category_group_id, $values_by_property_id, $selected_category_id, false, null, true, false);
     /** @var Pagination $pages */
     $pages = $result['pages'];
     if (Yii::$app->response->is_prefiltered_page) {
         $pages->route = '/' . Yii::$app->request->pathInfo;
         $pages->params = [];
     }
     $allSorts = $result['allSorts'];
     $products = $result['products'];
     // throw 404 if we are at filtered page without any products
     if (!Yii::$app->request->isAjax && !empty($values_by_property_id) && empty($products)) {
         throw new EmptyFilterHttpException();
     }
     if (null !== ($selected_category = $selected_category_id)) {
         if ($selected_category_id > 0) {
             if (null !== ($selected_category = Category::findById($selected_category_id, null))) {
                 if (!empty($selected_category->meta_description)) {
                     $this->view->registerMetaTag(['name' => 'description', 'content' => ContentBlockHelper::compileContentString($selected_category->meta_description, Product::className() . ":{$selected_category->id}:meta_description", new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(ContentBlock::className()), ActiveRecordHelper::getCommonTag(Category::className())]]))], 'meta_description');
                 }
                 $this->view->title = $selected_category->title;
             }
         }
     }
     if (is_null($selected_category) || !$selected_category->active) {
         throw new NotFoundHttpException();
     }
     if (!empty($title_append)) {
         $this->view->title .= " " . $title_append;
     }
     if (!empty($title_prepend)) {
         $this->view->title = "{$title_prepend} {$this->view->title}";
     }
     $this->view->blocks['h1'] = $selected_category->h1;
     $this->view->blocks['announce'] = $selected_category->announce;
     $this->view->blocks['content'] = $selected_category->content;
     $this->loadDynamicContent($object->id, 'shop/product/list', $request->get());
     $params = ['model' => $selected_category, 'selected_category' => $selected_category, 'selected_category_id' => $selected_category_id, 'selected_category_ids' => $selected_category_ids, 'values_by_property_id' => $values_by_property_id, 'products' => $products, 'object' => $object, 'category_group_id' => $category_group_id, 'pages' => $pages, 'title_append' => $title_append, 'selections' => $request->get(), 'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, null, $values_by_property_id), 'allSorts' => $allSorts];
     $viewFile = $this->computeViewFile($selected_category, 'list');
     if (Yii::$app->request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $content = $this->renderAjax($viewFile, $params);
         $filters = '';
         $activeWidgets = ThemeActiveWidgets::getActiveWidgets();
         foreach ($activeWidgets as $activeWidget) {
             if ($activeWidget->widget->widget == Widget::className()) {
                 /** @var ThemeWidgets $widgetModel */
                 $widgetModel = $activeWidget->widget;
                 /** @var BaseWidget $widgetClassName */
                 $widgetClassName = $widgetModel->widget;
                 $widgetConfiguration = Json::decode($widgetModel->configuration_json, true);
                 if (!is_array($widgetConfiguration)) {
                     $widgetConfiguration = [];
                 }
                 $activeWidgetConfiguration = Json::decode($activeWidget->configuration_json, true);
                 if (!is_array($activeWidgetConfiguration)) {
                     $activeWidgetConfiguration = [];
                 }
                 $config = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration);
                 $config['themeWidgetModel'] = $widgetModel;
                 $config['partRow'] = $activeWidget->part;
                 $config['activeWidget'] = $activeWidget;
                 $filters = $widgetClassName::widget($config);
             }
         }
         return ['content' => $content, 'filters' => $filters, 'title' => $this->view->title, 'url' => Url::to(['/shop/product/list', 'last_category_id' => $selected_category_id, 'properties' => $values_by_property_id])];
     } else {
         return $this->render($viewFile, $params);
     }
 }
Пример #2
0
 /**
  * Renders specified theme part with all it's widget corresponding current theme variation
  * @param string $key Theme part key(ie. header or pre-footer)
  * @param array $params
  * @return string
  * @throws InvalidConfigException
  */
 public static function renderPart($key, $params = [])
 {
     $parts = static::getAllParts();
     Yii::beginProfile('Render theme part:' . $key);
     Yii::trace('Render theme part:' . $key);
     /** @var array $model */
     $model = null;
     foreach ($parts as $part) {
         if ($part['key'] === $key) {
             $model = $part;
             break;
         }
     }
     if ($model === null) {
         throw new InvalidConfigException("Can't find part with key {$key}");
     }
     /** @var ViewElementsGathener $viewElementsGathener */
     $viewElementsGathener = Yii::$app->viewElementsGathener;
     if (static::shouldCache($model)) {
         $result = Yii::$app->cache->get(static::getCacheKey($model));
         if ($result !== false) {
             Yii::endProfile('Render theme part:' . $key);
             $cachedData = $viewElementsGathener->getCachedData('ThemePart:' . $key);
             if (is_array($cachedData)) {
                 $viewElementsGathener->repeatGatheredData(Yii::$app->view, $cachedData);
             }
             return $result;
         }
         $viewElementsGathener->startGathering('ThemePart:' . $key, static::getCacheDependency($model));
     }
     $model['id'] = intval($model['id']);
     $widgets = array_reduce(ThemeActiveWidgets::getActiveWidgets(), function ($carry, $item) use($model) {
         if ($item['part_id'] === $model['id']) {
             $carry[] = $item;
         }
         return $carry;
     }, []);
     ArrayHelper::multisort($widgets, 'sort_order');
     $result = array_reduce($widgets, function ($carry, ThemeActiveWidgets $activeWidget) use($model, $params) {
         /** @var ThemeWidgets $widgetModel */
         $widgetModel = $activeWidget->widget;
         /** @var BaseWidget $widgetClassName */
         $widgetClassName = $widgetModel->widget;
         $widgetConfiguration = Json::decode(static::_php7JsonPatch($widgetModel->configuration_json), true);
         if (!is_array($widgetConfiguration)) {
             $widgetConfiguration = [];
         }
         $activeWidgetConfiguration = Json::decode(static::_php7JsonPatch($activeWidget->configuration_json), true);
         if (!is_array($activeWidgetConfiguration)) {
             $activeWidgetConfiguration = [];
         }
         $merged = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration);
         $config = ArrayHelper::merge($merged, $params);
         $config['themeWidgetModel'] = $widgetModel;
         $config['partRow'] = $model;
         $config['activeWidget'] = $activeWidget;
         $carry .= $widgetClassName::widget($config);
         return $carry;
     }, '');
     if (static::shouldCache($model)) {
         Yii::$app->cache->set(static::getCacheKey($model), $result, $model['cache_lifetime'], static::getCacheDependency($model));
         $viewElementsGathener->endGathering();
     }
     Yii::endProfile('Render theme part:' . $key);
     return $result;
 }