Пример #1
0
 public static function getTags($fields = [], $with = [], $pageSize = false, $page = false)
 {
     $model = Meta::find()->where(['type' => Meta::TYPE_TAG, 'status' => Meta::STATUS_ACTIVE]);
     $countModel = clone $model;
     $pagination = new Pagination(['totalCount' => $countModel->count('id'), 'pageSize' => $pageSize]);
     if ($page) {
         $pagination->setPage($page, true);
     }
     return ['data' => $model->select($fields)->with($with)->limit($pagination->getLimit())->offset($pagination->getOffset())->orderBy('created_at desc')->asArray()->all(), 'pagination' => $pagination];
 }
Пример #2
0
 public function search($params)
 {
     $query = Meta::find();
     $query->andFilterWhere(['type' => Meta::TYPE_ARTICLE_CATEGORIES]);
     $query->with('parent');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'content_total' => $this->content_total, 'created_at' => $this->created_at, 'parent_id' => $this->parent_id]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'description', $this->description]);
     return $dataProvider;
 }
Пример #3
0
 public function search($params)
 {
     $query = Meta::find();
     switch ($this->scenario) {
         case self::SCENARIO_TAG:
             $query->andFilterWhere(['type' => self::TYPE_TAG]);
             break;
         case self::SCENARIO_LINK_CATEGORIES:
             $query->andFilterWhere(['type' => self::TYPE_LINK_CATEGORIES]);
             break;
         case self::SCENARIO_ARTICLE_CATEGORIES:
         case self::SCENARIO_DEFAULT:
         default:
             $query->andFilterWhere(['type' => self::TYPE_ARTICLE_CATEGORIES]);
     }
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'parent_id' => $this->parent_id, 'content_total' => $this->content_total]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'description', $this->description]);
     return $dataProvider;
 }
Пример #4
0
<?php

use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kartik\datecontrol\DateControl;
/**
 * @var yii\web\View $this
 * @var common\models\Meta $model
 * @var yii\widgets\ActiveForm $form
 */
$parents = \yii\helpers\ArrayHelper::map(\common\models\Meta::find()->where(['status' => \common\models\Meta::STATUS_ACTIVE, 'type' => \common\models\Meta::TYPE_ARTICLE_CATEGORIES, 'parent_id' => 0])->asArray()->all(), 'id', 'name');
$parents = \yii\helpers\ArrayHelper::merge([0 => '顶级分类'], $parents);
?>

<div class="meta-form">

    <?php 
$form = ActiveForm::begin(['type' => ActiveForm::TYPE_HORIZONTAL]);
echo Form::widget(['model' => $model, 'form' => $form, 'columns' => 1, 'attributes' => ['name' => ['type' => Form::INPUT_TEXT, 'options' => ['maxlength' => 32, 'placeholder' => 'Enter Name...']], 'slug' => ['type' => Form::INPUT_TEXT, 'options' => ['maxlength' => 255, 'placeholder' => 'Enter Slug...']], 'parent_id' => ['type' => Form::INPUT_WIDGET, 'widgetClass' => \kartik\widgets\Select2::className(), 'options' => ['data' => $parents]], 'description' => ['type' => Form::INPUT_TEXTAREA, 'options' => ['rows' => 5, 'maxlength' => 255, 'placeholder' => 'Enter Description...']]]]);
?>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <?php 
echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
?>
        </div>
    </div>
    <?php 
ActiveForm::end();
?>
Пример #5
0
 /**
  * 保存或插入内容的标签
  * @param string|array $tags 标签
  * @return bool
  * @throws ErrorException
  * @throws \Exception
  */
 public function saveTags($tags)
 {
     if (is_string($tags)) {
         if (($tags = trim($tags)) === '') {
             $tags = [];
         } else {
             $tags = explode(',', $tags);
         }
     }
     //对标签去除空格并去重
     $tags = array_unique(array_map('trim', $tags));
     //如果标签为空并且是新增内容,则直接返回真
     if (empty($tags) && !$this->id) {
         return true;
     }
     $relationshipModel = new Relationship();
     $metaModel = new Meta(['scenario' => Meta::SCENARIO_TAG]);
     //取出Meta表已经有的同名标签
     $existTags = $metaModel->find()->where(['type' => Meta::TYPE_TAG])->andFilterWhere(['in', 'name', $tags])->select(['name'])->asArray()->column();
     //取出数据库没有的标签名称(要添加到Meta表的标签)
     $newTags = array_diff($tags, $existTags);
     if (!empty($newTags)) {
         //如果有新增的标签
         foreach ($newTags as $tag) {
             $model = clone $metaModel;
             $model->setAttributes(['name' => $tag]);
             if (!$model->insert()) {
                 throw new ErrorException(current($model->getFirstErrors()));
             }
         }
     }
     //如果标签不为空
     if (!empty($tags)) {
         //找出本内容的标签
         $contentTags = $metaModel->find()->where(['type' => Meta::TYPE_TAG, 'name' => $tags, 'status' => Meta::STATUS_ACTIVE])->select(['id', 'name'])->asArray()->all();
     } else {
         $contentTags = [];
     }
     //取出久的标签
     $oldTags = ArrayHelper::getColumn($this->tags, 'name');
     $updateCountersTagIds = [];
     if (!empty($contentTags)) {
         //准备插入的文章关联标签
         $relationshipInsertData = [];
         foreach ($contentTags as $tag) {
             //如果要加的标签之前就有则跳过
             if (in_array($tag['name'], $oldTags)) {
                 continue;
             }
             $relationshipInsertData[] = ['content_id' => $this->id, 'meta_id' => $tag['id']];
             $updateCountersTagIds[] = $tag['id'];
         }
         if (!empty($relationshipInsertData)) {
             if (!$relationshipModel->getDb()->createCommand()->batchInsert($relationshipModel->tableName(), ['content_id', 'meta_id'], $relationshipInsertData)->execute()) {
                 throw new \ErrorException('插入文章标签关联失败');
             }
         }
     }
     $deleteTagIds = [];
     foreach ($this->tags as $tag) {
         if (!in_array($tag->name, $tags)) {
             $deleteTagIds[] = $tag->id;
         }
     }
     //内容新增的标签的内容统计+1
     if (!empty($updateCountersTagIds)) {
         if (!$metaModel->updateAllCounters(['content_total' => 1], ['id' => $updateCountersTagIds])) {
             throw new ErrorException('更新标签内容统计失败');
         }
     }
     if (!empty($deleteTagIds)) {
         //内容之前的标签被删除则内容统计-1
         $metaModel->updateAllCounters(['content_total' => -1], ['id' => $deleteTagIds]);
         //然后删除内容里这些不要的标签
         $relationshipModel->deleteAll(['content_id' => $this->id, 'meta_id' => $deleteTagIds]);
     }
     return true;
 }
Пример #6
0
 public function getAll($fields = [], $with = [])
 {
     return self::$model->find()->select($fields)->with($with)->where(['type' => Meta::TYPE_ARTICLE_CATEGORIES, 'status' => Meta::STATUS_ACTIVE])->asArray()->all();
 }