Skip to content

Commit

Permalink
Merge pull request noumo#9 from noumo/slug-behavior
Browse files Browse the repository at this point in the history
slugs update
  • Loading branch information
noumo committed Feb 10, 2015
2 parents c1f01fb + 442ef63 commit 4fdd479
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 80 deletions.
2 changes: 2 additions & 0 deletions components/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 0 additions & 5 deletions helpers/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
}
}
1 change: 1 addition & 0 deletions messages/ru/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => 'Создать настройку',
Expand Down
7 changes: 2 additions & 5 deletions modules/catalog/api/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="/admin/catalog/a/create/?slug='.$id_slug.'" target="_blank">'.Yii::t('easyii/catalog/api', 'Create category').'</a>');
}
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'));
}
}
}
4 changes: 0 additions & 4 deletions modules/catalog/controllers/AController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()){
Expand Down
4 changes: 0 additions & 4 deletions modules/catalog/controllers/ItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 24 additions & 4 deletions modules/catalog/models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}],
];
}

Expand Down Expand Up @@ -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();
Expand All @@ -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'];
}
}
30 changes: 25 additions & 5 deletions modules/catalog/models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}]
];
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'];
}
}
2 changes: 1 addition & 1 deletion modules/catalog/scheme/catalog_items.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 2 additions & 5 deletions modules/file/api/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="/admin/file/a/create/?slug='.$id_slug.'" target="_blank">'.Yii::t('easyii/file/api', 'Create file').'</a>');
}
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'));
}
}
}
4 changes: 0 additions & 4 deletions modules/file/controllers/AController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
31 changes: 26 additions & 5 deletions modules/file/models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()]
];
}
Expand All @@ -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;
Expand All @@ -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'];
}
}
7 changes: 2 additions & 5 deletions modules/gallery/api/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="/admin/gallery/a/create/?slug='.$id_slug.'" target="_blank">'.Yii::t('easyii/gallery/api', 'Create album').'</a>');
}
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'));
}
}
}
5 changes: 0 additions & 5 deletions modules/gallery/controllers/AController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 24 additions & 4 deletions modules/gallery/models/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}],
];
}

Expand Down Expand Up @@ -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();
Expand All @@ -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'];
}
}
7 changes: 2 additions & 5 deletions modules/page/api/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="/admin/page/a/create/?slug='.$id_slug.'" target="_blank">'.Yii::t('easyii/page/api', 'Create page').'</a>');
}
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'));
}
}
}
Loading

0 comments on commit 4fdd479

Please sign in to comment.