Преглед изворни кода

Change post form for messages

Visman пре 4 година
родитељ
комит
12b0432f02

+ 15 - 15
app/Models/Pages/Edit.php

@@ -36,8 +36,8 @@ class Edit extends Page
             return $this->c->Message->message('Bad request');
         }
 
-        $topic       = $post->parent;
-        $editSubject = $post->id === $topic->first_post_id;
+        $topic     = $post->parent;
+        $firstPost = $post->id === $topic->first_post_id;
 
         $this->c->Lang->load('post');
 
@@ -46,7 +46,7 @@ class Edit extends Page
         }
 
         if ('POST' === $method) {
-            $v = $this->messageValidator($post, 'EditPost', $args, true, $editSubject);
+            $v = $this->messageValidator($post, 'EditPost', $args, true, $firstPost);
 
             if (
                 $v->validation($_POST)
@@ -68,7 +68,7 @@ class Edit extends Page
                 );
 
                 if (
-                    $editSubject
+                    $firstPost
                     && $this->user->usePoll
                     && $v->poll_enable
                 ) {
@@ -88,7 +88,7 @@ class Edit extends Page
         }
 
         if (
-            $editSubject
+            $firstPost
             && $this->user->usePoll
         ) {
             if (
@@ -124,9 +124,9 @@ class Edit extends Page
         $this->onlinePos = 'topic-' . $topic->id;
         $this->canonical = $post->linkEdit;
         $this->robots    = 'noindex';
-        $this->formTitle = $editSubject ? __('Edit topic') : __('Edit post');
+        $this->formTitle = $firstPost ? __('Edit topic') : __('Edit post');
         $this->crumbs    = $this->crumbs($this->formTitle, $topic);
-        $this->form      = $this->messageForm($args, $post, 'EditPost', true, $editSubject);
+        $this->form      = $this->messageForm($post, 'EditPost', $args, true, $firstPost, false);
 
         return $this;
     }
@@ -136,13 +136,13 @@ class Edit extends Page
      */
     protected function endEdit(Post $post, Validator $v): Page
     {
-        $now         = \time();
-        $executive   = $this->user->isAdmin || $this->user->isModerator($post);
-        $topic       = $post->parent;
-        $editSubject = $post->id === $topic->first_post_id;
-        $calcPost    = false;
-        $calcTopic   = false;
-        $calcForum   = false;
+        $now       = \time();
+        $executive = $this->user->isAdmin || $this->user->isModerator($post);
+        $topic     = $post->parent;
+        $firstPost = $post->id === $topic->first_post_id;
+        $calcPost  = false;
+        $calcTopic = false;
+        $calcForum = false;
 
         // текст сообщения
         if ($post->message !== $v->message) {
@@ -171,7 +171,7 @@ class Edit extends Page
             $post->edit_post     = $v->edit_post ? 1 : 0;
         }
 
-        if ($editSubject) {
+        if ($firstPost) {
             // заголовок темы
             if ($topic->subject !== $v->subject) {
                 $topic->subject  = $v->subject;

+ 3 - 3
app/Models/Pages/Post.php

@@ -90,7 +90,7 @@ class Post extends Page
         $this->robots    = 'noindex';
         $this->crumbs    = $this->crumbs(__('Post new topic'), $forum);
         $this->formTitle = __('Post new topic');
-        $this->form      = $this->messageForm($args, $forum, 'NewTopic', false, true);
+        $this->form      = $this->messageForm($forum, 'NewTopic', $args, false, true, false);
 
         return $this;
     }
@@ -114,7 +114,7 @@ class Post extends Page
         $this->onlinePos = 'topic-' . $topic->id;
 
         if ('POST' === $method) {
-            $v = $this->messageValidator($topic, 'NewReply', $args);
+            $v = $this->messageValidator($topic, 'NewReply', $args, false, false);
 
             if ($this->user->isGuest) {
                 $v = $this->c->Test->beforeValidation($v);
@@ -160,7 +160,7 @@ class Post extends Page
         $this->robots     = 'noindex';
         $this->crumbs     = $this->crumbs(__('Post a reply'), $topic);
         $this->formTitle  = __('Post a reply');
-        $this->form       = $this->messageForm($args, $topic, 'NewReply');
+        $this->form       = $this->messageForm($topic, 'NewReply', $args, false, false, false);
         $this->postsTitle = __('Topic review');
         $this->posts      = $topic->review();
 

+ 70 - 64
app/Models/Pages/PostFormTrait.php

@@ -18,13 +18,15 @@ trait PostFormTrait
     /**
      * Возвращает данные для построения формы создания темы/сообщения
      */
-    protected function messageForm(array $args, Model $model, string $marker, bool $editPost = false, bool $editSubject = false, bool $quickReply = false): array
+    protected function messageForm(Model $model, string $marker, array $args, bool $edit, bool $first, bool $quick): array
     {
         $vars = $args['_vars'] ?? null;
+
         unset($args['_vars']);
 
-        $autofocus = $quickReply ? null : true;
-        $form = [
+        $notPM     = $this->fIndex !== self::FI_PM;
+        $autofocus = $quick ? null : true;
+        $form      = [
             'action' => $this->c->Router->link($marker, $args),
             'hidden' => [
                 'token' => $this->c->Csrf->create($marker, $args),
@@ -44,6 +46,7 @@ trait PostFormTrait
         ];
 
         $fieldset = [];
+
         if ($this->user->isGuest) {
             $fieldset['username'] = [
                 'class'     => 'w1',
@@ -67,7 +70,7 @@ trait PostFormTrait
             $autofocus = null;
         }
 
-        if ($editSubject) {
+        if ($first) {
             $fieldset['subject'] = [
                 'class'     => 'w0',
                 'type'      => 'text',
@@ -99,75 +102,78 @@ trait PostFormTrait
         $form['sets']['uesm'] = [
             'fields' => $fieldset,
         ];
-        $autofocus = null;
 
-        $fieldset = [];
-        if (
-            $this->user->isAdmin
-            || $this->user->isModerator($model)
-        ) {
-            if ($editSubject) {
-                $fieldset['stick_topic'] = [
-                    'type'    => 'checkbox',
-                    'label'   => __('Stick topic'),
-                    'value'   => '1',
-                    'checked' => (bool) ($vars['stick_topic'] ?? false),
-                ];
-                $fieldset['stick_fp'] = [
-                    'type'    => 'checkbox',
-                    'label'   => __('Stick first post'),
-                    'value'   => '1',
-                    'checked' => (bool) ($vars['stick_fp'] ?? false),
-                ];
-            } elseif (! $editPost) {
-                $fieldset['merge_post'] = [
-                    'type'    => 'checkbox',
-                    'label'   => __('Merge posts'),
-                    'value'   => '1',
-                    'checked' => (bool) ($vars['merge_post'] ?? true),
-                ];
-            }
+        $autofocus = null;
+        $fieldset  = [];
 
+        if ($notPM) {
             if (
-                $editPost
-                && ! $model->user->isGuest
-                && ! $model->user->isAdmin
+                $this->user->isAdmin
+                || $this->user->isModerator($model)
             ) {
-                $fieldset['edit_post'] = [
-                    'type'    => 'checkbox',
-                    'label'   => __('EditPost edit'),
-                    'value'   => '1',
-                    'checked' => (bool) ($vars['edit_post'] ?? false),
-                ];
-            }
-        }
-
-        if (
-            ! $editPost
-            && '1' == $this->c->config->o_topic_subscriptions
-            && $this->user->email_confirmed
-        ) {
-            $subscribed = ! $editSubject && $model->is_subscribed;
+                if ($first) {
+                    $fieldset['stick_topic'] = [
+                        'type'    => 'checkbox',
+                        'label'   => __('Stick topic'),
+                        'value'   => '1',
+                        'checked' => (bool) ($vars['stick_topic'] ?? false),
+                    ];
+                    $fieldset['stick_fp'] = [
+                        'type'    => 'checkbox',
+                        'label'   => __('Stick first post'),
+                        'value'   => '1',
+                        'checked' => (bool) ($vars['stick_fp'] ?? false),
+                    ];
+                } elseif (! $edit) {
+                    $fieldset['merge_post'] = [
+                        'type'    => 'checkbox',
+                        'label'   => __('Merge posts'),
+                        'value'   => '1',
+                        'checked' => (bool) ($vars['merge_post'] ?? true),
+                    ];
+                }
 
-            if ($quickReply) {
                 if (
-                    $subscribed
-                    || $this->user->auto_notify
+                    $edit
+                    && ! $model->user->isGuest
+                    && ! $model->user->isAdmin
                 ) {
-                    $form['hidden']['subscribe'] = '1';
+                    $fieldset['edit_post'] = [
+                        'type'    => 'checkbox',
+                        'label'   => __('EditPost edit'),
+                        'value'   => '1',
+                        'checked' => (bool) ($vars['edit_post'] ?? false),
+                    ];
+                }
+            }
+
+            if (
+                ! $edit
+                && '1' == $this->c->config->o_topic_subscriptions
+                && $this->user->email_confirmed
+            ) {
+                $subscribed = ! $first && $model->is_subscribed;
+
+                if ($quick) {
+                    if (
+                        $subscribed
+                        || $this->user->auto_notify
+                    ) {
+                        $form['hidden']['subscribe'] = '1';
+                    }
+                } else {
+                    $fieldset['subscribe'] = [
+                        'type'    => 'checkbox',
+                        'label'   => $subscribed ? __('Stay subscribed') : __('New subscribe'),
+                        'value'   => '1',
+                        'checked' => (bool) ($vars['subscribe'] ?? ($subscribed || $this->user->auto_notify)),
+                    ];
                 }
-            } else {
-                $fieldset['subscribe'] = [
-                    'type'    => 'checkbox',
-                    'label'   => $subscribed ? __('Stay subscribed') : __('New subscribe'),
-                    'value'   => '1',
-                    'checked' => (bool) ($vars['subscribe'] ?? ($subscribed || $this->user->auto_notify)),
-                ];
             }
         }
 
         if (
-            ! $quickReply
+            ! $quick
             && '1' == $this->c->config->o_smilies
         ) {
             $fieldset['hide_smilies'] = [
@@ -186,10 +192,11 @@ trait PostFormTrait
         }
 
         if (
-            $editSubject
+            $first
+            && $notPM
             && $this->user->usePoll
         ) {
-            $term = $editPost && $model->parent->poll_term
+            $term = $edit && $model->parent->poll_term
                 ? $model->parent->poll_term
                 : $this->c->config->i_poll_term;
 
@@ -263,7 +270,6 @@ trait PostFormTrait
             $this->pageHeader('pollJS', 'script', 9000, [
                 'src' => $this->publicLink('/js/poll.js'),
             ]);
-
         }
 
         return $form;

+ 17 - 10
app/Models/Pages/PostValidatorTrait.php

@@ -98,10 +98,12 @@ trait PostValidatorTrait
     /**
      * Подготовка валидатора к проверке данных из формы создания темы/сообщения
      */
-    protected function messageValidator(Model $model, string $marker, array $args, bool $editPost = false, bool $editSubject = false): Validator
+    protected function messageValidator(Model $model, string $marker, array $args, bool $edit, bool $first): Validator
     {
         $this->c->Lang->load('validator');
 
+        $notPM = $this->fIndex !== self::FI_PM;
+
         if ($this->user->isGuest) {
             $ruleEmail    = ('1' == $this->c->config->p_force_guest_email ? 'required|' : '') . 'string:trim|email:noban';
             $ruleUsername = 'required|string:trim|username';
@@ -111,10 +113,13 @@ trait PostValidatorTrait
         }
 
         if (
-            $this->user->isAdmin
-            || $this->user->isModerator($model)
+            $notPM
+            && (
+                $this->user->isAdmin
+                || $this->user->isModerator($model)
+            )
         ) {
-            if ($editSubject) {
+            if ($first) {
                 $ruleStickTopic = 'checkbox';
                 $ruleStickFP    = 'checkbox';
             } else {
@@ -122,15 +127,15 @@ trait PostValidatorTrait
                 $ruleStickFP    = 'absent';
             }
             if (
-                ! $editSubject
-                && ! $editPost
+                ! $first
+                && ! $edit
             ) {
                 $ruleMergePost  = 'checkbox';
             } else {
                 $ruleMergePost  = 'absent';
             }
             if (
-                $editPost
+                $edit
                 && ! $model->user->isGuest
                 && ! $model->user->isAdmin
             ) {
@@ -147,14 +152,15 @@ trait PostValidatorTrait
             $executive          = false;
         }
 
-        if ($editSubject) {
+        if ($first) {
             $ruleSubject = 'required|string:trim,spaces|min:1|max:70|' . ($executive ? '' : 'noURL|') . 'check_subject';
         } else {
             $ruleSubject = 'absent';
         }
 
         if (
-            ! $editPost
+            ! $edit
+            && $notPM
             && '1' == $this->c->config->o_topic_subscriptions
             && $this->user->email_confirmed
         ) {
@@ -202,7 +208,8 @@ trait PostValidatorTrait
             ]);
 
         if (
-            $editSubject
+            $first
+            && $notPM
             && $this->user->usePoll
         ) {
             $v->addValidators([

+ 1 - 1
app/Models/Pages/Topic.php

@@ -147,7 +147,7 @@ class Topic extends Page
             $topic->canReply
             && '1' == $this->c->config->o_quickpost
         ) {
-            $this->form     = $this->messageForm(['id' => $topic->id], $topic, 'NewReply', false, false, true);
+            $this->form     = $this->messageForm($topic, 'NewReply', ['id' => $topic->id], false, false, true);
         }
 
         if (