Delete.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages;
  10. use ForkBB\Models\Page;
  11. use ForkBB\Models\Post\Post;
  12. use function \ForkBB\__;
  13. use function \ForkBB\dt;
  14. class Delete extends Page
  15. {
  16. /**
  17. * Удаление сообщения/темы
  18. */
  19. public function delete(array $args, string $method): Page
  20. {
  21. $post = $this->c->posts->load($args['id']);
  22. if (
  23. empty($post)
  24. || ! $post->canDelete
  25. ) {
  26. return $this->c->Message->message('Bad request');
  27. }
  28. $topic = $post->parent;
  29. $deleteTopic = $post->id === $topic->first_post_id;
  30. $this->c->Lang->load('validator');
  31. $this->c->Lang->load('delete');
  32. if ('POST' === $method) {
  33. $v = $this->c->Validator->reset()
  34. ->addRules([
  35. 'token' => 'token:DeletePost',
  36. 'confirm' => 'checkbox',
  37. 'delete' => 'required|string',
  38. ])->addAliases([
  39. ])->addArguments([
  40. 'token' => $args,
  41. ]);
  42. if (
  43. ! $v->validation($_POST)
  44. || '1' !== $v->confirm
  45. ) {
  46. return $this->c->Redirect->page('ViewPost', $args)->message('No confirm redirect');
  47. }
  48. if ($deleteTopic) {
  49. $redirect = $this->c->Redirect
  50. ->page('Forum', ['id' => $topic->forum_id, 'name' => $topic->parent->forum_name])
  51. ->message('Topic del redirect');
  52. $this->c->topics->delete($topic);
  53. } else {
  54. $redirect = $this->c->Redirect
  55. ->page('ViewPost', ['id' => $this->c->posts->previousPost($post)])
  56. ->message('Post del redirect');
  57. $this->c->posts->delete($post);
  58. }
  59. return $redirect;
  60. }
  61. $this->nameTpl = 'post';
  62. $this->onlinePos = 'topic-' . $topic->id;
  63. $this->canonical = $post->linkDelete;
  64. $this->robots = 'noindex';
  65. $this->formTitle = $deleteTopic ? 'Delete topic' : 'Delete post';
  66. $this->crumbs = $this->crumbs($this->formTitle, $topic);
  67. $this->posts = [$post];
  68. $this->postsTitle = $deleteTopic ? 'Delete topic info' : 'Delete info';
  69. $this->form = $this->formDelete($args, $post, $deleteTopic);
  70. return $this;
  71. }
  72. /**
  73. * Подготавливает массив данных для формы
  74. */
  75. protected function formDelete(array $args, Post $post, bool $deleteTopic): array
  76. {
  77. return [
  78. 'action' => $this->c->Router->link(
  79. 'DeletePost',
  80. [
  81. 'id' => $post->id,
  82. ]
  83. ),
  84. 'hidden' => [
  85. 'token' => $this->c->Csrf->create(
  86. 'DeletePost',
  87. [
  88. 'id' => $post->id,
  89. ]
  90. ),
  91. ],
  92. 'sets' => [
  93. 'info' => [
  94. 'info' => [
  95. [
  96. 'value' => __(['Topic %s', $post->parent->name]),
  97. 'html' => true,
  98. ],
  99. [
  100. 'value' => __([$deleteTopic ? 'Topic by' : 'Reply by', $post->poster, dt($post->posted)]),
  101. 'html' => true,
  102. ],
  103. ],
  104. ],
  105. 'confirm' => [
  106. 'fields' => [
  107. 'confirm' => [
  108. 'type' => 'checkbox',
  109. 'label' => $deleteTopic ? 'Confirm delete topic' : 'Confirm delete post',
  110. 'checked' => false,
  111. ],
  112. ],
  113. ],
  114. ],
  115. 'btns' => [
  116. 'delete' => [
  117. 'type' => 'submit',
  118. 'value' => __($deleteTopic ? 'Delete topic' : 'Delete post'),
  119. ],
  120. 'cancel' => [
  121. 'type' => 'btn',
  122. 'value' => __('Cancel'),
  123. 'link' => $this->c->Router->link('ViewPost', $args),
  124. ],
  125. ],
  126. ];
  127. }
  128. }