PostFormTrait.php 9.6 KB

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