PMView.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. class PMView extends AbstractPM
  21. {
  22. /**
  23. * Списки новых, текущих и архивных приватных топиков
  24. */
  25. public function view(array $args, string $method): Page
  26. {
  27. $this->args = $args;
  28. $this->pms->page = $args['more1'] ?? 1;
  29. if (
  30. isset($args['more2'])
  31. || ! $this->pms->hasPage()
  32. ) {
  33. return $this->c->Message->message('Not Found', true, 404);
  34. }
  35. $this->pmIndex = $this->pms->area;
  36. if ('POST' === $method) {
  37. $this->c->Lang->load('validator');
  38. $v = $this->c->Validator->reset()
  39. ->addValidators([
  40. 'action_process' => [$this, 'vActionProcess'],
  41. ])->addRules([
  42. 'token' => 'token:PMAction',
  43. 'ids' => 'required|array',
  44. 'ids.*' => 'required|integer|min:1|max:9999999999',
  45. 'confirm' => 'integer',
  46. Cnst::ACTION_ARCHIVE => 'string',
  47. Cnst::ACTION_DELETE => 'string',
  48. 'action' => 'action_process',
  49. ])->addAliases([
  50. ])->addArguments([
  51. 'token' => $this->args,
  52. ])->addMessages([
  53. 'ids' => 'No dialogs',
  54. ]);
  55. if ($v->validation($_POST)) {
  56. if (null === $v->action) {
  57. $this->nameTpl = 'pm/form';
  58. $this->form = $this->formConfirm($v, $this->args);
  59. $this->formClass = 'post';
  60. $this->formTitle = Cnst::PT_ARCHIVE === $this->vStatus ? 'InfoSaveQt' : 'InfoDeleteQt';
  61. $this->pmCrumbs[] = Cnst::PT_ARCHIVE === $this->vStatus ? 'InfoSaveQ' : 'InfoDeleteQ';
  62. return $this;
  63. } elseif (1 !== $v->confirm) {
  64. return $this->c->Redirect->page('PMAction', $this->args)->message('No confirm redirect');
  65. } else {
  66. $topics = $this->pms->loadByIds(Cnst::PTOPIC, $v->ids);
  67. foreach ($topics as $topic) {
  68. $topic->status = $this->vStatus;
  69. }
  70. // удаление (при $topic->isFullDeleted) или обновление диалогов и пользователя
  71. $this->pms->delete(...$topics); // ????
  72. if (Cnst::PT_ARCHIVE === $this->vStatus) {
  73. $message = 'Dialogues moved to archive redirect';
  74. $args['action'] = Cnst::ACTION_ARCHIVE;
  75. } else {
  76. $message = 'Dialogues deleted redirect';
  77. unset($args['second']);
  78. }
  79. unset($args['more1']);
  80. return $this->c->Redirect->page('PMAction', $args)->message($message);
  81. }
  82. }
  83. $this->fIswev = $v->getErrors();
  84. }
  85. $this->nameTpl = 'pm/view';
  86. $this->pmList = $this->pms->pmListCurPage();
  87. $this->pagination = $this->pms->pagination;
  88. if ($this->pmList) {
  89. $this->form = $this->form($this->args);
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Определяет действие
  95. */
  96. public function vActionProcess(Validator $v, $action)
  97. {
  98. if (! empty($v->getErrors())) {
  99. return $action;
  100. }
  101. if (! empty($v->{Cnst::ACTION_ARCHIVE}) ) {
  102. $this->vStatus = Cnst::PT_ARCHIVE;
  103. if ($this->user->g_pm_limit > 0) {
  104. if ($this->pms->totalArchive >= $this->user->g_pm_limit) {
  105. $v->addError('Archive is full');
  106. return $action;
  107. } elseif ($this->pms->totalArchive + \count($v->ids) > $this->user->g_pm_limit) {
  108. $v->addError('Cannot be moved');
  109. return $action;
  110. }
  111. }
  112. } elseif (! empty($v->{Cnst::ACTION_DELETE})) {
  113. $this->vStatus = Cnst::PT_DELETED;
  114. } else {
  115. $v->addError('Unknown action selected');
  116. return $action;
  117. }
  118. foreach ($v->ids as $id) {
  119. if (! $this->pms->accessTopic($id)) {
  120. $v->addError(['Dialogue %s is not yours', $id]);
  121. return $action;
  122. }
  123. }
  124. return $action;
  125. }
  126. /**
  127. * Создает массив данных для формы подтверждения
  128. */
  129. protected function formConfirm(Validator $v, array $args): array
  130. {
  131. $headers = [];
  132. foreach ($this->pms->loadByIds(Cnst::PTOPIC, $v->ids) as $topic) {
  133. $headers[] = __(['Dialogue %s', $topic->name]);
  134. }
  135. $btn = Cnst::PT_ARCHIVE === $this->vStatus ? Cnst::ACTION_ARCHIVE : Cnst::ACTION_DELETE;
  136. $form = [
  137. 'action' => $this->c->Router->link('PMAction', $args),
  138. 'hidden' => [
  139. 'token' => $this->c->Csrf->create('PMAction', $args),
  140. 'ids' => $v->ids,
  141. 'action' => $v->{$btn},
  142. ],
  143. 'sets' => [
  144. 'info' => [
  145. 'info' => [
  146. [
  147. 'value' => \implode('<br>', $headers),
  148. 'html' => true,
  149. ],
  150. ],
  151. ],
  152. 'action' => [
  153. 'fields' => [
  154. 'confirm' => [
  155. 'type' => 'checkbox',
  156. 'label' => 'Confirm action',
  157. 'checked' => false,
  158. ],
  159. ],
  160. ],
  161. ],
  162. 'btns' => [
  163. $btn => [
  164. 'type' => 'submit',
  165. 'value' => __($v->{$btn}),
  166. ],
  167. 'cancel' => [
  168. 'type' => 'btn',
  169. 'value' => __('Cancel'),
  170. 'link' => $this->c->Router->link('PMAction', $args),
  171. ],
  172. ],
  173. ];
  174. return $form;
  175. }
  176. /**
  177. * Создает массив данных для формы удалени/переноса в архив
  178. */
  179. protected function form(array $args): array
  180. {
  181. $form = [
  182. 'id' => 'id-form-pmview',
  183. 'action' => $this->c->Router->link('PMAction', $args),
  184. 'hidden' => [
  185. 'token' => $this->c->Csrf->create('PMAction', $args),
  186. ],
  187. 'sets' => [],
  188. 'btns' => [],
  189. ];
  190. if (Cnst::ACTION_ARCHIVE !== $this->pms->area) {
  191. $form['btns'][Cnst::ACTION_ARCHIVE] = [
  192. 'class' => ['origin'],
  193. 'type' => 'submit',
  194. 'value' => __('To archive'),
  195. ];
  196. }
  197. $form['btns'][Cnst::ACTION_DELETE] = [
  198. 'class' => ['origin'],
  199. 'type' => 'submit',
  200. 'value' => __('Delete'),
  201. ];
  202. return $form;
  203. }
  204. }