szurubooru/src/Controllers/IndexController.php

79 lines
2.4 KiB
PHP
Raw Normal View History

2013-10-05 19:24:08 +02:00
<?php
2013-10-05 21:24:20 +02:00
class IndexController
2013-10-05 19:24:08 +02:00
{
/**
* @route /
* @route /index
*/
2013-10-05 21:22:28 +02:00
public function indexAction()
2013-10-05 19:24:08 +02:00
{
2013-10-05 21:22:28 +02:00
$this->context->subTitle = 'home';
2013-10-19 13:38:20 +02:00
$this->context->stylesheets []= 'index-index.css';
$this->context->transport->postCount = Model_Post::getAllPostCount();
2013-10-19 13:38:20 +02:00
$featuredPost = $this->getFeaturedPost();
if ($featuredPost)
2013-10-19 13:38:20 +02:00
{
$this->context->featuredPost = $featuredPost;
$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]);
2013-10-19 13:38:20 +02:00
}
2013-10-05 19:24:08 +02:00
}
/**
* @route /help
2013-11-21 15:16:27 +01:00
* @route /help/{tab}
*/
2013-11-21 15:16:27 +01:00
public function helpAction($tab = null)
{
2013-11-21 15:16:27 +01:00
if (empty($this->config->help->paths) or empty($this->config->help->title))
throw new SimpleException('Help is disabled');
$tab = $tab ?: array_keys($this->config->help->subTitles)[0];
if (!isset($this->config->help->paths[$tab]))
throw new SimpleException('Invalid tab');
$this->context->path = TextHelper::absolutePath($this->config->help->paths[$tab]);
2013-10-25 17:20:11 +02:00
$this->context->stylesheets []= 'index-help.css';
$this->context->subTitle = 'help';
2013-11-21 15:16:27 +01:00
$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)
2013-12-14 11:14:36 +01:00
->orderBy($this->config->main->dbDriver == 'sqlite' ? 'random()' : 'rand()')
->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);
}
2013-10-05 19:24:08 +02:00
}