Exemplo n.º 1
0
 /**
  * Advanced search.
  * @return ActiveDataProvider
  */
 public function searchAdvanced()
 {
     if ($this->type == 'topics') {
         $query = Thread::find();
         if (Yii::$app->user->isGuest) {
             $query->joinWith(['category' => function ($q) {
                 $q->andWhere([Category::tableName() . '.visible' => 1]);
             }, 'forum' => function ($q) {
                 $q->andWhere([Forum::tableName() . '.visible' => 1]);
             }]);
         }
         if (!empty($this->query)) {
             $words = explode(' ', preg_replace('/\\s+/', ' ', $this->query));
             foreach ($words as $word) {
                 if ($this->match == 'all') {
                     $query->andWhere(['like', 'name', $word]);
                 } else {
                     $query->orWhere(['like', 'name', $word]);
                 }
             }
         }
         if (!empty($this->author)) {
             $query->andWhere(['like', 'username', $this->author])->joinWith(['author']);
         }
         if (!empty($this->date_from) && empty($this->date_to)) {
             $query->andWhere(['>=', Thread::tableName() . '.created_at', $this->date_from]);
         } elseif (!empty($this->date_to) && empty($this->date_from)) {
             $this->date_to += 23 * 3600 + 59 * 60 + 59;
             // 23:59:59
             $query->andWhere(['<=', Thread::tableName() . '.created_at', $this->date_to]);
         } elseif (!empty($this->date_to) && !empty($this->date_from)) {
             if ($this->date_from > $this->date_to) {
                 $tmp = $this->date_to;
                 $this->date_to = $this->date_from;
                 $this->date_from = $tmp;
             }
             $this->date_to += 23 * 3600 + 59 * 60 + 59;
             // 23:59:59
             $query->andWhere(['<=', Thread::tableName() . '.created_at', $this->date_to]);
             $query->andWhere(['>=', Thread::tableName() . '.created_at', $this->date_from]);
         }
         if (!empty($this->forums)) {
             if (is_array($this->forums)) {
                 $forums = [];
                 foreach ($this->forums as $f) {
                     if (is_numeric($f)) {
                         $forums[] = (int) $f;
                     }
                 }
                 if (!empty($forums)) {
                     $query->andWhere(['forum_id' => $forums]);
                 }
             }
         }
         $sort = ['defaultOrder' => [Thread::tableName() . '.id' => SORT_DESC], 'attributes' => [Thread::tableName() . '.id' => ['asc' => [Thread::tableName() . '.id' => SORT_ASC], 'desc' => [Thread::tableName() . '.id' => SORT_DESC], 'default' => SORT_DESC]]];
     } else {
         $query = Vocabulary::find()->select('post_id, thread_id');
         if (Yii::$app->user->isGuest) {
             $query->joinWith(['posts' => function ($q) {
                 $q->joinWith(['forum' => function ($qu) {
                     $qu->andWhere([Forum::tableName() . '.visible' => 1])->joinWith(['category' => function ($que) {
                         $que->andWhere([Category::tableName() . '.visible' => 1]);
                     }]);
                 }]);
             }]);
         } else {
             $query->joinWith(['posts']);
         }
         if (!empty($this->query)) {
             $words = explode(' ', preg_replace('/\\s+/', ' ', $this->query));
             $countWords = 0;
             foreach ($words as $word) {
                 $query->orWhere(['like', 'word', $word]);
                 $countWords++;
             }
             $query->groupBy('post_id');
             if ($this->match == 'all' && $countWords > 1) {
                 $query->select(['post_id', 'thread_id', 'COUNT(post_id) AS c'])->having(['>', 'c', $countWords - 1]);
             }
         }
         if (!empty($this->author)) {
             $query->andWhere(['like', 'username', $this->author])->joinWith(['posts' => function ($q) {
                 $q->joinWith(['user']);
             }]);
         }
         if (!empty($this->date_from) && empty($this->date_to)) {
             $query->andWhere(['>=', Post::tableName() . '.updated_at', $this->date_from]);
         } elseif (!empty($this->date_to) && empty($this->date_from)) {
             $this->date_to += 23 * 3600 + 59 * 60 + 59;
             // 23:59:59
             $query->andWhere(['<=', Post::tableName() . '.updated_at', $this->date_to]);
         } elseif (!empty($this->date_to) && !empty($this->date_from)) {
             if ($this->date_from > $this->date_to) {
                 $tmp = $this->date_to;
                 $this->date_to = $this->date_from;
                 $this->date_from = $tmp;
             }
             $this->date_to += 23 * 3600 + 59 * 60 + 59;
             // 23:59:59
             $query->andWhere(['<=', Post::tableName() . '.updated_at', $this->date_to]);
             $query->andWhere(['>=', Post::tableName() . '.updated_at', $this->date_from]);
         }
         if (!empty($this->forums)) {
             if (is_array($this->forums)) {
                 $forums = [];
                 foreach ($this->forums as $f) {
                     if (is_numeric($f)) {
                         $forums[] = (int) $f;
                     }
                 }
                 if (!empty($forums)) {
                     $query->andWhere(['forum_id' => $forums]);
                 }
             }
         }
         $sort = ['defaultOrder' => ['post_id' => SORT_DESC], 'attributes' => ['post_id' => ['asc' => ['post_id' => SORT_ASC], 'desc' => ['post_id' => SORT_DESC], 'default' => SORT_DESC]]];
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => $sort]);
     return $dataProvider;
 }
