From d274f1c044427ec7054624696acaf9a4e9ef410a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 23 Nov 2013 15:57:38 +0100 Subject: [PATCH] Fixed featured post quirks - fixes for empty database - when post was deleted, new one is selected automatically --- src/Controllers/IndexController.php | 70 +++++++++++++++++------------ src/Controllers/PostController.php | 2 +- src/Models/Model_Property.php | 2 +- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/Controllers/IndexController.php b/src/Controllers/IndexController.php index 06a64213..c0c21e5e 100644 --- a/src/Controllers/IndexController.php +++ b/src/Controllers/IndexController.php @@ -11,36 +11,12 @@ class IndexController $this->context->stylesheets []= 'index-index.css'; $this->context->transport->postCount = Model_Post::getAllPostCount(); - $featuredPostRotationTime = $this->config->misc->featuredPostMaxDays * 24 * 3600; - - $featuredPostId = Model_Property::get(Model_Property::FeaturedPostId); - $featuredPostUserId = Model_Property::get(Model_Property::FeaturedPostUserId); - $featuredPostDate = Model_Property::get(Model_Property::FeaturedPostDate); - if (!$featuredPostId or $featuredPostDate + $featuredPostRotationTime < time()) + $featuredPost = $this->getFeaturedPost(); + if ($featuredPost) { - $featuredPostId = R::$f->begin() - ->select('id') - ->from('post') - ->where('type = ?')->put(PostType::Image) - ->and('safety = ?')->put(PostSafety::Safe) - ->orderBy('random()') - ->desc() - ->get('row')['id']; - $featuredPostUserId = null; - $featuredPostDate = time(); - Model_Property::set(Model_Property::FeaturedPostId, $featuredPostId); - Model_Property::set(Model_Property::FeaturedPostUserId, $featuredPostUserId); - Model_Property::set(Model_Property::FeaturedPostDate, $featuredPostDate); - } - - if ($featuredPostId !== null) - { - $featuredPost = Model_Post::locate($featuredPostId); - R::preload($featuredPost, ['user', 'comment', 'favoritee']); - $featuredPostUser = R::findOne('user', 'id = ?', [$featuredPostUserId]); $this->context->featuredPost = $featuredPost; - $this->context->featuredPostUser = $featuredPostUser; - $this->context->featuredPostDate = $featuredPostDate; + $this->context->featuredPostDate = Model_Property::get(Model_Property::FeaturedPostDate); + $this->context->featuredPostUser = Model_User::locate(Model_Property::get(Model_Property::FeaturedPostUserName), false); $this->context->pageThumb = \Chibi\UrlHelper::route('post', 'thumb', ['name' => $featuredPost->name]); } } @@ -62,4 +38,42 @@ class IndexController $this->context->subTitle = 'help'; $this->context->tab = $tab; } + + private function getFeaturedPost() + { + $featuredPostRotationTime = $this->config->misc->featuredPostMaxDays * 24 * 3600; + + $featuredPostId = Model_Property::get(Model_Property::FeaturedPostId); + $featuredPostDate = Model_Property::get(Model_Property::FeaturedPostDate); + + //check if too old + if (!$featuredPostId or $featuredPostDate + $featuredPostRotationTime < time()) + return $this->featureNewPost(); + + //check if post was deleted + $featuredPost = Model_Post::locate($featuredPostId, false, false); + if (!$featuredPost) + return $this->featureNewPost(); + + return $featuredPost; + } + + private function featureNewPost() + { + $featuredPostId = R::$f->begin() + ->select('id') + ->from('post') + ->where('type = ?')->put(PostType::Image) + ->and('safety = ?')->put(PostSafety::Safe) + ->orderBy('random()') + ->desc() + ->get('row')['id']; + if (!$featuredPostId) + return null; + + Model_Property::set(Model_Property::FeaturedPostId, $featuredPostId); + Model_Property::set(Model_Property::FeaturedPostDate, time()); + Model_Property::set(Model_Property::FeaturedPostUserName, null); + return Model_Post::locate($featuredPostId); + } } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index ca52e93e..25ed9ef2 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -424,8 +424,8 @@ class PostController $post = Model_Post::locate($id); PrivilegesHelper::confirmWithException(Privilege::FeaturePost); Model_Property::set(Model_Property::FeaturedPostId, $post->id); - Model_Property::set(Model_Property::FeaturedPostUserId, $this->context->user->id); Model_Property::set(Model_Property::FeaturedPostDate, time()); + Model_Property::set(Model_Property::FeaturedPostUserName, $this->context->user->name); StatusHelper::success(); LogHelper::log('{user} featured {post} on main page', ['post' => TextHelper::reprPost($post)]); } diff --git a/src/Models/Model_Property.php b/src/Models/Model_Property.php index 3ee9b97e..06da4df8 100644 --- a/src/Models/Model_Property.php +++ b/src/Models/Model_Property.php @@ -2,7 +2,7 @@ class Model_Property extends RedBean_SimpleModel { const FeaturedPostId = 0; - const FeaturedPostUserId = 1; + const FeaturedPostUserName = 1; const FeaturedPostDate = 2; static $allProperties = null;