Jelajahi Sumber

Update Post Form/Validator for poll

Visman 4 tahun lalu
induk
melakukan
f78f197601
2 mengubah file dengan 99 tambahan dan 1 penghapusan
  1. 59 1
      app/Models/Pages/PostFormTrait.php
  2. 40 0
      app/Models/Pages/PostValidatorTrait.php

+ 59 - 1
app/Models/Pages/PostFormTrait.php

@@ -183,12 +183,70 @@ trait PostFormTrait
         }
 
         if ($fieldset) {
-            $form['sets']['sett'] = [
+            $form['sets']['uesm-options'] = [
                 'legend' => __('Options'),
                 'fields' => $fieldset,
             ];
         }
 
+        if (
+            $editSubject
+            && '1' == $this->c->config->b_poll_enabled
+        ) {
+            $fieldset = [];
+
+            $fieldset['poll_enable'] = [
+                'type'    => 'checkbox',
+                'label'   => __('Include poll'),
+                'value'   => '1',
+                'checked' => (bool) ($vars['poll_enable'] ?? false),
+            ];
+            $fieldset['poll[hide_result]'] = [
+                'type'    => 'checkbox',
+                'label'   => __('Hide poll results up to %s voters', $this->c->config->i_poll_term), // ???? при редактировании взять данные из топика?
+                'value'   => '1',
+                'checked' => (bool) ($vars['poll']['hide_result'] ?? false),
+            ];
+
+            $form['sets']['uesm-poll'] = [
+                'legend' => __('Poll legend'),
+                'fields' => $fieldset,
+            ];
+
+            for ($qid = 1; $qid <= $this->c->config->i_poll_max_questions; $qid++) {
+                $fieldset = [];
+
+                $fieldset["poll[question][{$qid}]"] = [
+                    'type'      => 'text',
+                    'maxlength' => '255',
+                    'caption'   => __('Question text label'),
+                    'value'     => $vars['poll']['question'][$qid] ?? null,
+                ];
+                $fieldset["poll[type][{$qid}]"] = [
+                    'type'    => 'number',
+                    'min'     => '1',
+                    'max'     => (string) $this->c->config->i_poll_max_fields,
+                    'value'   => $vars['poll']['type'][$qid] ?? 1,
+                    'caption' => __('Answer type label'),
+                    'info'    => __('Answer type help'),
+                ];
+
+                for ($fid = 1; $fid <= $this->c->config->i_poll_max_fields; $fid++) {
+                    $fieldset["poll[answer][{$qid}][{$fid}]"] = [
+                        'type'      => 'text',
+                        'maxlength' => '255',
+                        'caption'   => __('Answer %s label', $fid),
+                        'value'     => $vars['poll']['answer'][$qid][$fid] ?? null,
+                    ];
+                }
+
+                $form['sets']["uesm-q-{$qid}"] = [
+                    'legend' => __('Question %s legend', $qid),
+                    'fields' => $fieldset,
+                ];
+            }
+        }
+
         return $form;
     }
 }

+ 40 - 0
app/Models/Pages/PostValidatorTrait.php

@@ -195,6 +195,46 @@ trait PostValidatorTrait
                 'username.login' => 'Login format',
             ]);
 
+        if (
+            $editSubject
+            && '1' == $this->c->config->b_poll_enabled
+        ) {
+            $v->addValidators([
+                'check_poll'  => [$this, 'vCheckPoll'],
+            ])->addRules([
+                'poll_enable'      => 'checkbox|check_poll',
+                'poll.hide_result' => 'checkbox',
+                'poll.question.*'  => 'string:trim|max:255',
+                'poll.type.*'      => 'integer|min:1|max:' . $this->c->config->i_poll_max_fields,
+                'poll.answer.*.*'  => 'string:trim|max:255',
+            ]);
+        }
+
         return $v;
     }
+
+    /**
+     * Дополнительная проверка опроса
+     */
+    public function vCheckPoll(Validator $v, $enable, $attr)
+    {
+        if (
+            false !== $enable
+            && empty($v->getErrors())
+        ) {
+            $poll = $this->c->polls->create([
+                'question' => $v->poll['question'],
+                'answer'   => $v->poll['answer'],
+                'type'     => $v->poll['type'],
+            ]);
+
+            $result = $this->c->polls->revision($poll);
+
+            if (true !== $result) {
+                $v->addError($result);
+            }
+        }
+
+        return $enable;
+    }
 }