Source of file NewsHolder.php
Size: 9,657 Bytes - Last Modified: 2021-12-23T10:20:20+00:00
/var/www/docs.ssmods.com/process/src/code/pages/NewsHolder.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 | <?php class NewsHolder extends Page { /** * @var string */ private static $description = 'News article collection holder'; /** * @var array */ private static $allowed_children = array( 'NewsArticle' ); /** * @var array */ private static $extensions = array( 'Lumberjack', ); /** * @var int */ private static $articles_per_page = 10; /** * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->fieldByName('Root.ChildPages')->setTitle('News Articles'); if (Config::inst()->get('NewsArticle', 'enable_tags')) { $fields->addFieldsToTab( 'Root.Tags', GridField::create( 'Tags', 'News Article Tags', NewsTag::get(), GridFieldConfig_recordEditor::create() ) ); } if (Config::inst()->get('NewsArticle', 'author_mode') == 'object') { $fields->addFieldsToTab( 'Root.Authors', GridField::create( 'Authors', 'News Article Authors', NewsAuthor::get(), GridFieldConfig_recordEditor::create() ) ); } $this->extend('updateNewsHolderCMSFields', $fields); return $fields; } /** * @param int $tag * @param int $year * @param int $month * @return DataList */ public function getArticleList($tag = null, $year = null, $month = null) { $list = NewsArticle::get()->filter('ParentID', $this->ID); // filter by tag? if ($tag) { $list = $list->filter('Tags.ID:exactMatch', $tag); } // filter by date range? $year = (int) $year; $month = (int) $month; if ($year && $month) { $beginDate = "$year-$month-01 00:00:00"; $endDate = date('Y-m-d H:i:s', strtotime("$year-$month-1 00:00:00 +1 month")); $list = $list->where("(\"NewsArticle\".\"PublishDate\">='$beginDate' AND \"NewsArticle\".\"PublishDate\"<'$endDate')"); } elseif ($year) { $beginDate = "$year-01-01 00:00:00"; $endDate = "$year-12-31 23:59:59"; $list = $list->where("(\"NewsArticle\".\"PublishDate\">='$beginDate' AND \"NewsArticle\".\"PublishDate\"<'$endDate')"); } return $list; } /** * List of News tags used by articles in this section * * @return DataList **/ public function getTagList() { $articleIDs = $this->getArticleList()->column('ID'); $articleIDs = implode(',', $articleIDs); $tags = NewsTag::get() ->innerJoin('NewsArticle_Tags', '"NewsTag"."ID"="NewsArticle_Tags"."NewsTagID"') ->where("\"NewsArticle_Tags\".\"NewsArticleID\" IN ($articleIDs)") ->sort('Title'); return $tags; } /** * Generates an ArrayList of archive years/months that contain news articles. * The currently filtered tag is considered an limits archive dates too. * * @return ArrayList **/ public function getArchiveList($link = null, $currentYear = null, $currentMonth = null, $tag = null) { $link = $link ? $link : $this->Link(); $articles = $this->getArticleList($tag); $years = array(); foreach ($articles as $article) { $date = $article->obj('PublishDate'); $year = $date->Format('Y'); if ($year) { $monthNumber = $date->Format('n'); $monthName = $date->Format('M'); // if this month is aleady set, we don't need to set it again. if (isset($years[$year]['Months'][$monthNumber])) { continue; } // Check if the currently processed month is the one that is selected via GET params. $active = false; if (isset($monthNumber)) { $active = (((int) $currentYear)==$year && ((int) $currentMonth)==$monthNumber); } // Build the link - keep the tag and date filter, but reset the pagination. $link = HTTP::setGetVar('month', $monthNumber, $link, '&'); $link = HTTP::setGetVar('year', $year, $link, '&'); $monthLink = HTTP::setGetVar('start', null, $link, '&'); $yearLink = HTTP::setGetVar('month', null, $link, '&'); // Set up the relevant year array, if not yet available. if (!isset($years[$year])) { // Check if the currently processed year is the one that is selected via GET params. $isActiveYear = (((int) $currentYear)==$year); $years[$year] = array( 'YearName'=> $year, 'Months'=> array(), 'Link' => $yearLink, 'Active'=> $isActiveYear, 'LinkingMode'=> $isActiveYear ? "current" : "link" ); } if (!isset($years[$year]['Months'][$monthNumber])) { $years[$year]['Months'][$monthNumber] = array( 'MonthName'=>$monthName, 'MonthNumber'=>$monthNumber, 'Link'=>$monthLink, 'Active'=>$active, 'LinkingMode'=> $active ? "current" : "link" ); } } } // ArrayList will not recursively walk through the supplied array, so manually build nested ArrayLists. foreach ($years as &$year) { $year['Months'] = new ArrayList($year['Months']); } // Reverse the list so the most recent years appear first. return new ArrayList(array_reverse($years)); } /** * Used by templates to access and iterate over archive years/months * * @return ArrayList **/ public function HolderController() { return ModelAsController::controller_for($this); } } class NewsHolder_Controller extends Page_Controller { /** * @var array */ private static $allowed_actions = array( 'rss' ); /** * Used by templates to access and iterate over articles * * @param $limit - number of articles per page (defaults to articles_per_page config value) * @return PaginatedList **/ public function Articles($limit = null) { $tag = $this->getCurrentTag(); $year = $this->getCurrentYear(); $month = $this->getCurrentMonth(); $start = (int) $this->request->requestVar('start') ?: null; $limit = $limit ? $limit : NewsHolder::config()->get('articles_per_page'); $list = $this->data()->getArticleList($tag, $year, $month); $paged = new PaginatedList($list, $this->request); $paged->setPageLength($limit); return $paged; } /** * Used by templates to access and iterate over archive years/months * * @return ArrayList **/ public function Archive() { $link = Director::makeRelative($_SERVER['REQUEST_URI']); $currentYear = $this->getCurrentYear(); $currentMonth = $this->getCurrentMonth(); $tag = $this->getCurrentTag(); return $this->data()->getArchiveList($link, $currentYear, $currentMonth, $tag); } /** * Link to RSS feed * * @return string **/ public function RSSLink() { return $this->Link('rss'); } /** * Renders RSS feed * * @return HTMLText **/ public function rss() { $rss = new RSSFeed($this->data()->getArticleList()->limit(20), $this->Link(), $this->Title); return $rss->outputToBrowser(); } /** * getLinkedTagList * Gets a list of tags with links, keeping current year and month request vars * * @return ArrayList **/ public function getLinkedTagList() { $link = $this->Link(); $link = HTTP::setGetVar('year', $this->getCurrentYear(), $link, '&'); $link = HTTP::setGetVar('month', $this->getCurrentMonth(), $link, '&'); $tags = $this->data()->getTagList(); $list = ArrayList::create(); $currentTag = $this->getCurrentTag(); $list->push(ArrayData::create(array( "Title" => 'All', "Link" => HTTP::setGetVar('tag', null, $link, '&'), "Current" => (!$currentTag) ))); foreach ($tags as $tag) { $list->push(ArrayData::create(array( "Title" => $tag->Title, "Link" => HTTP::setGetVar('tag', $tag->ID, $link, '&'), "Current" => ($tag->ID == $currentTag) ))); } return $list; } /** * getCurrentYear * * @return int **/ public function getCurrentYear() { return (int) $this->request->requestVar('year') ?: null; } /** * getCurrentMonth * * @return int **/ public function getCurrentMonth() { return (int) $this->request->requestVar('month') ?: null; } /** * getCurrentTag * * @return int **/ public function getCurrentTag() { return (int) $this->request->requestVar('tag') ?: null; } } |