Beispiel #1
0
 public function search()
 {
     $query = self::find()->select('post_id, thread_id')->where(['like', 'word', $this->query]);
     if (Yii::$app->user->isGuest) {
         $query->joinWith(['posts' => function ($q) {
             $q->joinWith(['forum'])->where([Forum::tableName() . '.visible' => 1]);
         }]);
     } else {
         $query->joinWith(['posts']);
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['thread_id' => SORT_DESC], 'attributes' => ['thread_id' => ['asc' => ['thread_id' => SORT_ASC], 'desc' => ['thread_id' => SORT_DESC], 'default' => SORT_DESC]]]]);
     return $dataProvider;
 }
Beispiel #2
0
 /**
  * Searches for posts added by given user.
  * @param integer $user_id
  * @return ActiveDataProvider
  */
 public function searchByUser($user_id)
 {
     $query = self::find();
     $query->where(['author_id' => $user_id]);
     if (Yii::$app->user->isGuest) {
         $query->joinWith(['forum' => function ($q) {
             $q->where([Forum::tableName() . '.visible' => 1]);
         }]);
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => 10, 'pageSizeLimit' => false, 'forcePageParam' => false]]);
     $dataProvider->sort->defaultOrder = ['id' => SORT_ASC];
     return $dataProvider;
 }
