@@ -34,361 +34,361 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class DatedUpdateHolderController extends PageController |
| 36 | 36 | { |
| 37 | - private static $allowed_actions = [ |
|
| 38 | - 'rss', |
|
| 39 | - 'atom', |
|
| 40 | - 'DateRangeForm', |
|
| 41 | - ]; |
|
| 42 | - |
|
| 43 | - private static $casting = [ |
|
| 44 | - 'MetaTitle' => 'Text', |
|
| 45 | - 'FilterDescription' => 'Text', |
|
| 46 | - ]; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The session key for storing temporary form messages |
|
| 50 | - * |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - const TEMP_FORM_MESSAGE = __CLASS__ . '.TempFormMessage'; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Get the meta title for the current action |
|
| 57 | - * |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function getMetaTitle() |
|
| 61 | - { |
|
| 62 | - $title = $this->data()->getTitle(); |
|
| 63 | - $filter = $this->FilterDescription(); |
|
| 64 | - if ($filter) { |
|
| 65 | - $title = "{$title} - {$filter}"; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $this->extend('updateMetaTitle', $title); |
|
| 69 | - return $title; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Returns a description of the current filter |
|
| 74 | - * |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - public function FilterDescription() |
|
| 78 | - { |
|
| 79 | - $params = $this->parseParams(); |
|
| 80 | - |
|
| 81 | - $filters = array(); |
|
| 82 | - if ($params['tag']) { |
|
| 83 | - $term = TaxonomyTerm::get_by_id(TaxonomyTerm::class, $params['tag']); |
|
| 84 | - if ($term) { |
|
| 85 | - $filters[] = _t( |
|
| 86 | - 'CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_WITHIN', |
|
| 87 | - 'within' |
|
| 88 | - ) . ' "' . $term->Name . '"'; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - if ($params['from'] || $params['to']) { |
|
| 93 | - if ($params['from']) { |
|
| 94 | - $from = strtotime($params['from']); |
|
| 95 | - if ($params['to']) { |
|
| 96 | - $to = strtotime($params['to']); |
|
| 97 | - $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_BETWEEN', 'between') . ' ' |
|
| 98 | - . date('j/m/Y', $from) . ' and ' . date('j/m/Y', $to); |
|
| 99 | - } else { |
|
| 100 | - $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on') |
|
| 101 | - . ' ' . date('j/m/Y', $from); |
|
| 102 | - } |
|
| 103 | - } else { |
|
| 104 | - $to = strtotime($params['to']); |
|
| 105 | - $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on') |
|
| 106 | - . ' ' . date('j/m/Y', $to); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ($params['year'] && $params['month']) { |
|
| 111 | - $timestamp = mktime(1, 1, 1, $params['month'], 1, $params['year']); |
|
| 112 | - $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_IN', 'in') |
|
| 113 | - . ' ' . date('F', $timestamp) . ' ' . $params['year']; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - if ($filters) { |
|
| 117 | - return $this->getUpdateName() . ' ' . implode(' ', $filters); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function getUpdateName() |
|
| 122 | - { |
|
| 123 | - return Config::inst()->get($this->data()->ClassName, 'update_name'); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - protected function init() |
|
| 127 | - { |
|
| 128 | - parent::init(); |
|
| 129 | - RSSFeed::linkToFeed($this->Link() . 'rss', $this->getSubscriptionTitle()); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Parse URL parameters. |
|
| 134 | - * |
|
| 135 | - * @param bool $produceErrorMessages Set to false to omit session messages. |
|
| 136 | - */ |
|
| 137 | - public function parseParams($produceErrorMessages = true) |
|
| 138 | - { |
|
| 139 | - $tag = $this->request->getVar('tag'); |
|
| 140 | - $from = $this->request->getVar('from'); |
|
| 141 | - $to = $this->request->getVar('to'); |
|
| 142 | - $year = $this->request->getVar('year'); |
|
| 143 | - $month = $this->request->getVar('month'); |
|
| 144 | - |
|
| 145 | - if ($tag == '') { |
|
| 146 | - $tag = null; |
|
| 147 | - } |
|
| 148 | - if ($from == '') { |
|
| 149 | - $from = null; |
|
| 150 | - } |
|
| 151 | - if ($to == '') { |
|
| 152 | - $to = null; |
|
| 153 | - } |
|
| 154 | - if ($year == '') { |
|
| 155 | - $year = null; |
|
| 156 | - } |
|
| 157 | - if ($month == '') { |
|
| 158 | - $month = null; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if (isset($tag)) { |
|
| 162 | - $tag = (int)$tag; |
|
| 163 | - } |
|
| 164 | - if (isset($from)) { |
|
| 165 | - $from = urldecode($from); |
|
| 166 | - $parser = DBDatetime::create(); |
|
| 167 | - $parser->setValue($from); |
|
| 168 | - $from = $parser->Format('y-MM-dd'); |
|
| 169 | - } |
|
| 170 | - if (isset($to)) { |
|
| 171 | - $to = urldecode($to); |
|
| 172 | - $parser = DBDatetime::create(); |
|
| 173 | - $parser->setValue($to); |
|
| 174 | - $to = $parser->Format('y-MM-dd'); |
|
| 175 | - } |
|
| 176 | - if (isset($year)) { |
|
| 177 | - $year = (int)$year; |
|
| 178 | - } |
|
| 179 | - if (isset($month)) { |
|
| 180 | - $month = (int)$month; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // If only "To" has been provided filter by single date. Normalise by swapping with "From". |
|
| 184 | - if (isset($to) && !isset($from)) { |
|
| 185 | - list($to, $from) = [$from, $to]; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - // Flip the dates if the order is wrong. |
|
| 189 | - if (isset($to) && isset($from) && strtotime($from) > strtotime($to)) { |
|
| 190 | - list($to, $from) = [$from, $to]; |
|
| 191 | - |
|
| 192 | - if ($produceErrorMessages) { |
|
| 193 | - $this->getRequest()->getSession()->set(self::TEMP_FORM_MESSAGE, _t( |
|
| 194 | - __CLASS__ . '.FilterAppliedMessage', |
|
| 195 | - 'Filter has been applied with the dates reversed.' |
|
| 196 | - )); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // Notify the user that filtering by single date is taking place. |
|
| 201 | - if (isset($from) && !isset($to)) { |
|
| 202 | - if ($produceErrorMessages) { |
|
| 203 | - $this->getRequest()->getSession()->set(self::TEMP_FORM_MESSAGE, _t( |
|
| 204 | - __CLASS__ . '.DateRangeFilterMessage', |
|
| 205 | - 'Filtered by a single date.' |
|
| 206 | - )); |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - return [ |
|
| 211 | - 'tag' => $tag, |
|
| 212 | - 'from' => $from, |
|
| 213 | - 'to' => $to, |
|
| 214 | - 'year' => $year, |
|
| 215 | - 'month' => $month, |
|
| 216 | - ]; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Build the link - keep the date range, reset the rest. |
|
| 221 | - */ |
|
| 222 | - public function AllTagsLink() |
|
| 223 | - { |
|
| 224 | - $link = HTTP::setGetVar('tag', null, null, '&'); |
|
| 225 | - $link = HTTP::setGetVar('month', null, $link, '&'); |
|
| 226 | - $link = HTTP::setGetVar('year', null, $link, '&'); |
|
| 227 | - $link = HTTP::setGetVar('start', null, $link, '&'); |
|
| 228 | - |
|
| 229 | - return $link; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * List tags and attach links. |
|
| 234 | - */ |
|
| 235 | - public function UpdateTagsWithLinks() |
|
| 236 | - { |
|
| 237 | - $tags = $this->UpdateTags(); |
|
| 238 | - |
|
| 239 | - $processed = ArrayList::create(); |
|
| 240 | - |
|
| 241 | - foreach ($tags as $tag) { |
|
| 242 | - // Build the link - keep the tag, and date range, but reset month, year and pagination. |
|
| 243 | - $link = HTTP::setGetVar('tag', $tag->ID, null, '&'); |
|
| 244 | - $link = HTTP::setGetVar('month', null, $link, '&'); |
|
| 245 | - $link = HTTP::setGetVar('year', null, $link, '&'); |
|
| 246 | - $link = HTTP::setGetVar('start', null, $link, '&'); |
|
| 247 | - |
|
| 248 | - $tag->Link = $link; |
|
| 249 | - $processed->push($tag); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $processed; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Get the TaxonomyTerm related to the current tag GET parameter. |
|
| 257 | - */ |
|
| 258 | - public function CurrentTag() |
|
| 259 | - { |
|
| 260 | - $tagID = $this->request->getVar('tag'); |
|
| 261 | - |
|
| 262 | - if (isset($tagID)) { |
|
| 263 | - return TaxonomyTerm::get_by_id(TaxonomyTerm::class, (int)$tagID); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Extract the available months based on the current query. |
|
| 269 | - * Only tag is respected. Pagination and months are ignored. |
|
| 270 | - */ |
|
| 271 | - public function AvailableMonths() |
|
| 272 | - { |
|
| 273 | - $params = $this->parseParams(); |
|
| 274 | - |
|
| 275 | - return DatedUpdateHolder::ExtractMonths( |
|
| 276 | - $this->Updates($params['tag'], $params['from'], $params['to']), |
|
| 277 | - Director::makeRelative($_SERVER['REQUEST_URI']), |
|
| 278 | - $params['year'], |
|
| 279 | - $params['month'] |
|
| 280 | - ); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Get the updates based on the current query. |
|
| 285 | - */ |
|
| 286 | - public function FilteredUpdates($pageSize = 20) |
|
| 287 | - { |
|
| 288 | - $params = $this->parseParams(); |
|
| 289 | - |
|
| 290 | - $items = $this->Updates( |
|
| 291 | - $params['tag'], |
|
| 292 | - $params['from'], |
|
| 293 | - $params['to'], |
|
| 294 | - $params['year'], |
|
| 295 | - $params['month'] |
|
| 296 | - ); |
|
| 297 | - |
|
| 298 | - // Apply pagination |
|
| 299 | - $list = PaginatedList::create($items, $this->getRequest()); |
|
| 300 | - $list->setPageLength($pageSize); |
|
| 301 | - return $list; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @return Form |
|
| 306 | - */ |
|
| 307 | - public function DateRangeForm() |
|
| 308 | - { |
|
| 309 | - $dateFromTitle = DBField::create_field('HTMLText', sprintf( |
|
| 310 | - '%s <span class="field-note">%s</span>', |
|
| 311 | - _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FROM_DATE', 'From date'), |
|
| 312 | - _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 30/12/2017)') |
|
| 313 | - )); |
|
| 314 | - $dateToTitle = DBField::create_field('HTMLText', sprintf( |
|
| 315 | - '%s <span class="field-note">%s</span>', |
|
| 316 | - _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.TO_DATE', 'To date'), |
|
| 317 | - _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 30/12/2017)') |
|
| 318 | - )); |
|
| 319 | - |
|
| 320 | - $fields = FieldList::create( |
|
| 321 | - DateField::create('from', $dateFromTitle), |
|
| 322 | - DateField::create('to', $dateToTitle), |
|
| 323 | - HiddenField::create('tag') |
|
| 324 | - ); |
|
| 325 | - |
|
| 326 | - $actions = FieldList::create( |
|
| 327 | - FormAction::create("doDateFilter") |
|
| 328 | - ->setTitle(_t(__CLASS__ . '.Filter', 'Filter')) |
|
| 329 | - ->addExtraClass('btn btn-primary primary'), |
|
| 330 | - FormAction::create("doDateReset") |
|
| 331 | - ->setTitle(_t(__CLASS__ . '.Clear', 'Clear')) |
|
| 332 | - ->addExtraClass('btn') |
|
| 333 | - ); |
|
| 334 | - |
|
| 335 | - $form = Form::create($this, 'DateRangeForm', $fields, $actions); |
|
| 336 | - $form->loadDataFrom($this->request->getVars()); |
|
| 337 | - $form->setFormMethod('get'); |
|
| 338 | - |
|
| 339 | - // Add any locally stored form messages before returning |
|
| 340 | - if ($formMessage = $this->getRequest()->getSession()->get(self::TEMP_FORM_MESSAGE)) { |
|
| 341 | - $form->setMessage($formMessage, ValidationResult::TYPE_WARNING); |
|
| 342 | - $this->getRequest()->getSession()->clear(self::TEMP_FORM_MESSAGE); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - return $form; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - public function doDateFilter() |
|
| 349 | - { |
|
| 350 | - $params = $this->parseParams(); |
|
| 351 | - |
|
| 352 | - // Build the link - keep the tag, but reset month, year and pagination. |
|
| 353 | - $link = HTTP::setGetVar('from', $params['from'], $this->AbsoluteLink(), '&'); |
|
| 354 | - $link = HTTP::setGetVar('to', $params['to'], $link, '&'); |
|
| 355 | - if (isset($params['tag'])) { |
|
| 356 | - $link = HTTP::setGetVar('tag', $params['tag'], $link, '&'); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $this->redirect($link); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - public function doDateReset() |
|
| 363 | - { |
|
| 364 | - $params = $this->parseParams(false); |
|
| 365 | - |
|
| 366 | - // Reset the link - only include the tag. |
|
| 367 | - $link = $this->AbsoluteLink(); |
|
| 368 | - if (isset($params['tag'])) { |
|
| 369 | - $link = HTTP::setGetVar('tag', $params['tag'], $link, '&'); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - $this->redirect($link); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - public function rss() |
|
| 376 | - { |
|
| 377 | - $rss = RSSFeed::create( |
|
| 378 | - $this->Updates()->sort('Created DESC')->limit(20), |
|
| 379 | - $this->Link('rss'), |
|
| 380 | - $this->getSubscriptionTitle() |
|
| 381 | - ); |
|
| 382 | - return $rss->outputToBrowser(); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - public function atom() |
|
| 386 | - { |
|
| 387 | - $atom = CwpAtomFeed::create( |
|
| 388 | - $this->Updates()->sort('Created DESC')->limit(20), |
|
| 389 | - $this->Link('atom'), |
|
| 390 | - $this->getSubscriptionTitle() |
|
| 391 | - ); |
|
| 392 | - return $atom->outputToBrowser(); |
|
| 393 | - } |
|
| 37 | + private static $allowed_actions = [ |
|
| 38 | + 'rss', |
|
| 39 | + 'atom', |
|
| 40 | + 'DateRangeForm', |
|
| 41 | + ]; |
|
| 42 | + |
|
| 43 | + private static $casting = [ |
|
| 44 | + 'MetaTitle' => 'Text', |
|
| 45 | + 'FilterDescription' => 'Text', |
|
| 46 | + ]; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The session key for storing temporary form messages |
|
| 50 | + * |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + const TEMP_FORM_MESSAGE = __CLASS__ . '.TempFormMessage'; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Get the meta title for the current action |
|
| 57 | + * |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function getMetaTitle() |
|
| 61 | + { |
|
| 62 | + $title = $this->data()->getTitle(); |
|
| 63 | + $filter = $this->FilterDescription(); |
|
| 64 | + if ($filter) { |
|
| 65 | + $title = "{$title} - {$filter}"; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $this->extend('updateMetaTitle', $title); |
|
| 69 | + return $title; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Returns a description of the current filter |
|
| 74 | + * |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + public function FilterDescription() |
|
| 78 | + { |
|
| 79 | + $params = $this->parseParams(); |
|
| 80 | + |
|
| 81 | + $filters = array(); |
|
| 82 | + if ($params['tag']) { |
|
| 83 | + $term = TaxonomyTerm::get_by_id(TaxonomyTerm::class, $params['tag']); |
|
| 84 | + if ($term) { |
|
| 85 | + $filters[] = _t( |
|
| 86 | + 'CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_WITHIN', |
|
| 87 | + 'within' |
|
| 88 | + ) . ' "' . $term->Name . '"'; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + if ($params['from'] || $params['to']) { |
|
| 93 | + if ($params['from']) { |
|
| 94 | + $from = strtotime($params['from']); |
|
| 95 | + if ($params['to']) { |
|
| 96 | + $to = strtotime($params['to']); |
|
| 97 | + $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_BETWEEN', 'between') . ' ' |
|
| 98 | + . date('j/m/Y', $from) . ' and ' . date('j/m/Y', $to); |
|
| 99 | + } else { |
|
| 100 | + $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on') |
|
| 101 | + . ' ' . date('j/m/Y', $from); |
|
| 102 | + } |
|
| 103 | + } else { |
|
| 104 | + $to = strtotime($params['to']); |
|
| 105 | + $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on') |
|
| 106 | + . ' ' . date('j/m/Y', $to); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ($params['year'] && $params['month']) { |
|
| 111 | + $timestamp = mktime(1, 1, 1, $params['month'], 1, $params['year']); |
|
| 112 | + $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_IN', 'in') |
|
| 113 | + . ' ' . date('F', $timestamp) . ' ' . $params['year']; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + if ($filters) { |
|
| 117 | + return $this->getUpdateName() . ' ' . implode(' ', $filters); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function getUpdateName() |
|
| 122 | + { |
|
| 123 | + return Config::inst()->get($this->data()->ClassName, 'update_name'); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + protected function init() |
|
| 127 | + { |
|
| 128 | + parent::init(); |
|
| 129 | + RSSFeed::linkToFeed($this->Link() . 'rss', $this->getSubscriptionTitle()); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Parse URL parameters. |
|
| 134 | + * |
|
| 135 | + * @param bool $produceErrorMessages Set to false to omit session messages. |
|
| 136 | + */ |
|
| 137 | + public function parseParams($produceErrorMessages = true) |
|
| 138 | + { |
|
| 139 | + $tag = $this->request->getVar('tag'); |
|
| 140 | + $from = $this->request->getVar('from'); |
|
| 141 | + $to = $this->request->getVar('to'); |
|
| 142 | + $year = $this->request->getVar('year'); |
|
| 143 | + $month = $this->request->getVar('month'); |
|
| 144 | + |
|
| 145 | + if ($tag == '') { |
|
| 146 | + $tag = null; |
|
| 147 | + } |
|
| 148 | + if ($from == '') { |
|
| 149 | + $from = null; |
|
| 150 | + } |
|
| 151 | + if ($to == '') { |
|
| 152 | + $to = null; |
|
| 153 | + } |
|
| 154 | + if ($year == '') { |
|
| 155 | + $year = null; |
|
| 156 | + } |
|
| 157 | + if ($month == '') { |
|
| 158 | + $month = null; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if (isset($tag)) { |
|
| 162 | + $tag = (int)$tag; |
|
| 163 | + } |
|
| 164 | + if (isset($from)) { |
|
| 165 | + $from = urldecode($from); |
|
| 166 | + $parser = DBDatetime::create(); |
|
| 167 | + $parser->setValue($from); |
|
| 168 | + $from = $parser->Format('y-MM-dd'); |
|
| 169 | + } |
|
| 170 | + if (isset($to)) { |
|
| 171 | + $to = urldecode($to); |
|
| 172 | + $parser = DBDatetime::create(); |
|
| 173 | + $parser->setValue($to); |
|
| 174 | + $to = $parser->Format('y-MM-dd'); |
|
| 175 | + } |
|
| 176 | + if (isset($year)) { |
|
| 177 | + $year = (int)$year; |
|
| 178 | + } |
|
| 179 | + if (isset($month)) { |
|
| 180 | + $month = (int)$month; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // If only "To" has been provided filter by single date. Normalise by swapping with "From". |
|
| 184 | + if (isset($to) && !isset($from)) { |
|
| 185 | + list($to, $from) = [$from, $to]; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // Flip the dates if the order is wrong. |
|
| 189 | + if (isset($to) && isset($from) && strtotime($from) > strtotime($to)) { |
|
| 190 | + list($to, $from) = [$from, $to]; |
|
| 191 | + |
|
| 192 | + if ($produceErrorMessages) { |
|
| 193 | + $this->getRequest()->getSession()->set(self::TEMP_FORM_MESSAGE, _t( |
|
| 194 | + __CLASS__ . '.FilterAppliedMessage', |
|
| 195 | + 'Filter has been applied with the dates reversed.' |
|
| 196 | + )); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // Notify the user that filtering by single date is taking place. |
|
| 201 | + if (isset($from) && !isset($to)) { |
|
| 202 | + if ($produceErrorMessages) { |
|
| 203 | + $this->getRequest()->getSession()->set(self::TEMP_FORM_MESSAGE, _t( |
|
| 204 | + __CLASS__ . '.DateRangeFilterMessage', |
|
| 205 | + 'Filtered by a single date.' |
|
| 206 | + )); |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + return [ |
|
| 211 | + 'tag' => $tag, |
|
| 212 | + 'from' => $from, |
|
| 213 | + 'to' => $to, |
|
| 214 | + 'year' => $year, |
|
| 215 | + 'month' => $month, |
|
| 216 | + ]; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Build the link - keep the date range, reset the rest. |
|
| 221 | + */ |
|
| 222 | + public function AllTagsLink() |
|
| 223 | + { |
|
| 224 | + $link = HTTP::setGetVar('tag', null, null, '&'); |
|
| 225 | + $link = HTTP::setGetVar('month', null, $link, '&'); |
|
| 226 | + $link = HTTP::setGetVar('year', null, $link, '&'); |
|
| 227 | + $link = HTTP::setGetVar('start', null, $link, '&'); |
|
| 228 | + |
|
| 229 | + return $link; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * List tags and attach links. |
|
| 234 | + */ |
|
| 235 | + public function UpdateTagsWithLinks() |
|
| 236 | + { |
|
| 237 | + $tags = $this->UpdateTags(); |
|
| 238 | + |
|
| 239 | + $processed = ArrayList::create(); |
|
| 240 | + |
|
| 241 | + foreach ($tags as $tag) { |
|
| 242 | + // Build the link - keep the tag, and date range, but reset month, year and pagination. |
|
| 243 | + $link = HTTP::setGetVar('tag', $tag->ID, null, '&'); |
|
| 244 | + $link = HTTP::setGetVar('month', null, $link, '&'); |
|
| 245 | + $link = HTTP::setGetVar('year', null, $link, '&'); |
|
| 246 | + $link = HTTP::setGetVar('start', null, $link, '&'); |
|
| 247 | + |
|
| 248 | + $tag->Link = $link; |
|
| 249 | + $processed->push($tag); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $processed; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Get the TaxonomyTerm related to the current tag GET parameter. |
|
| 257 | + */ |
|
| 258 | + public function CurrentTag() |
|
| 259 | + { |
|
| 260 | + $tagID = $this->request->getVar('tag'); |
|
| 261 | + |
|
| 262 | + if (isset($tagID)) { |
|
| 263 | + return TaxonomyTerm::get_by_id(TaxonomyTerm::class, (int)$tagID); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Extract the available months based on the current query. |
|
| 269 | + * Only tag is respected. Pagination and months are ignored. |
|
| 270 | + */ |
|
| 271 | + public function AvailableMonths() |
|
| 272 | + { |
|
| 273 | + $params = $this->parseParams(); |
|
| 274 | + |
|
| 275 | + return DatedUpdateHolder::ExtractMonths( |
|
| 276 | + $this->Updates($params['tag'], $params['from'], $params['to']), |
|
| 277 | + Director::makeRelative($_SERVER['REQUEST_URI']), |
|
| 278 | + $params['year'], |
|
| 279 | + $params['month'] |
|
| 280 | + ); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Get the updates based on the current query. |
|
| 285 | + */ |
|
| 286 | + public function FilteredUpdates($pageSize = 20) |
|
| 287 | + { |
|
| 288 | + $params = $this->parseParams(); |
|
| 289 | + |
|
| 290 | + $items = $this->Updates( |
|
| 291 | + $params['tag'], |
|
| 292 | + $params['from'], |
|
| 293 | + $params['to'], |
|
| 294 | + $params['year'], |
|
| 295 | + $params['month'] |
|
| 296 | + ); |
|
| 297 | + |
|
| 298 | + // Apply pagination |
|
| 299 | + $list = PaginatedList::create($items, $this->getRequest()); |
|
| 300 | + $list->setPageLength($pageSize); |
|
| 301 | + return $list; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @return Form |
|
| 306 | + */ |
|
| 307 | + public function DateRangeForm() |
|
| 308 | + { |
|
| 309 | + $dateFromTitle = DBField::create_field('HTMLText', sprintf( |
|
| 310 | + '%s <span class="field-note">%s</span>', |
|
| 311 | + _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FROM_DATE', 'From date'), |
|
| 312 | + _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 30/12/2017)') |
|
| 313 | + )); |
|
| 314 | + $dateToTitle = DBField::create_field('HTMLText', sprintf( |
|
| 315 | + '%s <span class="field-note">%s</span>', |
|
| 316 | + _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.TO_DATE', 'To date'), |
|
| 317 | + _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 30/12/2017)') |
|
| 318 | + )); |
|
| 319 | + |
|
| 320 | + $fields = FieldList::create( |
|
| 321 | + DateField::create('from', $dateFromTitle), |
|
| 322 | + DateField::create('to', $dateToTitle), |
|
| 323 | + HiddenField::create('tag') |
|
| 324 | + ); |
|
| 325 | + |
|
| 326 | + $actions = FieldList::create( |
|
| 327 | + FormAction::create("doDateFilter") |
|
| 328 | + ->setTitle(_t(__CLASS__ . '.Filter', 'Filter')) |
|
| 329 | + ->addExtraClass('btn btn-primary primary'), |
|
| 330 | + FormAction::create("doDateReset") |
|
| 331 | + ->setTitle(_t(__CLASS__ . '.Clear', 'Clear')) |
|
| 332 | + ->addExtraClass('btn') |
|
| 333 | + ); |
|
| 334 | + |
|
| 335 | + $form = Form::create($this, 'DateRangeForm', $fields, $actions); |
|
| 336 | + $form->loadDataFrom($this->request->getVars()); |
|
| 337 | + $form->setFormMethod('get'); |
|
| 338 | + |
|
| 339 | + // Add any locally stored form messages before returning |
|
| 340 | + if ($formMessage = $this->getRequest()->getSession()->get(self::TEMP_FORM_MESSAGE)) { |
|
| 341 | + $form->setMessage($formMessage, ValidationResult::TYPE_WARNING); |
|
| 342 | + $this->getRequest()->getSession()->clear(self::TEMP_FORM_MESSAGE); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + return $form; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + public function doDateFilter() |
|
| 349 | + { |
|
| 350 | + $params = $this->parseParams(); |
|
| 351 | + |
|
| 352 | + // Build the link - keep the tag, but reset month, year and pagination. |
|
| 353 | + $link = HTTP::setGetVar('from', $params['from'], $this->AbsoluteLink(), '&'); |
|
| 354 | + $link = HTTP::setGetVar('to', $params['to'], $link, '&'); |
|
| 355 | + if (isset($params['tag'])) { |
|
| 356 | + $link = HTTP::setGetVar('tag', $params['tag'], $link, '&'); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $this->redirect($link); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + public function doDateReset() |
|
| 363 | + { |
|
| 364 | + $params = $this->parseParams(false); |
|
| 365 | + |
|
| 366 | + // Reset the link - only include the tag. |
|
| 367 | + $link = $this->AbsoluteLink(); |
|
| 368 | + if (isset($params['tag'])) { |
|
| 369 | + $link = HTTP::setGetVar('tag', $params['tag'], $link, '&'); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + $this->redirect($link); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + public function rss() |
|
| 376 | + { |
|
| 377 | + $rss = RSSFeed::create( |
|
| 378 | + $this->Updates()->sort('Created DESC')->limit(20), |
|
| 379 | + $this->Link('rss'), |
|
| 380 | + $this->getSubscriptionTitle() |
|
| 381 | + ); |
|
| 382 | + return $rss->outputToBrowser(); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + public function atom() |
|
| 386 | + { |
|
| 387 | + $atom = CwpAtomFeed::create( |
|
| 388 | + $this->Updates()->sort('Created DESC')->limit(20), |
|
| 389 | + $this->Link('atom'), |
|
| 390 | + $this->getSubscriptionTitle() |
|
| 391 | + ); |
|
| 392 | + return $atom->outputToBrowser(); |
|
| 393 | + } |
|
| 394 | 394 | } |