/** * Создает DataProvider на основе переданных данных * @param $params - параметры * @return ActiveDataProvider */ public function search($params) { $this->scenario = 'search'; // Устанавливаем сценарий поиска $query = Document::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $this::COUNT], 'sort' => array('defaultOrder' => ['created_at' => SORT_DESC])]); $this->load($params); // Если валидация не пройдена, то ничего не выводить if (!$this->validate()) { $query->where('0=1'); return $dataProvider; } // Фильтрация $query->andFilterWhere(['id' => $this->id, 'position' => $this->position, 'status' => $this->status, 'is_folder' => $this->is_folder, 'parent_id' => $this->parent_id, 'template_id' => $this->template_id, 'updated_at' => $this->updated_at, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by]); if ($this->created_at) { $date = new \DateTime($this->created_at); $this->created_at = $date->format('Y-m-d'); } $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'alias', $this->alias])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'meta_keywords', $this->meta_keywords])->andFilterWhere(['like', 'meta_description', $this->meta_description])->andFilterWhere(['like', 'annotation', $this->annotation])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'created_at', $this->created_at]); if ($this->id_from) { $query->andFilterWhere(['>=', 'id', $this->id_from]); } if ($this->id_till) { $query->andFilterWhere(['<=', 'id', $this->id_till]); } if ($this->position_from) { $query->andFilterWhere(['>=', 'position', $this->position_from]); } if ($this->position_till) { $query->andFilterWhere(['<=', 'position', $this->position_till]); } if ($this->created_at_from) { $date_from = new \DateTime($this->created_at_from); $query->andFilterWhere(['>=', 'created_at', $date_from->format('Y-m-d')]); } if ($this->created_at_till) { $date_till = new \DateTime($this->created_at_till); $query->andFilterWhere(['<=', 'created_at', $date_till->format('Y-m-d')]); } if ($this->updated_at_from) { $date_from = new \DateTime($this->updated_at_from); $query->andFilterWhere(['>=', 'updated_at', $date_from->format('Y-m-d')]); } if ($this->updated_at_till) { $date_till = new \DateTime($this->updated_at_till); $query->andFilterWhere(['<=', 'updated_at', $date_till->format('Y-m-d')]); } return $dataProvider; }
<?php /** * @package yii2-document * @author Yuri Shekhovtsov <*****@*****.**> * @copyright Copyright © Yuri Shekhovtsov, lowbase.ru, 2015 - 2016 * @version 1.0.0 */ use lowbase\document\components\TreeWidget; use lowbase\document\models\Document; use yii\bootstrap\Modal; /* @var $this yii\web\View */ /* @var $model app\modules\document\models\DocumentSearch */ /* @var $form yii\widgets\ActiveForm */ Modal::begin(['header' => '<h1 class="text-center">' . Yii::t('document', 'Дерево документов') . '</h1>', 'toggleButton' => false, 'id' => 'tree', 'options' => ['tabindex' => false]]); ?> <div class="lb-document-module-tree"> <?php echo TreeWidget::widget(['data' => Document::find()->orderBy(['position' => SORT_ASC])->all()]); ?> </div> <?php Modal::end();
/** * Получить список документов массивом * @param null $parent_id - родительский документ * @return array [ID => Название] */ public static function getAll($parent_id = null) { $documents = []; if ($parent_id) { $model = Document::find()->where(['parent_id' => $parent_id])->all(); } else { $model = Document::find()->all(); } if ($model) { foreach ($model as $m) { $documents[$m->id] = $m->name; } } return $documents; }
/** * Публичное отображение документа * @param $alias - Url-адрес документа * @return string * @throws NotFoundHttpException */ public function actionShow($alias) { // Отображаем только опубликованные документы $model = Document::find()->where(['alias' => $alias, 'status' => Document::STATUS_ACTIVE])->one(); if ($model == null) { throw new NotFoundHttpException(Yii::t('document', 'Запрашиваемая страница не найдена.')); } Visit::check($model->id); // Фиксируем просмотр $views = Visit::getAll($model->id); // Считаем просмотры $likes = Like::getAll($model->id); // Считаем лайки // Если задан шаблон отображения, то отображаем согласно нему, иначе стандартное отображение статьи $template = isset($model->template) && $model->template->path ? $model->template->path : '@vendor/lowbase/yii2-document/views/document/template/default'; return $this->render($template, ['model' => $model, 'views' => $views ? $views[0]->count : 0, 'likes' => $likes ? $likes[0]->count : 0]); }