/** * 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); }