|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\seo\models; |
|
4
|
|
|
|
|
5
|
|
|
use yii\data\ActiveDataProvider; |
|
6
|
|
|
use yii\db\ActiveRecord; |
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This is the model class for table "seo_redirect". |
|
12
|
|
|
* |
|
13
|
|
|
* @property integer $id |
|
14
|
|
|
* @property string $type |
|
15
|
|
|
* @property string $from |
|
16
|
|
|
* @property string $to |
|
17
|
|
|
* @property boolean $active |
|
18
|
|
|
*/ |
|
19
|
|
|
class Redirect extends ActiveRecord |
|
20
|
|
|
{ |
|
21
|
|
|
const TYPE_STATIC = 'STATIC'; |
|
22
|
|
|
const TYPE_PREG = 'PREG'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return bool|string |
|
26
|
|
|
*/ |
|
27
|
|
|
private static function getFilename() |
|
28
|
|
|
{ |
|
29
|
|
|
return \Yii::getAlias('@app/modules/seo/redirects/redirectsArray.php'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function getTypes() |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
self::TYPE_STATIC => Yii::t('app', 'static'), |
|
36
|
|
|
self::TYPE_PREG => Yii::t('app', 'regular'), |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function tableName() |
|
44
|
|
|
{ |
|
45
|
|
|
return '{{%seo_redirect}}'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
View Code Duplication |
public function rules() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
[['type', 'from', 'to'], 'string'], |
|
55
|
|
|
[['from', 'to'], 'required'], |
|
56
|
|
|
[['active'], 'boolean'] |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public function attributeLabels() |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'id' => \Yii::t('app', 'ID'), |
|
67
|
|
|
'type' => \Yii::t('app', 'Type'), |
|
68
|
|
|
'from' => \Yii::t('app', 'From'), |
|
69
|
|
|
'to' => \Yii::t('app', 'To'), |
|
70
|
|
|
'active' => \Yii::t('app', 'Active'), |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function scenarios() |
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
|
|
'default' => ['id', 'type', 'from', 'to', 'active'], |
|
81
|
|
|
'search' => ['id', 'type', 'from', 'to', 'active'], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Search redirects |
|
88
|
|
|
* @param $params |
|
89
|
|
|
* @return ActiveDataProvider |
|
90
|
|
|
*/ |
|
91
|
|
|
public function search($params) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->load($params); |
|
94
|
|
|
$query = self::find(); |
|
95
|
|
|
foreach ($this->attributes as $name => $value) { |
|
96
|
|
|
if ($value !== null && $value !== '') { |
|
97
|
|
|
if ($name == 'id' || $name == 'type' || $name == 'active') { |
|
98
|
|
|
$query->andWhere("`$name` = :$name", [":$name" => $value]); |
|
99
|
|
|
} else { |
|
100
|
|
|
$query->andWhere("`$name` LIKE :$name", [":$name" => "%$value%"]); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
$dataProvider = new ActiveDataProvider( |
|
105
|
|
|
[ |
|
106
|
|
|
'query' => $query, |
|
107
|
|
|
'pagination' => [ |
|
108
|
|
|
'pageSize' => 10, |
|
109
|
|
|
], |
|
110
|
|
|
] |
|
111
|
|
|
); |
|
112
|
|
|
return $dataProvider; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public static function generateRedirectFile() |
|
116
|
|
|
{ |
|
117
|
|
|
$head = "<?php\n"; |
|
118
|
|
|
$head .= "/*\n"; |
|
119
|
|
|
$head .= " * This file is generated automatically\n"; |
|
120
|
|
|
$head .= "*/\n"; |
|
121
|
|
|
$redirects = [ |
|
122
|
|
|
'static' => static::find() |
|
123
|
|
|
->select(['to', 'from']) |
|
124
|
|
|
->where(['active' => true, 'type' => self::TYPE_STATIC]) |
|
125
|
|
|
->asArray(true) |
|
126
|
|
|
->indexBy('from') |
|
127
|
|
|
->column(), |
|
128
|
|
|
'regular' => static::find() |
|
129
|
|
|
->select(['to', 'from']) |
|
130
|
|
|
->where(['active' => true, 'type' => self::TYPE_PREG]) |
|
131
|
|
|
->asArray(true) |
|
132
|
|
|
->indexBy('from') |
|
133
|
|
|
->column() |
|
134
|
|
|
]; |
|
135
|
|
|
$body = "return ".var_export($redirects, true).";\n"; |
|
136
|
|
|
return file_put_contents(self::getFilename(), $head."\n".$body); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public static function deleteRedirectFile() |
|
140
|
|
|
{ |
|
141
|
|
|
return unlink(self::getFilename()); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|