Commit 585f04ca06d450506961a845bc9622f7965167f6

Authored by Administrator
1 parent 37979782

15.04.16 seo widget

backend/controllers/SeoDynamicController.php
@@ -36,7 +36,7 @@ class SeoDynamicController extends Controller @@ -36,7 +36,7 @@ class SeoDynamicController extends Controller
36 public function actionIndex($seo_category_id) 36 public function actionIndex($seo_category_id)
37 { 37 {
38 $searchModel = new SeoDynamicSearch(); 38 $searchModel = new SeoDynamicSearch();
39 - $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 39 + $dataProvider = $searchModel->search($seo_category_id,Yii::$app->request->queryParams);
40 40
41 return $this->render('index', [ 41 return $this->render('index', [
42 'searchModel' => $searchModel, 42 'searchModel' => $searchModel,
backend/views/seo-dynamic/_form.php
@@ -16,6 +16,10 @@ use yii\widgets\ActiveForm; @@ -16,6 +16,10 @@ use yii\widgets\ActiveForm;
16 16
17 <?= $form->field($model, 'action')->textInput(['maxlength' => true]) ?> 17 <?= $form->field($model, 'action')->textInput(['maxlength' => true]) ?>
18 18
  19 + <?= $form->field($model, 'param')->textInput(['maxlength' => true]) ?>
  20 +
  21 + <?= $form->field($model, 'key')->textInput(['maxlength' => true]) ?>
  22 +
19 <?= $form->field($model, 'fields')->textInput(['maxlength' => true]) ?> 23 <?= $form->field($model, 'fields')->textInput(['maxlength' => true]) ?>
20 24
21 <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?> 25 <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
common/models/SeoDynamic.php
@@ -15,8 +15,10 @@ use Yii; @@ -15,8 +15,10 @@ use Yii;
15 * @property string $title 15 * @property string $title
16 * @property string $h1 16 * @property string $h1
17 * @property string $description 17 * @property string $description
18 - * @property string $seo-text 18 + * @property string $seo_text
19 * @property integer $status 19 * @property integer $status
  20 + * @property string $param
  21 + * @property string $key
20 * 22 *
21 * @property SeoCategory $seoCategory 23 * @property SeoCategory $seoCategory
22 */ 24 */
@@ -39,7 +41,7 @@ class SeoDynamic extends \yii\db\ActiveRecord @@ -39,7 +41,7 @@ class SeoDynamic extends \yii\db\ActiveRecord
39 [['seo_category_id', 'status'], 'integer'], 41 [['seo_category_id', 'status'], 'integer'],
40 [['seo_text'], 'string'], 42 [['seo_text'], 'string'],
41 [['name', 'action'], 'string', 'max' => 200], 43 [['name', 'action'], 'string', 'max' => 200],
42 - [['fields', 'title', 'h1', 'description'], 'string', 'max' => 255], 44 + [['fields', 'title', 'h1', 'description', 'param', 'key'], 'string', 'max' => 255],
43 [['seo_category_id'], 'exist', 'skipOnError' => true, 'targetClass' => SeoCategory::className(), 'targetAttribute' => ['seo_category_id' => 'seo_category_id']], 45 [['seo_category_id'], 'exist', 'skipOnError' => true, 'targetClass' => SeoCategory::className(), 'targetAttribute' => ['seo_category_id' => 'seo_category_id']],
44 ]; 46 ];
45 } 47 }
@@ -60,6 +62,8 @@ class SeoDynamic extends \yii\db\ActiveRecord @@ -60,6 +62,8 @@ class SeoDynamic extends \yii\db\ActiveRecord
60 'description' => Yii::t('app', 'Description'), 62 'description' => Yii::t('app', 'Description'),
61 'seo_text' => Yii::t('app', 'Seo Text'), 63 'seo_text' => Yii::t('app', 'Seo Text'),
62 'status' => Yii::t('app', 'Status'), 64 'status' => Yii::t('app', 'Status'),
  65 + 'param' => Yii::t('app', 'Param'),
  66 + 'key' => Yii::t('app', 'Key'),
63 ]; 67 ];
64 } 68 }
65 69
common/models/SeoDynamicSearch.php
@@ -39,7 +39,7 @@ class SeoDynamicSearch extends SeoDynamic @@ -39,7 +39,7 @@ class SeoDynamicSearch extends SeoDynamic
39 * 39 *
40 * @return ActiveDataProvider 40 * @return ActiveDataProvider
41 */ 41 */
42 - public function search($params) 42 + public function search($seo_category_id, $params)
43 { 43 {
44 $query = SeoDynamic::find(); 44 $query = SeoDynamic::find();
45 45
@@ -60,7 +60,7 @@ class SeoDynamicSearch extends SeoDynamic @@ -60,7 +60,7 @@ class SeoDynamicSearch extends SeoDynamic
60 // grid filtering conditions 60 // grid filtering conditions
61 $query->andFilterWhere([ 61 $query->andFilterWhere([
62 'seo_dynamic_id' => $this->seo_dynamic_id, 62 'seo_dynamic_id' => $this->seo_dynamic_id,
63 - 'seo_category_id' => $this->seo_category_id, 63 + 'seo_category_id' => $seo_category_id,
64 'status' => $this->status, 64 'status' => $this->status,
65 ]); 65 ]);
66 66
console/migrations/m160414_101644_add_param_to_seo_dynamic.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\db\Migration;
  4 +
  5 +/**
  6 + * Handles adding the columns `param`
  7 + * for table `seo_dynamic`.
  8 + */
  9 +class m160414_101644_add_param_to_seo_dynamic extends Migration
  10 +{
  11 + /**
  12 + * @inheritdoc
  13 + */
  14 + public function up()
  15 + {
  16 + $this->addColumn('seo_dynamic', 'param', $this->string());
  17 + }
  18 +
  19 + /**
  20 + * @inheritdoc
  21 + */
  22 + public function down()
  23 + {
  24 + $this->dropColumn('seo_dynamic', 'param');
  25 + }
  26 +}
console/migrations/m160414_214745_add_key_to_seo_dynamic.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\db\Migration;
  4 +
  5 +/**
  6 + * Handles adding the columns `key`
  7 + * for table `seo_dynamic`.
  8 + */
  9 +class m160414_214745_add_key_to_seo_dynamic extends Migration
  10 +{
  11 + /**
  12 + * @inheritdoc
  13 + */
  14 + public function up()
  15 + {
  16 + $this->addColumn('seo_dynamic', 'key', $this->string());
  17 + }
  18 +
  19 + /**
  20 + * @inheritdoc
  21 + */
  22 + public function down()
  23 + {
  24 + $this->dropColumn('seo_dynamic', 'key');
  25 + }
  26 +}
frontend/views/catalog/product.php
1 <?php 1 <?php
2 /** @var $this \yii\web\View */ 2 /** @var $this \yii\web\View */
3 /** @var $dataProvider \yii\data\ActiveDataProvider */ 3 /** @var $dataProvider \yii\data\ActiveDataProvider */
4 - 4 +die('here1');
5 $this->title = $product->name; 5 $this->title = $product->name;
6 foreach($product->category->getParents()->all() as $parent) { 6 foreach($product->category->getParents()->all() as $parent) {
7 $this->params['breadcrumbs'][] = ['label' => $parent->categoryName->value, 'url' => ['catalog/category', 'category' => $parent]]; 7 $this->params['breadcrumbs'][] = ['label' => $parent->categoryName->value, 'url' => ['catalog/category', 'category' => $parent]];
frontend/views/catalog/products.php
@@ -3,14 +3,21 @@ @@ -3,14 +3,21 @@
3 /** @var $productProvider \yii\data\ActiveDataProvider */ 3 /** @var $productProvider \yii\data\ActiveDataProvider */
4 /** @var $brandProvider \yii\data\ActiveDataProvider */ 4 /** @var $brandProvider \yii\data\ActiveDataProvider */
5 5
  6 +
6 use yii\helpers\Url; 7 use yii\helpers\Url;
7 use common\modules\product\helpers\ProductHelper; 8 use common\modules\product\helpers\ProductHelper;
8 9
  10 +
9 $this->title = $category->categoryName->value; 11 $this->title = $category->categoryName->value;
10 foreach($category->getParents()->all() as $parent) { 12 foreach($category->getParents()->all() as $parent) {
11 $this->params['breadcrumbs'][] = ['label' => $parent->categoryName->value, 'url' => ['catalog/category', 'category' => $parent]]; 13 $this->params['breadcrumbs'][] = ['label' => $parent->categoryName->value, 'url' => ['catalog/category', 'category' => $parent]];
12 } 14 }
13 $this->params['breadcrumbs'][] = $category->categoryName->value; 15 $this->params['breadcrumbs'][] = $category->categoryName->value;
  16 +$this->params['seo']['seo_text'] = 'TEST SEO TEXT';
  17 +$this->params['seo']['h1'] = 'TEST H1';
  18 +$this->params['seo']['description'] = 'TEST DESCRIPTION';
  19 +$this->params['seo']['fields']['name'] = 'TEST NAME FROM FIELD';
  20 +$this->params['seo']['key']= 'product_list';
14 ?> 21 ?>
15 <script type="text/javascript"> 22 <script type="text/javascript">
16 <?php if ($priceLimits['min'] < $priceLimits['max']) :?> 23 <?php if ($priceLimits['min'] < $priceLimits['max']) :?>
frontend/views/event/index.php
@@ -2,6 +2,10 @@ @@ -2,6 +2,10 @@
2 use yii\widgets\ListView; 2 use yii\widgets\ListView;
3 use frontend\widgets\Seo; 3 use frontend\widgets\Seo;
4 $this->params['breadcrumbs'][] = ['label' => 'Акции', 'url' => ['index']]; 4 $this->params['breadcrumbs'][] = ['label' => 'Акции', 'url' => ['index']];
  5 +$this->params['seo']['seo_text'] = 'TEST SEO TEXT';
  6 +$this->params['seo']['h1'] = 'TEST H1';
  7 +$this->params['seo']['description'] = 'TEST DESCRIPTION';
  8 +$this->params['seo']['fields']['name'] = 'TEST NAME FROM FIELD';
5 ?> 9 ?>
6 10
7 <?php 11 <?php
frontend/views/event/view.php
@@ -22,5 +22,5 @@ $this-&gt;params[&#39;name&#39;] = $this-&gt;title; @@ -22,5 +22,5 @@ $this-&gt;params[&#39;name&#39;] = $this-&gt;title;
22 </div> 22 </div>
23 </div> 23 </div>
24 <div class="seo-text"> 24 <div class="seo-text">
25 - <?= Seo::widget(['row'=>'seo_text'])?> 25 +<!-- --><?//= Seo::widget(['row'=>'seo_text'])?>
26 </div> 26 </div>
frontend/views/layouts/main.php
@@ -19,24 +19,8 @@ AppAsset::register($this); @@ -19,24 +19,8 @@ AppAsset::register($this);
19 <html lang="en"> 19 <html lang="en">
20 <head> 20 <head>
21 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 21 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
22 - <title><?= Seo::widget([  
23 - 'row'=>'title',  
24 - 'own_attr'=>$this->title,  
25 - 'fields' =>[  
26 - 'name' => isset($this->params[ 'name' ]) ? $this->params[ 'name' ] : ''  
27 - ]  
28 - ])?>  
29 - </title>  
30 - <?php $this->registerMetaTag([  
31 - 'name' => 'description',  
32 - 'content' => Seo::widget([  
33 - 'row'=>'description',  
34 - 'own_attr'=>isset($this->params[ 'description' ]) ? $this->params[ 'description' ] : '',  
35 - 'fields' =>[  
36 - 'name' => isset($this->params[ 'name' ]) ? $this->params[ 'name' ] : ''  
37 - ]  
38 - ])  
39 - ]); ?> 22 + <title><?= Seo::widget([ 'row'=>'title'])?></title>
  23 + <?= Seo::widget([ 'row'=>'description'])?>
40 <?php $this->head() ?> 24 <?php $this->head() ?>
41 <?//= Html::csrfMetaTags() ?> 25 <?//= Html::csrfMetaTags() ?>
42 <link href='https://fonts.googleapis.com/css?family=Ubuntu:400,300italic,300,400italic,500,500italic,700italic,700' rel='stylesheet' type='text/css'> 26 <link href='https://fonts.googleapis.com/css?family=Ubuntu:400,300italic,300,400italic,500,500italic,700italic,700' rel='stylesheet' type='text/css'>
frontend/widgets/Seo.php
1 <?php 1 <?php
2 namespace frontend\widgets; 2 namespace frontend\widgets;
3 -use common\models\SeoCategory;  
4 use common\models\SeoDynamic; 3 use common\models\SeoDynamic;
5 use yii\base\Widget; 4 use yii\base\Widget;
6 use yii\helpers\Html; 5 use yii\helpers\Html;
@@ -11,9 +10,20 @@ class Seo extends Widget @@ -11,9 +10,20 @@ class Seo extends Widget
11 public $row; 10 public $row;
12 public $own_attr; 11 public $own_attr;
13 public $fields; 12 public $fields;
  13 + public $description;
  14 + public $title;
  15 + public $seo_text;
  16 + public $h1;
  17 + public $key;
  18 + public $project_name;
  19 + const SEO_TEXT = 'seo_text';
  20 + const DESCRIPTION = 'description';
  21 + const H1 = 'h1';
  22 + const TITLE = 'title';
14 23
15 public function init(){ 24 public function init(){
16 - $this->url = $_SERVER['REQUEST_URI']; 25 + $this->url = \Yii::$app->request->url;
  26 + $this->project_name = \Yii::$app->name;
17 parent::init(); 27 parent::init();
18 28
19 } 29 }
@@ -21,27 +31,62 @@ class Seo extends Widget @@ -21,27 +31,62 @@ class Seo extends Widget
21 31
22 public function run() 32 public function run()
23 { 33 {
24 - $row = '';  
25 34
  35 + $seoData = $this->getViewData();
  36 + foreach($seoData as $key=>$value){
  37 + $this->$key = $value;
  38 + }
26 39
27 - $widgetData = $this->findSeoByUrl();  
28 - if($widgetData instanceof \common\models\Seo){  
29 40
30 - $row = $widgetData->{$this->row};  
31 - } else{ 41 + switch ($this->row) {
  42 + case self::SEO_TEXT:
  43 + $this->selectSeoData(self::SEO_TEXT);
32 44
33 - $widgetData = $this->findSeoByDynamic();  
34 - if($widgetData instanceof SeoDynamic){  
35 - $row = $widgetData->{$this->row};  
36 - }  
37 - }  
38 - if(!empty($this->own_attr) && empty($row)) { 45 + break;
  46 + case self::H1:
  47 + $this->selectSeoData(self::H1);
  48 + break;
  49 + case self::TITLE:
39 50
40 - $row = $this->own_attr;  
41 - } 51 + $filter = \Yii::$app->request->get('filter', []);
  52 + if(!empty($filter)){
  53 + $filter_row = '';
  54 + foreach($filter as $sub_filter_name => $sub_filter_array){
  55 + if($sub_filter_name=='options'){
  56 + foreach($sub_filter_array as $f_name=>$f_values){
  57 + $filter_row .= $f_name.':'.implode(',',$f_values).'|';
  58 + }
  59 + }
  60 +
  61 + }
  62 + $this->fields['name'] = $filter_row;
  63 + }
  64 +
  65 + $title = $this->selectSeoData(self::TITLE);
  66 +
  67 +
  68 +
  69 + if(!empty($title)){
  70 + return $title;
  71 + } else {
  72 + return $this->project_name;
  73 + }
  74 +
  75 + break;
  76 + case self::DESCRIPTION:
  77 + $description = $this->selectSeoData(self::DESCRIPTION);
42 78
  79 + if(!empty($description)){
43 80
44 - return $this->replaceData($row); 81 + $this->getView()->registerMetaTag([
  82 + 'name' => 'description',
  83 + 'content' => $description
  84 + ]);
  85 +
  86 + }
  87 +
  88 + break;
  89 + }
45 90
46 91
47 } 92 }
@@ -54,6 +99,7 @@ class Seo extends Widget @@ -54,6 +99,7 @@ class Seo extends Widget
54 $str = str_replace('{'.$field_name.'}', $field_value, $str); 99 $str = str_replace('{'.$field_name.'}', $field_value, $str);
55 } 100 }
56 } 101 }
  102 + $str = str_replace('{project_name}', $this->project_name, $str);
57 return Html::encode($str); 103 return Html::encode($str);
58 } 104 }
59 105
@@ -64,6 +110,54 @@ class Seo extends Widget @@ -64,6 +110,54 @@ class Seo extends Widget
64 110
65 protected function findSeoByDynamic() 111 protected function findSeoByDynamic()
66 { 112 {
67 - return SeoDynamic::find()->joinWith('seoCategory')->where(['controller'=> \Yii::$app->controller->id, 'action'=>\Yii::$app->controller->action->id])->one(); 113 + $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller'=> \Yii::$app->controller->id, 'action'=>\Yii::$app->controller->action->id]);
  114 + if(!empty($this->key)){
  115 + $query->andWhere(['key'=>$this->key]);
  116 + }
  117 + return $query->one();
  118 + }
  119 +
  120 +
  121 + protected function getViewData(){
  122 + $params = $this->getView()->params;
  123 + if(isset($params['seo'])){
  124 + return $params['seo'];
  125 + } else {
  126 + return [];
  127 + }
  128 + }
  129 +
  130 + protected function selectSeoData($param){
  131 + $result = '';
  132 +
  133 +
  134 +
  135 + if(!empty($this->$param)){
  136 +
  137 + $result = $this->$param;
  138 +
  139 + } else {
  140 +
  141 + $widgetData = $this->findSeoByUrl();
  142 +
  143 + if($widgetData instanceof \common\models\Seo){
  144 +
  145 + $result = $widgetData->$param;
  146 + } else {
  147 +
  148 + $widgetData = $this->findSeoByDynamic();
  149 +
  150 + if($widgetData instanceof SeoDynamic){
  151 +
  152 + $result = $widgetData->$param;
  153 +
  154 + }
  155 + }
  156 + }
  157 +
  158 + return $this->replaceData($result);
  159 +
68 } 160 }
  161 +
  162 +
69 } 163 }
70 \ No newline at end of file 164 \ No newline at end of file