public function flagComment() { if (!$this->request->is('ajax')) { $this->redirect('/'); } else { $userId = $this->Session->read('Auth.User.id'); $this->autoRender = $this->layout = false; if (!empty($this->request->data)) { $commentId = $this->request->data['commentId']; $this->CommentsFlag->set($this->data); $this->CommentsFlag->set('user_id', $userId); $this->CommentsFlag->set('comment_id', $commentId); if (!$this->CommentsFlag->userAlreadyFlagged($userId, $commentId)) { if ($this->CommentsFlag->save($this->request->data)) { $message = array('response' => 'success'); } } else { $message = array('response' => 'alreadyFlagged'); } return json_encode($message); } } }