PostFormTrait.php 9.5 KB

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