From 442ef634525ecb5a04589bda0cd3e534824e47ea Mon Sep 17 00:00:00 2001 From: noumo Date: Tue, 10 Feb 2015 12:45:39 +0300 Subject: [PATCH] slugs update --- components/ActiveRecord.php | 2 ++ helpers/Data.php | 5 --- messages/ru/admin.php | 1 + modules/catalog/api/Catalog.php | 7 ++--- modules/catalog/controllers/AController.php | 4 --- .../catalog/controllers/ItemsController.php | 4 --- modules/catalog/models/Category.php | 28 ++++++++++++++--- modules/catalog/models/Item.php | 30 +++++++++++++++--- modules/catalog/scheme/catalog_items.sql | 2 +- modules/file/api/File.php | 7 ++--- modules/file/controllers/AController.php | 4 --- modules/file/models/File.php | 31 ++++++++++++++++--- modules/gallery/api/Gallery.php | 7 ++--- modules/gallery/controllers/AController.php | 5 --- modules/gallery/models/Album.php | 28 ++++++++++++++--- modules/page/api/Page.php | 7 ++--- modules/page/controllers/AController.php | 4 --- modules/page/models/Page.php | 30 +++++++++++++++--- modules/page/scheme/pages.sql | 2 +- modules/text/api/Text.php | 7 ++--- modules/text/models/Text.php | 8 ++--- 21 files changed, 143 insertions(+), 80 deletions(-) diff --git a/components/ActiveRecord.php b/components/ActiveRecord.php index 36b61f6..57bc35e 100644 --- a/components/ActiveRecord.php +++ b/components/ActiveRecord.php @@ -3,6 +3,8 @@ class ActiveRecord extends \yii\db\ActiveRecord { + public static $slugPattern = '/^[0-9a-z-]{0,128}$/'; + public static function find() { return new ActiveQuery(get_called_class()); diff --git a/helpers/Data.php b/helpers/Data.php index fd43fba..4562061 100644 --- a/helpers/Data.php +++ b/helpers/Data.php @@ -22,9 +22,4 @@ public static function cache($key, $duration, $callable) } return $data; } - - public static function generateSlug($string) - { - return StringHelper::truncate(Inflector::slug($string), 128, ''); - } } \ No newline at end of file diff --git a/messages/ru/admin.php b/messages/ru/admin.php index 996e7d9..bb3e5e2 100644 --- a/messages/ru/admin.php +++ b/messages/ru/admin.php @@ -49,6 +49,7 @@ 'Not found' => 'Не найдено', 'Create error. {0}' => 'Ошибка при создании. {0}', 'Update error. {0}' => 'Ошибка при обновлении. {0}', + 'Slug can contain only 0-9, a-z and "-" characters (max: 128).' => 'Метка может содержать только 0-9, a-z и "-" (макс. 128).', 'Only for developer' => 'Только для разработчика', 'Create setting' => 'Создать настройку', diff --git a/modules/catalog/api/Catalog.php b/modules/catalog/api/Catalog.php index 1055582..c64b879 100644 --- a/modules/catalog/api/Catalog.php +++ b/modules/catalog/api/Catalog.php @@ -210,14 +210,11 @@ private function notFound($id_slug) if(Yii::$app->user->isGuest) { return $this->createCatObject(''); } - elseif(preg_match('/^[a-zA-Z][\w_-]*$/', $id_slug)){ + elseif(preg_match(Category::$slugPattern, $id_slug)){ return $this->createCatObject(''.Yii::t('easyii/catalog/api', 'Create category').''); } - elseif(is_numeric($id_slug)){ - return $this->createCatObject($this->errorText('WRONG CATEGORY_ID')); - } else{ - return $this->createCatObject($this->errorText('WRONG CATEGORY_SLUG')); + return $this->createCatObject($this->errorText('WRONG CATEGORY IDENTIFIER')); } } } \ No newline at end of file diff --git a/modules/catalog/controllers/AController.php b/modules/catalog/controllers/AController.php index 64a8488..61e4a86 100644 --- a/modules/catalog/controllers/AController.php +++ b/modules/catalog/controllers/AController.php @@ -60,10 +60,6 @@ public function actionCreate($slug = null) } } - if($model->slug == '' && $this->module->settings['categoryAutoSlug']){ - $model->slug = \yii\easyii\helpers\Data::generateSlug($model->title); - } - $model->status = Category::STATUS_ON; if($model->save()){ diff --git a/modules/catalog/controllers/ItemsController.php b/modules/catalog/controllers/ItemsController.php index bbf92cb..6d004b4 100644 --- a/modules/catalog/controllers/ItemsController.php +++ b/modules/catalog/controllers/ItemsController.php @@ -56,10 +56,6 @@ public function actionCreate($id) } } - if(!$model->slug && $this->module->settings['itemAutoSlug']){ - $model->slug = \yii\easyii\helpers\Data::generateSlug($model->title); - } - if($model->save()){ $this->flash('success', Yii::t('easyii/catalog', 'Item created')); return $this->redirect('/admin/catalog/items/edit/'.$model->primaryKey); diff --git a/modules/catalog/models/Category.php b/modules/catalog/models/Category.php index 8b42515..4bf1434 100644 --- a/modules/catalog/models/Category.php +++ b/modules/catalog/models/Category.php @@ -2,6 +2,7 @@ namespace yii\easyii\modules\catalog\models; use Yii; +use yii\behaviors\SluggableBehavior; use yii\easyii\behaviors\CacheFlush; use yii\easyii\behaviors\SortableModel; @@ -38,13 +39,15 @@ public function rules() { return [ ['title', 'required'], - [['title', 'slug'], 'trim'], - [['title', 'slug'], 'string', 'max' => 128], + ['title', 'trim'], + ['title', 'string', 'max' => 128], ['thumb', 'image'], ['item_count', 'integer'], - ['slug', 'match', 'pattern' => '/^[a-zA-Z][\w_-]*$/'], - ['slug', 'unique'], + ['slug', 'match', 'pattern' => self::$slugPattern, 'message' => Yii::t('easyii', 'Slug can contain only 0-9, a-z and "-" characters (max: 128).')], ['slug', 'default', 'value' => null], + ['slug', 'unique', 'when' => function($model){ + return $model->slug && !self::autoSlug(); + }], ]; } @@ -80,6 +83,18 @@ public function beforeSave($insert) } } + public function beforeValidate() + { + if(self::autoSlug()){ + $this->attachBehavior('sluggable', [ + 'class' => SluggableBehavior::className(), + 'attribute' => 'title', + 'ensureUnique' => true + ]); + } + return parent::beforeValidate(); + } + public function afterFind() { parent::afterFind(); @@ -103,4 +118,9 @@ public function afterDelete() @unlink(Yii::getAlias('@webroot') . $this->thumb); } } + + public static function autoSlug() + { + return Yii::$app->getModule('admin')->activeModules['catalog']->settings['categoryAutoSlug']; + } } \ No newline at end of file diff --git a/modules/catalog/models/Item.php b/modules/catalog/models/Item.php index 3f4728c..ec1b8c4 100644 --- a/modules/catalog/models/Item.php +++ b/modules/catalog/models/Item.php @@ -2,6 +2,7 @@ namespace yii\easyii\modules\catalog\models; use Yii; +use yii\behaviors\SluggableBehavior; use yii\easyii\behaviors\SortableModel; use yii\easyii\models\Photo; @@ -17,13 +18,15 @@ public function rules() { return [ ['title', 'required'], - [['title', 'slug'], 'trim'], - ['title', 'string', 'max' => 256], + ['title', 'trim'], + ['title', 'string', 'max' => 128], ['thumb', 'image'], - ['slug', 'match', 'pattern' => '/^[a-zA-Z][\w_-]*$/'], - ['slug', 'unique'], + ['description', 'safe'], + ['slug', 'match', 'pattern' => self::$slugPattern, 'message' => Yii::t('easyii', 'Slug can contain only 0-9, a-z and "-" characters (max: 128).')], ['slug', 'default', 'value' => null], - ['description', 'safe'] + ['slug', 'unique', 'when' => function($model){ + return $model->slug && !self::autoSlug(); + }] ]; } @@ -57,6 +60,18 @@ public function beforeSave($insert) } } + public function beforeValidate() + { + if(self::autoSlug()){ + $this->attachBehavior('sluggable', [ + 'class' => SluggableBehavior::className(), + 'attribute' => 'title', + 'ensureUnique' => true + ]); + } + return parent::beforeValidate(); + } + public function afterFind() { parent::afterFind(); @@ -85,4 +100,9 @@ public function afterDelete() @unlink(Yii::getAlias('@webroot') . $this->thumb); } } + + public static function autoSlug() + { + return Yii::$app->getModule('admin')->activeModules['catalog']->settings['itemAutoSlug']; + } } \ No newline at end of file diff --git a/modules/catalog/scheme/catalog_items.sql b/modules/catalog/scheme/catalog_items.sql index 336bc26..d2de9ca 100644 --- a/modules/catalog/scheme/catalog_items.sql +++ b/modules/catalog/scheme/catalog_items.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS `easyii_catalog_items` ( `item_id` int(11) NOT NULL, `category_id` int(11) NOT NULL, - `title` varchar(256) NOT NULL, + `title` varchar(128) NOT NULL, `description` text NOT NULL, `data` text NOT NULL, `thumb` varchar(128) NOT NULL, diff --git a/modules/file/api/File.php b/modules/file/api/File.php index b12a2bd..13f0b19 100644 --- a/modules/file/api/File.php +++ b/modules/file/api/File.php @@ -128,14 +128,11 @@ private function notFound($id_slug) if(Yii::$app->user->isGuest) { return $this->createObject(''); } - elseif(preg_match('/^[a-zA-Z][\w_-]*$/', $id_slug)){ + elseif(preg_match(FileModel::$slugPattern, $id_slug)){ return $this->createObject(''.Yii::t('easyii/file/api', 'Create file').''); } - elseif(is_numeric($id_slug)){ - return $this->createObject($this->errorText('WRONG FILE_ID')); - } else{ - return $this->createObject($this->errorText('WRONG FILE_SLUG')); + return $this->createObject($this->errorText('WRONG FILE IDENTIFIER')); } } } \ No newline at end of file diff --git a/modules/file/controllers/AController.php b/modules/file/controllers/AController.php index 04494e3..f074979 100644 --- a/modules/file/controllers/AController.php +++ b/modules/file/controllers/AController.php @@ -50,10 +50,6 @@ public function actionCreate($slug = null) $model->file = Upload::file($fileInstanse, 'files', false); $model->size = $fileInstanse->size; - if(!$model->slug && $this->module->settings['autoSlug']){ - $model->slug = \yii\easyii\helpers\Data::generateSlug($model->title); - } - if($model->save()){ $this->flash('success', Yii::t('easyii/file', 'File created')); return $this->redirect('/admin/file'); diff --git a/modules/file/models/File.php b/modules/file/models/File.php index 67ca0f5..1870e30 100644 --- a/modules/file/models/File.php +++ b/modules/file/models/File.php @@ -2,6 +2,7 @@ namespace yii\easyii\modules\file\models; use Yii; +use yii\behaviors\SluggableBehavior; use yii\easyii\behaviors\SortableModel; class File extends \yii\easyii\components\ActiveRecord @@ -16,11 +17,14 @@ public function rules() return [ ['file', 'file'], ['title', 'required'], - [['title', 'slug'], 'trim'], - ['slug', 'match', 'pattern' => '/^[a-zA-Z][\w_-]*$/'], - ['slug', 'unique'], + ['title', 'string', 'max' => 128], + ['title', 'trim'], + ['slug', 'match', 'pattern' => self::$slugPattern, 'message' => Yii::t('easyii', 'Slug can contain only 0-9, a-z and "-" characters (max: 128).')], ['slug', 'default', 'value' => null], - [['downloads', 'size'], 'number', 'integerOnly' => true], + ['slug', 'unique', 'when' => function($model){ + return $model->slug && !self::autoSlug(); + }], + [['downloads', 'size'], 'integer'], ['time', 'default', 'value' => time()] ]; } @@ -44,7 +48,7 @@ public function behaviors() public function beforeSave($insert) { if (parent::beforeSave($insert)) { - if(!$this->isNewRecord && $this->file !== $this->oldAttributes['file']){ + if(!$insert && $this->file !== $this->oldAttributes['file']){ @unlink(Yii::getAlias('@webroot').$this->oldAttributes['file']); } return true; @@ -53,10 +57,27 @@ public function beforeSave($insert) } } + public function beforeValidate() + { + if(self::autoSlug()){ + $this->attachBehavior('sluggable', [ + 'class' => SluggableBehavior::className(), + 'attribute' => 'title', + 'ensureUnique' => true + ]); + } + return parent::beforeValidate(); + } + public function afterDelete() { parent::afterDelete(); @unlink(Yii::getAlias('@webroot').$this->file); } + + public static function autoSlug() + { + return Yii::$app->getModule('admin')->activeModules['file']->settings['autoSlug']; + } } \ No newline at end of file diff --git a/modules/gallery/api/Gallery.php b/modules/gallery/api/Gallery.php index 36b4e83..1d4c8b7 100644 --- a/modules/gallery/api/Gallery.php +++ b/modules/gallery/api/Gallery.php @@ -174,14 +174,11 @@ private function notFound($id_slug) if(Yii::$app->user->isGuest) { return $this->createObject(''); } - elseif(preg_match('/^[a-zA-Z][\w_-]*$/', $id_slug)){ + elseif(preg_match(Album::$slugPattern, $id_slug)){ return $this->createObject(''.Yii::t('easyii/gallery/api', 'Create album').''); } - elseif(is_numeric($id_slug)){ - return $this->createObject($this->errorText('WRONG ALBUM_ID')); - } else{ - return $this->createObject($this->errorText('WRONG ALBUM_SLUG')); + return $this->createObject($this->errorText('WRONG ALBUM IDENTIFIER')); } } } \ No newline at end of file diff --git a/modules/gallery/controllers/AController.php b/modules/gallery/controllers/AController.php index 6971774..6b4250b 100644 --- a/modules/gallery/controllers/AController.php +++ b/modules/gallery/controllers/AController.php @@ -56,11 +56,6 @@ public function actionCreate($slug = null) $model->thumb = ''; } } - - if(!$model->slug && $this->module->settings['autoSlug']){ - $model->slug = \yii\easyii\helpers\Data::generateSlug($model->title); - } - if($model->save()){ $this->flash('success', Yii::t('easyii/gallery', 'Album created')); return $this->redirect('/admin/gallery/a/photos/'.$model->primaryKey); diff --git a/modules/gallery/models/Album.php b/modules/gallery/models/Album.php index 425b963..e7972c9 100644 --- a/modules/gallery/models/Album.php +++ b/modules/gallery/models/Album.php @@ -2,7 +2,7 @@ namespace yii\easyii\modules\gallery\models; use Yii; - +use yii\behaviors\SluggableBehavior; use yii\easyii\behaviors\SortableModel; use yii\easyii\models\Photo; @@ -31,10 +31,13 @@ public function rules() return [ ['title', 'required'], ['title', 'trim'], + ['title', 'string', 'max' => 128], ['thumb', 'image'], - ['slug', 'match', 'pattern' => '/^[a-zA-Z][\w_-]*$/'], - ['slug', 'unique'], - ['slug', 'default', 'value' => null] + ['slug', 'match', 'pattern' => self::$slugPattern, 'message' => Yii::t('easyii', 'Slug can contain only 0-9, a-z and "-" characters (max: 128).')], + ['slug', 'default', 'value' => null], + ['slug', 'unique', 'when' => function($model){ + return $model->slug && !self::autoSlug(); + }], ]; } @@ -66,6 +69,18 @@ public function beforeSave($insert) } } + public function beforeValidate() + { + if(self::autoSlug()){ + $this->attachBehavior('sluggable', [ + 'class' => SluggableBehavior::className(), + 'attribute' => 'title', + 'ensureUnique' => true + ]); + } + return parent::beforeValidate(); + } + public function getPhotos() { return $this->hasMany(Photo::className(), ['item_id' => 'album_id'])->where(['module' => 'gallery'])->sort(); @@ -83,4 +98,9 @@ public function afterDelete() @unlink(Yii::getAlias('@webroot').$this->thumb); } } + + public static function autoSlug() + { + return Yii::$app->getModule('admin')->activeModules['gallery']->settings['autoSlug']; + } } \ No newline at end of file diff --git a/modules/page/api/Page.php b/modules/page/api/Page.php index 82baa66..2f55771 100644 --- a/modules/page/api/Page.php +++ b/modules/page/api/Page.php @@ -50,14 +50,11 @@ private function notFound($id_slug) if(Yii::$app->user->isGuest) { return $this->createObject(''); } - elseif(preg_match('/^[a-zA-Z][\w_-]*$/', $id_slug)){ + elseif(preg_match(PageModel::$slugPattern, $id_slug)){ return $this->createObject(''.Yii::t('easyii/page/api', 'Create page').''); } - elseif(is_numeric($id_slug)){ - return $this->createObject($this->errorText('WRONG PAGE_ID')); - } else{ - return $this->createObject($this->errorText('WRONG PAGE_SLUG')); + return $this->createObject($this->errorText('WRONG PAGE IDENTIFIER')); } } } \ No newline at end of file diff --git a/modules/page/controllers/AController.php b/modules/page/controllers/AController.php index 5fb91dd..425fc9c 100644 --- a/modules/page/controllers/AController.php +++ b/modules/page/controllers/AController.php @@ -32,10 +32,6 @@ public function actionCreate($slug = null) return ActiveForm::validate($model); } else{ - if(!$model->slug && $this->module->settings['autoSlug']){ - $model->slug = \yii\easyii\helpers\Data::generateSlug($model->title); - } - if($model->save()){ $this->flash('success', Yii::t('easyii/page', 'Page created')); return $this->redirect('/admin/page'); diff --git a/modules/page/models/Page.php b/modules/page/models/Page.php index 219ec92..0d8e75a 100644 --- a/modules/page/models/Page.php +++ b/modules/page/models/Page.php @@ -2,6 +2,7 @@ namespace yii\easyii\modules\page\models; use Yii; +use yii\behaviors\SluggableBehavior; class Page extends \yii\easyii\components\ActiveRecord { @@ -14,11 +15,13 @@ public function rules() { return [ [['title','text'], 'required'], - [['title', 'text', 'slug'], 'trim'], - ['title', 'string', 'max' => 256], - ['slug', 'unique'], - ['slug', 'match', 'pattern' => '/^[a-zA-Z][\w_-]*$/'], - ['slug', 'default', 'value' => null] + [['title', 'text'], 'trim'], + ['title', 'string', 'max' => 128], + ['slug', 'match', 'pattern' => self::$slugPattern, 'message' => Yii::t('easyii', 'Slug can contain only 0-9, a-z and "-" characters (max: 128).')], + ['slug', 'default', 'value' => null], + ['slug', 'unique', 'when' => function($model){ + return $model->slug && !self::autoSlug(); + }], ]; } @@ -30,4 +33,21 @@ public function attributeLabels() 'slug' => Yii::t('easyii', 'Slug'), ]; } + + public function beforeValidate() + { + if(self::autoSlug()){ + $this->attachBehavior('sluggable', [ + 'class' => SluggableBehavior::className(), + 'attribute' => 'title', + 'ensureUnique' => true + ]); + } + return parent::beforeValidate(); + } + + public static function autoSlug() + { + return Yii::$app->getModule('admin')->activeModules['page']->settings['autoSlug']; + } } \ No newline at end of file diff --git a/modules/page/scheme/pages.sql b/modules/page/scheme/pages.sql index a2d3d5d..4004ef3 100644 --- a/modules/page/scheme/pages.sql +++ b/modules/page/scheme/pages.sql @@ -1,6 +1,6 @@ CREATE TABLE IF NOT EXISTS `easyii_pages` ( `page_id` int(11) NOT NULL, - `title` varchar(256) NOT NULL, + `title` varchar(128) NOT NULL, `text` text NOT NULL, `slug` varchar(128) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; diff --git a/modules/text/api/Text.php b/modules/text/api/Text.php index 9b9596d..8f73e50 100644 --- a/modules/text/api/Text.php +++ b/modules/text/api/Text.php @@ -56,14 +56,11 @@ private function notFound($id_slug) if(Yii::$app->user->isGuest) { return ''; } - elseif(preg_match('/^[a-zA-Z][\w_-]*$/', $id_slug)){ + elseif(preg_match(TextModel::$slugPattern, $id_slug)){ return ''.Yii::t('easyii/text/api', 'Create text').''; } - elseif(is_numeric($id_slug)){ - return $this->errorText('WRONG TEXT_ID'); - } else{ - return $this->errorText('WRONG TEXT_SLUG'); + return $this->errorText('WRONG TEXT IDENTIFIER'); } } } \ No newline at end of file diff --git a/modules/text/models/Text.php b/modules/text/models/Text.php index 807bfec..8be8e06 100644 --- a/modules/text/models/Text.php +++ b/modules/text/models/Text.php @@ -18,10 +18,10 @@ public function rules() return [ ['text_id', 'number', 'integerOnly' => true], ['text', 'required'], - [['text', 'slug'], 'trim'], - ['slug', 'match', 'pattern' => '/^[a-zA-Z][\w_-]*$/'], - ['slug', 'unique'], - ['slug', 'default', 'value' => null] + ['text', 'trim'], + ['slug', 'match', 'pattern' => self::$slugPattern, 'message' => Yii::t('easyii', 'Slug can contain only 0-9, a-z and "-" characters (max: 128).')], + ['slug', 'default', 'value' => null], + ['slug', 'unique'] ]; }