Process tag cloud page through Slim controller

This commit is contained in:
ArthurHoaro 2020-01-26 14:35:25 +01:00
parent 03340c18ea
commit c266a89d0f
9 changed files with 107 additions and 49 deletions

View file

@ -87,7 +87,7 @@ function endsWith($haystack, $needle, $case = true)
*
* @param mixed $input Data to escape: a single string or an array of strings.
*
* @return string escaped.
* @return string|array escaped.
*/
function escape($input)
{

View file

@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace Shaarli\Front\Controller;
use Slim\Http\Request;
use Slim\Http\Response;
/**
* Class TagCloud
*
* Slim controller used to render the tag cloud page.
*
* @package Front\Controller
*/
class TagCloudController extends ShaarliController
{
public function index(Request $request, Response $response): Response
{
if ($this->container->loginManager->isLoggedIn() === true) {
$visibility = $this->container->sessionManager->getSessionParameter('visibility');
}
$searchTags = $request->getQueryParam('searchtags');
$filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
$tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null);
// We sort tags alphabetically, then choose a font size according to count.
// First, find max value.
$maxCount = 0;
foreach ($tags as $count) {
$maxCount = max($maxCount, $count);
}
alphabetical_sort($tags, false, true);
$logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
$tagList = [];
foreach ($tags as $key => $value) {
if (in_array($key, $filteringTags)) {
continue;
}
// Tag font size scaling:
// default 15 and 30 logarithm bases affect scaling,
// 2.2 and 0.8 are arbitrary font sizes in em.
$size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
$tagList[$key] = [
'count' => $value,
'size' => number_format($size, 2, '.', ''),
];
}
$searchTags = implode(' ', escape($filteringTags));
$data = [
'search_tags' => $searchTags,
'tags' => $tagList,
];
$data = $this->executeHooks($data);
foreach ($data as $key => $value) {
$this->assignView($key, $value);
}
$searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
$this->assignView(
'pagetitle',
$searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli')
);
return $response->write($this->render('tag.cloud'));
}
/**
* @param mixed[] $data Template data
*
* @return mixed[] Template data after active plugins render_picwall hook execution.
*/
protected function executeHooks(array $data): array
{
$this->container->pluginManager->executeHooks(
'render_tagcloud',
$data,
['loggedin' => $this->container->loginManager->isLoggedIn()]
);
return $data;
}
}

View file

@ -202,4 +202,14 @@ class SessionManager
{
return $this->session;
}
/**
* @param mixed $default value which will be returned if the $key is undefined
*
* @return mixed Content stored in session
*/
public function getSessionParameter(string $key, $default = null)
{
return $this->session[$key] ?? $default;
}
}

View file

@ -44,7 +44,7 @@ http://<replace_domain>/?do=import
http://<replace_domain>/login
http://<replace_domain>/picture-wall
http://<replace_domain>/?do=pluginadmin
http://<replace_domain>/?do=tagcloud
http://<replace_domain>/tag-cloud
http://<replace_domain>/?do=taglist
```

View file

@ -616,49 +616,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
// -------- Tag cloud
if ($targetPage == Router::$PAGE_TAGCLOUD) {
$visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
$filteringTags = isset($_GET['searchtags']) ? explode(' ', $_GET['searchtags']) : [];
$tags = $bookmarkService->bookmarksCountPerTag($filteringTags, $visibility);
// We sort tags alphabetically, then choose a font size according to count.
// First, find max value.
$maxcount = 0;
foreach ($tags as $value) {
$maxcount = max($maxcount, $value);
}
alphabetical_sort($tags, false, true);
$logMaxCount = $maxcount > 1 ? log($maxcount, 30) : 1;
$tagList = array();
foreach ($tags as $key => $value) {
if (in_array($key, $filteringTags)) {
continue;
}
// Tag font size scaling:
// default 15 and 30 logarithm bases affect scaling,
// 2.2 and 0.8 are arbitrary font sizes in em.
$size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
$tagList[$key] = array(
'count' => $value,
'size' => number_format($size, 2, '.', ''),
);
}
$searchTags = implode(' ', escape($filteringTags));
$data = array(
'search_tags' => $searchTags,
'tags' => $tagList,
);
$pluginManager->executeHooks('render_tagcloud', $data, array('loggedin' => $loginManager->isLoggedIn()));
foreach ($data as $key => $value) {
$PAGE->assign($key, $value);
}
$searchTags = ! empty($searchTags) ? $searchTags .' - ' : '';
$PAGE->assign('pagetitle', $searchTags. t('Tag cloud') .' - '. $conf->get('general.title', 'Shaarli'));
$PAGE->renderPage('tag.cloud');
header('Location: ./tag-cloud');
exit;
}
@ -1916,6 +1874,7 @@ $app->group('', function () {
$this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login');
$this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout');
$this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall');
$this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:index')->setName('tagcloud');
$this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
})->add('\Shaarli\Front\ShaarliMiddleware');

View file

@ -30,7 +30,7 @@
</li>
{/if}
<li class="pure-menu-item" id="shaarli-menu-tags">
<a href="./?do=tagcloud" class="pure-menu-link">{'Tag cloud'|t}</a>
<a href="./tag-cloud" class="pure-menu-link">{'Tag cloud'|t}</a>
</li>
{if="$thumbnails_enabled"}
<li class="pure-menu-item" id="shaarli-menu-picwall">

View file

@ -15,7 +15,7 @@
<h2 class="window-title">{'Tag cloud'|t} - {$countTags} {'tags'|t}</h2>
{if="!empty($search_tags)"}
<p class="center">
<a href="?searchtags={$search_tags|urlencode}" class="pure-button pure-button-shaarli">
<a href="./?searchtags={$search_tags|urlencode}" class="pure-button pure-button-shaarli">
{'List all links with those tags'|t}
</a>
</p>

View file

@ -1,7 +1,7 @@
<div class="pure-g">
<div class="pure-u-1 pure-alert pure-alert-success tag-sort">
{'Sort by:'|t}
<a href="./?do=tagcloud">{'Cloud'|t}</a> &middot;
<a href="./tag-cloud">{'Cloud'|t}</a> &middot;
<a href="./?do=taglist&sort=usage">{'Most used'|t}</a> &middot;
<a href="./?do=taglist&sort=alpha">{'Alphabetical'|t}</a>
</div>

View file

@ -31,7 +31,7 @@
{if="$showatom"}
<li><a href="{$feedurl}?do=atom{$searchcrits}" class="nomobile">ATOM Feed</a></li>
{/if}
<li><a href="./?do=tagcloud">Tag cloud</a></li>
<li><a href="./tag-cloud">Tag cloud</a></li>
<li><a href="./picture-wall{function="ltrim($searchcrits, '&')"}">Picture wall</a></li>
<li><a href="./?do=daily">Daily</a></li>
{loop="$plugins_header.buttons_toolbar"}