示例#1
0
 public function createAction()
 {
     if (!$this->_helper->requireUser()->isValid()) {
         return;
     }
     if (!$this->_helper->requireAuth()->setAuthParams(null, null, 'comment')->isValid()) {
         return;
     }
     $viewer = Engine_Api::_()->user()->getViewer();
     //$subject = Engine_Api::_()->core()->getSubject();
     $this->view->subject = $subject = $this->_subject;
     $this->view->form = $form = new Core_Form_Comment_Create();
     if (!$this->getRequest()->isPost()) {
         $this->view->status = false;
         $this->view->error = Zend_Registry::get('Zend_Translate')->_("Invalid request method");
         return;
     }
     if (!$form->isValid($this->_getAllParams())) {
         $this->view->status = false;
         $this->view->error = Zend_Registry::get('Zend_Translate')->_("Invalid data");
         return;
     }
     // Process
     // Filter HTML
     $filter = new Zend_Filter();
     $filter->addFilter(new Engine_Filter_Censor());
     $filter->addFilter(new Engine_Filter_HtmlSpecialChars());
     $body = $form->getValue('body');
     $body = $filter->filter($body);
     $db = $subject->comments()->getCommentTable()->getAdapter();
     $db->beginTransaction();
     try {
         $subject->comments()->addComment($viewer, $body);
         $activityApi = Engine_Api::_()->getDbtable('actions', 'activity');
         $notifyApi = Engine_Api::_()->getDbtable('notifications', 'activity');
         $subjectOwner = $subject->getOwner('user');
         // Activity
         $action = $activityApi->addActivity($viewer, $subject, 'comment_' . $subject->getType(), '', array('owner' => $subjectOwner->getGuid(), 'body' => $body));
         //$activityApi->attachActivity($action, $subject);
         // Notifications
         // Add notification for owner (if user and not viewer)
         $this->view->subject = $subject->getGuid();
         $this->view->owner = $subjectOwner->getGuid();
         if ($subjectOwner->getType() == 'user' && $subjectOwner->getIdentity() != $viewer->getIdentity()) {
             $notifyApi->addNotification($subjectOwner, $viewer, $subject, 'commented', array('label' => $subject->getShortType()));
         }
         // Add a notification for all users that commented or like except the viewer and poster
         // @todo we should probably limit this
         $commentedUserNotifications = array();
         foreach ($subject->comments()->getAllCommentsUsers() as $notifyUser) {
             if ($notifyUser->getIdentity() == $viewer->getIdentity() || $notifyUser->getIdentity() == $subjectOwner->getIdentity()) {
                 continue;
             }
             // Don't send a notification if the user both commented and liked this
             $commentedUserNotifications[] = $notifyUser->getIdentity();
             $notifyApi->addNotification($notifyUser, $viewer, $subject, 'commented_commented', array('label' => $subject->getShortType()));
         }
         // Add a notification for all users that liked
         // @todo we should probably limit this
         foreach ($subject->likes()->getAllLikesUsers() as $notifyUser) {
             // Skip viewer and owner
             if ($notifyUser->getIdentity() == $viewer->getIdentity() || $notifyUser->getIdentity() == $subjectOwner->getIdentity()) {
                 continue;
             }
             // Don't send a notification if the user both commented and liked this
             if (in_array($notifyUser->getIdentity(), $commentedUserNotifications)) {
                 continue;
             }
             $notifyApi->addNotification($notifyUser, $viewer, $subject, 'liked_commented', array('label' => $subject->getShortType()));
         }
         // Increment comment count
         Engine_Api::_()->getDbtable('statistics', 'core')->increment('core.comments');
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     $this->view->status = true;
     $this->view->message = 'Comment added';
     $this->view->body = $this->view->action('list', 'comment', 'core', array('type' => $this->_getParam('type'), 'id' => $this->_getParam('id'), 'format' => 'html', 'page' => 1));
     $this->_helper->contextSwitch->initContext();
 }