Для категорий сделать страницу “Справочник Категорий”

parent 9dca5a70
......@@ -43,6 +43,7 @@ class Module extends \common\components\WebModule
{
return array(
'Записи' => '/blog/post-admin/manage',
'Теги' => '/blog/tag-admin/manage',
);
}
}
......@@ -43,9 +43,11 @@ class TagBehavior extends Behavior
$tg = PostTag::find()->where(['name' => $tag])->one();
if(!$tg)
{
$tg = new PostTag;
$tg->name = $tag;
$tg->save();
// Убрана возможность создавать новые теги
return false;
// $tg = new PostTag;
// $tg->name = $tag;
// $tg->save();
}
$tgs = new PostTagAssign;
......
<?php
namespace common\modules\blog\controllers;
use Yii;
use common\modules\blog\models\PostTag;
use common\modules\blog\models\SearchPostTag;
use common\components\AdminController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* TagAdminController implements the CRUD actions for PostTag model.
*/
class TagAdminController extends AdminController
{
public static function actionsTitles(){
return [
'Manage' => 'Управление тегами',
'Create' => 'Добавление тега',
'Update' => 'Редактирование тега',
'Delete' => 'Удаление тега',
'View' => 'Просмотр тега',
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all PostTag models.
* @return mixed
*/
public function actionManage()
{
$searchModel = new SearchPostTag();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('manage', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single PostTag model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new PostTag model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new PostTag();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing PostTag model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing PostTag model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['manage']);
}
/**
* Finds the PostTag model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return PostTag the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = PostTag::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
......@@ -91,6 +91,26 @@ class PostTag extends \common\components\ActiveRecordModel
return $this->hasMany(PostTagAssign::className(), ['tag_id' => 'id']);
}
public function beforeDelete()
{
if (parent::beforeDelete())
{
if($this->assigns)
{
foreach ($this->assigns as $assign)
{
$assign->delete();
}
}
return true;
}
else
{
return false;
}
}
public function getUrl()
{
return Url::to(['/blog/tag/' . $this->name]);
......
<?php
namespace common\modules\blog\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\modules\blog\models\PostTag;
/**
* SearchPostTag represents the model behind the search form about `common\modules\blog\models\PostTag`.
*/
class SearchPostTag extends PostTag
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'created_at', 'updated_at'], 'integer'],
[['name'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = PostTag::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name]);
return $dataProvider;
}
}
......@@ -50,7 +50,7 @@ use common\modules\content\widgets\MetaTagsWidget;
],
'triggerKeys' => ['enter', 'space', 'tab'],
]
]); ?>
])->hint('Пожалуйста используйте только следующие тэги: “Реклама”, “Аналитика”, “Секреты бизнеса”,”Для души”, “Гаджеты”, “Разработка”, "АртПроект"'); ?>
<ul class="nav nav-pills">
<?php $c = 0; foreach ($model->getLangsHelper() as $i => $content) : $c++; ?>
......
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\SearchPost */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="post-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'url') ?>
<?= $form->field($model, 'active') ?>
<?= $form->field($model, 'created_at') ?>
<?= $form->field($model, 'updated_at') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\PostTag */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="post-tag-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\PostTag */
$this->title = 'Create Post Tag';
$this->params['breadcrumbs'][] = ['label' => 'Post Tags', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-tag-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel common\modules\blog\models\SearchPostTag */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Post Tags';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-tag-index">
<p>
<?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
'name',
[
'class' => 'common\components\ColorActionColumn',
'template' => '{update} {delete}',
],
],
]); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\PostTag */
$this->title = 'Update Post Tag: ' . ' ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Post Tags', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="post-tag-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\PostTag */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Post Tags', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-tag-view">
<p>
<?= Html::a('Редактировать', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Удалить', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'name',
],
]) ?>
</div>
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