From 63ec2df6b0550327a66cec16f23ccfadb31cdb44 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 5 May 2024 01:32:04 +0300 Subject: [PATCH] add channel sort order --- config/example.json | 2 ++ src/Model/Database.php | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/config/example.json b/config/example.json index e4da1f8..42cb860 100644 --- a/config/example.json +++ b/config/example.json @@ -63,6 +63,7 @@ { "source":"https://www.omglinux.com/feed", "alias":"omglinux.gmi", + "order":1, "enabled":true, "item": { @@ -95,6 +96,7 @@ { "source":"https://omgubuntu.co.uk/feed", "alias":"omgubuntu.gmi", + "order":2, "enabled":false, "item": { diff --git a/src/Model/Database.php b/src/Model/Database.php index 80b793b..423cb30 100644 --- a/src/Model/Database.php +++ b/src/Model/Database.php @@ -13,6 +13,7 @@ class Database ?string $username = null, ?string $password = null ) { + // Init connection $this->_database = new \PDO( sprintf( 'sqlite:%s', @@ -40,11 +41,13 @@ class Database \PDO::FETCH_OBJ ); + // Init structure $this->_database->query(' CREATE TABLE IF NOT EXISTS "channel" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "time" INTEGER NOT NULL, + "order" INTEGER NOT NULL, "alias" VARCHAR NOT NULL, "source" TEXT NOT NULL, "link" TEXT, @@ -90,7 +93,7 @@ class Database public function getChannels(): ?array { $query = $this->_database->query( - 'SELECT * FROM `channel`' + 'SELECT * FROM `channel` ORDER BY `order` ASC, `title` ASC, `id` ASC' ); if ($result = $query->fetchAll()) @@ -142,17 +145,18 @@ class Database } public function addChannel( - string $alias, - string $source, - ?string $link, - ?string $title, - ?string $description, - ?int $time = null + string $alias, + string $source, + ?string $link = null, + ?string $title = null, + ?string $description = null, + ?int $time = null, + ?int $order = null ): ?int { $query = $this->_database->prepare( - 'INSERT INTO `channel` (`alias`, `source`, `link`, `title`, `description`, `time`) - VALUES (:alias, :source, :link, :title, :description, :time)' + 'INSERT INTO `channel` (`alias`, `source`, `link`, `title`, `description`, `time`, `order`) + VALUES (:alias, :source, :link, :title, :description, :time, :order)' ); $query->execute( @@ -162,7 +166,8 @@ class Database ':link' => $link, ':title' => $title, ':description' => $description, - ':time' => $time ? $time : time() + ':time' => $time ? $time : time(), + ':order' => $order ? $order : 0 ] ); @@ -199,7 +204,7 @@ class Database { $query = $this->_database->query( sprintf( - 'SELECT * FROM `channelItem` ORDER BY `time` DESC LIMIT %d,%d', + 'SELECT * FROM `channelItem` ORDER BY `pubTime` DESC, `time` DESC, `id` DESC LIMIT %d,%d', $start, $limit )