kitodo-presentation/Classes/Plugins/Calendar.php

308 lines
15 KiB
PHP
Raw Normal View History

<?php
namespace Kitodo\Dlf\Plugins;
2016-09-23 12:24:46 +02:00
/**
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
*
2016-09-23 12:24:46 +02:00
* This file is part of the Kitodo and TYPO3 projects.
*
2016-09-23 12:24:46 +02:00
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
2019-03-13 08:29:51 +01:00
use Kitodo\Dlf\Common\Helper;
/**
2019-03-14 22:41:35 +01:00
* Plugin 'Calendar' for the 'dlf' extension
*
2019-03-14 22:41:35 +01:00
* @author Alexander Bigga <alexander.bigga@slub-dresden.de>
* @package TYPO3
* @subpackage dlf
* @access public
*/
class Calendar extends \Kitodo\Dlf\Common\AbstractPlugin {
public $scriptRelPath = 'Classes/Plugins/Calendar.php';
/**
* The main method of the PlugIn
*
2019-03-14 22:41:35 +01:00
* @access public
*
2019-03-14 22:41:35 +01:00
* @param string $content: The PlugIn content
* @param array $conf: The PlugIn configuration
*
2019-03-14 22:41:35 +01:00
* @return string The content that is displayed on the website
*/
public function main($content, $conf) {
// Nothing to do here.
return $content;
}
/**
* The Calendar Method
*
2019-03-14 22:41:35 +01:00
* @access public
*
2019-03-14 22:41:35 +01:00
* @param string $content: The PlugIn content
* @param array $conf: The PlugIn configuration
*
2019-03-14 22:41:35 +01:00
* @return string The content that is displayed on the website
*/
public function calendar($content, $conf) {
$this->init($conf);
// Load current document.
$this->loadDocument();
if ($this->doc === NULL) {
// Quit without doing anything if required variables are not set.
return $content;
}
// Load template file.
$this->getTemplate('###TEMPLATECALENDAR###');
// Get all children of year anchor.
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_documents.uid AS uid, tx_dlf_documents.title AS title, tx_dlf_documents.year AS year',
'tx_dlf_documents',
2019-03-14 22:41:35 +01:00
'tx_dlf_documents.structure='.Helper::getIdFromIndexName('issue', 'tx_dlf_structures', $this->doc->pid)
.' AND tx_dlf_documents.partof='.intval($this->doc->uid)
.Helper::whereClause('tx_dlf_documents'),
'',
'title ASC',
''
);
// Process results.
while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
2019-03-14 17:39:19 +01:00
$issues[] = [
'uid' => $resArray['uid'],
'title' => $resArray['title'],
'year' => $resArray['year']
2019-03-14 17:39:19 +01:00
];
}
2019-03-16 12:03:25 +01:00
// We need an array of issues with month number as key.
2019-03-14 17:39:19 +01:00
$calendarIssues = [];
foreach ($issues as $issue) {
$calendarIssues[date('n', strtotime($issue['year']))][date('j', strtotime($issue['year']))][] = $issue;
}
2019-03-14 17:39:19 +01:00
$allIssues = [];
// Get subpart templates.
$subparts['list'] = $this->cObj->getSubpart($this->template, '###ISSUELIST###');
$subparts['month'] = $this->cObj->getSubpart($this->template, '###CALMONTH###');
$subparts['week'] = $this->cObj->getSubpart($subparts['month'], '###CALWEEK###');
$subparts['singleday'] = $this->cObj->getSubpart($subparts['list'], '###SINGLEDAY###');
// Build calendar for given year.
$year = date('Y', strtotime($issues[0]['year']));
2019-03-13 08:29:51 +01:00
$subPartContent = '';
for ($i = 0; $i <= 11; $i++) {
2019-03-14 17:39:19 +01:00
$markerArray = [
'###DAYMON_NAME###' => strftime('%a', strtotime('last Monday')),
'###DAYTUE_NAME###' => strftime('%a', strtotime('last Tuesday')),
'###DAYWED_NAME###' => strftime('%a', strtotime('last Wednesday')),
'###DAYTHU_NAME###' => strftime('%a', strtotime('last Thursday')),
'###DAYFRI_NAME###' => strftime('%a', strtotime('last Friday')),
'###DAYSAT_NAME###' => strftime('%a', strtotime('last Saturday')),
'###DAYSUN_NAME###' => strftime('%a', strtotime('last Sunday')),
2019-03-16 12:03:25 +01:00
'###MONTHNAME###' => strftime('%B', strtotime($year.'-'.($i + 1).'-1'))
2019-03-14 17:39:19 +01:00
];
// Reset week content of new month.
$subWeekPartContent = '';
$firstOfMonth = strtotime($year.'-'.($i + 1).'-1');
$lastOfMonth = strtotime('last day of', ($firstOfMonth));
$firstOfMonthStart = strtotime('last Monday', $firstOfMonth);
// There are never more than 6 weeks in a month.
for ($j = 0; $j <= 5; $j++) {
$firstDayOfWeek = strtotime('+ '.$j.' Week', $firstOfMonthStart);
2019-03-14 17:39:19 +01:00
$weekArray = [
'###DAYMON###' => '&nbsp;',
'###DAYTUE###' => '&nbsp;',
'###DAYWED###' => '&nbsp;',
'###DAYTHU###' => '&nbsp;',
'###DAYFRI###' => '&nbsp;',
'###DAYSAT###' => '&nbsp;',
'###DAYSUN###' => '&nbsp;',
2019-03-14 17:39:19 +01:00
];
// Every week has seven days. ;-)
for ($k = 0; $k <= 6; $k++) {
$currentDayTime = strtotime('+ '.$k.' Day', $firstDayOfWeek);
2019-03-14 22:41:35 +01:00
if ($currentDayTime >= $firstOfMonth
&& $currentDayTime <= $lastOfMonth) {
$dayLinks = '';
2019-03-14 17:39:19 +01:00
$dayLinksText = [];
$dayLinksList = '';
$currentMonth = date('n', $currentDayTime);
if (is_array($calendarIssues[$currentMonth])) {
foreach ($calendarIssues[$currentMonth] as $id => $day) {
if ($id == date('j', $currentDayTime)) {
$dayLinks = $id;
foreach ($day as $issue) {
$dayLinkLabel = empty($issue['title']) ? strftime('%x', $currentDayTime) : $issue['title'];
2019-03-14 17:39:19 +01:00
$linkConf = [
'useCacheHash' => 1,
'parameter' => $this->conf['targetPid'],
'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($issue['uid']),
'ATagParams' => ' class="title"',
2019-03-14 17:39:19 +01:00
];
$dayLinksText[] = $this->cObj->typoLink($dayLinkLabel, $linkConf);
// Save issues for list view.
$allIssues[$currentDayTime][] = $this->cObj->typoLink($dayLinkLabel, $linkConf);
}
}
}
if (!empty($dayLinksText)) {
$dayLinksList = '<ul>';
foreach ($dayLinksText as $link) {
$dayLinksList .= '<li>'.$link.'</li>';
}
$dayLinksList .= '</ul>';
}
$dayLinkDiv = '<div class="issues"><h4>'.strftime('%d', $currentDayTime).'</h4><div>'.$dayLinksList.'</div></div>';
}
switch (strftime('%w', strtotime('+ '.$k.' Day', $firstDayOfWeek))) {
2019-03-14 22:41:35 +01:00
case '0':
$weekArray['###DAYSUN###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
break;
2019-03-14 22:41:35 +01:00
case '1':
$weekArray['###DAYMON###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
break;
2019-03-14 22:41:35 +01:00
case '2':
$weekArray['###DAYTUE###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
break;
2019-03-14 22:41:35 +01:00
case '3':
$weekArray['###DAYWED###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
break;
2019-03-14 22:41:35 +01:00
case '4':
$weekArray['###DAYTHU###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
break;
2019-03-14 22:41:35 +01:00
case '5':
$weekArray['###DAYFRI###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
break;
2019-03-14 22:41:35 +01:00
case '6':
$weekArray['###DAYSAT###'] = ((int) $dayLinks === (int) date('j', $currentDayTime)) ? $dayLinkDiv : strftime('%d', $currentDayTime);
break;
}
}
}
// Fill the weeks.
$subWeekPartContent .= $this->cObj->substituteMarkerArray($subparts['week'], $weekArray);
}
// Fill the month markers.
$subPartContent .= $this->cObj->substituteMarkerArray($subparts['month'], $markerArray);
// Fill the week markers with the week entries.
$subPartContent = $this->cObj->substituteSubpart($subPartContent, '###CALWEEK###', $subWeekPartContent);
}
// Link to years overview
2019-03-14 17:39:19 +01:00
$linkConf = [
'useCacheHash' => 1,
'parameter' => $this->conf['targetPid'],
'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($this->doc->parentId),
2019-03-14 17:39:19 +01:00
];
$allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', TRUE).' '.$this->doc->getTitle($this->doc->parentId), $linkConf);
// Link to current year.
2019-03-14 17:39:19 +01:00
$linkConf = [
'useCacheHash' => 1,
'parameter' => $this->conf['targetPid'],
'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($this->doc->uid),
2019-03-14 17:39:19 +01:00
];
$yearLink = $this->cObj->typoLink($year, $linkConf);
2019-03-13 08:29:51 +01:00
$subPartContentList = '';
// Prepare list as alternative of the calendar view.
foreach ($allIssues as $dayTime => $issues) {
$markerArrayDay['###DATE_STRING###'] = strftime('%A, %x', $dayTime);
$markerArrayDay['###ITEMS###'] = '';
foreach ($issues as $issue) {
$markerArrayDay['###ITEMS###'] .= $issue;
}
$subPartContentList .= $this->cObj->substituteMarkerArray($subparts['singleday'], $markerArrayDay);
}
$this->template = $this->cObj->substituteSubpart($this->template, '###SINGLEDAY###', $subPartContentList);
if (count($allIssues) < 6) {
$listViewActive = TRUE;
} else {
$listViewActive = FALSE;
}
2019-03-14 17:39:19 +01:00
$markerArray = [
'###CALENDARVIEWACTIVE###' => $listViewActive ? '' : 'active',
'###LISTVIEWACTIVE###' => $listViewActive ? 'active' : '',
'###CALYEAR###' => $yearLink,
'###CALALLYEARS###' => $allYearsLink,
'###LABEL_CALENDAR###' => $this->pi_getLL('label.view_calendar'),
'###LABEL_LIST_VIEW###' => $this->pi_getLL('label.view_list'),
2019-03-14 17:39:19 +01:00
];
$this->template = $this->cObj->substituteMarkerArray($this->template, $markerArray);
return $this->cObj->substituteSubpart($this->template, '###CALMONTH###', $subPartContent);
}
/**
* The Year Method
*
2019-03-14 22:41:35 +01:00
* @access public
*
2019-03-14 22:41:35 +01:00
* @param string $content: The PlugIn content
* @param array $conf: The PlugIn configuration
*
2019-03-14 22:41:35 +01:00
* @return string The content that is displayed on the website
*/
public function years($content, $conf) {
$this->init($conf);
// Load current document.
$this->loadDocument();
if ($this->doc === NULL) {
// Quit without doing anything if required variables are not set.
return $content;
}
// Load template file.
$this->getTemplate('###TEMPLATEYEAR###');
// Get subpart templates
$subparts['year'] = $this->cObj->getSubpart($this->template, '###LISTYEAR###');
2019-03-14 22:41:35 +01:00
// Get the title of the anchor file
$titleAnchor = $this->doc->getTitle($this->doc->uid);
2019-03-14 22:41:35 +01:00
// Get all children of anchor. This should be the year anchor documents
$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_dlf_documents.uid AS uid, tx_dlf_documents.title AS title',
'tx_dlf_documents',
2019-03-14 22:41:35 +01:00
'tx_dlf_documents.structure='.Helper::getIdFromIndexName('year', 'tx_dlf_structures', $this->doc->pid)
.' AND tx_dlf_documents.partof='.intval($this->doc->uid)
.Helper::whereClause('tx_dlf_documents'),
'',
'title ASC',
''
);
// Process results.
while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
2019-03-14 17:39:19 +01:00
$years[] = [
'title' => $resArray['title'],
'uid' => $resArray['uid']
2019-03-14 17:39:19 +01:00
];
}
$subYearPartContent = '';
if (count($years) > 0) {
foreach ($years as $id => $year) {
2019-03-14 17:39:19 +01:00
$linkConf = [
'useCacheHash' => 1,
'parameter' => $this->conf['targetPid'],
'additionalParams' => '&'.$this->prefixId.'[id]='.urlencode($year['uid']),
'title' => $titleAnchor.': '.$year['title']
2019-03-14 17:39:19 +01:00
];
$yearArray = [
'###YEARNAME###' => $this->cObj->typoLink($year['title'], $linkConf),
2019-03-14 17:39:19 +01:00
];
$subYearPartContent .= $this->cObj->substituteMarkerArray($subparts['year'], $yearArray);
}
}
// link to years overview (should be itself here)
2019-03-14 17:39:19 +01:00
$linkConf = [
'useCacheHash' => 1,
'parameter' => $this->conf['targetPid'],
'additionalParams' => '&'.$this->prefixId.'[id]='.$this->doc->uid,
2019-03-14 17:39:19 +01:00
];
$allYearsLink = $this->cObj->typoLink($this->pi_getLL('allYears', '', TRUE).' '.$this->doc->getTitle($this->doc->uid), $linkConf);
// Fill markers.
2019-03-14 17:39:19 +01:00
$markerArray = [
'###LABEL_CHOOSE_YEAR###' => $this->pi_getLL('label.please_choose_year'),
'###CALALLYEARS###' => $allYearsLink
2019-03-14 17:39:19 +01:00
];
$this->template = $this->cObj->substituteMarkerArray($this->template, $markerArray);
2019-03-14 22:41:35 +01:00
// Fill the week markers
return $this->cObj->substituteSubpart($this->template, '###LISTYEAR###', $subYearPartContent);
}
}