Added safety browsing settings to post searching

This commit is contained in:
Marcin Kurczewski 2014-10-03 21:09:40 +02:00
parent 8743cda1a0
commit 0e59147423
4 changed files with 65 additions and 1 deletions

1
TODO
View file

@ -5,7 +5,6 @@ everything related to posts:
- post listing
- better thumbnail loading
- comment count
- regard safety settings
- regard disliked settings
- search filters
- comment:rr-

View file

@ -4,6 +4,7 @@ namespace Szurubooru\Controllers;
final class PostController extends AbstractController
{
private $config;
private $authService;
private $privilegeService;
private $postService;
private $postSearchParser;
@ -13,6 +14,7 @@ final class PostController extends AbstractController
public function __construct(
\Szurubooru\Config $config,
\Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\PrivilegeService $privilegeService,
\Szurubooru\Services\PostService $postService,
\Szurubooru\SearchServices\Parsers\PostSearchParser $postSearchParser,
@ -21,6 +23,7 @@ final class PostController extends AbstractController
\Szurubooru\Controllers\ViewProxies\SnapshotViewProxy $snapshotViewProxy)
{
$this->config = $config;
$this->authService = $authService;
$this->privilegeService = $privilegeService;
$this->postService = $postService;
$this->postSearchParser = $postSearchParser;
@ -60,8 +63,11 @@ final class PostController extends AbstractController
public function getFiltered()
{
$this->privilegeService->assertPrivilege(\Szurubooru\Privilege::LIST_POSTS);
$filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader);
$filter->setPageSize($this->config->posts->postsPerPage);
$this->decorateFilterFromBrowsingSettings($filter);
$result = $this->postService->getFiltered($filter);
$entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig());
return [
@ -149,4 +155,32 @@ final class PostController extends AbstractController
\Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_TAGS => true,
];
}
private function decorateFilterFromBrowsingSettings($filter)
{
$userSettings = $this->authService->getLoggedInUser()->getBrowsingSettings();
if (!$userSettings)
return;
if ($userSettings->listPosts and !count($filter->getRequirementsByType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_SAFETY)))
{
$values = [];
if (!\Szurubooru\Helpers\TypeHelper::toBool($userSettings->listPosts->safe))
$values[] = \Szurubooru\Entities\Post::POST_SAFETY_SAFE;
if (!\Szurubooru\Helpers\TypeHelper::toBool($userSettings->listPosts->sketchy))
$values[] = \Szurubooru\Entities\Post::POST_SAFETY_SKETCHY;
if (!\Szurubooru\Helpers\TypeHelper::toBool($userSettings->listPosts->unsafe))
$values[] = \Szurubooru\Entities\Post::POST_SAFETY_UNSAFE;
if (count($values))
{
$requirementValue = new \Szurubooru\SearchServices\Requirements\RequirementCompositeValue();
$requirementValue->setValues($values);
$requirement = new \Szurubooru\SearchServices\Requirements\Requirement();
$requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_SAFETY);
$requirement->setValue($requirementValue);
$requirement->setNegated(true);
$filter->addRequirement($requirement);
}
}
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Szurubooru\Helpers;
class TypeHelper
{
public static function toBool($value)
{
if ($value === 0)
return false;
if ($value === 'false')
return false;
if ($value === false)
return false;
if (is_array($value) and count($value) === 1)
return false;
if ($value === null)
return false;
return true;
}
}

View file

@ -30,6 +30,17 @@ class BasicFilter implements IFilter
return $this->requirements;
}
public function getRequirementsByType($type)
{
$requirements = [];
foreach ($this->getRequirements() as $key => $requirement)
{
if ($requirement->getType() === $type)
$requirements[$key] = $requirement;
}
return $requirements;
}
public function getPageSize()
{
return $this->pageSize;