Browse Source

Auto slug if not specified: pages, blog

master
Egorka 6 years ago
parent
commit
a83b4809da
  1. 9
      common/modules/blog/entities/BlogCategory.php
  2. 8
      common/modules/blog/entities/BlogPost.php
  3. 12
      common/modules/blog/entities/BlogTag.php
  4. 8
      common/modules/blog/forms/BlogCategoryForm.php
  5. 1
      common/modules/blog/forms/BlogPostForm.php
  6. 1
      common/modules/blog/forms/BlogTagForm.php
  7. 8
      common/modules/blog/forms/BlogTagSingleForm.php
  8. 1
      common/modules/blog/messages/ru/blog.php
  9. 16
      common/modules/blog/views/manage/category/_form.php
  10. 16
      common/modules/blog/views/manage/tag/_form.php
  11. 8
      common/modules/pages/entities/Page.php
  12. 11
      common/modules/pages/forms/PageForm.php
  13. 1
      common/modules/pages/messages/ru/page.php
  14. 4
      common/modules/pages/services/PageManageService.php
  15. 16
      common/modules/pages/views/manage/page/_form.php

9
common/modules/blog/entities/BlogCategory.php

@ -3,6 +3,7 @@
namespace common\modules\blog\entities;
use core\behaviors\MetaBehavior;
use yii\behaviors\SluggableBehavior;
use core\entities\Meta;
use yii\db\ActiveRecord;
use Yii;
@ -82,7 +83,13 @@ class BlogCategory extends ActiveRecord
public function behaviors(): array
{
return [
MetaBehavior::className(),
MetaBehavior::class,
[
'class' => SluggableBehavior::class,
'attribute' => 'title',
'ensureUnique' => true,
'preserveNonEmptyValues' => true,
],
];
}
}

8
common/modules/blog/entities/BlogPost.php

