|
@@ -3,16 +3,44 @@
|
|
namespace ForkBB\Models\Topic;
|
|
namespace ForkBB\Models\Topic;
|
|
|
|
|
|
use ForkBB\Models\Action;
|
|
use ForkBB\Models\Action;
|
|
|
|
+use ForkBB\Models\Forum\Model as Forum;
|
|
use ForkBB\Models\Topic\Model as Topic;
|
|
use ForkBB\Models\Topic\Model as Topic;
|
|
use InvalidArgumentException;
|
|
use InvalidArgumentException;
|
|
|
|
|
|
class Load extends Action
|
|
class Load extends Action
|
|
{
|
|
{
|
|
|
|
+ /**
|
|
|
|
+ * Создает текст запрос
|
|
|
|
+ */
|
|
|
|
+ protected function getSql(string $where, bool $full): string
|
|
|
|
+ {
|
|
|
|
+ if ($this->c->user->isGuest) {
|
|
|
|
+ $sql = 'SELECT t.*
|
|
|
|
+ FROM ::topics AS t
|
|
|
|
+ WHERE ' . $where;
|
|
|
|
+ } elseif ($full) {
|
|
|
|
+ $sql = 'SELECT t.*, s.user_id AS is_subscribed, mof.mf_mark_all_read, mot.mt_last_visit, mot.mt_last_read
|
|
|
|
+ FROM ::topics AS t
|
|
|
|
+ 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 ' . $where;
|
|
|
|
+ } else {
|
|
|
|
+ $sql = 'SELECT t.*, mot.mt_last_visit, mot.mt_last_read
|
|
|
|
+ FROM ::topics AS t
|
|
|
|
+ LEFT JOIN ::mark_of_topic AS mot ON (mot.uid=?i:uid AND t.id=mot.tid)
|
|
|
|
+ WHERE ' . $where;
|
|
|
|
+ }
|
|
|
|
+ return $sql;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Загружает тему из БД
|
|
* Загружает тему из БД
|
|
*
|
|
*
|
|
* @param int $id
|
|
* @param int $id
|
|
*
|
|
*
|
|
|
|
+ * @throws InvalidArgumentException
|
|
|
|
+ *
|
|
* @return null|Topic
|
|
* @return null|Topic
|
|
*/
|
|
*/
|
|
public function load(int $id): ?Topic
|
|
public function load(int $id): ?Topic
|
|
@@ -25,20 +53,7 @@ class Load extends Action
|
|
':tid' => $id,
|
|
':tid' => $id,
|
|
':uid' => $this->c->user->id,
|
|
':uid' => $this->c->user->id,
|
|
];
|
|
];
|
|
- if ($this->c->user->isGuest) {
|
|
|
|
- $sql = 'SELECT t.*
|
|
|
|
- FROM ::topics AS t
|
|
|
|
- WHERE t.id=?i:tid';
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
- $sql = 'SELECT t.*, s.user_id AS is_subscribed, mof.mf_mark_all_read, mot.mt_last_visit, mot.mt_last_read
|
|
|
|
- FROM ::topics AS t
|
|
|
|
- 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 t.id=?i:tid';
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ $sql = $this->getSql('t.id=?i:tid', true);
|
|
$data = $this->c->DB->query($sql, $vars)->fetch();
|
|
$data = $this->c->DB->query($sql, $vars)->fetch();
|
|
|
|
|
|
// тема отсутствует или недоступна
|
|
// тема отсутствует или недоступна
|
|
@@ -48,7 +63,7 @@ class Load extends Action
|
|
|
|
|
|
$topic = $this->manager->create($data);
|
|
$topic = $this->manager->create($data);
|
|
|
|
|
|
- if (! $topic->parent) {
|
|
|
|
|
|
+ if (! $topic->parent instanceof Forum) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -56,4 +71,39 @@ class Load extends Action
|
|
|
|
|
|
return $topic;
|
|
return $topic;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Загружает список тем из БД
|
|
|
|
+ *
|
|
|
|
+ * @throws InvalidArgumentException
|
|
|
|
+ */
|
|
|
|
+ public function loadByIds(array $ids, bool $full): array
|
|
|
|
+ {
|
|
|
|
+ foreach ($ids as $id) {
|
|
|
|
+ if (! \is_int($id) || $id < 1) {
|
|
|
|
+ throw new InvalidArgumentException('Expected a positive topic id');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $vars = [
|
|
|
|
+ ':ids' => $ids,
|
|
|
|
+ ':uid' => $this->c->user->id,
|
|
|
|
+ ];
|
|
|
|
+ $sql = $this->getSql('t.id IN (?ai:ids)', $full);
|
|
|
|
+ $stmt = $this->c->DB->query($sql, $vars);
|
|
|
|
+
|
|
|
|
+ $result = [];
|
|
|
|
+ while ($row = $stmt->fetch()) {
|
|
|
|
+ $topic = $this->manager->create($row);
|
|
|
|
+
|
|
|
|
+ if ($topic->parent instanceof Forum) {
|
|
|
|
+ $result[] = $topic;
|
|
|
|
+
|
|
|
|
+ if (! empty($row['mf_mark_all_read'])) {
|
|
|
|
+ $topic->parent->__mf_mark_all_read = $row['mf_mark_all_read'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
}
|
|
}
|