Source of file DatedUpdateHolderController.php
Size: 12,531 Bytes - Last Modified: 2021-12-24T06:43:14+00:00
/var/www/docs.ssmods.com/process/src/src/PageTypes/DatedUpdateHolderController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 | <?php namespace CWP\CWP\PageTypes; use CWP\Core\Feed\CwpAtomFeed; use InvalidArgumentException; use PageController; use SilverStripe\Control\Director; use SilverStripe\Control\HTTP; use SilverStripe\Control\RSS\RSSFeed; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\DateField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\HiddenField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\PaginatedList; use SilverStripe\ORM\ValidationResult; use SilverStripe\Taxonomy\TaxonomyTerm; /** * The parameters apply in the following preference order: * - Highest priority: Tag & date (or date range) * - Month (and Year) * - Pagination * * So, when the user click on a tag link, the pagination, and month will be reset, but not the date filter. Also, * changing the date will not affect the tag, but will reset the month and pagination. * * When the user clicks on a month, pagination will be reset, but tags retained. Pagination retains all other * parameters. */ class DatedUpdateHolderController extends PageController { private static $allowed_actions = [ 'rss', 'atom', 'DateRangeForm', ]; private static $casting = [ 'MetaTitle' => 'Text', 'FilterDescription' => 'Text', ]; /** * The session key for storing temporary form messages * * @var string */ const TEMP_FORM_MESSAGE = __CLASS__ . '.TempFormMessage'; /** * Get the meta title for the current action * * @return string */ public function getMetaTitle() { $title = $this->data()->getTitle(); $filter = $this->FilterDescription(); if ($filter) { $title = "{$title} - {$filter}"; } $this->extend('updateMetaTitle', $title); return $title; } /** * Returns a description of the current filter * * @return string */ public function FilterDescription() { $params = $this->parseParams(); $filters = array(); if ($params['tag']) { $term = TaxonomyTerm::get_by_id(TaxonomyTerm::class, $params['tag']); if ($term) { $filters[] = _t( 'CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_WITHIN', 'within' ) . ' "' . $term->Name . '"'; } } if ($params['from'] || $params['to']) { if ($params['from']) { $from = strtotime($params['from']); if ($params['to']) { $to = strtotime($params['to']); $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_BETWEEN', 'between') . ' ' . date('j/m/Y', $from) . ' and ' . date('j/m/Y', $to); } else { $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on') . ' ' . date('j/m/Y', $from); } } else { $to = strtotime($params['to']); $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on') . ' ' . date('j/m/Y', $to); } } if ($params['year'] && $params['month']) { $timestamp = mktime(1, 1, 1, $params['month'], 1, $params['year']); $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_IN', 'in') . ' ' . date('F', $timestamp) . ' ' . $params['year']; } if ($filters) { return $this->getUpdateName() . ' ' . implode(' ', $filters); } } public function getUpdateName() { return Config::inst()->get($this->data()->ClassName, 'update_name'); } protected function init() { parent::init(); RSSFeed::linkToFeed($this->Link() . 'rss', $this->getSubscriptionTitle()); } /** * Parse URL parameters. * * @param bool $produceErrorMessages Set to false to omit session messages. */ public function parseParams($produceErrorMessages = true) { $tag = $this->request->getVar('tag'); $from = $this->request->getVar('from'); $to = $this->request->getVar('to'); $year = $this->request->getVar('year'); $month = $this->request->getVar('month'); if ($tag == '') { $tag = null; } if ($from == '') { $from = null; } if ($to == '') { $to = null; } if ($year == '') { $year = null; } if ($month == '') { $month = null; } if (isset($tag)) { $tag = (int)$tag; } try { if (isset($from)) { $from = urldecode($from); $parser = DBDatetime::create(); $parser->setValue($from); $from = $parser->Format('y-MM-dd'); } if (isset($to)) { $to = urldecode($to); $parser = DBDatetime::create(); $parser->setValue($to); $to = $parser->Format('y-MM-dd'); } } catch (InvalidArgumentException $e) { if ($produceErrorMessages) { $this->getRequest()->getSession()->set(self::TEMP_FORM_MESSAGE, _t( __CLASS__ . '.InvalidDateFormat', 'Dates must be in "y-MM-dd" format.' )); } } if (isset($year)) { $year = (int)$year; } if (isset($month)) { $month = (int)$month; } // If only "To" has been provided filter by single date. Normalise by swapping with "From". if (isset($to) && !isset($from)) { list($to, $from) = [$from, $to]; } // Flip the dates if the order is wrong. if (isset($to) && isset($from) && strtotime($from) > strtotime($to)) { list($to, $from) = [$from, $to]; if ($produceErrorMessages) { $this->getRequest()->getSession()->set(self::TEMP_FORM_MESSAGE, _t( __CLASS__ . '.FilterAppliedMessage', 'Filter has been applied with the dates reversed.' )); } } // Notify the user that filtering by single date is taking place. if (isset($from) && !isset($to)) { if ($produceErrorMessages) { $this->getRequest()->getSession()->set(self::TEMP_FORM_MESSAGE, _t( __CLASS__ . '.DateRangeFilterMessage', 'Filtered by a single date.' )); } } return [ 'tag' => $tag, 'from' => $from, 'to' => $to, 'year' => $year, 'month' => $month, ]; } /** * Build the link - keep the date range, reset the rest. */ public function AllTagsLink() { $link = HTTP::setGetVar('tag', null, null, '&'); $link = HTTP::setGetVar('month', null, $link, '&'); $link = HTTP::setGetVar('year', null, $link, '&'); $link = HTTP::setGetVar('start', null, $link, '&'); return $link; } /** * List tags and attach links. */ public function UpdateTagsWithLinks() { $tags = $this->UpdateTags(); $processed = ArrayList::create(); foreach ($tags as $tag) { // Build the link - keep the tag, and date range, but reset month, year and pagination. $link = HTTP::setGetVar('tag', $tag->ID, null, '&'); $link = HTTP::setGetVar('month', null, $link, '&'); $link = HTTP::setGetVar('year', null, $link, '&'); $link = HTTP::setGetVar('start', null, $link, '&'); $tag->Link = $link; $processed->push($tag); } return $processed; } /** * Get the TaxonomyTerm related to the current tag GET parameter. */ public function CurrentTag() { $tagID = $this->request->getVar('tag'); if (isset($tagID)) { return TaxonomyTerm::get_by_id(TaxonomyTerm::class, (int)$tagID); } } /** * Extract the available months based on the current query. * Only tag is respected. Pagination and months are ignored. */ public function AvailableMonths() { $params = $this->parseParams(); return DatedUpdateHolder::ExtractMonths( $this->Updates($params['tag'], $params['from'], $params['to']), Director::makeRelative($_SERVER['REQUEST_URI']), $params['year'], $params['month'] ); } /** * Get the updates based on the current query. */ public function FilteredUpdates($pageSize = 20) { $params = $this->parseParams(); $items = $this->Updates( $params['tag'], $params['from'], $params['to'], $params['year'], $params['month'] ); // Apply pagination $list = PaginatedList::create($items, $this->getRequest()); $list->setPageLength($pageSize); return $list; } /** * @return Form */ public function DateRangeForm() { $dateFromTitle = DBField::create_field('HTMLText', sprintf( '%s <span class="field-note">%s</span>', _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FROM_DATE', 'From date'), _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 30/12/2017)') )); $dateToTitle = DBField::create_field('HTMLText', sprintf( '%s <span class="field-note">%s</span>', _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.TO_DATE', 'To date'), _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 30/12/2017)') )); $fields = FieldList::create( DateField::create('from', $dateFromTitle), DateField::create('to', $dateToTitle), HiddenField::create('tag') ); $actions = FieldList::create( FormAction::create("doDateFilter") ->setTitle(_t(__CLASS__ . '.Filter', 'Filter')) ->addExtraClass('btn btn-primary primary'), FormAction::create("doDateReset") ->setTitle(_t(__CLASS__ . '.Clear', 'Clear')) ->addExtraClass('btn') ); $form = Form::create($this, 'DateRangeForm', $fields, $actions); $form->loadDataFrom($this->request->getVars()); $form->setFormMethod('get'); // Add any locally stored form messages before returning if ($formMessage = $this->getRequest()->getSession()->get(self::TEMP_FORM_MESSAGE)) { $form->setMessage($formMessage, ValidationResult::TYPE_WARNING); $this->getRequest()->getSession()->clear(self::TEMP_FORM_MESSAGE); } return $form; } public function doDateFilter() { $params = $this->parseParams(); // Build the link - keep the tag, but reset month, year and pagination. $link = HTTP::setGetVar('from', $params['from'], $this->AbsoluteLink(), '&'); $link = HTTP::setGetVar('to', $params['to'], $link, '&'); if (isset($params['tag'])) { $link = HTTP::setGetVar('tag', $params['tag'], $link, '&'); } $this->redirect($link); } public function doDateReset() { $params = $this->parseParams(false); // Reset the link - only include the tag. $link = $this->AbsoluteLink(); if (isset($params['tag'])) { $link = HTTP::setGetVar('tag', $params['tag'], $link, '&'); } $this->redirect($link); } public function rss() { $rss = RSSFeed::create( $this->Updates()->sort('Created DESC')->limit(20), $this->Link('rss'), $this->getSubscriptionTitle() ); return $rss->outputToBrowser(); } public function atom() { $atom = CwpAtomFeed::create( $this->Updates()->sort('Created DESC')->limit(20), $this->Link('atom'), $this->getSubscriptionTitle() ); return $atom->outputToBrowser(); } } |