Exemplo n.º 2
0
 /**
  * Updates tag words.
  * @throws Exception
  */
 protected function _updateWords()
 {
     try {
         $vocabulary = [];
         $allWords = $this->_prepareWords();
         $this->_addNewWords($allWords);
         $query = (new Query())->from(Vocabulary::tableName())->where(['word' => $allWords]);
         foreach ($query->each() as $vocabularyNew) {
             $vocabulary[$vocabularyNew['id']] = [$vocabularyNew['id'], $this->id];
         }
         if (!empty($vocabulary)) {
             Yii::$app->db->createCommand()->batchInsert('{{%podium_vocabulary_junction}}', ['word_id', 'post_id'], array_values($vocabulary))->execute();
         }
         $query = (new Query())->from('{{%podium_vocabulary_junction}}')->where(['post_id' => $this->id]);
         foreach ($query->each() as $junk) {
             if (!array_key_exists($junk['word_id'], $vocabulary)) {
                 Yii::$app->db->createCommand()->delete('{{%podium_vocabulary_junction}}', ['id' => $junk['id']])->execute();
             }
         }
     } catch (Exception $e) {
         Log::error($e->getMessage(), null, __METHOD__);
         throw $e;
     }
 }
 /**
  * Searching through the forum.
  * @return string
  */
 public function actionSearch()
 {
     $dataProvider = null;
     $searchModel = new Vocabulary();
     if ($searchModel->load(Yii::$app->request->get(), '')) {
         $dataProvider = $searchModel->search();
     } else {
         $model = new SearchForm();
         $model->match = 'all';
         $model->type = 'posts';
         $model->display = 'topics';
         $categories = Category::find()->orderBy(['name' => SORT_ASC])->all();
         $forums = Forum::find()->orderBy(['name' => SORT_ASC])->all();
         $list = [];
         foreach ($categories as $cat) {
             $catlist = [];
             foreach ($forums as $for) {
                 if ($for->category_id == $cat->id) {
                     $catlist[$for->id] = '|-- ' . Html::encode($for->name);
                 }
             }
             $list[Html::encode($cat->name)] = $catlist;
         }
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             if (empty($model->query) && empty($model->author)) {
                 $this->error(Yii::t('podium/flash', "You have to enter words or author's name first."));
             } else {
                 $stop = false;
                 if (!empty($model->query)) {
                     $words = explode(' ', preg_replace('/\\s+/', ' ', $model->query));
                     $checkedWords = [];
                     foreach ($words as $word) {
                         if (mb_strlen($word, 'UTF-8') > 2) {
                             $checkedWords[] = $word;
                         }
                     }
                     $model->query = implode(' ', $checkedWords);
                     if (mb_strlen($model->query, 'UTF-8') < 3) {
                         $this->error(Yii::t('podium/flash', 'You have to enter word at least 3 characters long.'));
                         $stop = true;
                     }
                 }
                 if (!$stop) {
                     $dataProvider = $model->searchAdvanced();
                 }
             }
         }
         return $this->render('search', ['model' => $model, 'list' => $list, 'dataProvider' => $dataProvider, 'query' => $model->query, 'author' => $model->author]);
     }
     return $this->render('search', ['dataProvider' => $dataProvider, 'query' => $searchModel->query]);
 }