示例#1
0
 /**
  * Like toggle
  */
 public function toggleLike($resource_id, $resource_type)
 {
     if (!Zend_Auth::getInstance()->hasIdentity() || !$resource_id || !$resource_type) {
         return null;
     }
     $user_id = Zend_Auth::getInstance()->getIdentity()->id;
     if ($this->isLiked($resource_id, $resource_type)) {
         $result = $this->delete(array('resource_id = ?' => (int) $resource_id, 'resource_type = ?' => $resource_type, 'user_id = ?' => (int) $user_id));
         $state = 0;
     } else {
         $data = array('user_id' => (int) $user_id, 'resource_type' => $resource_type, 'resource_id' => (int) $resource_id, 'created_on' => Application_Plugin_Common::now());
         $ret = $this->insert($data);
         $state = 1;
     }
     $likes_count = $this->getLikesCount($resource_id, $resource_type);
     // notify author
     $Notifications = new Application_Model_Notifications();
     if ($state == 1) {
         // find resource author
         switch ($resource_type) {
             case 'post':
                 $Posts = new Application_Model_Posts();
                 $resource_author = array($Posts->getPostAuthorId($resource_id));
                 break;
             case 'comment':
                 $Comments = new Application_Model_Comments();
                 $resource_author = array($Comments->getCommentAuthorId($resource_id));
                 break;
             case 'image':
                 $Images = new Application_Model_Images();
                 $resource_author = array($Images->getImageOwnerId($resource_id));
                 break;
             default:
                 $resource_author = false;
                 break;
         }
         if ($resource_author) {
             // notify resource owner
             $Notifications->pushNotification($resource_author, 2, 'like', $ret);
         }
     }
     return array('count' => $likes_count, 'state' => $state);
 }
 /**
  * Edit comment (ajax)
  */
 public function editAction()
 {
     $request = $this->getRequest();
     $user_role = Zend_Auth::getInstance()->getIdentity()->role;
     $comment_id = (int) $request->getParam('id', false);
     $Comments = new Application_Model_Comments();
     $comment = $Comments->getComment($comment_id);
     if (!$comment && !isset($comment['content'])) {
         $this->getHelper('json')->sendJson($this->view->translate('Resource not available'));
         return;
     }
     // check if my comment or an admin
     if ($Comments->getCommentAuthorId($comment_id) != Zend_Auth::getInstance()->getIdentity()->id && ($user_role != 'admin' && $user_role != 'reviewer')) {
         $this->getHelper('json')->sendJson($this->view->translate('Error - not permitted'));
         return;
     }
     // load and fill up form
     $edit_comment_form = new Application_Form_EditComment();
     $edit_comment_form->getElement('comment')->setValue($comment['content']);
     // get and render form only
     if ($request->isPost() && $request->getParam('form_render')) {
         $edit_comment_form->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/comments/edit/id/' . $comment_id);
         $this->getHelper('json')->sendJson($edit_comment_form->render());
         return;
     }
     if ($request->isPost() && $edit_comment_form->isValid($_POST)) {
         $comment_content = $edit_comment_form->getElement('comment')->getValue();
         $comment_content = Application_Plugin_Common::prepareComment($comment_content);
         // drop on false
         if ($comment_content === false) {
             $this->getHelper('json')->sendJson($this->view->translate('Error - not permitted'));
             return;
         }
         $ret = $Comments->updateComment($comment_id, $comment_content);
         $this->getHelper('json')->sendJson($this->view->RenderOutput($comment_content, 'comment'));
         return;
     }
     $this->getHelper('json')->sendJson($this->view->translate('Error - not permitted'));
     return;
 }