/** * Create * */ public function createAction() { $form = new Forum_Form_Post_Create(); if ($this->getRequest()->isPost() && $form->isValid($this->_getAllParams())) { $values = $form->getValues(); $categoryId = $this->_getParam('category'); // @todo check category ID $values['categoryId'] = $categoryId; $post = new Forum_Model_Post(); $post->setFromArray($values); $post->save(); $this->_helper->flashMessenger->addMessage('Post created'); $this->_helper->redirector->gotoRoute(array('id' => $post->id), 'forumpost'); } $this->view->form = $form; }
public function postCreateAction() { if (!$this->_helper->requireUser()->isValid()) { return; } if (!$this->_helper->requireSubject('forum_topic')->isValid()) { return; } $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer(); $this->view->topic = $topic = Engine_Api::_()->core()->getSubject('forum_topic'); $this->view->forum = $forum = $topic->getParent(); if (!$this->_helper->requireAuth()->setAuthParams($forum, null, 'post.create')->isValid()) { return; } if ($topic->closed) { return; } $this->view->form = $form = new Forum_Form_Post_Create(); $quote_id = $this->getRequest()->getParam('quote_id'); if (!empty($quote_id)) { $quote = Engine_Api::_()->getItem('forum_post', $quote_id); $form->body->setValue("<blockquote><strong>" . $quote->getOwner()->__toString() . " said:</strong><br />" . $quote->body . "</blockquote><br />"); } if (!$this->getRequest()->isPost()) { return; } if (!$form->isValid($this->getRequest()->getPost())) { return; } // Process $values = $form->getValues(); $values['body'] = nl2br($values['body']); $values['user_id'] = $viewer->getIdentity(); $values['topic_id'] = $topic->getIdentity(); $values['forum_id'] = $forum->getIdentity(); $topicTable = Engine_Api::_()->getDbtable('topics', 'forum'); $topicWatchesTable = Engine_Api::_()->getDbtable('topicWatches', 'forum'); $postTable = Engine_Api::_()->getDbtable('posts', 'forum'); $userTable = Engine_Api::_()->getItemTable('user'); $notifyApi = Engine_Api::_()->getDbtable('notifications', 'activity'); $activityApi = Engine_Api::_()->getDbtable('actions', 'activity'); $viewer = Engine_Api::_()->user()->getViewer(); $topicOwner = $topic->getOwner(); $isOwnTopic = $viewer->isSelf($topicOwner); $watch = (bool) $values['watch']; $isWatching = $topicWatchesTable->select()->from($topicWatchesTable->info('name'), 'watch')->where('resource_id = ?', $forum->getIdentity())->where('topic_id = ?', $topic->getIdentity())->where('user_id = ?', $viewer->getIdentity())->limit(1)->query()->fetchColumn(0); $db = $postTable->getAdapter(); $db->beginTransaction(); try { $post = $postTable->createRow(); $post->setFromArray($values); $post->save(); if ($values['photo']) { $post->setPhoto($form->photo); } // Watch if (false === $isWatching) { $topicWatchesTable->insert(array('resource_id' => $forum->getIdentity(), 'topic_id' => $topic->getIdentity(), 'user_id' => $viewer->getIdentity(), 'watch' => (bool) $watch)); } else { if ($watch != $isWatching) { $topicWatchesTable->update(array('watch' => (bool) $watch), array('resource_id = ?' => $forum->getIdentity(), 'topic_id = ?' => $topic->getIdentity(), 'user_id = ?' => $viewer->getIdentity())); } } // Activity $action = $activityApi->addActivity($viewer, $topic, 'forum_topic_reply'); if ($action) { $action->attach($post, Activity_Model_Action::ATTACH_DESCRIPTION); } // Notifications $notifyUserIds = $topicWatchesTable->select()->from($topicWatchesTable->info('name'), 'user_id')->where('resource_id = ?', $forum->getIdentity())->where('topic_id = ?', $topic->getIdentity())->where('watch = ?', 1)->query()->fetchAll(Zend_Db::FETCH_COLUMN); foreach ($userTable->find($notifyUserIds) as $notifyUser) { // Don't notify self if ($notifyUser->isSelf($viewer)) { continue; } if ($notifyUser->isSelf($topicOwner)) { $type = 'forum_topic_response'; } else { $type = 'forum_topic_reply'; } $notifyApi->addNotification($notifyUser, $viewer, $topic, $type, array('message' => $this->view->BBCode($post->body))); } $db->commit(); } catch (Exception $e) { $db->rollBack(); throw $e; } return $this->_redirectCustom($post); }