Blog Module: add tags

parent 4592a94e
......@@ -22,6 +22,7 @@ class AppAsset extends AssetBundle
'plugins/bootstrap-datetimepicker/css/datetimepicker.css',
'plugins/switchery/switchery.min.css',
'plugins/powerange/powerange.min.css',
'plugins/jquery-tag-it/css/jquery.tagit.css',
'css/site.css',
];
public $js = [
......@@ -29,6 +30,7 @@ class AppAsset extends AssetBundle
'plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js',
'plugins/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.ru.js',
'plugins/tinymce/js/tinymce/tinymce.min.js',
'plugins/jquery-tag-it/js/tag-it.min.js',
'js/form-load.js'
];
public $depends = [
......@@ -36,6 +38,6 @@ class AppAsset extends AssetBundle
'yii\jui\JuiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
'backend\assets\HeadAsset'
'backend\assets\HeadAsset',
];
}
......@@ -66,6 +66,10 @@ return [
'assetManager'=>[
'linkAssets' => true,
'bundles' => [
'xj\tagit\TagitAsset' => [
'js' => [],
'css' => []
],
'yii\bootstrap\BootstrapPluginAsset' => [
'js' => [
],
......
<!-- ================== BEGIN BASE JS ================== -->
<?php $this->registerJsFile('/plugins/jquery/jquery-1.9.1.min.js', ['position' => \yii\web\View::POS_HEAD ]);?>
<?php $this->registerJsFile('/plugins/jquery/jquery-migrate-1.1.0.min.js', ['position' => \yii\web\View::POS_END ]);?>
<?php $this->registerJsFile('/plugins/jquery-ui/ui/minified/jquery-ui.min.js', ['position' => \yii\web\View::POS_END ]);?>
<?php $this->registerJsFile('/plugins/bootstrap/js/bootstrap.min.js', ['position' => \yii\web\View::POS_END ]);?>
<!--[if lt IE 9]>
......@@ -28,7 +26,6 @@
<?php $this->registerJsFile('/plugins/bootstrap-select/bootstrap-select.js', ['position' => \yii\web\View::POS_END ]);?>
<?php //$this->registerJsFile('/js/dashboard.min.js', ['position' => \yii\web\View::POS_END ]);?>
<?php $this->registerJsFile('/js/dashboard.js', ['position' => \yii\web\View::POS_END ]);?>
<?php $this->registerJsFile('/js/apps.min.js', ['position' => \yii\web\View::POS_END ]);?>
<!-- ================== END PAGE LEVEL JS ================== -->
......
<?php
namespace common\modules\blog\components;
use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use common\modules\blog\models\PostTag;
use common\modules\blog\models\PostTagAssign;
class TagBehavior extends Behavior
{
public $tags;
public function events()
{
return [
ActiveRecord::EVENT_AFTER_FIND => 'eventFind',
ActiveRecord::EVENT_AFTER_UPDATE => 'Save',
ActiveRecord::EVENT_AFTER_INSERT => 'Insert',
ActiveRecord::EVENT_BEFORE_DELETE => 'Delete',
];
}
private function clearPostTags()
{
if($this->owner->postTagAssigns)
{
foreach ($this->owner->postTagAssigns as $tag)
{
$tag->delete();
}
}
}
private function insertPostTags()
{
if($this->tags)
{
foreach ($this->tags as $tag)
{
$tg = PostTag::find()->where(['name' => $tag])->one();
if(!$tg)
{
$tg = new PostTag;
$tg->name = $tag;
$tg->save();
}
$tgs = new PostTagAssign;
$tgs->post_id = $this->owner->id;
$tgs->tag_id = $tg->id;
$tgs->save();
}
}
}
public function eventFind($event)
{
$this->tags = array_keys(ArrayHelper::map($this->owner->postTags, 'name', 'id'));
}
public function Save($event)
{
$this->clearPostTags();
$this->insertPostTags();
return true;
}
public function Insert($event)
{
$this->insertPostTags();
return true;
}
public function Delete($event)
{
$this->clearPostTags();
return true;
}
}
......@@ -6,10 +6,13 @@ use Yii;
use common\components\AdminController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;
use yii\helpers\ArrayHelper;
use common\modules\languages\models\Languages;
use common\modules\blog\models\Post;
use common\modules\blog\models\PostLang;
use common\modules\blog\models\PostTag;
use common\modules\blog\models\SearchPost;
/**
......@@ -24,6 +27,7 @@ class PostAdminController extends AdminController
'Update' => 'Редактирование записи',
'Delete' => 'Удаление записи',
'View' => 'Просмотр записи',
'Autocomplete' => 'Автокомплит для поля тегов'
];
}
......@@ -155,6 +159,13 @@ class PostAdminController extends AdminController
return $this->redirect(['index']);
}
public function actionAutocomplete($term)
{
Yii::$app->response->format = Response::FORMAT_JSON;
return array_keys(ArrayHelper::map(PostTag::find()->where(['like', 'name', $term])->all(), 'name', 'id'));
}
/**
* Finds the Post model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
......
......@@ -6,7 +6,7 @@ use Yii;
use common\modules\languages\models\Languages;
use common\modules\blog\models\PostLang;
use common\modules\blog\models\PostTags;
use common\modules\blog\models\PostTag;
use common\models\MetaTags;
/**
......@@ -55,7 +55,7 @@ class Post extends \common\components\ActiveRecordModel
[['active', 'created_at', 'updated_at'], 'integer'],
[['url'], 'string', 'max' => 255],
[['url'], 'unique'],
[['preview', 'unlinkFile'], 'safe'],
[['preview', 'unlinkFile', 'tags'], 'safe'],
[['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif'],
];
}
......@@ -76,6 +76,9 @@ class Post extends \common\components\ActiveRecordModel
'langClass' => 'common\modules\blog\models\PostLang',
'actions' => ['create', 'update']
],
'tags' => [
'class' => 'common\modules\blog\components\TagBehavior',
],
'file' => [
'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior',
'path' => '@frontend/web',
......@@ -95,6 +98,7 @@ class Post extends \common\components\ActiveRecordModel
'url' => 'Ссылка',
'active' => 'Видимость',
'file' => 'Изображение',
'tags' => 'Теги',
'preview' => 'Изображение',
'unlinkFile' => 'Удалить изображение',
'created_at' => 'Дата добавления',
......@@ -151,8 +155,16 @@ class Post extends \common\components\ActiveRecordModel
/**
* @return \yii\db\ActiveQuery
*/
public function getTags()
public function getPostTags()
{
return $this->hasMany(PostTag::className(), ['id' => 'tag_id'])->viaTable('posts_tags_assign', ['post_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPostTagAssigns()
{
return $this->hasMany(PostTags::className(), ['id' => 'tag_id'])->viaTable('posts_tags_assign', ['post_id' => 'id']);
return $this->hasMany(PostTagAssign::className(), ['post_id' => 'id']);
}
}
......@@ -5,7 +5,7 @@ namespace common\modules\blog\models;
use Yii;
use common\modules\blog\models\Post;
use common\modules\blog\models\PostTags;
use common\modules\blog\models\PostTag;
/**
* This is the model class for table "posts_tags_assign".
......@@ -44,7 +44,7 @@ class PostTagAssign extends \common\components\ActiveRecordModel
[['tag_id', 'post_id', 'created_at', 'updated_at'], 'integer'],
[['tag_id', 'post_id'], 'unique', 'targetAttribute' => ['tag_id', 'post_id'], 'message' => 'The combination of Tag ID and Post ID has already been taken.'],
[['post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Post::className(), 'targetAttribute' => ['post_id' => 'id']],
[['tag_id'], 'exist', 'skipOnError' => true, 'targetClass' => PostTags::className(), 'targetAttribute' => ['tag_id' => 'id']],
[['tag_id'], 'exist', 'skipOnError' => true, 'targetClass' => PostTag::className(), 'targetAttribute' => ['tag_id' => 'id']],
];
}
......
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use \xj\tagit\Tagit;
use common\modules\blog\models\Post;
use common\modules\content\widgets\MetaTagsWidget;
......@@ -39,6 +41,16 @@ use common\modules\content\widgets\MetaTagsWidget;
<?= $form->field($model, 'unlinkFile')->checkbox(); ?>
<?= $form->field($model, 'tags')->widget(Tagit::className(), [
'clientOptions' => [
'tagSource' => Url::to(['autocomplete']),
'autocomplete' => [
'delay' => 500,
'minLength' => 1,
],
]
]); ?>
<ul class="nav nav-pills">
<?php $c = 0; foreach ($model->getLangsHelper() as $i => $content) : $c++; ?>
<li class="<?=($c==1?'active':'')?>"><a href="#lang-<?=$content->lang->url?>" data-toggle="tab"><?=$content->lang->name?></a></li>
......@@ -70,3 +82,11 @@ use common\modules\content\widgets\MetaTagsWidget;
<?php ActiveForm::end(); ?>
</div>
<script type="text/javascript">
$(function(){
$('#jquery-tagIt-tags').tagit({
availableTags: ["c++", "java", "php", "javascript", "ruby", "python", "c"]
});
});
</script>
\ No newline at end of file
......@@ -34,7 +34,8 @@
"himiklab/yii2-sitemap-module" : "*",
"mirocow/yii2-minify-view" : "*",
"kartik-v/yii2-widget-fileinput": "@dev",
"nodge/yii2-eauth": "~2.0"
"nodge/yii2-eauth": "~2.0",
"xj/yii2-tagit-widget": "*"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",
......
<?php
return [
'Update' => 'Редактировать',
'Delete' => 'Удалить',
'Request a call' => 'Заказать звонок',
];
\ No newline at end of file
<?php
return [
'Update' => 'Редактировать',
'Delete' => '.удалить',
];
\ No newline at end of file
<?php
return [
'Surname' => 'Фамилия',
'Name' => 'Имя',
'Patronymic' => 'Отчество',
'Name Add' => 'Фамилия, имя, отчество родственника',
'Status Add' => 'Степень родства',
'Status Fail' => 'Отклонена',
'Gender' => 'Ваш пол',
'Day' => 'День',
'Month' => 'Месяц',
'Year' => 'Год',
'Ser_doc' => 'Серия паспорта',
'Nom_doc' => 'Номер паспорта',
'Vyd_doc' => 'Кем выдан паспорт',
'Request Sum' => 'Сумма',
'Request Day' => 'Кол-во дней',
'Status' => 'Статус',
'Created At' => 'Создана',
];
\ No newline at end of file
......@@ -4,7 +4,7 @@
<div class="col-md-4 col-xs-4 col-sm-12">
<a href="mailto:info@task-on.com" class="foot_mail">info@task-on.com</a>
</div>
<div class="col-md-4 col-xs-4 col-sm-12"><a href="#zvonok_form" class="zvonok_bt popup-form"><span>Заказать звонок</span></a></div>
<div class="col-md-4 col-xs-4 col-sm-12"><a href="#zvonok_form" class="zvonok_bt popup-form"><span><?=Yii::t('app', 'Request a call');?></span></a></div>
<div class="col-md-4 col-xs-4 col-sm-12">
<!-- <div class="phone_hover_foot">Стоимость звонка 0 руб,<br/> в том числе с мобильного</div> -->
<span class="foot_phone"><?=\common\models\Settings::getValue('content-phone');?></span>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment