/** * 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]); } } } } } }
/** * 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; }