1 <?php
2
3 4 5 6 7 8 9
10 class NewsHolder extends Page {
11
12 static $icon = 'news/images/rss';
13 static $allowed_children = array('NewsEntry', 'NewsHolder');
14
15 static $db = array(
16 'SortType' => 'Int',
17 'Template' => 'Text'
18 );
19
20 static $defaults = array(
21 'SortType' => 2,
22 'AutoChild' => 0,
23 "ShowInMenus" => 0,
24 "ShowInSearch" => 0,
25 );
26
27 static $subpage_children = 'NewsEntry';
28
29 function getCMSFields() {
30 $fields = parent::getCMSFields();
31 $fields->removeFieldFromTab('Root.Content.Main', 'Content');
32 $fields->addFieldToTab('Root.Content.Main',
33 new OptionsetField('SortType', $this->fieldLabel('SortType'), array(
34 1 => _t('NewsHolder.TYPE_SORT_ASC', "ASC"),
35 2 => _t('NewsHolder.TYPE_SORT_DESC', "DESC"),
36 ))
37 );
38 $sc = SiteConfig::current_site_config();
39 if ($sc->NewsCustomTemplates) {
40 $fields->addFieldToTab('Root.Content.Main', new TextField('Template', _t('NewsHolder.db_Template')));
41 }
42 $fields->addFieldToTab('Root.Content', new Tab('tabSubPages', _t('NewsHolder.TABLABEL', 'News')), 'Metadata');
43 $subClass = self::$subpage_children;
44 $sp = new SubpageListField('Subpages', $this, $subClass, 'Date DESC');
45 $sp->Sortable = false;
46 $url = '<a href=\"admin/show/$ID\">$value</a>';
47 $sp->setFieldFormatting(array_combine(array_keys(singleton($subClass)->summaryFields()), array_fill(0, count(singleton($subClass)->summaryFields()), $url)));
48 $fields->addFieldToTab('Root.Content.tabSubPages', $sp);
49
50 return $fields;
51 }
52
53 54 55
56
57 public function Entries($start = '', $limit = '',$start_date = null, $end_date = null) {
58 $filter = '';
59 if ($start_date) {
60 $filter .= ' AND DATE(Date) >= \'' . $start_date->date() . '\'';
61 if ($end_date) {
62 $filter .= ' AND DATE(Date) <= \'' . $end_date->date() . '\'';
63 }
64 }
65 if ($limit != '') {
66 if ($start != '') {
67 $limit = "$start,$limit";
68 }
69 }
70
71 if ($this->SortType == 1) {
72 $sort = 'ASC';
73 } else {
74
75 $sort = 'DESC';
76 }
77 $res = DataObject::get("NewsEntry", "`ParentID` = $this->ID AND `Status` LIKE '%Published%' $filter", "`NewsEntry`.Date $sort, `NewsEntry`.ID $sort", "", "$limit");
78 return $res;
79 }
80
81
82
83
84 }
85
86 class NewsHolder_Controller extends News_Controller {
87
88 function init() {
89 parent::init();
90 RSSFeed::linkToFeed($this->Link("rss"), _t('NewsHolder.RSSFEED', "RSS feed"));
91 }
92
93 function index() {
94 $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
95 if ($start < 0) $start = 0;
96
97 $sc = SiteConfig::current_site_config();
98
99 $limit = $sc->NewsPerPage;
100 $limit = ($limit > 0) ? $limit : 10;
101 $data = $this->Entries($start, $limit);
102 if ($this->hasMethod('setSEOVars')) {
103 $this->setSEOVars($data);
104 }
105 return ($sc->NewsCustomTemplates && $this->Template) ? $this->renderWith(array($this->Template, $this->ClassName, 'Page'), array('Items' => $data)) : $this->render(array('Items' => $data));
106 }
107
108 public function view() {
109 $this->parseURL();
110
111 $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
112 if ($start < 0) $start = 0;
113
114 $sc = SiteConfig::current_site_config();
115
116 $limit = $sc->NewsPerPage;
117 $limit = ($limit > 0) ? $limit : 10;
118 $data = $this->Entries($start, $limit,$this->start_date,$this->end_date);
119 if ($this->hasMethod('setSEOVars')) {
120 $this->setSEOVars($data);
121 }
122 return ($sc->NewsCustomTemplates && $this->Template) ? $this->renderWith(array($this->Template, $this->ClassName, 'Page'), array('Items' => $data)) : $this->render(array('Items' => $data));
123 }
124
125 function CurrentPeriod() {
126 switch ($this->view) {
127 case 'year':
128 return $this->year;
129
130 case 'month':
131 return $this->StartDate()->formatI18N('%B %Y');
132
133 case 'day':
134 return $this->StartDate()->formatI18N('%e.%m.%Y');
135
136 case 'range':
137 return $this->StartDate()->rangeString($this->EndDate()) ;
138 }
139 return false;
140 }
141
142 function StartDate() {
143 if (!$this->start_date) return false;
144 $date = new Date('StartDate');
145 $date->setValue($this->start_date->get());
146 return $date;
147 }
148
149 function EndDate() {
150 if (!$this->end_date) return false;
151 $date = new Date('EndDate');
152 $date->setValue($this->end_date->get());
153 return $date;
154 }
155
156 function ViewType() {
157 return $this->view;
158 }
159
160 function () {
161 global $project;
162 HTTP::set_cache_age(3600);
163
164 $sc = SiteConfig::current_site_config();
165 $Name = $sc->Title;
166 $altName = $project . _t('NewsHolder.RSSNAME', ' news');
167 $entries = $this->Entries(0, $sc->NewsInRSS);
168
169 if ($entries) {
170 $rss = new RSSFeed($entries, $this->Link() . 'rss', ($Name ? $Name : $altName), "", "Title", "Description");
171 $rss->outputToBrowser();
172 }
173 }
174
175 }
176
177
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-