Fixed featured post quirks
- fixes for empty database - when post was deleted, new one is selected automatically
This commit is contained in:
parent
7e56fce470
commit
d274f1c044
3 changed files with 44 additions and 30 deletions
|
@ -11,36 +11,12 @@ class IndexController
|
||||||
$this->context->stylesheets []= 'index-index.css';
|
$this->context->stylesheets []= 'index-index.css';
|
||||||
$this->context->transport->postCount = Model_Post::getAllPostCount();
|
$this->context->transport->postCount = Model_Post::getAllPostCount();
|
||||||
|
|
||||||
$featuredPostRotationTime = $this->config->misc->featuredPostMaxDays * 24 * 3600;
|
$featuredPost = $this->getFeaturedPost();
|
||||||
|
if ($featuredPost)
|
||||||
$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())
|
|
||||||
{
|
{
|
||||||
$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->featuredPost = $featuredPost;
|
||||||
$this->context->featuredPostUser = $featuredPostUser;
|
$this->context->featuredPostDate = Model_Property::get(Model_Property::FeaturedPostDate);
|
||||||
$this->context->featuredPostDate = $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]);
|
$this->context->pageThumb = \Chibi\UrlHelper::route('post', 'thumb', ['name' => $featuredPost->name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,4 +38,42 @@ class IndexController
|
||||||
$this->context->subTitle = 'help';
|
$this->context->subTitle = 'help';
|
||||||
$this->context->tab = $tab;
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -424,8 +424,8 @@ class PostController
|
||||||
$post = Model_Post::locate($id);
|
$post = Model_Post::locate($id);
|
||||||
PrivilegesHelper::confirmWithException(Privilege::FeaturePost);
|
PrivilegesHelper::confirmWithException(Privilege::FeaturePost);
|
||||||
Model_Property::set(Model_Property::FeaturedPostId, $post->id);
|
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::FeaturedPostDate, time());
|
||||||
|
Model_Property::set(Model_Property::FeaturedPostUserName, $this->context->user->name);
|
||||||
StatusHelper::success();
|
StatusHelper::success();
|
||||||
LogHelper::log('{user} featured {post} on main page', ['post' => TextHelper::reprPost($post)]);
|
LogHelper::log('{user} featured {post} on main page', ['post' => TextHelper::reprPost($post)]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
class Model_Property extends RedBean_SimpleModel
|
class Model_Property extends RedBean_SimpleModel
|
||||||
{
|
{
|
||||||
const FeaturedPostId = 0;
|
const FeaturedPostId = 0;
|
||||||
const FeaturedPostUserId = 1;
|
const FeaturedPostUserName = 1;
|
||||||
const FeaturedPostDate = 2;
|
const FeaturedPostDate = 2;
|
||||||
|
|
||||||
static $allProperties = null;
|
static $allProperties = null;
|
||||||
|
|
Loading…
Reference in a new issue