PostFormTrait.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace ForkBB\Models\Pages;
  3. use ForkBB\Models\Model;
  4. use function \ForkBB\__;
  5. trait PostFormTrait
  6. {
  7. /**
  8. * Возвращает данные для построения формы создания темы/сообщения
  9. *
  10. * @param array $args
  11. * @param Model $model
  12. * @param string $marker
  13. * @param bool $editPost
  14. * @param bool $editSubject
  15. * @param bool $quickReply
  16. *
  17. * @return array
  18. */
  19. protected function messageForm(array $args, Model $model, string $marker, bool $editPost = false, bool $editSubject = false, bool $quickReply = false): array
  20. {
  21. $vars = $args['_vars'] ?? null;
  22. unset($args['_vars']);
  23. $autofocus = $quickReply ? null : true;
  24. $form = [
  25. 'action' => $this->c->Router->link($marker, $args),
  26. 'hidden' => [
  27. 'token' => $this->c->Csrf->create($marker, $args),
  28. ],
  29. 'sets' => [],
  30. 'btns' => [
  31. 'submit' => [
  32. 'type' => 'submit',
  33. 'value' => __('Submit'),
  34. 'accesskey' => 's',
  35. ],
  36. 'preview' => [
  37. 'type' => 'submit',
  38. 'value' => __('Preview'),
  39. 'accesskey' => 'p',
  40. 'class' => 'f-minor',
  41. ],
  42. ],
  43. ];
  44. $fieldset = [];
  45. if ($this->user->isGuest) {
  46. $fieldset['username'] = [
  47. 'class' => 'w1',
  48. 'type' => 'text',
  49. 'maxlength' => 25,
  50. 'caption' => __('Username'),
  51. 'required' => true,
  52. 'pattern' => '^.{2,25}$',
  53. 'value' => $vars['username'] ?? null,
  54. 'autofocus' => $autofocus,
  55. ];
  56. $fieldset['email'] = [
  57. 'class' => 'w2',
  58. 'type' => 'text',
  59. 'maxlength' => 80,
  60. 'caption' => __('Email'),
  61. 'required' => '1' == $this->c->config->p_force_guest_email,
  62. 'pattern' => '.+@.+',
  63. 'value' => $vars['email'] ?? null,
  64. ];
  65. $autofocus = null;
  66. }
  67. if ($editSubject) {
  68. $fieldset['subject'] = [
  69. 'class' => 'w0',
  70. 'type' => 'text',
  71. 'maxlength' => 70,
  72. 'caption' => __('Subject'),
  73. 'required' => true,
  74. 'value' => $vars['subject'] ?? null,
  75. 'autofocus' => $autofocus,
  76. ];
  77. $autofocus = null;
  78. }
  79. $fieldset['message'] = [
  80. 'class' => 'w0',
  81. 'type' => 'textarea',
  82. 'caption' => __('Message'),
  83. 'required' => true,
  84. 'value' => $vars['message'] ?? null,
  85. 'bb' => [
  86. ['link', __('BBCode'), __('1' == $this->c->config->p_message_bbcode ? 'on' : 'off')],
  87. ['link', __('url tag'), __('1' == $this->c->config->p_message_bbcode && '1' == $this->user->g_post_links ? 'on' : 'off')],
  88. ['link', __('img tag'), __('1' == $this->c->config->p_message_bbcode && '1' == $this->c->config->p_message_img_tag ? 'on' : 'off')],
  89. ['link', __('Smilies'), __('1' == $this->c->config->o_smilies ? 'on' : 'off')],
  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. ! $quickReply
  138. && '1' == $this->c->config->o_smilies
  139. ) {
  140. $fieldset['hide_smilies'] = [
  141. 'type' => 'checkbox',
  142. 'label' => __('Hide smilies'),
  143. 'value' => '1',
  144. 'checked' => isset($vars['hide_smilies']) ? (bool) $vars['hide_smilies'] : false,
  145. ];
  146. }
  147. if ($fieldset) {
  148. $form['sets']['sett'] = [
  149. 'legend' => __('Options'),
  150. 'fields' => $fieldset,
  151. ];
  152. }
  153. return $form;
  154. }
  155. }