Selaa lähdekoodia

2018-01-09 delete iterator

Visman 7 vuotta sitten
vanhempi
commit
2f384c94fa
2 muutettua tiedostoa jossa 31 lisäystä ja 164 poistoa
  1. 0 86
      app/Models/Topic/Model.php
  2. 31 78
      app/Models/Topic/Posts.php

+ 0 - 86
app/Models/Topic/Model.php

@@ -245,92 +245,6 @@ class Model extends DataModel
         $this->page = empty($result['flag']) ? null : (int) ceil(($result['num'] + 1) / $this->c->user->disp_posts);
     }
 
-    /**
-     * Возвращает массив сообщений с установленной рание страницы темы
-     *
-     * @throws InvalidArgumentException
-     *
-     * @return array
-     */
-    public function posts()
-    {
-        if (! $this->hasPage()) {
-            throw new InvalidArgumentException('Bad number of displayed page');
-        }
-
-        $offset = ($this->page - 1) * $this->c->user->disp_posts;
-        $vars = [
-            ':tid'    => $this->id,
-            ':offset' => $offset,
-            ':rows'   => $this->c->user->disp_posts,
-        ];
-        $sql = 'SELECT id
-                FROM ::posts
-                WHERE topic_id=?i:tid
-                ORDER BY id LIMIT ?i:offset, ?i:rows';
-
-        $ids = $this->c->DB->query($sql, $vars)->fetchAll(\PDO::FETCH_COLUMN);
-        if (empty($ids)) {
-            return [];
-        }
-
-        // приклейка первого сообщения темы
-        if ($this->stick_fp || $this->poll_type) {
-            $ids[] = $this->first_post_id;
-        }
-
-        $vars = [
-            ':ids' => $ids,
-        ];
-        $sql = 'SELECT id, message, poster, posted
-                FROM ::warnings
-                WHERE id IN (?ai:ids)';
-
-        $warnings = $this->c->DB->query($sql, $vars)->fetchAll(\PDO::FETCH_GROUP);
-
-        $vars = [
-            ':ids' => $ids,
-        ];
-        $sql = 'SELECT u.warning_all, u.gender, u.email, u.title, u.url, u.location, u.signature,
-                       u.email_setting, u.num_posts, u.registered, u.admin_note, u.messages_enable,
-                       u.group_id,
-                       p.id, p.poster as username, p.poster_id, p.poster_ip, p.poster_email, p.message,
-                       p.hide_smilies, p.posted, p.edited, p.edited_by, p.edit_post, p.user_agent, p.topic_id,
-                       g.g_user_title, g.g_promote_next_group, g.g_pm
-                FROM ::posts AS p
-                INNER JOIN ::users AS u ON u.id=p.poster_id
-                INNER JOIN ::groups AS g ON g.g_id=u.group_id
-                WHERE p.id IN (?ai:ids) ORDER BY p.id';
-
-        $stmt = $this->c->DB->query($sql, $vars);
-
-        $postCount = 0;
-        $timeMax = 0;
-        $result = [];
-
-        while ($cur = $stmt->fetch()) {
-            if ($cur['posted'] > $timeMax) {
-                $timeMax = $cur['posted'];
-            }
-
-            // номер сообшения в теме
-            if ($cur['id'] == $this->first_post_id && $offset > 0) {
-                $cur['postNumber'] = 1;
-            } else {
-                ++$postCount;
-                $cur['postNumber'] = $offset + $postCount;
-            }
-
-            if (isset($warnings[$cur['id']])) {
-                $cur['warnings'] = $warnings[$cur['id']];
-            }
-
-            $result[] = $this->c->posts->create($cur);
-        }
-        $this->timeMax = $timeMax;
-        return $result;
-    }
-
     /**
      * Статус показа/подсчета просмотров темы
      *

+ 31 - 78
app/Models/Topic/Posts.php

@@ -3,48 +3,29 @@
 namespace ForkBB\Models\Topic;
 
 use ForkBB\Models\Method;
-use Iterator;
 use PDO;
 use InvalidArgumentException;
 use RuntimeException;
 
-class Posts extends Method implements Iterator
+class Posts extends Method
 {
-    protected $key;
-
-    protected $row;
-
-    protected $stmt;
-
-    protected $warnings;
-
-    protected $postCount;
-
-    protected $post;
-
-    protected $offset;
-
     /**
-     * 
-     * 
-     * @throws RuntimeException
-     * 
-     * @return null|Method
+     * Возвращает массив сообщений с установленной ранее страницы темы
+     *
+     * @throws InvalidArgumentException
+     *
+     * @return array
      */
     public function posts()
     {
-        if ($this->model->id < 1) {
-            throw new RuntimeException('The model does not have ID');
-        }
-
         if (! $this->model->hasPage()) {
             throw new InvalidArgumentException('Bad number of displayed page');
         }
 
-        $this->offset = ($this->model->page - 1) * $this->c->user->disp_posts;
+        $offset = ($this->model->page - 1) * $this->c->user->disp_posts;
         $vars = [
             ':tid'    => $this->model->id,
-            ':offset' => $this->offset,
+            ':offset' => $offset,
             ':rows'   => $this->c->user->disp_posts,
         ];
         $sql = 'SELECT id
@@ -54,7 +35,7 @@ class Posts extends Method implements Iterator
 
         $ids = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
         if (empty($ids)) {
-            return null;
+            return [];
         }
 
         // приклейка первого сообщения темы
@@ -69,7 +50,7 @@ class Posts extends Method implements Iterator
                 FROM ::warnings
                 WHERE id IN (?ai:ids)';
 
-        $this->warnings = $this->c->DB->query($sql, $vars)->fetchAll(\PDO::FETCH_GROUP);
+        $warnings = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_GROUP);
 
         $vars = [
             ':ids' => $ids,
@@ -85,60 +66,32 @@ class Posts extends Method implements Iterator
                 INNER JOIN ::groups AS g ON g.g_id=u.group_id
                 WHERE p.id IN (?ai:ids) ORDER BY p.id';
 
-        $this->stmt = $this->c->DB->query($sql, $vars);
-        $this->model->timeMax = 0;
-        $this->postCount = 0;
-        $this->post = $this->c->posts->create();
+        $stmt = $this->c->DB->query($sql, $vars);
 
-        return $this;
-    }
+        $postCount = 0;
+        $timeMax = 0;
+        $result = [];
 
