Moved tag listing to API

This commit is contained in:
Marcin Kurczewski 2014-05-04 08:42:18 +02:00
parent 259eabfaaa
commit 97c17c68a0
6 changed files with 68 additions and 43 deletions

View file

@ -116,18 +116,18 @@ $commentValidation =
\Chibi\Router::register(['CommentController', 'editView'], 'GET', '/comment/{id}/edit', $commentValidation); \Chibi\Router::register(['CommentController', 'editView'], 'GET', '/comment/{id}/edit', $commentValidation);
\Chibi\Router::register(['CommentController', 'editAction'], 'POST', '/comment/{id}/edit', $commentValidation); \Chibi\Router::register(['CommentController', 'editAction'], 'POST', '/comment/{id}/edit', $commentValidation);
foreach (['GET', 'POST'] as $method) $tagValidation =
{ [
$tagValidation =
[
'page' => '\d*', 'page' => '\d*',
'filter' => '[^\/]+', 'filter' => '[^\/]+',
]; ];
\Chibi\Router::register(['TagController', 'listAction'], $method, '/tags', $tagValidation); \Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags', $tagValidation);
\Chibi\Router::register(['TagController', 'listAction'], $method, '/tags/{filter}', $tagValidation); \Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags/{page}', $tagValidation);
\Chibi\Router::register(['TagController', 'listAction'], $method, '/tags/{page}', $tagValidation); \Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags/{filter}/{page}', $tagValidation);
\Chibi\Router::register(['TagController', 'listAction'], $method, '/tags/{filter}/{page}', $tagValidation);
foreach (['GET', 'POST'] as $method)
{
\Chibi\Router::register(['TagController', 'autoCompleteAction'], $method, '/tags-autocomplete', $tagValidation); \Chibi\Router::register(['TagController', 'autoCompleteAction'], $method, '/tags-autocomplete', $tagValidation);
\Chibi\Router::register(['TagController', 'relatedAction'], $method, '/tags-related', $tagValidation); \Chibi\Router::register(['TagController', 'relatedAction'], $method, '/tags-related', $tagValidation);
\Chibi\Router::register(['TagController', 'mergeAction'], $method, '/tags-merge', $tagValidation); \Chibi\Router::register(['TagController', 'mergeAction'], $method, '/tags-merge', $tagValidation);

View file

@ -0,0 +1,39 @@
<?php
class ListTagsJob extends AbstractJob
{
public function execute()
{
$page = $this->getArgument(self::PAGE_NUMBER);
$query = $this->getArgument(self::QUERY);
$page = max(1, intval($page));
$tagsPerPage = intval(getConfig()->browsing->tagsPerPage);
$tags = TagSearchService::getEntitiesRows($query, $tagsPerPage, $page);
$tagCount = TagSearchService::getEntityCount($query);
$pageCount = ceil($tagCount / $tagsPerPage);
$page = min($pageCount, $page);
$ret = new StdClass;
$ret->tags = $tags;
$ret->tagCount = $tagCount;
$ret->page = $page;
$ret->pageCount = $pageCount;
return $ret;
}
public function requiresPrivilege()
{
return Privilege::ListTags;
}
public function requiresAuthentication()
{
return false;
}
public function requiresConfirmedEmail()
{
return false;
}
}

View file

@ -1,39 +1,25 @@
<?php <?php
class TagController class TagController
{ {
public function listAction($filter = null, $page = 1) public function listView($filter = 'order:alpha,asc', $page = 1)
{ {
$ret = Api::run(
new ListTagsJob(),
[
ListTagsJob::PAGE_NUMBER => $page,
ListTagsJob::QUERY => $filter,
]);
$context = getContext(); $context = getContext();
$context->viewName = 'tag-list-wrapper'; $context->viewName = 'tag-list-wrapper';
Access::assert(Privilege::ListTags);
$suppliedFilter = $filter ?: 'order:alpha,asc';
$page = max(1, intval($page));
$tagsPerPage = intval(getConfig()->browsing->tagsPerPage);
$tags = TagSearchService::getEntitiesRows($suppliedFilter, $tagsPerPage, $page);
$tagCount = TagSearchService::getEntityCount($suppliedFilter);
$pageCount = ceil($tagCount / $tagsPerPage);
$page = min($pageCount, $page);
$context->filter = $suppliedFilter;
$context->transport->tags = $tags;
if ($context->json)
{
$context->transport->tags = array_values(array_map(function($tag) {
return ['name' => $tag['name'], 'count' => $tag['post_count']];
}, $context->transport->tags));
}
else
{
$context->highestUsage = TagSearchService::getMostUsedTag()['post_count']; $context->highestUsage = TagSearchService::getMostUsedTag()['post_count'];
$context->filter = $filter;
$context->transport->tags = $ret->tags;
$context->transport->paginator = new StdClass; $context->transport->paginator = new StdClass;
$context->transport->paginator->page = $page; $context->transport->paginator->page = $ret->page;
$context->transport->paginator->pageCount = $pageCount; $context->transport->paginator->pageCount = $ret->pageCount;
$context->transport->paginator->entityCount = $tagCount; $context->transport->paginator->entityCount = $ret->tagCount;
$context->transport->paginator->entities = $tags; $context->transport->paginator->entities = $ret->tags;
}
} }
public function autoCompleteAction() public function autoCompleteAction()

View file

@ -3,7 +3,7 @@ Assets::setSubTitle('tags');
Assets::addStylesheet('tag-list.css'); Assets::addStylesheet('tag-list.css');
$tabs = []; $tabs = [];
if (Access::check(Privilege::ListTags)) $tabs['list'] = ['List', 'listAction']; if (Access::check(Privilege::ListTags)) $tabs['list'] = ['List', 'listView'];
if (Access::check(Privilege::RenameTags)) $tabs['rename'] = ['Rename', 'renameAction']; if (Access::check(Privilege::RenameTags)) $tabs['rename'] = ['Rename', 'renameAction'];
if (Access::check(Privilege::MergeTags)) $tabs['merge'] = ['Merge', 'mergeAction']; if (Access::check(Privilege::MergeTags)) $tabs['merge'] = ['Merge', 'mergeAction'];
if (Access::check(Privilege::MassTag)) $tabs['mass-tag-redirect'] = ['Mass tag', 'massTagRedirectAction']; if (Access::check(Privilege::MassTag)) $tabs['mass-tag-redirect'] = ['Mass tag', 'massTagRedirectAction'];

View file

@ -16,7 +16,7 @@
<?php else: ?> <?php else: ?>
<li> <li>
<?php endif ?> <?php endif ?>
<a href="<?= \Chibi\Router::linkTo(['TagController', 'listAction'], ['filter' => $key]) ?>"><?= $text ?></a> <a href="<?= \Chibi\Router::linkTo(['TagController', 'listView'], ['filter' => $key]) ?>"><?= $text ?></a>
</li> </li>
<?php endforeach ?> <?php endforeach ?>
</ul> </ul>

View file

@ -46,7 +46,7 @@
{ {
$registerNavItem( $registerNavItem(
'Tags', 'Tags',
\Chibi\Router::linkTo(['TagController', 'listAction']), \Chibi\Router::linkTo(['TagController', 'listView']),
$activeController == 'tag'); $activeController == 'tag');
} }