public function deleteAction($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();
}
$parameters = ["id = ?0 AND (users_id = ?1 OR 'Y' = ?2)", 'bind' => [$id, $usersId, $this->session->get('identity-moderator')]];
if (!($post = Posts::findFirst($parameters))) {
$this->flashSession->error('The discussion does not exist');
return $this->response->redirect();
}
if (Posts::IS_DELETED == $post->deleted) {
$this->flashSession->error("The post is already deleted");
return $this->response->redirect();
}
if ($post->sticked == 'Y') {
$this->flashSession->error("The discussion cannot be deleted because it's sticked");
return $this->response->redirect();
}
$post->deleted = Posts::IS_DELETED;
if ($post->save()) {
$usersId = $this->session->get('identity');
if ($post->users_id != $usersId) {
/** @var Users $user */
if ($user = Users::findFirstById($usersId)) {
if ($user->moderator == 'Y') {
$user->increaseKarma(Karma::MODERATE_DELETE_POST);
$user->save();
}
}
$user = $post->user;
$user->decreaseKarma(Karma::DELETE_POST);
$user->save();
}
$this->flashSession->success('Discussion was successfully deleted');
return $this->response->redirect();
}
$this->flashSession->error(join('<br>', $post->getMessages()));
return $this->response->redirect();
}