瀏覽代碼

Add poll 5

Add poll editing timeout.
Visman 4 年之前
父節點
當前提交
a28e5e5370
共有 3 個文件被更改,包括 68 次插入25 次删除
  1. 30 10
      app/Models/Pages/Edit.php
  2. 19 14
      app/Models/Pages/PostFormTrait.php
  3. 19 1
      app/Models/Poll/Model.php

+ 30 - 10
app/Models/Pages/Edit.php

@@ -47,7 +47,7 @@ class Edit extends Page
             }
             }
 
 
             $this->fIswev  = $v->getErrors();
             $this->fIswev  = $v->getErrors();
-            $args['_vars'] = $v->getData(); //????
+            $args['_vars'] = $v->getData();
 
 
             if (
             if (
                 null !== $v->preview
                 null !== $v->preview
@@ -67,7 +67,7 @@ class Edit extends Page
                 }
                 }
             }
             }
         } else {
         } else {
-            $args['_vars'] = [ //????
+            $args['_vars'] = [
                 'message'      => $post->message,
                 'message'      => $post->message,
                 'subject'      => $topic->subject,
                 'subject'      => $topic->subject,
                 'hide_smilies' => $post->hide_smilies,
                 'hide_smilies' => $post->hide_smilies,
@@ -75,21 +75,37 @@ class Edit extends Page
                 'stick_fp'     => $topic->stick_fp,
                 'stick_fp'     => $topic->stick_fp,
                 'edit_post'    => $post->edit_post,
                 'edit_post'    => $post->edit_post,
             ];
             ];
+        }
 
 
+        if (
+            $editSubject
+            && $this->user->usePoll
+        ) {
             if (
             if (
-                $editSubject
-                && $this->user->usePoll
-                && ($poll = $topic->poll) instanceof Poll
+                ($poll = $topic->poll) instanceof Poll
+                && (
+                    ! $poll->canEdit
+                    || 'POST' !== $method
+                )
             ) {
             ) {
-                $args['_vars'] += [
-                    'poll_enable' => $topic->poll_type > 0,
-                    'poll' => [
+                $args['_vars'] = \array_merge($args['_vars'], [
+                    'pollNoEdit'   => ! $poll->canEdit,
+                    'poll_enable'  => $topic->poll_type > 0,
+                    'poll'         => [
                         'hide_result' => $topic->poll_term > 0,
                         'hide_result' => $topic->poll_term > 0,
                         'question'    => $poll->question,
                         'question'    => $poll->question,
                         'type'        => $poll->type,
                         'type'        => $poll->type,
                         'answer'      => $poll->answer,
                         'answer'      => $poll->answer,
                     ],
                     ],
-                ];
+                ]);
+            }
+
+            if (
+                null !== $this->previewHtml
+                && $args['_vars']['poll_enable']
+            ) {
+                $this->poll = $this->c->polls->create($args['_vars']['poll']);
+                $this->c->polls->revision($this->poll, true);
             }
             }
         }
         }
 
 
@@ -205,11 +221,15 @@ class Edit extends Page
     /**
     /**
      * Изменяет(удаляет/добавляет) данные опроса
      * Изменяет(удаляет/добавляет) данные опроса
      */
      */
