/**
  * View Topic Action
  */
 public function actionviewtopic()
 {
     if (isset($_GET['topicid']) && ($model = ForumTopics::model()->findByPk($_GET['topicid']))) {
         // Make sure the alias matches to avoid duplicated content
         if ($model->alias != $model->getAlias($_GET['alias'])) {
             throw new CHttpException(404, Yii::t('forum', 'Sorry, We could not find that topic.'));
         }
         // Did we add a new post?
         $newPost = new ForumPosts();
         if (isset($_POST['ForumPosts'])) {
             // Make sure we have access
             if (!Yii::app()->user->checkAccess('op_forum_post_posts')) {
                 throw new CHttpException(403, Yii::t('forum', 'Sorry, You are not allowed to perform that operation.'));
             }
             $newPost->attributes = $_POST['ForumPosts'];
             $newPost->topicid = $model->id;
             $newPost->visible = 1;
             if ($newPost->save()) {
                 // Update last post time and author
                 $model->lastpostdate = time();
                 $model->lastpostauthorid = Yii::app()->user->id;
                 $model->update();
                 // Send notifications to the ones subscribed
                 $topicSubscribtions = TopicSubs::model()->with(array('user', 'topic'))->findAll('topicid=:topicid', array(':topicid' => $model->id));
                 // Loop and email
                 if ($topicSubscribtions) {
                     foreach ($topicSubscribtions as $sub) {
                         $email = Yii::app()->email;
                         // We skip the user that actually posted the new post
                         if ($sub->userid == Yii::app()->user->id) {
                             continue;
                         }
                         // Email to the users email address
                         $email->subject = Yii::t('forum', "New post in a topic you are subscribed to '{title}'", array('{title}' => $sub->topic->title));
                         $email->to = $sub->user->email;
                         $email->from = Yii::app()->params['emailout'];
                         $email->replyTo = Yii::app()->params['emailout'];
                         $email->message = Yii::t('forum', "Dear {user}, <br /><br />A new post was made by '{author}' in the topic '{topic}' you are subscribed to. To visit the topic please click the following link<br /><br />{link}<br /><br />\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t  <small>To unsubscribe from receiving updates for this topic please click the following link {unlink}</small>.<br /><br />\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t  Regards, The {name} Team.", array('{user}' => $sub->user->username, '{author}' => $newPost->author->username, '{topic}' => CHtml::encode($sub->topic->title), '{link}' => $this->createAbsoluteUrl('/forum/topic/' . $sub->topic->id . '-' . $sub->topic->alias, array('lang' => false)), '{unlink}' => $this->createAbsoluteUrl('/forum/unsubscribe', array('id' => $sub->topic->id, 'lang' => false)), '{name}' => Yii::app()->name));
                         $email->send();
                     }
                 }
                 Yii::app()->user->setFlash('success', Yii::t('forum', 'Thank You. Your post was submitted.'));
                 $this->redirect('/forum/topic/' . $model->id . '-' . $model->alias . '/page/' . $_POST['lastpage'] . '#post' . $newPost->id);
             }
         }
         // Increase the views count
         $model->views++;
         $model->update();
         // Grab posts
         $criteria = new CDbCriteria();
         $criteria->condition = 'topicid=:tid AND (visible=:visible OR visible=:mod)';
         $criteria->params = array(':tid' => $model->id, ':visible' => 1, ':mod' => Yii::app()->user->checkAccess('op_forum_post_posts') ? 0 : 1);
         $count = ForumPosts::model()->count($criteria);
         $pages = new CPagination($count);
         $pages->pageSize = self::POST_PAGE_SIZE;
         $pages->route = '/forum/topic/' . $model->id . '-' . $model->alias;
         $pages->params = array('lang' => false);
         $pages->applyLimit($criteria);
         $posts = ForumPosts::model()->byDateAsc()->with(array('author'))->findAll($criteria);
         // Show titles and nav
         $this->pageTitle[] = Yii::t('forum', 'Viewing Topic: {title}', array('{title}' => CHtml::encode($model->title)));
         $this->breadcrumbs[Yii::t('forum', 'Viewing Topic: {title}', array('{title}' => $model->title))] = '';
         $markdown = new MarkdownParser();
         // Are we subscribed into this topic?
         $subscribed = TopicSubs::model()->find('topicid=:topicid AND userid=:userid', array(':topicid' => $model->id, ':userid' => Yii::app()->user->id));
         // Render
         $this->render('viewtopic', array('subscribed' => $subscribed, 'markdown' => $markdown, 'model' => $model, 'posts' => $posts, 'newPost' => $newPost, 'count' => $count, 'pages' => $pages));
     } else {
         throw new CHttpException(404, Yii::t('forum', 'Sorry, We could not find that topic.'));
     }
 }