@ -4,7 +4,6 @@ namespace common\modules\blog\entities;
use common\modules\blog\entities\queries\BlogPostQuery;
use core\behaviors\MetaBehavior;
use core\behaviors\RevisionBehavior;
use core\entities\Meta;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use yii\behaviors\TimestampBehavior;
@ -12,6 +11,7 @@ use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
use yiidreamteam\upload\ImageUploadBehavior;
use yii\behaviors\SluggableBehavior;
use Yii;
@ -317,6 +317,12 @@ class BlogPost extends ActiveRecord
'relations' => ['blogTagAssignments', 'blogComments'],
],
[
'class' => SluggableBehavior::class,
'attribute' => 'title',
'ensureUnique' => true,
'preserveNonEmptyValues' => true,
],
[
// todo Image Sizes to settings or theme settings
'class' => ImageUploadBehavior::class,
'attribute' => 'image',

12
common/modules/blog/entities/BlogTag.php

@ -4,6 +4,7 @@ namespace common\modules\blog\entities;
use yii\db\ActiveRecord;
use yii\caching\TagDependency;
use yii\behaviors\SluggableBehavior;
use Yii;
/**
@ -43,4 +44,15 @@ class BlogTag extends ActiveRecord
{
return '{{%blog_tags}}';
}
public function behaviors() {
return [
[
'class' => SluggableBehavior::class,
'attribute' => 'name',
'ensureUnique' => true,
'preserveNonEmptyValues' => true,
],
];
}
}

8
common/modules/blog/forms/BlogCategoryForm.php

@ -41,7 +41,7 @@ class BlogCategoryForm extends CompositeForm
public function rules(): array
{
return [
[['name', 'slug'], 'required'],
[['name'], 'required'],
[['name', 'slug', 'title'], 'string', 'max' => 255],
[['description'], 'string'],
['slug', SlugValidator::class],
@ -60,6 +60,12 @@ class BlogCategoryForm extends CompositeForm
];
}
public function attributeHints() {
return [
'slug' => Yii::t('blog', 'SEO link will be generated automatically if not specified'),
];
}
public function updateSort() {
$this->sort = BlogCategory::find()->max('sort') + 1;
}

1
common/modules/blog/forms/BlogPostForm.php

@ -108,6 +108,7 @@ class BlogPostForm extends CompositeForm
public function attributeHints() {
return [
'published_at' => Yii::t('blog', 'The article will be published after the specified date if its status is not a draft'),
'slug' => Yii::t('page', 'SEO link will be generated automatically if not specified'),
];
}

1
common/modules/blog/forms/BlogTagForm.php

@ -4,7 +4,6 @@ namespace common\modules\blog\forms;
use common\modules\blog\entities\BlogPost;
use common\modules\blog\entities\BlogTag;
use common\modules\blog\entities\BlogTagAssignment;
use yii\base\Model;
use yii\helpers\ArrayHelper;

8
common/modules/blog/forms/BlogTagSingleForm.php

@ -28,7 +28,7 @@ class BlogTagSingleForm extends Model
public function rules(): array
{
return [
[['name', 'slug'], 'required'],
[['name'], 'required'],
[['name', 'slug'], 'string', 'max' => 255],
['slug', SlugValidator::class],
[['name', 'slug'], 'unique', 'targetClass' => BlogTag::class, 'filter' => $this->_tag ? ['<>', 'id', $this->_tag->id] : null]
@ -42,4 +42,10 @@ class BlogTagSingleForm extends Model
'slug' => Yii::t('blog', 'SEO link'),
];
}
public function attributeHints() {
return [
'slug' => Yii::t('page', 'SEO link will be generated automatically if not specified'),
];
}
}

1
common/modules/blog/messages/ru/blog.php

@ -77,4 +77,5 @@ return [
'Are you sure you want to restore this history item?' => 'Вы уверены, что хотите восстановить эту запись?',
'Are you sure you want to remove this history?' => 'Вы уверены, что хотите удалить всю историю?',
'View' => 'Просмотр',
'SEO link will be generated automatically if not specified' => 'ЧПУ ссылка будет сгенерирована автоматически, если не указано',
];

16
common/modules/blog/views/manage/category/_form.php

@ -7,6 +7,22 @@ use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model \common\modules\blog\forms\BlogCategoryForm */
/* @var $form yii\widgets\ActiveForm */
$js2 = '
$(".hint-block").each(function () {
var $hint = $(this);
var label = $hint.parent().find("label");
label.html(label.html() + \' <i style="color:#3c8dbc" class="fa fa-question-circle" aria-hidden="true"></i>\');
label.addClass("help").popover({
html: true,
trigger: "hover",
placement: "bottom",
content: $hint.html()
});
$(this).hide();
});
';
$this->registerJs($js2);
?>
<div class="category-form">

16
common/modules/blog/views/manage/tag/_form.php

@ -6,6 +6,22 @@ use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model \common\modules\blog\forms\BlogTagSingleForm */
/* @var $form yii\widgets\ActiveForm */
$js2 = '
$(".hint-block").each(function () {
var $hint = $(this);
var label = $hint.parent().find("label");
label.html(label.html() + \' <i style="color:#3c8dbc" class="fa fa-question-circle" aria-hidden="true"></i>\');
label.addClass("help").popover({
html: true,
trigger: "hover",
placement: "bottom",
content: $hint.html()
});
$(this).hide();
});
';
$this->registerJs($js2);
?>
<div class="tag-form">

8
common/modules/pages/entities/Page.php

@ -6,10 +6,12 @@ use common\modules\pages\entities\queries\PageQuery;
use paulzi\nestedsets\NestedSetsBehavior;
use core\behaviors\MetaBehavior;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\SluggableBehavior;
use common\behaviors\WidgetContentBehavior;
use yii\db\ActiveRecord;
use core\entities\Meta;
use Yii;
use yii\helpers\Inflector;
/**
* @property int $id
@ -83,6 +85,12 @@ class Page extends ActiveRecord
],
TimestampBehavior::class,
WidgetContentBehavior::class,
[
'class' => SluggableBehavior::class,
'attribute' => 'title',
'ensureUnique' => true,
'preserveNonEmptyValues' => true,
],
];
}

11
common/modules/pages/forms/PageForm.php

@ -42,12 +42,11 @@ class PageForm extends CompositeForm
public function rules(): array
{
return [
[['title', 'slug'], 'required'],
[['title'], 'required'],
[['parentId'], 'integer'],
[['title', 'slug'], 'string', 'max' => 255],
[['content'], 'string'],
['slug', SlugValidator::class],
//[['slug'], 'unique', 'targetClass' => Page::class, 'filter' => $this->_page ? ['<>', 'id', $this->_page->id] : null]
[['slug'], 'unique', 'targetClass' => Page::class, 'filter' => function (ActiveQuery $query) {
if ($this->type != Page::TYPE_PUBLIC) {
$query->andWhere($this->type . '=' . Page::TYPE_PUBLIC);
@ -72,7 +71,13 @@ class PageForm extends CompositeForm
];
}
public function parentsList(): array
public function attributeHints() {
return [
'slug' => Yii::t('page', 'SEO link will be generated automatically if not specified'),
];
}
public function parentsList(): array
{
return ArrayHelper::map(Page::find()->andWhere(['tree' => 1])->orderBy('lft')->asArray()->all(), 'id', function (array $page) {
return ($page['depth'] > 1 ? str_repeat('-- ', $page['depth'] - 1) . ' ' : '') . $page['title'];

1
common/modules/pages/messages/ru/page.php

@ -21,4 +21,5 @@ return [
'View' => 'Просмотр',
'Publish' => 'Публикация',
'Preview on site' => 'Просмотр на сайте',
'SEO link will be generated automatically if not specified' => 'ЧПУ ссылка будет сгенерирована автоматически, если не указано',
];

4
common/modules/pages/services/PageManageService.php

@ -42,7 +42,7 @@ class PageManageService
return $page;
}
public function edit($id, PageForm $form): void
public function edit($id, PageForm $form, $type = Page::TYPE_PUBLIC): void
{
$page = $this->pages->get($id);
@ -53,7 +53,7 @@ class PageManageService
$form->title,
$form->slug,
$form->content,
$form->type,
$type,
new Meta(
$form->meta->title,
$form->meta->description,

16
common/modules/pages/views/manage/page/_form.php

@ -7,6 +7,22 @@ use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model \common\modules\pages\forms\PageForm */
/* @var $form yii\widgets\ActiveForm */
$js2 = '
$(".hint-block").each(function () {
var $hint = $(this);
var label = $hint.parent().find("label");
label.html(label.html() + \' <i style="color:#3c8dbc" class="fa fa-question-circle" aria-hidden="true"></i>\');
label.addClass("help").popover({
html: true,
trigger: "hover",
placement: "bottom",
content: $hint.html()
});
$(this).hide();
});
';
$this->registerJs($js2);
?>
<div class="page-form">

Loading…
Cancel
Save