Skip to content

Commit ddb6514

Browse files
author
Igor Chepurnoy
committed
update migration, improve validation rules in the CmsModel
1 parent 4a1cdfd commit ddb6514

File tree

8 files changed

+48
-43
lines changed

8 files changed

+48
-43
lines changed

components/PageUrlRule.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ class PageUrlRule extends UrlRule
2727
* @param \yii\web\UrlManager $manager
2828
* @param \yii\web\Request $request
2929
*
30-
* @return boolean
30+
* @return array|bool
3131
*/
3232
public function parseRequest($manager, $request)
3333
{
3434
$pathInfo = $request->getPathInfo();
35-
// get path without '/' in end
3635
$url = preg_replace("#/$#", "", $pathInfo);
37-
// find page by url in db
3836
$page = (new CmsModel())->findPage($url);
39-
// redirect to page
37+
4038
if (!empty($page)) {
4139
$params['pageAlias'] = $url;
4240
$params['pageId'] = $page->id;
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
use yii\db\Schema;
43
use yii\db\Migration;
54

5+
/**
6+
* Class m150212_182851_init_cms
7+
*/
68
class m150212_182851_init_cms extends Migration
79
{
810
public function up()
@@ -14,17 +16,17 @@ public function up()
1416
}
1517

1618
$this->createTable('{{%Cms}}', [
17-
'id' => Schema::TYPE_PK,
18-
'url' => Schema::TYPE_STRING . '(255)',
19-
'title' => Schema::TYPE_STRING . '(255)',
20-
'content' => Schema::TYPE_TEXT,
21-
'status' => Schema::TYPE_SMALLINT,
22-
'commentAvailable' => 'TINYINT(1) DEFAULT 0',
23-
'metaTitle' => Schema::TYPE_TEXT,
24-
'metaDescription' => Schema::TYPE_TEXT,
25-
'metaKeywords' => Schema::TYPE_TEXT,
26-
'createdAt' => Schema::TYPE_INTEGER . ' NOT NULL',
27-
'updatedAt' => Schema::TYPE_INTEGER . ' NOT NULL',
19+
'id' => $this->primaryKey(),
20+
'url' => $this->string()->notNull(),
21+
'title' => $this->string()->notNull(),
22+
'content' => $this->text()->notNull(),
23+
'status' => $this->smallInteger()->notNull()->defaultValue(1),
24+
'commentAvailable' => $this->smallInteger()->notNull()->defaultValue(0),
25+
'metaTitle' => $this->text()->notNull(),
26+
'metaDescription' => $this->text(),
27+
'metaKeywords' => $this->text(),
28+
'createdAt' => $this->integer()->notNull(),
29+
'updatedAt' => $this->integer()->notNull()
2830
], $tableOptions);
2931

3032
}
@@ -33,4 +35,4 @@ public function down()
3335
{
3436
$this->dropTable('{{%Cms}}');
3537
}
36-
}
38+
}

models/CmsModel.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Yii;
66
use yii\db\ActiveRecord;
77
use yii2mod\cms\models\enumerables\CmsStatus;
8+
use yii2mod\enum\helpers\BooleanEnum;
89

910
/**
1011
* Cms model
@@ -37,12 +38,16 @@ public static function tableName()
3738
public function rules()
3839
{
3940
return [
40-
[['url', 'title', 'content', 'metaTitle'], 'required'],
41-
[['url'], 'match', 'pattern' => '/^[a-z0-9\/-]+$/'],
41+
[['title', 'content', 'url', 'metaTitle', 'metaDescription', 'metaKeywords'], 'trim'],
42+
[['title', 'content', 'url', 'metaTitle'], 'required'],
43+
['url', 'match', 'pattern' => '/^[a-z0-9\/-]+$/'],
44+
['url', 'unique'],
4245
[['content', 'metaTitle', 'metaDescription', 'metaKeywords'], 'string'],
43-
[['url'], 'unique'],
46+
['status', 'default', 'value' => CmsStatus::ENABLED],
47+
['status', 'in', 'range' => [CmsStatus::ENABLED, CmsStatus::DISABLED]],
48+
['commentAvailable', 'default', 'value' => BooleanEnum::NO],
4449
[['status', 'createdAt', 'updatedAt', 'commentAvailable'], 'integer'],
45-
[['url', 'title'], 'string', 'max' => 255]
50+
[['title', 'url'], 'string', 'max' => 255]
4651
];
4752
}
4853

models/CmsQuery.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use yii\db\ActiveQuery;
66
use yii2mod\cms\models\enumerables\CmsStatus;
7-
use yii2mod\settings\models\enumerables\SettingStatus;
87

98
/**
109
* Class CmsQuery

models/enumerables/CmsStatus.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
*/
1111
class CmsStatus extends BaseEnum
1212
{
13-
public static $messageCategory = 'yii2mod.cms';
1413
const ENABLED = 1;
1514
const DISABLED = 0;
1615

16+
/**
17+
* @var string message category
18+
*/
19+
public static $messageCategory = 'yii2mod.cms';
20+
1721
/**
1822
* @var array
1923
*/

models/search/CmsModelSearch.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ public function rules()
2222
}
2323

