diff --git a/app/Controllers/MetaController.php b/app/Controllers/MetaController.php index 256b120..29f7695 100644 --- a/app/Controllers/MetaController.php +++ b/app/Controllers/MetaController.php @@ -58,12 +58,10 @@ class MetaController extends Controller $onlineUsers[$user->id] = $user; } - // Get news - $news = new Category(config('general.news')); + //$news = new Category(config('general.news')); - // Merge index specific stuff with the global render data Template::vars([ - 'news' => $news->posts(3), + 'news' => [], 'stats' => [ 'userCount' => DB::table('users') ->where('rank_main', '!=', config('rank.banned')) diff --git a/app/Controllers/NewsController.php b/app/Controllers/NewsController.php index 846f464..249120b 100644 --- a/app/Controllers/NewsController.php +++ b/app/Controllers/NewsController.php @@ -7,6 +7,7 @@ namespace Sakura\Controllers; use Phroute\Phroute\Exception\HttpRouteNotFoundException; +use Sakura\DB; use Sakura\Config; use Sakura\News\Category; use Sakura\News\Post; @@ -18,24 +19,29 @@ use Sakura\News\Post; */ class NewsController extends Controller { + /** + * Shows all news posts in any category. + * @return string + */ + public function index(): string + { + $categories = DB::table('news_categories')->get(); + $news = new Category; + + return view('news/index', compact('categories', 'news')); + } + /** * Shows all posts in a specific category. - * @param string $category + * @param int $catId * @throws HttpRouteNotFoundException * @return string */ - public function category(string $category = ''): string + public function category(int $catId = 1): string { - // Check if the category is set - if ($category === '') { - // Fetch the default category from the config - $category = config('general.news'); - } + $category = new Category($catId); - // Create the category object - $category = new Category($category); - - if (!$category->posts()) { + if ($category->id === 0) { throw new HttpRouteNotFoundException; } diff --git a/app/News/Category.php b/app/News/Category.php index 619bbce..56a2abe 100644 --- a/app/News/Category.php +++ b/app/News/Category.php @@ -6,6 +6,7 @@ namespace Sakura\News; +use Carbon\Carbon; use Sakura\DB; /** @@ -16,42 +17,84 @@ use Sakura\DB; class Category { /** - * The name over this news category. + * Id of this category. + * @var int + */ + public $id = 0; + + /** + * Name of this category. * @var string */ public $name = ""; /** - * Constructor. - * @param string $name + * Description of this news category. + * @var string */ - public function __construct(string $name) + public $description = null; + + /** + * Whether this category should be hidden. + * @var bool + */ + public $hidden = false; + + /** + * Holds instances of Post. + * @var array + */ + private $postsCache = []; + + /** + * @param int $id + */ + public function __construct(int $id = 0) { - $this->name = $name; + if ($id !== 0) { + $data = DB::table('news_categories') + ->where('category_id', $id) + ->first(); + + if ($data) { + $this->id = intval($data->category_id); + $this->name = $data->category_name; + $this->category = $data->category_description; + $this->hidden = boolval($data->category_hidden); + } + } } /** * Gets the news posts in this category. * @param int $limit + * @param bool $excludeFuture + * @param bool $excludeDeleted * @return array */ - public function posts(int $limit = 0): array + public function posts(int $limit = 0, bool $excludeDeleted = true): array { - $postIds = DB::table('news') - ->where('news_category', $this->name) - ->orderBy('news_id', 'desc'); - if ($limit) { - $postIds->limit($limit); - } - $postIds = $postIds->get(['news_id']); - $postIds = array_column($postIds, 'news_id'); + if (!$this->postsCache) { + $posts = DB::table('news_posts') + ->orderBy('post_id', 'desc'); - $posts = []; + if ($this->id !== 0) { + $posts->where('category_id', $this->id); + } - foreach ($postIds as $post) { - $posts[$post] = new Post($post); + if ($excludeDeleted) { + $posts->whereNull('deleted_at'); + } + + if ($limit) { + $posts->limit($limit); + } + + $this->postsCache = array_map(function ($post) { + return new Post($post->post_id); + }, $posts->get(['post_id'])); } - return $posts; + return $this->postsCache; } } diff --git a/app/News/Post.php b/app/News/Post.php index e9ddb61..af016f3 100644 --- a/app/News/Post.php +++ b/app/News/Post.php @@ -6,6 +6,7 @@ namespace Sakura\News; +use Carbon\Carbon; use Sakura\Comment; use Sakura\DB; use Sakura\User; @@ -30,9 +31,9 @@ class Post /** * The category this post is part of. - * @var string + * @var int */ - public $category = ""; + public $category = 0; /** * The user who made this post. @@ -40,12 +41,6 @@ class Post */ public $user = 0; - /** - * The timestamp when this post was made. - * @var int - */ - public $time = 0; - /** * The title of this news post. * @var string @@ -58,6 +53,24 @@ class Post */ public $text = ""; + /** + * When the post was created. + * @var Carbon + */ + public $created = null; + + /** + * When the post was last updated. + * @var Carbon + */ + public $updated = null; + + /** + * When the post was deleted. + * @var Carbon + */ + public $deleted = null; + /** * A cache of the amount of comments this post has. * @var int @@ -76,19 +89,19 @@ class Post */ public function __construct(int $id = 0) { - // Get comment data from the database - $data = DB::table('news') - ->where('news_id', $id) + $data = DB::table('news_posts') + ->where('post_id', $id) ->first(); - // Check if anything was returned and assign data if ($data) { - $this->id = $data->news_id; - $this->category = $data->news_category; - $this->user = $data->user_id; - $this->time = $data->news_timestamp; - $this->title = $data->news_title; - $this->text = $data->news_content; + $this->id = intval($data->post_id); + $this->category = intval($data->category_id); + $this->user = intval($data->user_id); + $this->title = $data->post_title; + $this->text = $data->post_text; + $this->created = new Carbon($data->created_at); + $this->updated = new Carbon($data->updated_at); + $this->deleted = new Carbon($data->deleted_at); } } @@ -97,24 +110,24 @@ class Post */ public function save(): void { - // Create submission data, insert and update take the same format - $data = [ - 'news_category' => $this->category, - 'user_id' => $this->user, - 'news_timestamp' => $this->time, - 'news_title' => $this->title, - 'news_content' => $this->text, - ]; + // // Create submission data, insert and update take the same format + // $data = [ + // 'news_category' => $this->category, + // 'user_id' => $this->user, + // 'news_timestamp' => $this->time, + // 'news_title' => $this->title, + // 'news_content' => $this->text, + // ]; - // Update if id isn't 0 - if ($this->id) { - DB::table('news') - ->where('news_id', $this->id) - ->update($data); - } else { - $this->id = DB::table('news') - ->insertGetId($data); - } + // // Update if id isn't 0 + // if ($this->id) { + // DB::table('news') + // ->where('news_id', $this->id) + // ->update($data); + // } else { + // $this->id = DB::table('news') + // ->insertGetId($data); + // } } /** @@ -122,11 +135,11 @@ class Post */ public function delete(): void { - DB::table('news') - ->where('news_id', $this->id) - ->delete(); + // DB::table('news') + // ->where('news_id', $this->id) + // ->delete(); - $this->id = 0; + // $this->id = 0; } /** diff --git a/database/2016_12_12_203349_news_refactor.php b/database/2016_12_12_203349_news_refactor.php new file mode 100644 index 0000000..e7f78bf --- /dev/null +++ b/database/2016_12_12_203349_news_refactor.php @@ -0,0 +1,57 @@ +drop('news'); + + $schema->create('news_posts', function (Blueprint $table) { + $table->increments('post_id'); + $table->integer('category_id')->unsigned(); + $table->integer('user_id')->unsigned(); + $table->string('post_title'); + $table->text('post_text'); + $table->timestampTz('created_at')->useCurrent = true; + $table->timestampTz('updated_at')->useCurrent = true; + $table->timestampTz('deleted_at')->nullable()->default(null); + }); + + $schema->create('news_categories', function (Blueprint $table) { + $table->increments('category_id'); + $table->string('category_name'); + $table->string('category_description')->nullable(); + $table->boolean('category_hidden')->default(false); + }); + } + + /** + * Reverse the migrations. + * @return void + */ + public function down() + { + $schema = DB::getSchemaBuilder(); + + $schema->drop('news_posts'); + $schema->drop('news_categories'); + + $schema->create('news', function (Blueprint $table) { + $table->increments('news_id'); + $table->string('news_category', 255); + $table->integer('user_id')->unsigned(); + $table->integer('news_timestamp')->unsigned(); + $table->string('news_title', 255); + $table->text('news_content'); + }); + } +} diff --git a/resources/views/yuuno/elements/newsPost.twig b/resources/views/yuuno/elements/newsPost.twig deleted file mode 100644 index 0881518..0000000 --- a/resources/views/yuuno/elements/newsPost.twig +++ /dev/null @@ -1,17 +0,0 @@ -{% if newsHideTitle is not defined %}{{ post.title }}{% endif %} -
- -
-
- {{ post.userData.username }} -
-
-
- {{ post.text|raw }} -
-
-
-
- Posted - {% if newsHideCommentCount is not defined %}{{ post.commentCount }} comment{% if post.commentCount != 1 %}s{% endif %}{% endif %} -
diff --git a/resources/views/yuuno/forum/elements/forumBase.twig b/resources/views/yuuno/forum/elements/forumBase.twig index 5b9fd9a..94cc0fc 100644 --- a/resources/views/yuuno/forum/elements/forumBase.twig +++ b/resources/views/yuuno/forum/elements/forumBase.twig @@ -24,8 +24,8 @@ {% if not forum.type and forum.id > 0 %} {% set topics = forum.topics|batch(25) %} - {% set paginationPages = topics %} - {% set paginationUrl %}{{ route('forums.forum', forum.id) }}{% endset %} + {% set pagination_pages = topics %} + {% set pagination_url %}{{ route('forums.forum', forum.id) }}{% endset %} {% include 'forum/elements/forumBtns.twig' %} diff --git a/resources/views/yuuno/forum/elements/forumBtns.twig b/resources/views/yuuno/forum/elements/forumBtns.twig index 572c872..a052795 100644 --- a/resources/views/yuuno/forum/elements/forumBtns.twig +++ b/resources/views/yuuno/forum/elements/forumBtns.twig @@ -1,4 +1,4 @@ -{% set paginationClass = 'rightSide' %} +{% from 'macros.twig' import pagination %}
@@ -18,5 +18,7 @@ {% include 'forum/elements/forumMod.twig' %} {% endif %}
- {% include 'elements/pagination.twig' %} + {% if pagination_url is defined %} + {{ pagination(pagination_url, pagination_pages, 'rightSide') }} + {% endif %}
diff --git a/resources/views/yuuno/forum/topic.twig b/resources/views/yuuno/forum/topic.twig index 9880014..8cafe51 100644 --- a/resources/views/yuuno/forum/topic.twig +++ b/resources/views/yuuno/forum/topic.twig @@ -48,8 +48,8 @@ {% set posts = topic.posts|batch(10) %} - {% set paginationPages = posts %} - {% set paginationUrl = route('forums.topic', topic.id) %} + {% set pagination_pages = posts %} + {% set pagination_url = route('forums.topic', topic.id) %} {% endif %} {% block js %} diff --git a/resources/views/yuuno/macros.twig b/resources/views/yuuno/macros.twig index ff95237..fcbb163 100644 --- a/resources/views/yuuno/macros.twig +++ b/resources/views/yuuno/macros.twig @@ -6,3 +6,53 @@ {% endmacro %} + +{% macro news_post(id, text, created, user, title, comments) %} + {% if title is defined and title %} + {{ title }} + {% endif %} +
+ +
+
+ {{ user.username }} +
+
+
+ {{ text|raw }} +
+
+
+
+ Posted + {% if comments is defined and comments %}{{ comments }} comment{% if comments != 1 %}s{% endif %}{% endif %} +
+{% endmacro %} + +{% macro pagination(url, pages, class, name) %} + {% set separator %}{% if '%3F' in url|default('')|url_encode %}&{% else %}?{% endif %}{% endset %} + {% set current_page = get[name|default('page')]|default(1) %} + {% set url = url ~ separator ~ name|default('page') ~ "=" %} + + +{% endmacro %} \ No newline at end of file diff --git a/resources/views/yuuno/master.twig b/resources/views/yuuno/master.twig index f48a9c5..21f1f26 100644 --- a/resources/views/yuuno/master.twig +++ b/resources/views/yuuno/master.twig @@ -39,7 +39,7 @@