-    public function rewind()
-    {
-        $this->key = 0;
-    }
-  
-    public function current()
-    {
-        if (empty($this->row)) { //????
-            return false;
-        }
-
-        $cur = $this->row;
+        while ($cur = $stmt->fetch()) {
+            if ($cur['posted'] > $timeMax) {
+                $timeMax = $cur['posted'];
+            }
 
-        if ($cur['posted'] > $this->model->timeMax) {
-            $this->model->timeMax = $cur['posted'];
-        }
+            // номер сообшения в теме
+            if ($cur['id'] == $this->model->first_post_id && $offset > 0) {
+                $cur['postNumber'] = 1;
+            } else {
+                ++$postCount;
+                $cur['postNumber'] = $offset + $postCount;
+            }
 
-        // номер сообшения в теме
-        if ($cur['id'] == $this->model->first_post_id && $this->offset > 0) {
-            $cur['postNumber'] = 1;
-        } else {
-            ++$this->postCount;
-            $cur['postNumber'] = $this->offset + $this->postCount;
-        }
+            if (isset($warnings[$cur['id']])) {
+                $cur['warnings'] = $warnings[$cur['id']];
+            }
 
-        if (isset($this->warnings[$cur['id']])) {
-            $cur['warnings'] = $this->warnings[$cur['id']];
+            $result[] = $this->c->posts->create($cur);
         }
-
-        return $this->post->setAttrs($cur);
-    }
-  
-    public function key() 
-    {
-        return $this->key;
-    }
-  
-    public function next() 
-    {
-        ++$this->key;
+        $this->model->timeMax = $timeMax;
+        return $result;
     }
-  
-    public function valid()
-    {
-        $this->row = $this->stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $this->key);
-        return false !== $this->row;
-    }
-
 }