Example #1
0
 /**
  * Add Vote up or down for the post
  *
  * @param  integer $objectId this is id of posts
  * @param  string  $object   there are some object comments, posts, postsReplay see constant the above
  * @param  string  $way      positive or negative
  * @return boolen|array
  */
 public static function vote($objectId, $object, $way)
 {
     $auth = \Phalcon\DI::getDefault()->getAuth();
     $conditions = ['usersId = :usersId: AND objectId = :objectId: AND object = :object:', 'bind' => ['usersId' => $auth->getAuth()['id'], 'objectId' => $objectId, 'object' => $object]];
     if (!($vote = Vote::findFirst($conditions))) {
         $vote = new self();
     }
     // When the posts have vote then user is existing
     if ($vote->getUsersId()) {
         switch ($object) {
             case Vote::OBJECT_POSTS:
                 return ['type' => 'error', 'content' => t('You have already voted this post')];
                 break;
             default:
                 return ['type' => 'error', 'content' => t('You have already voted this post reply')];
                 break;
         }
     }
     if ($way == 'positive') {
         $vote->voteUp($objectId, $object);
     }
     if ($way == 'negative') {
         $vote->voteDown($objectId, $object);
     }
     if (!$vote->save()) {
         foreach ($vote->getMessages() as $m) {
             error_log($m->getMessage());
             //return ['type' => 'error','content' => $m->getMessage()];
         }
         return false;
     }
     return true;
 }