PostFormTrait.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\Model;
  11. use function \ForkBB\__;
  12. trait PostFormTrait
  13. {
  14. /**
  15. * Возвращает данные для построения формы создания темы/сообщения
  16. */
  17. protected function messageForm(?Model $model, string $marker, array $args, bool $edit, bool $first, bool $quick): array
  18. {
  19. $vars = $args['_vars'] ?? null;
  20. unset($args['_vars']);
  21. $notPM = $this->fIndex !== self::FI_PM;
  22. $autofocus = $quick ? null : true;
  23. $form = [
  24. 'action' => $this->c->Router->link($marker, $args),
  25. 'hidden' => [
  26. 'token' => $this->c->Csrf->create($marker, $args),
  27. ],
  28. 'sets' => [],
  29. 'btns' => [
  30. 'submit' => [
  31. 'type' => 'submit',
  32. 'value' => __('NewTopic' === $marker ? 'Create topic' : 'Submit'),
  33. ],
  34. 'preview' => [
  35. 'type' => 'submit',
  36. 'value' => __('Preview'),
  37. 'class' => ['f-opacity'],
  38. ],
  39. ],
  40. ];
  41. $fieldset = [];
  42. if ($this->user->isGuest) {
  43. $fieldset['username'] = [
  44. 'class' => ['w1'],
  45. 'type' => 'text',
  46. 'maxlength' => '25',
  47. 'caption' => 'Username',
  48. 'required' => true,
  49. 'pattern' => '^.{2,25}$',
  50. 'value' => $vars['username'] ?? null,
  51. 'autofocus' => $autofocus,
  52. ];
  53. $fieldset['email'] = [
  54. 'class' => ['w2'],
  55. 'type' => 'text',
  56. 'maxlength' => '80',
  57. 'caption' => 'Email',
  58. 'required' => 1 === $this->c->config->b_force_guest_email,
  59. 'pattern' => '.+@.+',
  60. 'value' => $vars['email'] ?? null,
  61. ];
  62. $autofocus = null;
  63. }
  64. if ($first) {
  65. $fieldset['subject'] = [
  66. 'class' => ['w0'],
  67. 'type' => 'text',
  68. 'maxlength' => '70',
  69. 'caption' => 'Subject',
  70. 'required' => true,
  71. 'value' => $vars['subject'] ?? null,
  72. 'autofocus' => $autofocus,
  73. ];
  74. $autofocus = null;
  75. }
  76. $fieldset['message'] = [
  77. 'class' => ['w0'],
  78. 'type' => 'textarea',
  79. 'caption' => 'Message',
  80. 'required' => true,
  81. 'value' => $vars['message'] ?? null,
  82. 'autofocus' => $autofocus,
  83. ];
  84. $form['sets']['uesm'] = [
  85. 'fields' => $fieldset,
  86. ];
  87. $autofocus = null;
  88. $fieldset = [];
  89. if ($notPM) {
  90. if (
  91. $this->user->isAdmin
  92. || $this->user->isModerator($model)
  93. ) {
  94. if ($first) {
  95. $fieldset['stick_topic'] = [
  96. 'type' => 'checkbox',
  97. 'label' => 'Stick topic',
  98. 'checked' => (bool) ($vars['stick_topic'] ?? false),
  99. ];
  100. $fieldset['stick_fp'] = [
  101. 'type' => 'checkbox',
  102. 'label' => 'Stick first post',
  103. 'checked' => (bool) ($vars['stick_fp'] ?? false),
  104. ];
  105. } elseif (! $edit) {
  106. $fieldset['merge_post'] = [
  107. 'type' => 'checkbox',
  108. 'label' => 'Merge posts',
  109. 'checked' => (bool) ($vars['merge_post'] ?? true),
  110. ];
  111. }
  112. if (
  113. $edit
  114. && ! $model->user->isGuest
  115. && ! $model->user->isAdmin
  116. ) {
  117. $fieldset['edit_post'] = [
  118. 'type' => 'checkbox',
  119. 'label' => 'EditPost edit',
  120. 'checked' => (bool) ($vars['edit_post'] ?? false),
  121. ];
  122. }
  123. }
  124. if (
  125. ! $edit
  126. && 1 === $this->c->config->b_topic_subscriptions
  127. && $this->user->email_confirmed
  128. ) {
  129. $subscribed = ! $first && $model->is_subscribed;
  130. if ($quick) {
  131. if (
  132. $subscribed
  133. || $this->user->auto_notify
  134. ) {
  135. $form['hidden']['subscribe'] = '1';
  136. }
  137. } else {
  138. $fieldset['subscribe'] = [
  139. 'type' => 'checkbox',
  140. 'label' => $subscribed ? 'Stay subscribed' : 'New subscribe',
  141. 'checked' => (bool) ($vars['subscribe'] ?? ($subscribed || $this->user->auto_notify)),
  142. ];
  143. }
  144. }
  145. }
  146. if (
  147. ! $quick
  148. && 1 === $this->c->config->b_smilies
  149. ) {
  150. $fieldset['hide_smilies'] = [
  151. 'type' => 'checkbox',
  152. 'label' => 'Hide smilies',
  153. 'checked' => (bool) ($vars['hide_smilies'] ?? false),
  154. ];
  155. }
  156. if ($fieldset) {
  157. $form['sets']['uesm-options'] = [
  158. 'legend' => 'Options',
  159. 'fields' => $fieldset,
  160. ];
  161. }
  162. if (
  163. $first
  164. && $notPM
  165. && $this->user->usePoll
  166. ) {
  167. $term = $edit && $model->parent->poll_term
  168. ? $model->parent->poll_term
  169. : $this->c->config->i_poll_term;
  170. $fieldset = [];
  171. $fieldset['poll_enable'] = [
  172. 'type' => 'checkbox',
  173. 'label' => 'Include poll',
  174. 'checked' => (bool) ($vars['poll_enable'] ?? false),
  175. 'disabled' => $vars['pollNoEdit'] ?? null,
  176. ];
  177. $fieldset["poll[duration]"] = [
  178. 'type' => 'number',
  179. 'min' => '0',
  180. 'max' => '366',
  181. 'value' => $vars['poll']['duration'] ?? 0,
  182. 'caption' => 'Poll duration label',
  183. 'help' => 'Poll duration help',
  184. 'disabled' => $vars['pollNoEdit'] ?? null,
  185. ];
  186. $fieldset['poll[hide_result]'] = [
  187. 'type' => 'checkbox',
  188. 'label' => ['Hide poll results up to %s voters', $term],
  189. 'checked' => (bool) ($vars['poll']['hide_result'] ?? false),
  190. 'disabled' => $vars['pollNoEdit'] ?? null,
  191. ];
  192. $form['sets']['uesm-poll'] = [
  193. 'legend' => 'Poll legend',
  194. 'fields' => $fieldset,
  195. ];
  196. for ($qid = 1; $qid <= $this->c->config->i_poll_max_questions; $qid++) {
  197. $fieldset = [];
  198. $fieldset["poll[question][{$qid}]"] = [
  199. 'type' => 'text',
  200. 'maxlength' => '240',
  201. 'caption' => 'Question text label',
  202. 'value' => $vars['poll']['question'][$qid] ?? null,
  203. 'disabled' => $vars['pollNoEdit'] ?? null,
  204. ];
  205. $fieldset["poll[type][{$qid}]"] = [
  206. 'type' => 'number',
  207. 'min' => '1',
  208. 'max' => (string) $this->c->config->i_poll_max_fields,
  209. 'value' => $vars['poll']['type'][$qid] ?? 1,
  210. 'caption' => 'Answer type label',
  211. 'help' => 'Answer type help',
  212. 'disabled' => $vars['pollNoEdit'] ?? null,
  213. ];
  214. for ($fid = 1; $fid <= $this->c->config->i_poll_max_fields; $fid++) {
  215. $fieldset["poll[answer][{$qid}][{$fid}]"] = [
  216. 'type' => 'text',
  217. 'maxlength' => '240',
  218. 'caption' => ['Answer %s label', $fid],
  219. 'value' => $vars['poll']['answer'][$qid][$fid] ?? null,
  220. 'disabled' => $vars['pollNoEdit'] ?? null,
  221. ];
  222. }
  223. $form['sets']["uesm-q-{$qid}"] = [
  224. 'legend' => ['Question %s legend', $qid],
  225. 'fields' => $fieldset,
  226. ];
  227. }
  228. $this->pageHeader('pollJS', 'script', 9000, [
  229. 'src' => $this->publicLink('/js/poll.js'),
  230. ]);
  231. }
  232. return $form;
  233. }
  234. }