2424
/**
25-
* Setup search function for filtering and sorting
25+
* Creates data provider instance with search query applied
2626
*
2727
* @param $params
28+
*
2829
* @return ActiveDataProvider
2930
*/
3031
public function search($params)
3132
{
3233
$query = self::find();
34+
3335
$dataProvider = new ActiveDataProvider([
3436
'query' => $query,
3537
'pagination' => [
@@ -41,13 +43,10 @@ public function search($params)
4143
'defaultOrder' => ['id' => SORT_DESC],
4244
]);
4345

44-
45-
// load the search form data and validate
4646
if (!($this->load($params))) {
4747
return $dataProvider;
4848
}
4949

50-
//adjust the query by adding the filters
5150
$query->andFilterWhere(['id' => $this->id]);
5251
$query->andFilterWhere(['status' => $this->status]);
5352
$query->andFilterWhere(['commentAvailable' => $this->commentAvailable]);

tests/PageActionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace yii2mod\cms\tests;
44

55
use Yii;
6-
use yii\web\NotFoundHttpException;
76
use yii2mod\cms\actions\PageAction;
87

98
/**

tests/TestCase.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ protected function destroyApplication()
8080
Yii::$app = null;
8181
}
8282

83-
8483
/**
8584
* @param array $config controller config.
8685
* @return Controller controller instance.
@@ -101,16 +100,16 @@ protected function setupTestDbData()
101100

102101
$db->createCommand()->createTable('Cms', [
103102
'id' => 'pk',
104-
'url' => 'string',
105-
'title' => 'string',
106-
'content' => 'text',
107-
'status' => 'integer',
108-
'commentAvailable' => 'integer',
109-
'metaTitle' => 'text',
103+
'url' => 'string not null',
104+
'title' => 'string not null',
105+
'content' => 'text not null',
106+
'status' => 'smallint not null default 1',
107+
'commentAvailable' => 'smallint not null default 0',
108+
'metaTitle' => 'text not null',
110109
'metaDescription' => 'text',
111110
'metaKeywords' => 'text',
112-
'createdAt' => 'integer',
113-
'updatedAt' => 'integer',
111+
'createdAt' => 'integer not null',
112+
'updatedAt' => 'integer not null',
114113
])->execute();
115114

116115
// Data :
@@ -119,22 +118,22 @@ protected function setupTestDbData()
119118
'url' => 'about-us',
120119
'title' => 'about',
121120
'content' => 'test content',
122-
'status' => 1,
123-
'commentAvailable' => 0,
124121
'metaTitle' => 'test content',
125122
'metaDescription' => 'test content',
126-
'metaKeywords' => 'test content'
123+
'metaKeywords' => 'test content',
124+
'createdAt' => time(),
125+
'updatedAt' => time(),
127126
])->execute();
128127

129128
$db->createCommand()->insert('Cms', [
130129
'url' => 'some-url',
131130
'title' => 'some title',
132131
'content' => 'My site name is {siteName}',
133-
'status' => 1,
134-
'commentAvailable' => 0,
135132
'metaTitle' => 'test content',
136133
'metaDescription' => 'test content',
137-
'metaKeywords' => 'test content'
134+
'metaKeywords' => 'test content',
135+
'createdAt' => time(),
136+
'updatedAt' => time(),
138137
])->execute();
139138
}
140139
}

0 commit comments

Comments
 (0)