Commit 2e452ecc53418deff28796d5ccd368722c4f6373
1 parent
14792485
added SeoCategory component, enable autogenerated robots
Showing
5 changed files
with
274 additions
and
3 deletions
Show diff stats
artweb/artbox-catalog/helpers/FilterHelper.php
@@ -77,7 +77,24 @@ | @@ -77,7 +77,24 @@ | ||
77 | { | 77 | { |
78 | $this->filterObj = \Yii::createObject(Filter::className()); | 78 | $this->filterObj = \Yii::createObject(Filter::className()); |
79 | } | 79 | } |
80 | - | 80 | + |
81 | + /** | ||
82 | + * return array of objects | ||
83 | + * @return array | ||
84 | + */ | ||
85 | + public function getActiveOptions(): array | ||
86 | + { | ||
87 | + $activeOptions = []; | ||
88 | + if(!empty($this->activeAliases)) { | ||
89 | + foreach ($this->activeAliases as $activeAlias) { | ||
90 | + $option = new \stdClass(); | ||
91 | + $option->title = $activeAlias->title; | ||
92 | + $activeOptions[] = $option; | ||
93 | + } | ||
94 | + } | ||
95 | + return $activeOptions; | ||
96 | + } | ||
97 | + | ||
81 | /** | 98 | /** |
82 | * Getter for $groupModels | 99 | * Getter for $groupModels |
83 | * | 100 | * |
1 | +<?php | ||
2 | + | ||
3 | + | ||
4 | +namespace frontend\components; | ||
5 | + | ||
6 | +use artbox\core\models\Alias; | ||
7 | +use artbox\core\models\interfaces\AliasInterface; | ||
8 | +use yii\base\Object; | ||
9 | + | ||
10 | + | ||
11 | +class SeoCategory extends Object implements AliasInterface | ||
12 | +{ | ||
13 | + const SINGLE_CATEGORY = 'single'; | ||
14 | + | ||
15 | + const ONE_OPTION = 'one_option'; | ||
16 | + | ||
17 | + const TWO_OPTIONS = 'two_options'; | ||
18 | + | ||
19 | + const MANY = 'many'; | ||
20 | + | ||
21 | + const TITLE_TEMPLATE = ' купити в Україні: Київ, Харків, Одесса - ціна, фото, відгуки | інтернет-магазин ACDC'; | ||
22 | + | ||
23 | + const DESC_TEMPLATE = 'Купити {inner text} в інтернет-магазині ACDC | Тільки оригінал > Доступні ціни > Покупка в кредит > Швидка доставка по Україні'; | ||
24 | + | ||
25 | + /** | ||
26 | + * @var \common\models\catalog\Categor0y | ||
27 | + */ | ||
28 | + public $category; | ||
29 | + | ||
30 | + /** | ||
31 | + * @var array | ||
32 | + */ | ||
33 | + public $options; | ||
34 | + | ||
35 | + /** | ||
36 | + * @var integer | ||
37 | + */ | ||
38 | + public $languageId; | ||
39 | + | ||
40 | + /** | ||
41 | + * @var string | ||
42 | + */ | ||
43 | + protected $mode; | ||
44 | + | ||
45 | + /** | ||
46 | + * @var string | ||
47 | + */ | ||
48 | + protected $title = ''; | ||
49 | + | ||
50 | + /** | ||
51 | + * @var string | ||
52 | + */ | ||
53 | + protected $description = ''; | ||
54 | + | ||
55 | + /** | ||
56 | + * @var string | ||
57 | + */ | ||
58 | + protected $h1 = ''; | ||
59 | + | ||
60 | + /** | ||
61 | + * @var string | ||
62 | + */ | ||
63 | + protected $robots = ''; | ||
64 | + | ||
65 | + /** | ||
66 | + * @var string | ||
67 | + */ | ||
68 | + protected $text = ''; | ||
69 | + | ||
70 | + /** | ||
71 | + * @var Alias | ||
72 | + */ | ||
73 | + public $categoryAlias; | ||
74 | + | ||
75 | + /** | ||
76 | + * @var array | ||
77 | + */ | ||
78 | + protected $templates = []; | ||
79 | + | ||
80 | + protected function countOptions() | ||
81 | + { | ||
82 | + return count($this->options); | ||
83 | + } | ||
84 | + | ||
85 | + /** | ||
86 | + * @inheritdoc | ||
87 | + */ | ||
88 | + public function init() | ||
89 | + { | ||
90 | + $this->selectMode(); | ||
91 | + | ||
92 | + $this->generateData(); | ||
93 | + | ||
94 | + $this->generateRobots(); | ||
95 | + } | ||
96 | + | ||
97 | + protected function selectMode() | ||
98 | + { | ||
99 | + if (empty($this->options)) { | ||
100 | + $this->mode = self::SINGLE_CATEGORY; | ||
101 | + } elseif ($this->countOptions() === 1) { | ||
102 | + $this->mode = self::ONE_OPTION; | ||
103 | + } elseif ($this->countOptions() === 2) { | ||
104 | + $this->mode = self::TWO_OPTIONS; | ||
105 | + } else { | ||
106 | + $this->mode = self::MANY; | ||
107 | + } | ||
108 | + } | ||
109 | + | ||
110 | + private function selectH1() | ||
111 | + { | ||
112 | +// if (!empty($this->categoryAlias->h1)) { | ||
113 | +// $this->h1 = $this->categoryAlias->h1; | ||
114 | +// } else { | ||
115 | + $this->h1 = $this->category->lang->title; | ||
116 | +// } | ||
117 | + } | ||
118 | + | ||
119 | + protected function generateData() | ||
120 | + { | ||
121 | + $this->selectH1(); | ||
122 | + $this->selectTitle(); | ||
123 | + $this->selectDescription(); | ||
124 | + } | ||
125 | + | ||
126 | + protected function selectTitle() | ||
127 | + { | ||
128 | + switch ($this->mode) { | ||
129 | + case self::SINGLE_CATEGORY: | ||
130 | + $this->title = $this->h1 . self::TITLE_TEMPLATE; | ||
131 | + break; | ||
132 | + case self::ONE_OPTION: | ||
133 | + $this->title = $this->h1 . ' ' . $this->options[0]->title . self::TITLE_TEMPLATE; | ||
134 | + break; | ||
135 | + case self::TWO_OPTIONS: | ||
136 | + $this->title = $this->h1 . ' ' . $this->options[0]->title | ||
137 | + . ' ' . $this->options[1]->title | ||
138 | + . self::TITLE_TEMPLATE; | ||
139 | + break; | ||
140 | + default: | ||
141 | + $this->title = $this->h1 . self::TITLE_TEMPLATE; | ||
142 | + } | ||
143 | + | ||
144 | + } | ||
145 | + | ||
146 | + protected function selectDescription() | ||
147 | + { | ||
148 | + switch ($this->mode) { | ||
149 | + case self::SINGLE_CATEGORY: | ||
150 | + $this->description = str_replace('{inner text}',$this->h1,self::DESC_TEMPLATE); | ||
151 | + break; | ||
152 | + case self::ONE_OPTION: | ||
153 | + $this->description = | ||
154 | + str_replace( | ||
155 | + '{inner text}', | ||
156 | + $this->h1 . ' ' . $this->options[0]->title, | ||
157 | + self::DESC_TEMPLATE | ||
158 | + ); | ||
159 | + break; | ||
160 | + case self::TWO_OPTIONS: | ||
161 | + $this->description = | ||
162 | + str_replace( | ||
163 | + '{inner text}', | ||
164 | + $this->h1 | ||
165 | + . ' ' . $this->options[0]->title | ||
166 | + . ' ' . $this->options[1]->title, | ||
167 | + self::DESC_TEMPLATE | ||
168 | + ); | ||
169 | + break; | ||
170 | + default: | ||
171 | + $this->description = str_replace('{inner text}',$this->h1,self::DESC_TEMPLATE); | ||
172 | + } | ||
173 | + } | ||
174 | + | ||
175 | + protected function generateRobots() | ||
176 | + { | ||
177 | + switch ($this->mode) { | ||
178 | + case self::SINGLE_CATEGORY: | ||
179 | + $this->robots = 'index, follow'; | ||
180 | + case self::ONE_OPTION : | ||
181 | + if (!empty($this->template) && !$this->template->isBasic) { | ||
182 | + $this->robots = $this->template->robots; | ||
183 | + } else { | ||
184 | + $this->robots = 'index, follow'; | ||
185 | + } | ||
186 | + break; | ||
187 | + case self::TWO_OPTIONS: | ||
188 | + if (!empty($this->template) && !$this->template->isBasic) { | ||
189 | + $this->robots = $this->template->robots; | ||
190 | + } else { | ||
191 | + $this->robots = 'index, follow'; | ||
192 | + } | ||
193 | + break; | ||
194 | + case self::MANY: | ||
195 | + $this->robots = 'noindex, nofollow'; | ||
196 | + break; | ||
197 | + } | ||
198 | + } | ||
199 | + | ||
200 | + public function getFields(): string | ||
201 | + { | ||
202 | + return ''; | ||
203 | + } | ||
204 | + | ||
205 | + public function getSeoText(): string | ||
206 | + { | ||
207 | + return $this->text ?? ''; | ||
208 | + } | ||
209 | + | ||
210 | + public function getId() | ||
211 | + { | ||
212 | + return 0; | ||
213 | + } | ||
214 | + | ||
215 | + public function getTitle(): string | ||
216 | + { | ||
217 | + return $this->title ?? ''; | ||
218 | + } | ||
219 | + | ||
220 | + public function getDesc(): string | ||
221 | + { | ||
222 | + return $this->description ?? ''; | ||
223 | + } | ||
224 | + | ||
225 | + public function getH1(): string | ||
226 | + { | ||
227 | + return mb_strtoupper(mb_substr($this->h1, 0, 1)).mb_strtolower(mb_substr($this->h1, 1)) ?? ''; | ||
228 | + } | ||
229 | + | ||
230 | + public function getText(): string | ||
231 | + { | ||
232 | + return $this->text ?? ''; | ||
233 | + } | ||
234 | + | ||
235 | + public function getRobots(): string | ||
236 | + { | ||
237 | + return $this->robots ?? ''; | ||
238 | + } | ||
239 | +} | ||
0 | \ No newline at end of file | 240 | \ No newline at end of file |
frontend/controllers/CategoryController.php
@@ -6,6 +6,7 @@ | @@ -6,6 +6,7 @@ | ||
6 | use artbox\catalog\models\Category; | 6 | use artbox\catalog\models\Category; |
7 | use artbox\core\components\SeoComponent; | 7 | use artbox\core\components\SeoComponent; |
8 | use artbox\core\models\Alias; | 8 | use artbox\core\models\Alias; |
9 | + use frontend\components\SeoCategory; | ||
9 | use yii\data\ActiveDataProvider; | 10 | use yii\data\ActiveDataProvider; |
10 | use yii\db\ActiveQuery; | 11 | use yii\db\ActiveQuery; |
11 | use yii\web\Controller; | 12 | use yii\web\Controller; |
@@ -38,6 +39,15 @@ | @@ -38,6 +39,15 @@ | ||
38 | $seo->setModel($model->lang); | 39 | $seo->setModel($model->lang); |
39 | $filterHelper = \Yii::$app->get('filter'); | 40 | $filterHelper = \Yii::$app->get('filter'); |
40 | $filterHelper->setFilter($filter); | 41 | $filterHelper->setFilter($filter); |
42 | + $seoData = new SeoCategory( | ||
43 | + [ | ||
44 | + 'options' => $filterHelper->getActiveOptions(), | ||
45 | + 'category' => $model, | ||
46 | + 'categoryAlias' => $seo->alias, | ||
47 | + ] | ||
48 | + ); | ||
49 | + $seo->loaded = false; | ||
50 | + $seo->setAlias($seoData); | ||
41 | $query = $filterHelper->buildQuery() | 51 | $query = $filterHelper->buildQuery() |
42 | ->innerJoinWith('category', false) | 52 | ->innerJoinWith('category', false) |
43 | ->andWhere([ 'product_to_category.category_id' => $model->id ]) | 53 | ->andWhere([ 'product_to_category.category_id' => $model->id ]) |
frontend/views/category/view.php
@@ -22,7 +22,7 @@ | @@ -22,7 +22,7 @@ | ||
22 | $seo = \Yii::$app->get('seo'); | 22 | $seo = \Yii::$app->get('seo'); |
23 | $filterHelper = \Yii::$app->get('filter'); | 23 | $filterHelper = \Yii::$app->get('filter'); |
24 | $view = $this; | 24 | $view = $this; |
25 | - $this->params[ 'breadcrumbs' ][] = $seo->title; | 25 | + $this->params[ 'breadcrumbs' ][] = $seo->h1; |
26 | ?> | 26 | ?> |
27 | <div id="content"> | 27 | <div id="content"> |
28 | <div class="container"> | 28 | <div class="container"> |
frontend/views/layouts/main.php
@@ -86,6 +86,12 @@ | @@ -86,6 +86,12 @@ | ||
86 | 'content' => $seo->desc, | 86 | 'content' => $seo->desc, |
87 | ] | 87 | ] |
88 | ); | 88 | ); |
89 | + $this->registerMetaTag( | ||
90 | + [ | ||
91 | + 'name' => 'robots', | ||
92 | + 'content' => $seo->robots, | ||
93 | + ] | ||
94 | + ); | ||
89 | 95 | ||
90 | $preloader = <<<JS | 96 | $preloader = <<<JS |
91 | $(".preloaded_") | 97 | $(".preloaded_") |
@@ -121,7 +127,6 @@ if((Yii::$app->controller->id ==='category'||'blog')) { | @@ -121,7 +127,6 @@ if((Yii::$app->controller->id ==='category'||'blog')) { | ||
121 | <head> | 127 | <head> |
122 | <meta charset="<?= \Yii::$app->charset ?>"> | 128 | <meta charset="<?= \Yii::$app->charset ?>"> |
123 | <meta name="viewport" content="width=device-width, initial-scale=1"> | 129 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
124 | - <meta name="robots" content="all"> | ||
125 | <link rel="icon" href="/img/favicon.ico" type="image/x-icon"> | 130 | <link rel="icon" href="/img/favicon.ico" type="image/x-icon"> |
126 | <?= Html::csrfMetaTags() ?> | 131 | <?= Html::csrfMetaTags() ?> |
127 | <title><?= Html::encode($seo->title) ?></title> | 132 | <title><?= Html::encode($seo->title) ?></title> |