/** * Gets number of user subscribed threads with new posts. * @return integer */ public function getSubscriptionsCount() { $cache = Cache::getInstance()->getElement('user.subscriptions', $this->id); if ($cache === false) { $cache = (new Query())->from(Subscription::tableName())->where(['user_id' => $this->id, 'post_seen' => Subscription::POST_NEW])->count(); Cache::getInstance()->setElement('user.subscriptions', $this->id, $cache); } return $cache; }
/** * Subscribing the thread of given ID. * @param integer $id * @return \yii\web\Response */ public function actionAdd($id = null) { if (Yii::$app->request->isAjax) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while adding this subscription!'), ['class' => 'text-danger'])]; if (!Yii::$app->user->isGuest) { if (is_numeric($id) && $id > 0) { $thread = Thread::findOne((int) $id); if ($thread) { $subscription = Subscription::findOne(['thread_id' => $thread->id, 'user_id' => User::loggedId()]); if ($subscription) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You are already subscribed to this thread.'), ['class' => 'text-info'])]; } else { $sub = new Subscription(); $sub->thread_id = $thread->id; $sub->user_id = User::loggedId(); $sub->post_seen = Subscription::POST_SEEN; if ($sub->save()) { $data = ['error' => 0, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'You have subscribed to this thread!'), ['class' => 'text-success'])]; } } } } } else { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to subscribe to this thread'), ['class' => 'text-info'])]; } return Json::encode($data); } else { return $this->redirect(['default/index']); } }
/** * Performs new post creation and subscription. * If previous post in thread has got the same author posts are merged. * @param Post $previous previous post * @return boolean * @throws Exception * @since 0.2 */ public function podiumNew($previous = null) { $transaction = static::getDb()->beginTransaction(); try { $id = null; if (!empty($previous) && $previous->author_id == User::loggedId()) { $previous->content .= '<hr>' . $this->content; $previous->edited = 1; $previous->touch('edited_at'); if ($previous->save()) { $previous->markSeen(); $previous->thread->touch('edited_post_at'); $id = $previous->id; $thread = $previous->thread; } } else { if ($this->save()) { $this->markSeen(); $this->forum->updateCounters(['posts' => 1]); $this->thread->updateCounters(['posts' => 1]); $this->thread->touch('new_post_at'); $this->thread->touch('edited_post_at'); $id = $this->id; $thread = $this->thread; } } if (empty($id)) { throw new Exception('Saved Post ID missing'); } Subscription::notify($thread->id); if ($this->subscribe && !$thread->subscription) { $subscription = new Subscription(); $subscription->user_id = User::loggedId(); $subscription->thread_id = $thread->id; $subscription->post_seen = Subscription::POST_SEEN; $subscription->save(); } $transaction->commit(); Cache::clearAfter('newPost'); Log::info('Post added', $id, __METHOD__); return true; } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Creating the post of given category ID, forum ID and thread ID. * This can be reply to selected post of given ID. * @param integer $cid * @param integer $fid * @param integer $tid * @param integer $pid * @return string|\yii\web\Response */ public function actionPost($cid = null, $fid = null, $tid = null, $pid = null) { if (!User::can(Rbac::PERM_CREATE_POST)) { if (Yii::$app->user->isGuest) { $this->warning(Yii::t('podium/flash', 'Please sign in to post a reply.')); return $this->redirect(['account/login']); } else { $this->error(Yii::t('podium/flash', 'Sorry! You do not have the required permission to perform this action.')); return $this->redirect(['default/index']); } } else { if (!is_numeric($cid) || $cid < 1 || !is_numeric($fid) || $fid < 1 || !is_numeric($tid) || $tid < 1) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.')); return $this->redirect(['default/index']); } $category = Category::findOne((int) $cid); if (!$category) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.')); return $this->redirect(['default/index']); } else { $forum = Forum::find()->where(['id' => (int) $fid, 'category_id' => $category->id])->limit(1)->one(); if (!$forum) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.')); return $this->redirect(['default/index']); } else { $thread = Thread::find()->where(['id' => (int) $tid, 'category_id' => $category->id, 'forum_id' => $forum->id])->limit(1)->one(); if (!$thread) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.')); return $this->redirect(['default/index']); } else { if ($thread->locked == 0 || $thread->locked == 1 && User::can(Rbac::PERM_UPDATE_THREAD, ['item' => $thread])) { $model = new Post(); $model->subscribe = 1; $postData = Yii::$app->request->post(); $replyFor = null; if (is_numeric($pid) && $pid > 0) { $replyFor = Post::findOne((int) $pid); if ($replyFor) { if (isset($postData['quote']) && !empty($postData['quote'])) { $model->content = Helper::prepareQuote($replyFor, $postData['quote']); } else { $model->content = Helper::prepareQuote($replyFor); } } } $preview = ''; $previous = Post::find()->where(['thread_id' => $thread->id])->orderBy(['id' => SORT_DESC])->limit(1)->one(); if ($model->load($postData)) { $model->thread_id = $thread->id; $model->forum_id = $forum->id; $model->author_id = User::loggedId(); if ($model->validate()) { if (isset($postData['preview-button'])) { $preview = $model->content; } else { $transaction = Post::getDb()->beginTransaction(); try { $id = null; if ($previous->author_id == User::loggedId()) { $previous->content .= '<hr>' . $model->content; $previous->edited = 1; $previous->edited_at = time(); if ($previous->save()) { $previous->markSeen(); $thread->touch('edited_post_at'); $id = $previous->id; } } else { if ($model->save(false)) { $model->markSeen(); $forum->updateCounters(['posts' => 1]); $thread->updateCounters(['posts' => 1]); $thread->touch('new_post_at'); $thread->touch('edited_post_at'); $id = $model->id; } } if ($id !== null) { Subscription::notify($thread->id); if ($model->subscribe && !$model->thread->subscription) { $subscription = new Subscription(); $subscription->user_id = User::loggedId(); $subscription->thread_id = $model->thread->id; $subscription->post_seen = Subscription::POST_SEEN; $subscription->save(); } $transaction->commit(); Cache::getInstance()->delete('forum.postscount'); Cache::getInstance()->deleteElement('user.postscount', User::loggedId()); Cache::getInstance()->delete('forum.latestposts'); Log::info('Post added', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'New reply has been added.')); return $this->redirect(['default/show', 'id' => $id]); } else { throw new Exception('Saved Post ID missing.'); } } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was an error while adding the reply. Contact administrator about this problem.')); } } } } return $this->render('post', ['replyFor' => $replyFor, 'preview' => $preview, 'model' => $model, 'category' => $category, 'forum' => $forum, 'thread' => $thread, 'previous' => $previous]); } else { $this->info(Yii::t('podium/flash', 'This thread is locked.')); return $this->redirect(['default/thread', 'cid' => $category->id, 'fid' => $forum->id, 'id' => $thread->id, 'slug' => $thread->slug]); } } } } } }
/** * Adds subscription for thread. * @param integer $thread thread's ID * @return boolean * @since 0.2 */ public static function add($thread) { if (!Yii::$app->user->isGuest) { $sub = new Subscription(); $sub->thread_id = $thread; $sub->user_id = User::loggedId(); $sub->post_seen = self::POST_SEEN; if ($sub->save()) { return true; } } return false; }
/** * Performs new thread with first post creation and subscription. * @return boolean * @since 0.2 */ public function podiumNew() { $transaction = static::getDb()->beginTransaction(); try { if ($this->save()) { $this->forum->updateCounters(['threads' => 1]); $post = new Post(); $post->content = $this->post; $post->thread_id = $this->id; $post->forum_id = $this->forum_id; $post->author_id = User::loggedId(); $post->likes = 0; $post->dislikes = 0; if ($post->save()) { $post->markSeen(); $this->forum->updateCounters(['posts' => 1]); $this->updateCounters(['posts' => 1]); $this->touch('new_post_at'); $this->touch('edited_post_at'); if ($this->subscribe) { $subscription = new Subscription(); $subscription->user_id = User::loggedId(); $subscription->thread_id = $this->id; $subscription->post_seen = Subscription::POST_SEEN; $subscription->save(); } } } $transaction->commit(); Cache::clearAfter('newThread'); Log::info('Thread added', $this->id, __METHOD__); return true; } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Subscribing the thread of given ID. * @param integer $id * @return \yii\web\Response */ public function actionAdd($id = null) { if (!Yii::$app->request->isAjax) { return $this->redirect(['default/index']); } $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while adding this subscription!'), ['class' => 'text-danger'])]; if (Yii::$app->user->isGuest) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to subscribe to this thread'), ['class' => 'text-info']); } if (is_numeric($id) && $id > 0) { $subscription = Subscription::find()->where(['thread_id' => $id, 'user_id' => User::loggedId()])->limit(1)->one(); if (!empty($subscription)) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You are already subscribed to this thread.'), ['class' => 'text-info']); } else { if (Subscription::add((int) $id)) { $data = ['error' => 0, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'You have subscribed to this thread!'), ['class' => 'text-success'])]; } } } return Json::encode($data); }
/** * Subscription relation. * @return Subscription */ public function getSubscription() { return $this->hasOne(Subscription::className(), ['thread_id' => 'id'])->where(['user_id' => User::loggedId()]); }
public function getSubscription() { return $this->hasOne(Subscription::className(), ['thread_id' => 'id'])->where(['user_id' => Yii::$app->user->id]); }