result($db->query($sql)); } /** * @return Post[] */ public static function getList(int $offset = 0, int $count = -1, bool $include_hidden = false, ?PostLanguage $filter_by_lang = null ): array { $db = DB(); $sql = "SELECT * FROM posts"; if (!$include_hidden) $sql .= " WHERE visible=1"; $sql .= " ORDER BY `date` DESC"; if ($offset != 0 || $count != -1) $sql .= " LIMIT $offset, $count"; $q = $db->query($sql); $posts = []; while ($row = $db->fetch($q)) { $posts[$row['id']] = $row; } if (!empty($posts)) { foreach ($posts as &$post) $post = new Post($post); $q = $db->query("SELECT * FROM posts_texts WHERE post_id IN (".implode(',', array_keys($posts)).")"); while ($row = $db->fetch($q)) { $posts[$row['post_id']]->registerText(new PostText($row)); } } if ($filter_by_lang !== null) $posts = array_filter($posts, fn(Post $post) => $post->hasLang($filter_by_lang)); return array_values($posts); } public static function add(array $data = []): ?Post { $db = DB(); if (!$db->insert('posts', $data)) return null; return self::get($db->insertId()); } public static function delete(Post $post): void { $db = DB(); $db->query("DELETE FROM posts WHERE id=?", $post->id); $text_ids = []; $q = $db->query("SELECT id FROM posts_texts WHERE post_id=?", $post->id); while ($row = $db->fetch($q)) $text_ids = $row['id']; previous_texts::delete(PreviousText::TYPE_POST_TEXT, $text_ids); $db->query("DELETE FROM posts_texts WHERE post_id=?", $post->id); } public static function get(int $id): ?Post { $db = DB(); $q = $db->query("SELECT * FROM posts WHERE id=?", $id); return $db->numRows($q) ? new Post($db->fetch($q)) : null; } public static function getText(int $text_id): ?PostText { $db = DB(); $q = $db->query("SELECT * FROM posts_texts WHERE id=?", $text_id); return $db->numRows($q) ? new PostText($db->fetch($q)) : null; } public static function getByName(string $short_name): ?Post { $db = DB(); $q = $db->query("SELECT * FROM posts WHERE short_name=?", $short_name); return $db->numRows($q) ? new Post($db->fetch($q)) : null; } public static function getPostsById(array $ids, bool $flat = false): array { if (empty($ids)) { return []; } $db = DB(); $posts = array_fill_keys($ids, null); $q = $db->query("SELECT * FROM posts WHERE id IN(".implode(',', $ids).")"); while ($row = $db->fetch($q)) { $posts[(int)$row['id']] = new Post($row); } if ($flat) { $list = []; foreach ($ids as $id) { $list[] = $posts[$id]; } unset($posts); return $list; } return $posts; } public static function getPostTextsById(array $ids, bool $flat = false): array { if (empty($ids)) { return []; } $db = DB(); $posts = array_fill_keys($ids, null); $q = $db->query("SELECT * FROM posts_texts WHERE id IN(".implode(',', $ids).")"); while ($row = $db->fetch($q)) { $posts[(int)$row['id']] = new PostText($row); } if ($flat) { $list = []; foreach ($ids as $id) { $list[] = $posts[$id]; } unset($posts); return $list; } return $posts; } /** * @param Upload $upload * @return PostText[] Array of PostTexts that includes specified upload */ public static function getTextsWithUpload(Upload $upload): array { $db = DB(); $q = $db->query("SELECT id FROM posts_texts WHERE md LIKE '%{image:{$upload->randomId}%'"); $ids = []; while ($row = $db->fetch($q)) $ids[] = (int)$row['id']; return self::getPostTextsById($ids, true); } }