/** * Add comment */ public function addComment($content, $resource_id, $resource_type) { if (!Zend_Auth::getInstance()->hasIdentity()) { return false; } if (!is_string($content) || !is_string($resource_type) || strlen($content) < 1) { return false; } $content = Application_Plugin_Common::limitInput($content); $author_id = Zend_Auth::getInstance()->getIdentity()->id; // find resource author switch ($resource_type) { case 'post': $Posts = new Application_Model_Posts(); $resource_author = $Posts->getPostAuthorId($resource_id); $resource_wall = $Posts->getPostsWallProfileData($resource_id); // for page comments written by page admin switch owner to be a page itself if ($resource_wall['type'] == 'page' && $resource_wall['owner'] == $author_id) { $author_id = $resource_wall['id']; $resource_author = $author_id; } break; case 'image': $Images = new Application_Model_Images(); $image = $Images->getImage($resource_id); $resource_author = $image['data']['uploaded_by']; break; default: $resource_author = 0; break; } $ret = $this->insert(array('author_id' => $author_id, 'resource_type' => $resource_type, 'resource_id' => $resource_id, 'created_on' => Application_Plugin_Common::now(), 'content' => $content, 'is_hidden' => 0)); $this->markOldAsHidden($resource_type, $resource_id); $Notifications = new Application_Model_Notifications(); // notify all users involved in comment discussion $notify_users = $this->getUsersCommented($resource_type, $resource_id, true); // notify resource author if not already on the list if (array_search($resource_author, $notify_users) === false) { $notify_users[] = $resource_author; } $Notifications->pushNotification($notify_users, 1, 'comment', $ret); // trigger hooks $data = array('comment_id' => $ret, 'content' => $content); Zend_Registry::get('hooks')->trigger('hook_data_aftersavecomment', $data); return $ret; }
/** * 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); }