szurubooru/src/Controllers/UserController.php

137 lines
3.3 KiB
PHP
Raw Normal View History

2013-10-05 19:24:08 +02:00
<?php
2013-10-05 21:24:20 +02:00
class UserController
2013-10-05 19:24:08 +02:00
{
2013-10-14 10:22:53 +02:00
private static function locateUser($key)
{
$user = R::findOne('user', 'name = ?', [$key]);
if (!$user)
throw new SimpleException('Invalid user name "' . $key . '"');
return $user;
}
2013-10-05 19:24:08 +02:00
/**
* @route /users
*/
public function listAction()
{
2013-10-05 21:22:28 +02:00
$this->context->subTitle = 'users';
2013-10-12 22:37:18 +02:00
throw new SimpleException('Not implemented');
2013-10-05 19:24:08 +02:00
}
2013-10-14 10:22:53 +02:00
2013-10-05 19:24:08 +02:00
/**
* @route /user/{name}
2013-10-14 10:22:53 +02:00
* @route /user/{name}/{tab}/{page}
2013-10-05 19:24:08 +02:00
* @validate name [^\/]+
2013-10-14 10:22:53 +02:00
* @validate tab favs|uploads
* @validate page \d*
2013-10-05 19:24:08 +02:00
*/
2013-10-14 10:22:53 +02:00
public function viewAction($name, $tab, $page)
2013-10-05 19:24:08 +02:00
{
2013-10-14 10:22:53 +02:00
$this->context->stylesheets []= 'user-view.css';
$this->context->stylesheets []= 'post-list.css';
$this->context->stylesheets []= 'paginator.css';
if ($this->config->browsing->endlessScrolling)
$this->context->scripts []= 'paginator-endless.js';
2013-10-05 21:22:28 +02:00
$this->context->subTitle = $name;
2013-10-14 10:22:53 +02:00
PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewUser);
$postsPerPage = intval($this->config->browsing->postsPerPage);
$user = self::locateUser($name);
if ($tab === null)
$tab = 'favs';
if ($page === null)
$page = 1;
$buildDbQuery = function($dbQuery, $user, $tab)
{
$dbQuery->from('post');
/* safety */
$allowedSafety = array_filter(PostSafety::getAll(), function($safety)
{
return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)) and
$this->context->user->hasEnabledSafety($safety);
});
$dbQuery->where('safety IN (' . R::genSlots($allowedSafety) . ')');
foreach ($allowedSafety as $s)
$dbQuery->put($s);
/* hidden */
if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden'))
$dbQuery->andNot('hidden');
/* tab */
switch ($tab)
{
case 'uploads':
$dbQuery
->and('uploader_id = ?')
->put($user->id);
break;
case 'favs':
$dbQuery
->and()
->exists()
->open()
->select('1')
->from('favoritee')
->where('post_id = post.id')
->and('favoritee.user_id = ?')
->put($user->id)
->close();
break;
}
};
$countDbQuery = R::$f->begin()->select('COUNT(*)')->as('count');
$buildDbQuery($countDbQuery, $user, $tab);
$postCount = intval($countDbQuery->get('row')['count']);
$pageCount = ceil($postCount / $postsPerPage);
$page = max(1, min($pageCount, $page));
$searchDbQuery = R::$f->begin()->select('*');
$buildDbQuery($searchDbQuery, $user, $tab);
$searchDbQuery->orderBy('id DESC')
->limit('?')
->put($postsPerPage)
->offset('?')
->put(($page - 1) * $postsPerPage);
$posts = $searchDbQuery->get();
$this->context->transport->user = $user;
$this->context->transport->tab = $tab;
$this->context->transport->page = $page;
$this->context->transport->postCount = $postCount;
$this->context->transport->pageCount = $pageCount;
$this->context->transport->posts = $posts;
2013-10-05 19:24:08 +02:00
}
2013-10-14 00:25:40 +02:00
2013-10-14 10:22:53 +02:00
2013-10-14 00:25:40 +02:00
/**
* @route /user/toggle-safety/{safety}
*/
public function toggleSafetyAction($safety)
{
if (!$this->context->loggedIn)
throw new SimpleException('Not logged in');
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleExcetpion('Invalid safety');
$this->context->user->enableSafety($safety,
!$this->context->user->hasEnabledSafety($safety));
R::store($this->context->user);
$this->context->transport->success = true;
}
2013-10-05 19:24:08 +02:00
}