public function subscribeAction($id)
{
if (!$this->checkTokenGet('post-' . $id)) {
return $this->response->redirect();
}
$usersId = $this->session->get('identity');
if (!$usersId) {
$this->flashSession->error('You must be logged first');
return $this->response->redirect();
}
$post = Posts::findFirstById($id);
if (!$post) {
$this->flashSession->error('The discussion does not exist');
return $this->response->redirect();
}
$subscription = PostsSubscribers::findFirst(['posts_id = ?0 AND users_id = ?1', 'bind' => [$post->id, $usersId]]);
if (!$subscription) {
$subscription = new PostsSubscribers();
$subscription->posts_id = $post->id;
$subscription->users_id = $usersId;
$subscription->created_at = time();
if ($subscription->save()) {
$this->flashSession->notice('You are now subscribed to this post');
}
}
return $this->response->redirect('discussion/' . $post->id . '/' . $post->slug);
}