/** * 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; }
/** * Returns latest visible posts for guests. * @param integer $limit Number of latest posts. * @return Post[] */ public static function getLatestPostsForGuests($limit = 5) { return static::find()->joinWith(['forum' => function ($query) { $query->andWhere([Forum::tableName() . '.visible' => 1])->joinWith(['category' => function ($query) { $query->andWhere([Category::tableName() . '.visible' => 1]); }]); }])->orderBy(['created_at' => SORT_DESC])->limit($limit)->all(); }
/** * Returns the verified post. * @param integer $category_id post's category ID * @param integer $forum_id post's forum ID * @param integer $thread_id post's thread ID * @param integer $id post's ID * @return Post * @since 0.2 */ public static function verify($category_id = null, $forum_id = null, $thread_id = null, $id = null) { if (!is_numeric($category_id) || $category_id < 1 || !is_numeric($forum_id) || $forum_id < 1 || !is_numeric($thread_id) || $thread_id < 1 || !is_numeric($id) || $id < 1) { return null; } return static::find()->joinWith(['thread', 'forum' => function ($query) use($category_id) { $query->joinWith(['category'])->andWhere([Category::tableName() . '.id' => $category_id]); }])->where([static::tableName() . '.id' => $id, static::tableName() . '.thread_id' => $thread_id, static::tableName() . '.forum_id' => $forum_id])->limit(1)->one(); }
/** * Updating the categories order. * @return string|\yii\web\Response */ public function actionSortCategory() { if (Yii::$app->request->isAjax) { if (User::can(Rbac::PERM_UPDATE_CATEGORY)) { $modelId = Yii::$app->request->post('id'); $new = Yii::$app->request->post('new'); if (is_numeric($modelId) && is_numeric($new) && $modelId > 0 && $new >= 0) { $moved = Category::findOne((int) $modelId); if ($moved) { $query = (new Query())->from(Category::tableName())->where('id != :id')->params([':id' => $moved->id])->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])->indexBy('id'); $next = 0; $newSort = -1; try { foreach ($query->each() as $id => $forum) { if ($next == (int) $new) { $newSort = $next; $next++; } Yii::$app->db->createCommand()->update(Category::tableName(), ['sort' => $next], 'id = :id', [':id' => $id])->execute(); $next++; } if ($newSort == -1) { $newSort = $next; } $moved->sort = $newSort; if (!$moved->save()) { return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', "Sorry! We can not save new categories' order."), ['class' => 'text-danger']); } else { Log::info('Categories orded updated', $moved->id, __METHOD__); return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', "New categories' order has been saved."), ['class' => 'text-success']); } } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', "Sorry! We can not save new categories' order."), ['class' => 'text-danger']); } } else { return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Sorry! We can not find Category with this ID.'), ['class' => 'text-danger']); } } else { return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Sorry! Sorting parameters are wrong.'), ['class' => 'text-danger']); } } else { return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You are not allowed to perform this action.'), ['class' => 'text-danger']); } } else { return $this->redirect(['admin/categories']); } }
/** * Sets new categories order. * @param integer $order new category sorting order number * @return boolean * @throws Exception * @since 0.2 */ public function newOrder($order) { try { $next = 0; $newSort = -1; $query = (new Query())->from(Category::tableName())->where('id != :id')->params([':id' => $this->id])->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])->indexBy('id'); foreach ($query->each() as $id => $forum) { if ($next == $order) { $newSort = $next; $next++; } Yii::$app->db->createCommand()->update(Category::tableName(), ['sort' => $next], 'id = :id', [':id' => $id])->execute(); $next++; } if ($newSort == -1) { $newSort = $next; } $this->sort = $newSort; if (!$this->save()) { throw new Exception('Categories order saving error'); } Log::info('Categories orded updated', $this->id, __METHOD__); return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Returns the verified thread. * @param integer $category_id thread's category ID * @param integer $forum_id thread's forum ID * @param integer $id thread's ID * @param string $slug thread's slug * @param boolean $guest whether caller is guest or registered user * @return Thread * @since 0.2 */ public static function verify($category_id = null, $forum_id = null, $id = null, $slug = null, $guest = true) { if (!is_numeric($category_id) || $category_id < 1 || !is_numeric($forum_id) || $forum_id < 1 || !is_numeric($id) || $id < 1 || empty($slug)) { return null; } return static::find()->joinWith(['forum' => function ($query) use($guest) { if ($guest) { $query->andWhere([Forum::tableName() . '.visible' => 1]); } $query->joinWith(['category' => function ($query) use($guest) { if ($guest) { $query->andWhere([Category::tableName() . '.visible' => 1]); } }]); }])->where([static::tableName() . '.id' => $id, static::tableName() . '.slug' => $slug, static::tableName() . '.forum_id' => $forum_id, static::tableName() . '.category_id' => $category_id])->limit(1)->one(); }
/** * Searches forums. * @param integer|null $category_id * @return ActiveDataProvider */ public function search($category_id = null, $onlyVisible = false) { $query = static::find(); if ($category_id) { $query->andWhere(['category_id' => $category_id]); } if ($onlyVisible) { $query->joinWith(['category' => function ($query) { $query->andWhere([Category::tableName() . '.visible' => 1]); }]); $query->andWhere([static::tableName() . '.visible' => 1]); } $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->defaultOrder = ['sort' => SORT_ASC, 'id' => SORT_ASC]; return $dataProvider; }