aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-17 11:06:39 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit69e29ff65ef56b886748c58ba5b037cf217c4a1d (patch)
treeab27ad9100bc72cb3c9417b623776bc8cfa6d421 /index.php
parent60ae241251b753fc052e50ebd95277dfcb074cb0 (diff)
downloadShaarli-69e29ff65ef56b886748c58ba5b037cf217c4a1d.tar.gz
Shaarli-69e29ff65ef56b886748c58ba5b037cf217c4a1d.tar.zst
Shaarli-69e29ff65ef56b886748c58ba5b037cf217c4a1d.zip
Process daily page through Slim controller
Diffstat (limited to 'index.php')
-rw-r--r--index.php111
1 files changed, 4 insertions, 107 deletions
diff --git a/index.php b/index.php
index 89a1e581..f8337d79 100644
--- a/index.php
+++ b/index.php
@@ -399,112 +399,6 @@ function showDailyRSS($bookmarkService, $conf, $loginManager)
399} 399}
400 400
401/** 401/**
402 * Show the 'Daily' page.
403 *
404 * @param PageBuilder $pageBuilder Template engine wrapper.
405 * @param BookmarkServiceInterface $bookmarkService instance.
406 * @param ConfigManager $conf Configuration Manager instance.
407 * @param PluginManager $pluginManager Plugin Manager instance.
408 * @param LoginManager $loginManager Login Manager instance
409 */
410function showDaily($pageBuilder, $bookmarkService, $conf, $pluginManager, $loginManager)
411{
412 if (isset($_GET['day'])) {
413 $day = $_GET['day'];
414 if ($day === date('Ymd', strtotime('now'))) {
415 $pageBuilder->assign('dayDesc', t('Today'));
416 } elseif ($day === date('Ymd', strtotime('-1 days'))) {
417 $pageBuilder->assign('dayDesc', t('Yesterday'));
418 }
419 } else {
420 $day = date('Ymd', strtotime('now')); // Today, in format YYYYMMDD.
421 $pageBuilder->assign('dayDesc', t('Today'));
422 }
423
424 $days = $bookmarkService->days();
425 $i = array_search($day, $days);
426 if ($i === false && count($days)) {
427 // no bookmarks for day, but at least one day with bookmarks
428 $i = count($days) - 1;
429 $day = $days[$i];
430 }
431 $previousday = '';
432 $nextday = '';
433
434 if ($i !== false) {
435 if ($i >= 1) {
436 $previousday = $days[$i - 1];
437 }
438 if ($i < count($days) - 1) {
439 $nextday = $days[$i + 1];
440 }
441 }
442 try {
443 $linksToDisplay = $bookmarkService->filterDay($day);
444 } catch (Exception $exc) {
445 error_log($exc);
446 $linksToDisplay = [];
447 }
448
449 $factory = new FormatterFactory($conf, $loginManager->isLoggedIn());
450 $formatter = $factory->getFormatter();
451 // We pre-format some fields for proper output.
452 foreach ($linksToDisplay as $key => $bookmark) {
453 $linksToDisplay[$key] = $formatter->format($bookmark);
454 // This page is a bit specific, we need raw description to calculate the length
455 $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
456 $linksToDisplay[$key]['description'] = $bookmark->getDescription();
457 }
458
459 $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
460 $data = array(
461 'pagetitle' => $conf->get('general.title') .' - '. format_date($dayDate, false),
462 'linksToDisplay' => $linksToDisplay,
463 'day' => $dayDate->getTimestamp(),
464 'dayDate' => $dayDate,
465 'previousday' => $previousday,
466 'nextday' => $nextday,
467 );
468
469 /* Hook is called before column construction so that plugins don't have
470 to deal with columns. */
471 $pluginManager->executeHooks('render_daily', $data, array('loggedin' => $loginManager->isLoggedIn()));
472
473 /* We need to spread the articles on 3 columns.
474 I did not want to use a JavaScript lib like http://masonry.desandro.com/
475 so I manually spread entries with a simple method: I roughly evaluate the
476 height of a div according to title and description length.
477 */
478 $columns = array(array(), array(), array()); // Entries to display, for each column.
479 $fill = array(0, 0, 0); // Rough estimate of columns fill.
480 foreach ($data['linksToDisplay'] as $key => $bookmark) {
481 // Roughly estimate length of entry (by counting characters)
482 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
483 // Description: 836 characters gives roughly 342 pixel height.
484 // This is not perfect, but it's usually OK.
485 $length = strlen($bookmark['title']) + (342 * strlen($bookmark['description'])) / 836;
486 if (! empty($bookmark['thumbnail'])) {
487 $length += 100; // 1 thumbnails roughly takes 100 pixels height.
488 }
489 // Then put in column which is the less filled:
490 $smallest = min($fill); // find smallest value in array.
491 $index = array_search($smallest, $fill); // find index of this smallest value.
492 array_push($columns[$index], $bookmark); // Put entry in this column.
493 $fill[$index] += $length;
494 }
495
496 $data['cols'] = $columns;
497
498 foreach ($data as $key => $value) {
499 $pageBuilder->assign($key, $value);
500 }
501
502 $pageBuilder->assign('pagetitle', t('Daily') .' - '. $conf->get('general.title', 'Shaarli'));
503 $pageBuilder->renderPage('daily');
504 exit;
505}
506
507/**
508 * Renders the linklist 402 * Renders the linklist
509 * 403 *
510 * @param pageBuilder $PAGE pageBuilder instance. 404 * @param pageBuilder $PAGE pageBuilder instance.
@@ -628,7 +522,8 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
628 522
629 // Daily page. 523 // Daily page.
630 if ($targetPage == Router::$PAGE_DAILY) { 524 if ($targetPage == Router::$PAGE_DAILY) {
631 showDaily($PAGE, $bookmarkService, $conf, $pluginManager, $loginManager); 525 header('Location: ./daily');
526 exit;
632 } 527 }
633 528
634 // ATOM and RSS feed. 529 // ATOM and RSS feed.
@@ -1850,6 +1745,8 @@ $app->group('', function () {
1850 $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall'); 1745 $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall');
1851 $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:cloud')->setName('tagcloud'); 1746 $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:cloud')->setName('tagcloud');
1852 $this->get('/tag-list', '\Shaarli\Front\Controller\TagCloudController:list')->setName('taglist'); 1747 $this->get('/tag-list', '\Shaarli\Front\Controller\TagCloudController:list')->setName('taglist');
1748 $this->get('/daily', '\Shaarli\Front\Controller\DailyController:index')->setName('daily');
1749
1853 $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag'); 1750 $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
1854})->add('\Shaarli\Front\ShaarliMiddleware'); 1751})->add('\Shaarli\Front\ShaarliMiddleware');
1855 1752