-    protected function changePoll(Topic $topic, Validator $v)
+    protected function changePoll(Topic $topic, Validator $v): void
     {
     {
         if ($topic->poll_type > 0 ) {
         if ($topic->poll_type > 0 ) {
             $poll = $topic->poll;
             $poll = $topic->poll;
 
 
+            if (! $poll->canEdit) {
+                return;
+            }
+
             // редактирование
             // редактирование
             if ($v->poll_enable) {
             if ($v->poll_enable) {
 #                $topic->poll_type  = 0;
 #                $topic->poll_type  = 0;

+ 19 - 14
app/Models/Pages/PostFormTrait.php

@@ -198,16 +198,18 @@ trait PostFormTrait
             $fieldset = [];
             $fieldset = [];
 
 
             $fieldset['poll_enable'] = [
             $fieldset['poll_enable'] = [
-                'type'    => 'checkbox',
-                'label'   => __('Include poll'),
-                'value'   => '1',
-                'checked' => (bool) ($vars['poll_enable'] ?? false),
+                'type'     => 'checkbox',
+                'label'    => __('Include poll'),
+                'value'    => '1',
+                'checked'  => (bool) ($vars['poll_enable'] ?? false),
+                'disabled' => $vars['pollNoEdit'] ?? null,
             ];
             ];
             $fieldset['poll[hide_result]'] = [
             $fieldset['poll[hide_result]'] = [
-                'type'    => 'checkbox',
-                'label'   => __('Hide poll results up to %s voters', $term),
-                'value'   => '1',
-                'checked' => (bool) ($vars['poll']['hide_result'] ?? false),
+                'type'     => 'checkbox',
+                'label'    => __('Hide poll results up to %s voters', $term),
+                'value'    => '1',
+                'checked'  => (bool) ($vars['poll']['hide_result'] ?? false),
+                'disabled' => $vars['pollNoEdit'] ?? null,
             ];
             ];
 
 
             $form['sets']['uesm-poll'] = [
             $form['sets']['uesm-poll'] = [
@@ -223,14 +225,16 @@ trait PostFormTrait
                     'maxlength' => '255',
                     'maxlength' => '255',
                     'caption'   => __('Question text label'),
                     'caption'   => __('Question text label'),
                     'value'     => $vars['poll']['question'][$qid] ?? null,
                     'value'     => $vars['poll']['question'][$qid] ?? null,
+                    'disabled'  => $vars['pollNoEdit'] ?? null,
                 ];
                 ];
                 $fieldset["poll[type][{$qid}]"] = [
                 $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'),
+                    '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'),
+                    'disabled' => $vars['pollNoEdit'] ?? null,
                 ];
                 ];
 
 
                 for ($fid = 1; $fid <= $this->c->config->i_poll_max_fields; $fid++) {
                 for ($fid = 1; $fid <= $this->c->config->i_poll_max_fields; $fid++) {
@@ -239,6 +243,7 @@ trait PostFormTrait
                         'maxlength' => '255',
                         'maxlength' => '255',
                         'caption'   => __('Answer %s label', $fid),
                         'caption'   => __('Answer %s label', $fid),
                         'value'     => $vars['poll']['answer'][$qid][$fid] ?? null,
                         'value'     => $vars['poll']['answer'][$qid][$fid] ?? null,
+                        'disabled'  => $vars['pollNoEdit'] ?? null,
                     ];
                     ];
                 }
                 }
 
 

+ 19 - 1
app/Models/Poll/Model.php

@@ -58,7 +58,7 @@ class Model extends DataModel
             return true;
             return true;
         }
         }
 
 
-        if (! $this->tid) {
+        if ($this->tid < 1) {
             return false;
             return false;
         }
         }
 
 
@@ -80,6 +80,7 @@ class Model extends DataModel
     {
     {
         return $this->tid > 0
         return $this->tid > 0
             && $this->c->user->usePoll
             && $this->c->user->usePoll
+            && 0 === $this->parent->closed
             && 1 === $this->parent->poll_type // ???? добавить ограничение по времени?
             && 1 === $this->parent->poll_type // ???? добавить ограничение по времени?
             && ! $this->userVoted;
             && ! $this->userVoted;
     }
     }
@@ -93,4 +94,21 @@ class Model extends DataModel
             && (0 === $this->parent->poll_term || $this->parent->poll_term < $this->parent->poll_votes);
             && (0 === $this->parent->poll_term || $this->parent->poll_term < $this->parent->poll_votes);
     }
     }
 
 
+    /**
+     * Статус возможности редактировать опрос
+     */
+    protected function getcanEdit(): bool
+    {
+        return $this->c->user->usePoll
+            && (
+                0 === $this->c->config->i_poll_time
+                || $this->tid < 1
+                || $this->c->user->isAdmin
+                || (
+                    $this->c->user->isAdmMod
+                    && $this->c->user->isModerator($this)
+                )
+                || 60 * $this->c->config->i_poll_time > \time() - $this->parent->poll_time
+            );
+    }
 }
 }