PostFormTrait.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. declare(strict_types=1);
  3. namespace ForkBB\Models\Pages;
  4. use ForkBB\Models\Model;
  5. use function \ForkBB\__;
  6. trait PostFormTrait
  7. {
  8. /**
  9. * Возвращает данные для построения формы создания темы/сообщения
  10. */
  11. protected function messageForm(array $args, Model $model, string $marker, bool $editPost = false, bool $editSubject = false, bool $quickReply = false): array
  12. {
  13. $vars = $args['_vars'] ?? null;
  14. unset($args['_vars']);
  15. $autofocus = $quickReply ? null : true;
  16. $form = [
  17. 'action' => $this->c->Router->link(
  18. $marker,
  19. $args
  20. ),
  21. 'hidden' => [
  22. 'token' => $this->c->Csrf->create(
  23. $marker,
  24. $args
  25. ),
  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' => isset($vars['stick_topic']) ? (bool) $vars['stick_topic'] : false,
  108. ];
  109. $fieldset['stick_fp'] = [
  110. 'type' => 'checkbox',
  111. 'label' => __('Stick first post'),
  112. 'value' => '1',
  113. 'checked' => isset($vars['stick_fp']) ? (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' => isset($vars['merge_post']) ? (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' => isset($vars['edit_post']) ? (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' => isset($vars['subscribe'])
  155. ? (bool) $vars['subscribe']
  156. : ($subscribed || $this->user->auto_notify),
  157. ];
  158. }
  159. }
  160. if (
  161. ! $quickReply
  162. && '1' == $this->c->config->o_smilies
  163. ) {
  164. $fieldset['hide_smilies'] = [
  165. 'type' => 'checkbox',
  166. 'label' => __('Hide smilies'),
  167. 'value' => '1',
  168. 'checked' => isset($vars['hide_smilies']) ? (bool) $vars['hide_smilies'] : false,
  169. ];
  170. }
  171. if ($fieldset) {
  172. $form['sets']['sett'] = [
  173. 'legend' => __('Options'),
  174. 'fields' => $fieldset,
  175. ];
  176. }
  177. return $form;
  178. }
  179. }