소스 검색

Update Post\Load

Visman 5 년 전
부모
커밋
d407c9310d
2개의 변경된 파일23개의 추가작업 그리고 137개의 파일을 삭제
  1. 20 131
      app/Models/Post/Load.php
  2. 3 6
      app/Models/Post/Manager.php

+ 20 - 131
app/Models/Post/Load.php

@@ -4,66 +4,26 @@ namespace ForkBB\Models\Post;
 
 use ForkBB\Models\Action;
 use ForkBB\Models\Post\Model as Post;
+use ForkBB\Models\Topic\Model as Topic;
 use InvalidArgumentException;
 
 class Load extends Action
 {
-    protected $aliases;
-
-    protected function queryFields(array $args): string
-    {
-        $result  = [];
-        $fields  = [];
-        $aliases = [];
-        foreach ($args as $alias => $rawFields) {
-            $aliases[$alias] = [];
-            foreach ($rawFields as $originalName => $replName) {
-                if (null === $replName || false === $replName) {
-                    continue;
-                }
-
-                $name = $alias . '.' . $originalName;
-
-                if (true === $replName && isset($fields[$originalName])) {
-                    $replName = "alias_{$alias}_{$originalName}";
-                    $result[] = $name . ' AS '. $replName;
-                    $aliases[$alias][$replName] = $originalName;
-                    $fields[$replName] = $alias;
-                } elseif (true === $replName) {
-                    $result[] = $name;
-                    $aliases[$alias][$originalName] = true;
-                    $fields[$originalName] = $alias;
-                } else {
-                    $result[] = $name . ' AS '. $replName;
-                    $aliases[$alias][$replName] = $replName; //???? $originalName;
-                    $fields[$replName] = $alias;
-                }
-            }
-        }
-        $this->aliases = $aliases;
-
-        return \implode(', ', $result);
-    }
-
-    protected function setData(array $args, array $data): void
+    /**
+     * Создает текст запрос
+     */
+    protected function getSql(string $where): string
     {
-        foreach ($args as $aliases => $model) {
-            $attrs = [];
-            foreach (\explode('.', $aliases) as $alias) {
-                if (empty($this->aliases[$alias])) {
-                    continue;
-                }
-                foreach ($this->aliases[$alias] as $key => $repl) {
-                    $name = true === $repl ? $key : $repl;
-                    $attrs[$name] = $data[$key];
-                }
-            }
-            $model->setAttrs($attrs);
-        }
+        $sql = 'SELECT p.*
+                FROM ::posts AS p
+                WHERE ' . $where;
+        return $sql;
     }
 
     /**
      * Загружает сообщение из БД с проверкой вхождения в указанную тему
+     * Загружает сообщение из БД
+     * Загружает тему этого сообщения в репозиторий topics
      * Проверка доступности
      *
      * @param int $id
@@ -73,12 +33,12 @@ class Load extends Action
      *
      * @return null|Post
      */
-    public function loadFromTopic(int $id, int $tid): ?Post
+    public function load(int $id, ?int $tid): ?Post
     {
         if ($id < 1) {
             throw new InvalidArgumentException('Expected a positive post id');
         }
-        if ($tid < 1) {
+        if (null !== $tid && $tid < 1) {
             throw new InvalidArgumentException('Expected a positive topic id');
         }
 
@@ -86,13 +46,13 @@ class Load extends Action
             ':pid' => $id,
             ':tid' => $tid,
         ];
-        $sql = 'SELECT p.*
-                FROM ::posts AS p
-                WHERE p.id=?i:pid AND p.topic_id=?i:tid';
-
+        $sql  = $this->getSql(
+            null !== $tid ?
+            'p.id=?i:pid AND p.topic_id=?i:tid' :
+            'p.id=?i:pid'
+        );
         $data = $this->c->DB->query($sql, $vars)->fetch();
 
-        // сообщение отсутствует или недоступено
         if (empty($data)) {
             return null;
         }
@@ -100,84 +60,13 @@ class Load extends Action
         $post  = $this->manager->create($data);
         $topic = $post->parent;
 
-        if (empty($topic) || $topic->moved_to || ! $topic->parent) {
+        if (! $topic instanceof Topic || $topic->moved_to || ! $topic->parent) {
             return null;
         }
-
-        return $post;
-    }
-
-    /**
-     * Загружает сообщение из БД
-     * Загружает тему этого сообщения
-     * Проверка доступности
-     *
-     * @param int $id
-     *
-     * @throws InvalidArgumentException
-     *
-     * @return null|Post
-     */
-    public function load(int $id): ?Post
-    {
-        if ($id < 1) {
-            throw new InvalidArgumentException('Expected a positive post id');
-        }
-
-        if ($this->c->user->isGuest) {
-            $vars = [
-                ':pid' => $id,
-                ':fields' => $this->queryFields([
-                    'p' => \array_map(function($val) {return true;}, $this->c->dbMap->posts), // все поля в true
-                    't' => \array_map(function($val) {return true;}, $this->c->dbMap->topics), // все поля в true
-                ]),
-            ];
-
-            $sql = 'SELECT ?p:fields
-                    FROM ::posts AS p
-                    INNER JOIN ::topics AS t ON t.id=p.topic_id
-                    WHERE p.id=?i:pid';
-        } else {
-            $vars = [
-                ':pid' => $id,
-                ':uid' => $this->c->user->id,
-                ':fields' => $this->queryFields([
-                    'p'   => \array_map(function($val) {return true;}, $this->c->dbMap->posts), // все поля в true
-                    't'   => \array_map(function($val) {return true;}, $this->c->dbMap->topics), // все поля в true
-                    's'   => ['user_id' => 'is_subscribed'],
-                    'mof' => ['mf_mark_all_read' => true],
-                    'mot' => ['mt_last_visit' => true, 'mt_last_read' => true],
-                ]),
-            ];
-
-            $sql = 'SELECT ?p:fields
-                    FROM ::posts AS p
-                    INNER JOIN ::topics AS t ON t.id=p.topic_id
-                    LEFT JOIN ::topic_subscriptions AS s ON (t.id=s.topic_id AND s.user_id=?i:uid)
-                    LEFT JOIN ::mark_of_forum AS mof ON (mof.uid=?i:uid AND t.forum_id=mof.fid)
-                    LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:uid AND t.id=mot.tid)
-                    WHERE p.id=?i:pid';
-        }
-
-        $data = $this->c->DB->query($sql, $vars)->fetch();
-
-        // сообщение отсутствует или недоступено
-        if (empty($data)) {
+        if (null !== $tid && $topic->id !== $tid) {
             return null;
         }
 
-        $post  = $this->manager->create();
-        $topic = $this->c->topics->create();
-        $this->setData(['p' => $post, 't.s.mof.mot' => $topic], $data);
-
-        if ($topic->moved_to || ! $topic->parent) {
-            return null;
-        }
-
-        $topic->parent->__mf_mark_all_read = $topic->mf_mark_all_read; //????
-
-        $this->c->topics->set($topic->id, $topic);
-
         return $post;
     }
 }

+ 3 - 6
app/Models/Post/Manager.php

@@ -20,7 +20,8 @@ class Manager extends ManagerModel
     }
 
     /**
-     * Загружает сообщение из БД
+     * Получает сообщение по id
+     * Получает сообщение по id и tid темы (с проверкой вхождения)
      *
      * @param int $id
      * @param int $tid
@@ -36,11 +37,7 @@ class Manager extends ManagerModel
                 return null;
             }
         } else {
-            if (null !== $tid) {
-                $post = $this->Load->loadFromTopic($id, $tid);
-            } else {
-                $post = $this->Load->load($id);
-            }
+            $post = $this->Load->load($id, $tid);
             $this->set($id, $post);
         }