public function indexAction() { // Get subject $subject = null; if (Engine_Api::_()->core()->hasSubject()) { $subject = Engine_Api::_()->core()->getSubject(); } else { if ($subject = $this->_getParam('subject')) { list($type, $id) = explode('_', $subject); $subject = Engine_Api::_()->getItem($type, $id); } else { if (($type = $this->_getParam('type')) && ($id = $this->_getParam('id'))) { $subject = Engine_Api::_()->getItem($type, $id); } } } if (!$subject instanceof Core_Model_Item_Abstract || !$subject->getIdentity() || !method_exists($subject, 'comments') && !method_exists($subject, 'likes')) { return $this->setNoRender(); } // Perms $viewer = Engine_Api::_()->user()->getViewer(); $this->view->canComment = $canComment = $subject->authorization()->isAllowed($viewer, 'comment'); $this->view->canDelete = $canDelete = $subject->authorization()->isAllowed($viewer, 'edit'); // Likes $this->view->viewAllLikes = $this->_getParam('viewAllLikes', false); $this->view->likes = $likes = $subject->likes()->getLikePaginator(); // Comments // If has a page, display oldest to newest if (null !== ($page = $this->_getParam('page'))) { $commentSelect = $subject->comments()->getCommentSelect(); $commentSelect->order('comment_id ASC'); $comments = Zend_Paginator::factory($commentSelect); $comments->setCurrentPageNumber($page); $comments->setItemCountPerPage(10); $this->view->comments = $comments; $this->view->page = $page; } else { // If not has a page, show the $commentSelect = $subject->comments()->getCommentSelect(); $commentSelect->order('comment_id DESC'); $comments = Zend_Paginator::factory($commentSelect); $comments->setCurrentPageNumber(1); $comments->setItemCountPerPage(4); $this->view->comments = $comments; $this->view->page = $page; } if ($viewer->getIdentity() && $canComment) { $this->view->form = $form = new Core_Form_Comment_Create(); //$form->setAction($this->view->url(array('action' => ''))) $form->populate(array('identity' => $subject->getIdentity(), 'type' => $subject->getType())); } // Hide if can't post and no comments if (!$canComment && !$canDelete && count($comments) <= 0 && count($likes) <= 0) { $this->setNoRender(); } }
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(); }