PMDelete.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\PM;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\PM\AbstractPM;
  13. use ForkBB\Models\Pages\PostFormTrait;
  14. use ForkBB\Models\Pages\PostValidatorTrait;
  15. use ForkBB\Models\PM\Cnst;
  16. use ForkBB\Models\PM\PPost;
  17. use ForkBB\Models\PM\PTopic;
  18. use InvalidArgumentException;
  19. use function \ForkBB\__;
  20. use function \ForkBB\dt;
  21. class PMDelete extends AbstractPM
  22. {
  23. /**
  24. * Удаление сообщения/темы
  25. */
  26. public function delete(array $args, string $method): Page
  27. {
  28. switch ($args['more2']) {
  29. case Cnst::ACTION_TOPIC:
  30. $deleteTopic = true;
  31. $topic = $this->pms->load(Cnst::PTOPIC, $args['more1']);
  32. if (! $topic instanceof PTopic) {
  33. return $this->c->Message->message('Bad request');
  34. }
  35. $post = $this->pms->load(Cnst::PPOST, $topic->first_post_id);
  36. break;
  37. case Cnst::ACTION_POST:
  38. $deleteTopic = false;
  39. $post = $this->pms->load(Cnst::PPOST, $args['more1']);
  40. if (
  41. ! $post instanceof PPost
  42. || ! $post->canDelete
  43. ) {
  44. return $this->c->Message->message('Bad request');
  45. }
  46. $topic = $post->parent;
  47. break;
  48. default:
  49. return $this->c->Message->message('Bad request');
  50. }
  51. $this->c->Lang->load('validator');
  52. $this->pms->area = $this->pms->inArea($topic);
  53. if ('POST' === $method) {
  54. $v = $this->c->Validator->reset()
  55. ->addRules([
  56. 'token' => 'token:PMAction',
  57. 'confirm' => 'checkbox',
  58. 'delete' => 'required|string',
  59. ])->addAliases([
  60. ])->addArguments([
  61. 'token' => $args,
  62. ]);
  63. if (
  64. ! $v->validation($_POST)
  65. || '1' !== $v->confirm
  66. ) {
  67. return $this->c->Redirect->url($post->link)->message('No confirm redirect');
  68. }
  69. if ($deleteTopic) {
  70. if ($this->pms->numCurrent + $this->pms->numArchive > 1) {
  71. $second = $this->pms->second;
  72. } else {
  73. $second = null;
  74. }
  75. $redirect = $this->c->Redirect
  76. ->page('PMAction', ['second' => $second, 'action' => $this->pms->area])
  77. ->message('Dialogue del redirect');
  78. $topic->status = Cnst::PT_DELETED;
  79. $this->pms->delete($topic);
  80. } else {
  81. $redirect = $this->c->Redirect
  82. ->url($post->linkPrevious)
  83. ->message('Message del redirect');
  84. $this->pms->delete($post);
  85. }
  86. return $redirect;
  87. }
  88. $this->targetUser = $topic->ztUser;
  89. $this->pmIndex = $this->pms->area;
  90. $this->nameTpl = 'pm/post';
  91. $this->formTitle = $deleteTopic ? 'Delete PT title' : 'Delete PM title';
  92. $this->form = $this->formDelete($args, $post, $deleteTopic);
  93. $this->postsTitle = $deleteTopic ? 'Delete dialogue info' : 'Delete info';
  94. $this->posts = [$post];
  95. $this->pmCrumbs[] = [
  96. $this->c->Router->link('PMAction', $args),
  97. $deleteTopic ? 'Delete dialogue' : 'Delete message',
  98. ];
  99. $this->pmCrumbs[] = $topic;
  100. return $this;
  101. }
  102. /**
  103. * Подготавливает массив данных для формы
  104. */
  105. protected function formDelete(array $args, PPost $post, bool $deleteTopic): array
  106. {
  107. return [
  108. 'action' => $this->c->Router->link('PMAction', $args),
  109. 'hidden' => [
  110. 'token' => $this->c->Csrf->create('PMAction', $args),
  111. ],
  112. 'sets' => [
  113. 'info' => [
  114. 'info' => [
  115. [
  116. 'value' => __(['Dialogue %s', $post->parent->name]),
  117. 'html' => true,
  118. ],
  119. [
  120. 'value' => __([
  121. $deleteTopic ? 'Dialogue by %1$s (%2$s)' : 'Message by %1$s (%2$s)',
  122. $post->poster,
  123. dt($post->posted)
  124. ]),
  125. 'html' => true,
  126. ],
  127. ],
  128. ],
  129. 'confirm' => [
  130. 'fields' => [
  131. 'confirm' => [
  132. 'type' => 'checkbox',
  133. 'label' => 'Confirm action',
  134. 'checked' => false,
  135. ],
  136. ],
  137. ],
  138. ],
  139. 'btns' => [
  140. 'delete' => [
  141. 'type' => 'submit',
  142. 'value' => __($deleteTopic ? 'Delete dialogue' : 'Delete message'),
  143. ],
  144. 'cancel' => [
  145. 'type' => 'btn',
  146. 'value' => __('Cancel'),
  147. 'link' => $post->link,
  148. ],
  149. ],
  150. ];
  151. }
  152. }