diff --git a/_sakura/changelog.json b/_sakura/changelog.json index bcd7136..65c797e 100644 --- a/_sakura/changelog.json +++ b/_sakura/changelog.json @@ -2634,6 +2634,68 @@ "user": "Flashwave" } + ], + + "20150911": [ + + "eminence", + { + "type": "UPD", + "change": "Migrate more user object references to the new API.", + "user": "Flashwave" + }, + { + "type": "ADD", + "change": "Add missing $data = null to the count database method.", + "user": "Flashwave" + }, + { + "type": "UPD", + "change": "Directly count index statistics from the database as opposed to retrieving an array and counting that.", + "user": "Flashwave" + }, + { + "type": "UPD", + "change": "Make the legacy getUser function reference the new API.", + "user": "Flashwave" + }, + { + "type": "UPD", + "change": "Moved index news posts over to the new API.", + "user": "Flashwave" + } + + ], + + "20150912": [ + + "eminence", + { + "type": "UPD", + "change": "Moved pagination to self-contained template file.", + "user": "Flashwave" + },, + { + "type": "UPD", + "change": "Moved news section over to new API.", + "user": "Flashwave" + }, + { + "type": "REM", + "change": "Removed old news function.", + "user": "Flashwave" + }, + { + "type": "FIX", + "change": "Fixed sorting in the changelog.", + "user": "Flashwave" + }, + { + "type": "REM", + "change": "Removed getjson method from changelog.", + "user": "Flashwave" + } + ] } diff --git a/_sakura/components/Database.php b/_sakura/components/Database.php index 82fe5a0..a33643a 100644 --- a/_sakura/components/Database.php +++ b/_sakura/components/Database.php @@ -64,7 +64,7 @@ class Database { } // Count from database - public static function count($table, $data, $prefix = null) { + public static function count($table, $data = null, $prefix = null) { return self::$_DATABASE->count($table, $data, $prefix); diff --git a/_sakura/components/Main.php b/_sakura/components/Main.php index 7c4d7d6..9af6b63 100644 --- a/_sakura/components/Main.php +++ b/_sakura/components/Main.php @@ -339,7 +339,7 @@ $errorPage .= ' $string = strip_tags($string); // If set also make the string lowercase - if($lower){ + if($lower) { $string = strtolower($string); @@ -357,33 +357,6 @@ $errorPage .= ' } - // Getting news posts - public static function getNewsPosts($limit = null, $pid = false) { - - // Get news posts - $newsPosts = Database::fetch('news', true, ($pid ? ['id' => [$limit, '=']] : null), ['id', true], ($limit && !$pid ? [$limit] : null)); - - // Get user data - foreach($newsPosts as $newsId => $newsPost) { - - $newsPosts[$newsId]['parsed'] = self::mdParse($newsPost['content']); - $newsPosts[$newsId]['udata'] = Users::getUser($newsPost['uid']); - $newsPosts[$newsId]['rdata'] = Users::getRank($newsPosts[$newsId]['udata']['rank_main']); - - // Check if a custom name colour is set and if so overwrite the rank colour - if($newsPosts[$newsId]['udata']['name_colour'] != null){ - - $newsPosts[$newsId]['rdata']['colour'] = $newsPosts[$newsId]['udata']['name_colour']; - - } - - } - - // Return posts - return $newsPosts; - - } - // Loading info pages public static function loadInfoPage($id) { diff --git a/_sakura/components/News.php b/_sakura/components/News.php index 3618820..e089e46 100644 --- a/_sakura/components/News.php +++ b/_sakura/components/News.php @@ -7,14 +7,80 @@ namespace Sakura; class News { - // Posts array - public $posts = []; + private $posts = []; // Posts array + private $posters = []; // Posters array (so we don't create a new user object every time) // Initialise the news object - function __construct($category) { + function __construct($category, $comments = true) { // Get the news posts and assign them to $posts - $this->posts = Database::fetch('news', true, ['category' => [$category, '=']], ['id', true]); + $posts = Database::fetch('news', true, ['category' => [$category, '=']], ['id', true]); + + // Attach poster data + foreach($posts as $post) { + + // Check if we already have an object for this user + if(!array_key_exists($post['uid'], $this->posters)) { + + // Create new object + $this->posters[$post['uid']] = new User($post['uid']); + + } + + // Parse the news post + $post['content_parsed'] = Main::mdParse($post['content']); + + // Attach the poster + $post['poster'] = $this->posters[$post['uid']]; + + // Add post to posts array + $this->posts[$post['id']] = $post; + + } + + } + + // Get the amount of posts + public function getCount() { + + return count($this->posts); + + } + + // Get the amount of posts + public function postExists($id) { + + return array_key_exists($id, $this->posts) ? $id : 0; + + } + + // Get a single post + public function getPost($id) { + + return array_key_exists($id, $this->posts) ? $this->posts[$id] : 0; + + } + + // Getting posts + public function getPosts($start = null, $end = null) { + + // Get posts + $posts = $this->posts; + + // Only return requested posts + if($start !== null && $end !== null) { + + // Slice the array + $posts = array_slice($posts, $start, $end, true); + + } elseif($start !== null) { + + // Devide the array in parts (pages) + $posts = array_chunk($posts, $start, true); + + } + + return $posts; } diff --git a/_sakura/components/Users.php b/_sakura/components/Users.php index 7d40c91..2d14dbc 100644 --- a/_sakura/components/Users.php +++ b/_sakura/components/Users.php @@ -1195,21 +1195,8 @@ class Users { // Get user data by id public static function getUser($id) { - // Execute query - $user = Database::fetch('users', false, ['id' => [$id, '=']]); - - // Return false if no user was found - if(empty($user)) { - - $user = self::$emptyUser; - - } - - // Parse the json in the additional section - $user['userData'] = json_decode($user['userData'], true); - // If user was found return user data - return $user; + return (new User($id))->data; } diff --git a/_sakura/components/database/mysql.php b/_sakura/components/database/mysql.php index a77b8d3..747a5f7 100644 --- a/_sakura/components/database/mysql.php +++ b/_sakura/components/database/mysql.php @@ -357,7 +357,7 @@ class MySQL { } // Count data from the database - public function count($table, $data, $prefix = null) { + public function count($table, $data = null, $prefix = null) { // Begin preparation of the statement $prepare = 'SELECT COUNT(*) FROM `' . ($prefix ? $prefix : Configuration::getLocalConfig('database', 'prefix')) . $table . '`'; diff --git a/_sakura/sakura.php b/_sakura/sakura.php index 93ee5b2..ebe6960 100644 --- a/_sakura/sakura.php +++ b/_sakura/sakura.php @@ -8,7 +8,7 @@ namespace Sakura; // Define Sakura version -define('SAKURA_VERSION', '20150910'); +define('SAKURA_VERSION', '20150912'); define('SAKURA_VLABEL', 'Eminence'); define('SAKURA_COLOUR', '#6C3082'); define('SAKURA_STABLE', false); diff --git a/_sakura/templates/yuuno/elements/newsPost.tpl b/_sakura/templates/yuuno/elements/newsPost.tpl index c67aad3..8c28abe 100644 --- a/_sakura/templates/yuuno/elements/newsPost.tpl +++ b/_sakura/templates/yuuno/elements/newsPost.tpl @@ -1,16 +1,16 @@ -{% if not page.view_post %}{{ newsPost.title }}{% endif %} +{% if not viewPost %}{{ post.title }}{% endif %}