Browse Source

Add poll 4

Update models and pages.
Visman 4 years ago
parent
commit
4a92aa3d86

+ 13 - 4
app/Models/Pages/Edit.php

@@ -6,6 +6,7 @@ namespace ForkBB\Models\Pages;
 
 use ForkBB\Core\Validator;
 use ForkBB\Models\Page;
+use ForkBB\Models\Poll\Model as Poll;
 use ForkBB\Models\Post\Model as Post;
 use ForkBB\Models\Topic\Model as Topic;
 use function \ForkBB\__;
@@ -55,6 +56,15 @@ class Edit extends Page
                 $this->previewHtml = $this->c->censorship->censor(
                     $this->c->Parser->parseMessage(null, (bool) $v->hide_smilies)
                 );
+
+                if (
+                    $editSubject
+                    && $this->user->usePoll
+                    && $v->poll_enable
+                ) {
+                    $this->poll = $this->c->polls->create($v->poll);
+                    $this->c->polls->revision($this->poll, true);
+                }
             }
         } else {
             $args['_vars'] = [ //????
@@ -68,10 +78,9 @@ class Edit extends Page
 
             if (
                 $editSubject
-                && '1' == $this->c->config->b_poll_enabled
+                && $this->user->usePoll
+                && ($poll = $topic->poll) instanceof Poll
             ) {
-                $poll = $topic->poll;
-
                 $args['_vars'] += [
                     'poll_enable' => $topic->poll_type > 0,
                     'poll' => [
@@ -159,7 +168,7 @@ class Edit extends Page
                 $topic->stick_fp = $v->stick_fp ? 1 : 0;
             }
             // опрос
-            if ('1' == $this->c->config->b_poll_enabled) {
+            if ($this->user->usePoll) {
                 $this->changePoll($topic, $v);
             }
         }

+ 9 - 1
app/Models/Pages/Post.php

@@ -59,6 +59,14 @@ class Post extends Page
                 $this->previewHtml = $this->c->censorship->censor(
                     $this->c->Parser->parseMessage(null, (bool) $v->hide_smilies)
                 );
+
+                if (
+                    $this->user->usePoll
+                    && $v->poll_enable
+                ) {
+                    $this->poll = $this->c->polls->create($v->poll);
+                    $this->c->polls->revision($this->poll, true);
+                }
             }
         }
 
@@ -242,7 +250,7 @@ class Post extends Page
             $topic->first_post_id = $post->id;
 
             if (
-                '1' == $this->c->config->b_poll_enabled
+                $this->user->usePoll
                 && $v->poll_enable
             ) {
                 $topic->poll_type  = 1;

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

@@ -189,7 +189,7 @@ trait PostFormTrait
 
         if (
             $editSubject
-            && '1' == $this->c->config->b_poll_enabled
+            && $this->user->usePoll
         ) {
             $term = $editPost && $model->parent->poll_term
                 ? $model->parent->poll_term

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

@@ -197,7 +197,7 @@ trait PostValidatorTrait
 
         if (
             $editSubject
-            && '1' == $this->c->config->b_poll_enabled
+            && $this->user->usePoll
         ) {
             $v->addValidators([
                 'check_poll'  => [$this, 'vCheckPoll'],

+ 7 - 0
app/Models/Pages/Topic.php

@@ -167,6 +167,13 @@ class Topic extends Page
             ]);
         }
 
+        if (
+            $topic->poll_type > 0
+            && '1' == $this->c->config->b_poll_enabled
+        ) {
+            $this->poll = $topic->poll;
+        }
+
         return $this;
     }
 

+ 44 - 0
app/Models/Poll/Model.php

@@ -49,4 +49,48 @@ class Model extends DataModel
         $this->tid = $topic->id;
     }
 
+    /**
+     * Статус голосования для текущего пользователя
+     */
+    protected function getuserVoted(): bool
+    {
+        if ($this->c->user->isGuest) {
+            return true;
+        }
+
+        if (! $this->tid) {
+            return false;
+        }
+
+        $vars = [
+            ':tid' => $this->tid,
+            ':uid' => $this->c->user->id,
+        ];
+        $query = 'SELECT 1
+            FROM ::poll_voted
+            WHERE tid=?i:tid AND uid=?i:uid';
+
+        return ! empty($this->c->DB->query($query, $vars)->fetch());
+    }
+
+    /**
+     * Статус возможности голосовать
+     */
+    protected function getcanVote(): bool
+    {
+        return $this->tid > 0
+            && $this->c->user->usePoll
+            && 1 === $this->parent->poll_type // ???? добавить ограничение по времени?
+            && ! $this->userVoted;
+    }
+
+    /**
+     * Статус возможности видеть результат
+     */
+    protected function getcanSeeResult(): bool
+    {
+        return ($this->c->user->usePoll || '1' == $this->c->config->b_poll_guest)
+            && (0 === $this->parent->poll_term || $this->parent->poll_term < $this->parent->poll_votes);
+    }
+
 }

+ 4 - 1
app/Models/Topic/Model.php

@@ -338,7 +338,10 @@ class Model extends DataModel
             ! empty($list)
             && (
                 $this->stick_fp
-                || $this->poll_type
+                || (
+                    $this->poll_type > 0
+                    && '1' == $this->c->config->b_poll_enabled
+                )
             )
             && ! \in_array($this->first_post_id, $list)
         ) {

+ 9 - 1
app/Models/User/Model.php

@@ -19,7 +19,7 @@ class Model extends DataModel
         parent::__construct($container);
 
         $this->zDepend = [
-            'group_id'     => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'isBanByName', 'link', 'viewUsers', 'showPostCount', 'searchUsers'],
+            'group_id'     => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'isBanByName', 'link', 'viewUsers', 'showPostCount', 'searchUsers', 'usePoll'],
             'id'           => ['isGuest', 'link', 'online'],
             'last_visit'   => ['currentVisit'],
             'show_sig'     => ['showSignature'],
@@ -371,4 +371,12 @@ class Model extends DataModel
 
         return parent::getAttrs();
     }
+
+    /**
+     * Статус возможности использования опросов
+     */
+    protected function getusePoll(): bool
+    {
+        return '1' == $this->c->config->b_poll_enabled && ! $this->isGuest;
+    }
 }