public function afterCreate() { $activity = new ActivityNotifications(); $activity->users_id = $this->users_id; if ($this->type == 'P') { $activity->type = 'O'; $activity->posts_id = $this->code1; $activity->posts_replies_id = 0; } else { if ($this->type == 'C') { $activity->type = 'V'; $activity->posts_id = $this->code2; $activity->posts_replies_id = $this->code1; } else { $activity->type = 'B'; $activity->posts_id = 1; $activity->posts_replies_id = 1; } } $activity->extra = $this->badge; $activity->users_origin_id = $this->users_id; var_dump($activity->save()); }
/** * Votes a post up * * @param int $id Post ID * @return Response */ public function voteUpAction($id = 0) { $response = new Response(); if (!$this->checkTokenGetJson()) { $csrfTokenError = ['status' => 'error', 'message' => 'Token error. This might be CSRF attack.']; return $response->setJsonContent($csrfTokenError); } /** * Find the post using get */ $post = Posts::findFirstById($id); if (!$post) { $contentNotExist = ['status' => 'error', 'message' => 'Post does not exist']; return $response->setJsonContent($contentNotExist); } $user = Users::findFirstById($this->session->get('identity')); if (!$user) { $contentlogIn = ['status' => 'error', 'message' => 'You must log in first to vote']; return $response->setJsonContent($contentlogIn); } if ($user->votes <= 0) { $contentDontHave = ['status' => 'error', 'message' => "You don't have enough votes available"]; return $response->setJsonContent($contentDontHave); } if (PostsVotes::count(['posts_id = ?0 AND users_id = ?1', 'bind' => [$post->id, $user->id]])) { $contentAlreadyVote = ['status' => 'error', 'message' => 'You have already voted this post']; return $response->setJsonContent($contentAlreadyVote); } $postVote = new PostsVotes(); $postVote->posts_id = $post->id; $postVote->users_id = $user->id; $postVote->vote = PostsVotes::VOTE_UP; if (!$postVote->save()) { foreach ($postVote->getMessages() as $message) { $contentError = ['status' => 'error', 'message' => $message->getMessage()]; return $response->setJsonContent($contentError); } } $post->votes_up++; if ($post->users_id != $user->id) { $post->user->increaseKarma(Karma::SOMEONE_DID_VOTE_MY_POST); $user->increaseKarma(Karma::VOTE_ON_SOMEONE_ELSE_POST); } if ($post->save()) { $user->votes--; if (!$user->save()) { foreach ($user->getMessages() as $message) { $contentErrorSave = ['status' => 'error', 'message' => $message->getMessage()]; return $response->setJsonContent($contentErrorSave); } } } if ($post->users_id != $user->id) { $activity = new ActivityNotifications(); $activity->users_id = $post->users_id; $activity->posts_id = $post->id; $activity->posts_replies_id = null; $activity->users_origin_id = $user->id; $activity->type = 'P'; $activity->save(); } $contentOk = ['status' => 'OK']; return $response->setJsonContent($contentOk); }
/** * Accepts a reply as correct answer */ public function acceptAction($id = 0) { $response = new Response(); /** * Find the post using get */ $postReply = PostsReplies::findFirstById($id); if (!$postReply) { $contentNotExist = array('status' => 'error', 'message' => 'Post reply does not exist'); return $response->setJsonContent($contentNotExist); } $user = Users::findFirstById($this->session->get('identity')); if (!$user) { $contentLogIn = array('status' => 'error', 'message' => 'You must log in first to vote'); return $response->setJsonContent($contentLogIn); } if ($postReply->accepted == 'Y') { $contentAlready = array('status' => 'error', 'message' => 'This reply is already accepted as answer'); return $response->setJsonContent($contentAlready); } if ($postReply->post->deleted) { $contentDeleted = array('status' => 'error', 'message' => 'Post associated to the reply is deleted'); return $response->setJsonContent($contentDeleted); } if ($postReply->post->accepted_answer == 'Y') { $contentAlreadyAnswer = array('status' => 'error', 'message' => 'This post already has an accepted answer'); return $response->setJsonContent($contentAlreadyAnswer); } if ($postReply->post->users_id != $user->id && $user->moderator != 'Y') { $contentCorrect = array('status' => 'error', 'message' => 'You can\'t accept this answer as correct'); return $response->setJsonContent($contentCorrect); } if ($postReply->post->users_id != $postReply->users_id) { $postReply->post->user->karma += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; $postReply->post->user->votes_points += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; $points = 30 + intval(abs($user->karma - $postReply->user->karma) / 1000); $parametersBounty = array('users_id = ?0 AND posts_replies_id = ?1', 'bind' => array($postReply->users_id, $postReply->id)); $postBounty = PostsBounties::findFirst($parametersBounty); if ($postBounty) { $points += $postBounty->points; } $postReply->user->karma += $points; $postReply->user->votes_points += $points; if ($postReply->users_id != $user->id && $postReply->post->users_id != $user->id) { $user->karma += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; $user->votes_points += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; } } $postReply->accepted = 'Y'; $postReply->post->accepted_answer = 'Y'; if ($postReply->save()) { if (!$user->save()) { foreach ($user->getMessages() as $message) { $contentError = array('status' => 'error', 'message' => $message->getMessage()); return $response->setJsonContent($contentError); } } } if ($user->id != $postReply->users_id) { $activity = new ActivityNotifications(); $activity->users_id = $postReply->users_id; $activity->posts_id = $postReply->post->id; $activity->posts_replies_id = $postReply->id; $activity->users_origin_id = $user->id; $activity->type = 'A'; $activity->save(); } $contentOk = array('status' => 'OK'); return $response->setJsonContent($contentOk); }
public function afterCreate() { if ($this->id > 0) { $activity = new Activities(); $activity->users_id = $this->users_id; $activity->posts_id = $this->posts_id; $activity->type = Activities::NEW_REPLY; $activity->save(); $toNotify = array(); /** * Notify users that always want notifications */ foreach (Users::find(array('notifications = "Y"', 'columns' => 'id')) as $user) { if ($this->users_id != $user->id) { $notification = new Notifications(); $notification->users_id = $user->id; $notification->posts_id = $this->posts_id; $notification->posts_replies_id = $this->id; $notification->type = 'C'; $notification->save(); $activity = new ActivityNotifications(); $activity->users_id = $user->id; $activity->posts_id = $this->posts_id; $activity->posts_replies_id = $this->id; $activity->users_origin_id = $this->users_id; $activity->type = 'C'; $activity->save(); $toNotify[$user->id] = $notification->id; } } /** * Register users subscribed to the post */ foreach (PostsSubscribers::findByPostsId($this->posts_id) as $subscriber) { if (!isset($toNotify[$subscriber->users_id])) { $notification = new Notifications(); $notification->users_id = $subscriber->users_id; $notification->posts_id = $this->posts_id; $notification->posts_replies_id = $this->id; $notification->type = 'C'; $notification->save(); $activity = new ActivityNotifications(); $activity->users_id = $subscriber->users_id; $activity->posts_id = $this->posts_id; $activity->posts_replies_id = $this->id; $activity->users_origin_id = $this->users_id; $activity->type = 'C'; $activity->save(); $toNotify[$subscriber->users_id] = $notification->id; } } /** * Register the user in the post's notifications */ if (!isset($toNotify[$this->users_id])) { $parameters = array('users_id = ?0 AND posts_id = ?1', 'bind' => array($this->users_id, $this->posts_id)); $hasNotifications = PostsNotifications::count($parameters); if (!$hasNotifications) { $notification = new PostsNotifications(); $notification->users_id = $this->users_id; $notification->posts_id = $this->posts_id; $notification->save(); } } /** * Notify users that have commented in the same post */ $postsNotifications = PostsNotifications::findByPostsId($this->posts_id); foreach ($postsNotifications as $postNotification) { if (!isset($toNotify[$postNotification->users_id])) { if ($postNotification->users_id != $this->users_id) { /** * Generate an e-mail notification */ $notification = new Notifications(); $notification->users_id = $postNotification->users_id; $notification->posts_id = $this->posts_id; $notification->posts_replies_id = $this->id; $notification->type = 'C'; $notification->save(); $activity = new ActivityNotifications(); $activity->users_id = $postNotification->users_id; $activity->posts_id = $this->posts_id; $activity->posts_replies_id = $this->id; $activity->users_origin_id = $this->users_id; $activity->type = 'C'; $activity->save(); $toNotify[$postNotification->users_id] = $notification->id; } } } /** * Queue notifications to be sent */ $this->getDI()->getQueue()->put($toNotify); } }