Exemplo n.º 1
0
 public function createAction()
 {
     $this->_helper->layout()->disableLayout();
     if (!$this->_helper->requireAuth()->setAuthParams(null, null, 'comment')->isValid()) {
         return;
     }
     $viewer = Engine_Api::_()->user()->getViewer();
     $subject = Engine_Api::_()->core()->getSubject();
     $this->view->form = $form = new Ynfeedback_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;
     }
     // 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 {
         if ($viewer->getIdentity() == 0) {
             $userTbl = Engine_Api::_()->getItemTable('user');
             $viewer = $userTbl->createRow();
             $viewer->displayname = $form->getValue('poster_name');
             $viewer->email = $form->getValue('poster_email');
         }
         $subject->comments()->addComment($viewer, $body);
         $activityApi = Engine_Api::_()->getDbtable('actions', 'activity');
         $notifyApi = Engine_Api::_()->getDbtable('notifications', 'activity');
         $subjectOwner = $subject->getOwner('user');
         // Activity
         if ($viewer->getIdentity()) {
             $action = $activityApi->addActivity($viewer, $subject, 'comment_' . $subject->getType(), '', array('owner' => $subjectOwner->getGuid(), 'body' => $body));
             // 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
             $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
             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;
     }
     //send notification to followers foreach idea
     Engine_Api::_()->ynfeedback()->sendNotificationToFollower($subject, 'ynfeedback_idea_new_comment', $subject, $subject);
     $this->view->status = true;
     $this->view->message = 'Comment added';
     $this->view->body = $this->view->action('list', 'comment', 'ynfeedback', array('type' => $this->_getParam('type'), 'id' => $this->_getParam('id'), 'format' => 'html', 'page' => 1));
     $this->_helper->contextSwitch->initContext();
 }