Beispiel #3
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;
 }
 /**
  * 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]);
 }
Beispiel #5
0
 /**
  * Updates moderator assignment for given forums.
  * @param array $newForums new assigned forums' IDs
  * @param array $oldForums old assigned forums' IDs
  * @return boolean
  * @since 0.2
  */
 public function updateModeratorForMany($newForums = [], $oldForums = [])
 {
     try {
         $add = [];
         foreach ($newForums as $forum) {
             if (!in_array($forum, $oldForums)) {
                 if ((new Query())->from(Forum::tableName())->where(['id' => $forum])->exists() && (new Query())->from(Mod::tableName())->where(['forum_id' => $forum, 'user_id' => $this->id])->exists() === false) {
                     $add[] = [$forum, $this->id];
                 }
             }
         }
         $remove = [];
         foreach ($oldForums as $forum) {
             if (!in_array($forum, $newForums)) {
                 if ((new Query())->from(Mod::tableName())->where(['forum_id' => $forum, 'user_id' => $this->id])->exists()) {
                     $remove[] = $forum;
                 }
             }
         }
         if (!empty($add)) {
             Yii::$app->db->createCommand()->batchInsert(Mod::tableName(), ['forum_id', 'user_id'], $add)->execute();
         }
         if (!empty($remove)) {
             Yii::$app->db->createCommand()->delete(Mod::tableName(), ['forum_id' => $remove, 'user_id' => $this->id])->execute();
         }
         Cache::getInstance()->delete('forum.moderators');
         Log::info('Moderators updated', null, __METHOD__);
         return true;
     } catch (Exception $e) {
         Log::error($e->getMessage(), null, __METHOD__);
     }
     return false;
 }
 /**
  * Updating the forums order.
  * @return string|\yii\web\Response
  */
 public function actionSortForum()
 {
     if (Yii::$app->request->isAjax) {
         if (User::can(Rbac::PERM_UPDATE_FORUM)) {
             $modelId = Yii::$app->request->post('id');
             $modelCategory = Yii::$app->request->post('category');
             $new = Yii::$app->request->post('new');
             if (is_numeric($modelId) && is_numeric($modelCategory) && is_numeric($new) && $modelId > 0 && $modelCategory > 0 && $new >= 0) {
                 $moved = Forum::findOne((int) $modelId);
                 $movedCategory = Category::findOne((int) $modelCategory);
                 if ($moved && $modelCategory && $moved->category_id == $movedCategory->id) {
                     $query = (new Query())->from(Forum::tableName())->where('id != :id AND category_id = :cid')->params([':id' => $moved->id, ':cid' => $movedCategory->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(Forum::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 forums' order."), ['class' => 'text-danger']);
                         } else {
                             Log::info('Forums orded updated', $moved->id, __METHOD__);
                             return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', "New forums' 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 forums' 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 Forum 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/forums']);
     }
 }
Beispiel #7
0
 public function getForum()
 {
     return $this->hasOne(Forum::className(), ['id' => 'forum_id']);
 }
Beispiel #8
0
 /**
  * Performs thread posts move with counters update.
  * @param integer $target new parent thread's ID
  * @param array $posts IDs of posts to move
  * @param string $name new thread's name if $target = 0
  * @param type $forum new thread's parent forum if $target = 0
  * @return boolean
  * @throws Exception
  * @since 0.2
  */
 public function podiumMovePostsTo($target = null, $posts = [], $name = null, $forum = null)
 {
     $transaction = static::getDb()->beginTransaction();
     try {
         if ($target == 0) {
             $parent = Forum::find()->where(['id' => $forum])->limit(1)->one();
             if (empty($parent)) {
                 throw new Exception('No parent forum of given ID found');
             }
             $newThread = new Thread();
             $newThread->name = $name;
             $newThread->posts = 0;
             $newThread->views = 0;
             $newThread->category_id = $parent->category_id;
             $newThread->forum_id = $parent->id;
             $newThread->author_id = User::loggedId();
             $newThread->save();
         } else {
             $newThread = Thread::find()->where(['id' => $target])->limit(1)->one();
             if (empty($newThread)) {
                 throw new Exception('No thread of given ID found');
             }
         }
         if (!empty($newThread)) {
             foreach ($posts as $post) {
                 if (!is_numeric($post) || $post < 1) {
                     throw new Exception('Incorrect post ID');
                 }
                 $newPost = Post::find()->where(['id' => $post, 'thread_id' => $this->id, 'forum_id' => $this->forum->id])->limit(1)->one();
                 if (empty($newPost)) {
                     throw new Exception('No post of given ID found');
                 }
                 $newPost->thread_id = $newThread->id;
                 $newPost->forum_id = $newThread->forum_id;
                 $newPost->save();
             }
             $wholeThread = false;
             if ($this->postCount) {
                 $this->updateCounters(['posts' => -count($posts)]);
                 $this->forum->updateCounters(['posts' => -count($posts)]);
             } else {
                 $wholeThread = true;
                 $this->delete();
                 $this->forum->updateCounters(['posts' => -count($posts), 'threads' => -1]);
             }
             $newThread->updateCounters(['posts' => count($posts)]);
             $newThread->forum->updateCounters(['posts' => count($posts)]);
             $transaction->commit();
             Cache::clearAfter('postMove');
             Log::info('Posts moved', null, __METHOD__);
             return true;
         }
     } catch (Exception $e) {
         $transaction->rollBack();
         Log::error($e->getMessage(), null, __METHOD__);
     }
     return false;
 }
Beispiel #9
0
 /**
  * Updating the forums order.
  * @return string|\yii\web\Response
  */
 public function actionSortForum()
 {
     if (!Yii::$app->request->isAjax) {
         return $this->redirect(['admin/forums']);
     }
     if (!User::can(Rbac::PERM_UPDATE_FORUM)) {
         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']);
     }
     $modelId = Yii::$app->request->post('id');
     $modelCategory = Yii::$app->request->post('category');
     $new = Yii::$app->request->post('new');
     if (!is_numeric($modelId) || !is_numeric($modelCategory) || !is_numeric($new)) {
         return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Sorry! Sorting parameters are wrong.'), ['class' => 'text-danger']);
     }
     $moved = Forum::find()->where(['id' => $modelId])->limit(1)->one();
     $movedCategory = Category::find()->where(['id' => $modelCategory])->limit(1)->one();
     if (empty($moved) || empty($modelCategory) || $moved->category_id != $movedCategory->id) {
         return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Sorry! We can not find Forum with this ID.'), ['class' => 'text-danger']);
     }
     if ($moved->newOrder((int) $new)) {
         return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', "New forums' order has been saved."), ['class' => 'text-success']);
     }
     return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', "Sorry! We can not save new forums' order."), ['class' => 'text-danger']);
 }
Beispiel #10
0
 /**
  * Sets new forums order.
  * @param integer $order new forum sorting order number
  * @return boolean
  * @throws Exception
  * @since 0.2
  */
 public function newOrder($order)
 {
     try {
         $next = 0;
         $newSort = -1;
         $query = (new Query())->from(Forum::tableName())->where('id != :id AND category_id = :cid')->params([':id' => $this->id, ':cid' => $this->category_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(Forum::tableName(), ['sort' => $next], 'id = :id', [':id' => $id])->execute();
             $next++;
         }
         if ($newSort == -1) {
             $newSort = $next;
         }
         $this->sort = $newSort;
         if (!$this->save()) {
             throw new Exception('Forums order saving error');
         }
         Log::info('Forums orded updated', $this->id, __METHOD__);
         return true;
     } catch (Exception $e) {
         Log::error($e->getMessage(), null, __METHOD__);
     }
     return false;
 }