/** * Gets number of active threads added by user of given ID. * @param integer $id * @return integer */ public static function findThreadsCount($id) { $cache = Cache::getInstance()->getElement('user.threadscount', $id); if ($cache === false) { $cache = (new Query())->from(Thread::tableName())->where(['author_id' => $id])->count(); Cache::getInstance()->setElement('user.threadscount', $id, $cache); } return $cache; }
/** * 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; }
/** * Gets number of active threads added by user. * @return integer */ public function getThreadsCount($id = null) { $cache = Cache::getInstance()->getElement('user.threadscount', empty($id) ? $this->getId() : $id); if ($cache === false) { $cache = (new Query())->from(Thread::tableName())->where(['author_id' => empty($id) ? $this->getId() : $id])->count(); Cache::getInstance()->setElement('user.threadscount', empty($id) ? $this->getId() : $id, $cache); } return $cache; }