1 <?php
2 3 4 5 6 7 8
9 class NewsLiveCalendarWidget extends LiveCalendarWidget {
10 protected function getEventsFor($start_date, $end_date) {
11 $start = ($start_date instanceof sfDate) ? $start_date : ($start_date !== null ? new sfDate($start_date) : new sfDate());
12 $end = ($end_date instanceof sfDate) ? $end_date : ($end_date !== null ? new sfDate($end_date) : new sfDate());
13 if ($start) {
14 $start = 'DATE(`Date`) >= \'' . $start->date() . '\'';
15 if ($end) {
16 $end = " AND DATE(`Date`) <= '" . $end->date() . "'";
17 }
18 }
19 $events = DataObject::get('NewsEntry', 'ParentID = ' . $this->calendar->ID . ' AND `Status` LIKE \'%Published%\' AND '.$start . $end);
20 $map = array();
21 if ( $events) {
22 foreach ($events as $event) {
23 $map[] = $event->Date;
24 }
25 }
26 return $map;
27 }
28
29 public function Link($action = null) {
30 if ($action === null)
31 $action = "";
32 return Director::baseURL() . "NewsArchive" . "/$action";
33 }
34
35 public function ShowMonthLink($month) {
36 return $this->Link('show') . '/' . $this->calendar->ID . "/" . $month . "/" . $this->anchor_start->format('Ymd');
37 }
38
39 protected function getNavigationOptions() {
40 $options = new DataObjectSet();
41 $counter = new sfDate($this->start_date->get());
42 $counter->subtractMonth(11);
43 for ($i = 0; $i < 12; $i++) {
44 $options->push(new ArrayData(array(
45 'Link' => $this->ShowMonthLink($counter->format('Ym')),
46 'Selected' => $this->start_date->format('Ym') == $counter->format('Ym') ? 'selected="selected"' : '',
47 'Month' => CalendarUtil::i18n_date('%B, %Y', $counter->get())
48 )));
49 $counter->addMonth();
50 }
51 unset($counter);
52 return $options;
53 }
54
55 function getMonthList(){
56 $options = new DataObjectSet();
57 $counter = new sfDate($this->start_date->get());
58 $counter = $counter->firstDayOfYear();
59 for ($i = 0; $i < 12; $i++) {
60 $options->push(new ArrayData(array(
61 'Link' => $counter->format('m'),
62 'AjaxLink' => $this->ShowMonthLink($counter->format('Ym')),
63 'Selected' => $this->start_date->format('Ym') == $counter->format('Ym') ? 'selected="selected"' : '',
64 'Month' => CalendarUtil::i18n_date('%B', $counter->get())
65 )));
66 $counter->addMonth();
67 }
68 unset($counter);
69 return $options;
70 }
71
72 function getYearList($reverse = false){
73 $firstNews = DataObject::get('NewsEntry', 'ParentID = ' . $this->calendar->ID . ' AND `Status` LIKE \'%Published%\' AND Date IS NOT NULL', 'Date ASC',null,1);
74 if ($firstNews) {
75 $arrayDate = explode('-', $firstNews->First()->Date);
76 $firstYear = (int) $arrayDate[0];
77 }
78 else {
79 $firstYear = (int) date('Y', strtotime('now'));
80 }
81 $currentYear = (int) date('Y', strtotime('now'));
82 $selectedYear = (int) $this->start_date->format('Y');
83 if (($selectedYear > $currentYear))
84 $currentYear = $selectedYear;
85 if ($selectedYear < $firstYear)
86 $firstYear = $selectedYear;
87 $options = new DataObjectSet();
88 for($i=$firstYear;$i<=$currentYear;$i++){
89 $year = new ArrayData(array(
90 'Link' => $i,
91 'AjaxLink' => $this->ShowMonthLink($i . $this->start_date->format('m')),
92 'Selected' => ($selectedYear == $i) ? 'selected="selected"' : '',
93 'Current' => ($selectedYear == $i) ? 'Current' : '',
94 'Month' => $i,
95 'Name' => $i,
96 'YearLink' => $this->calendar->Link('view') . "/" . $i,
97 ));
98 if ($reverse)
99 $options->unshift($year);
100 else
101 $options->push($year);
102 }
103 return $options;
104 }
105
106 public function forTemplate() {
107 $t = clone $this->start_date;
108 return $this->customise(array(
109 'Weeks' => $this->getWeeks(),
110 'NavigationOptions' => $this->getNavigationOptions(),
111 'CurrentMonthLink' => $this->calendar->Link('view') . "/" . $t->format('Ym'),
112 'PrevMonthLink' => $this->ShowMonthLink($t->subtractMonth()->format('Ym')),
113 'PrevYearLink' => $this->ShowMonthLink($t->subtractYear()->addMonth()->format('Ym')),
114 'NextMonthLink' => $this->ShowMonthLink($t->addMonth()->addYear()->format('Ym')),
115 'NextYearLink' => $this->ShowMonthLink($t->addYear()->subtractMonth()->format('Ym')),
116 'QuickMonthLink' => $this->getQuickMonthLink(),
117 'QuickWeekLink' => $this->getQuickWeekLink(),
118 'QuickWeekendLink' => $this->getQuickWeekendLink(),
119 'CalendarHeader' => $this->calendarHeader,
120 'Ajax' => Director::is_ajax(),
121 'Sun' => CalendarUtil::i18n_date('%a', $this->date_counter->previousDay(sfTIME::SUNDAY)->get()),
122 'Mon' => CalendarUtil::i18n_date('%a', $this->date_counter->addDay()->get()),
123 'Tue' => CalendarUtil::i18n_date('%a', $this->date_counter->addDay()->get()),
124 'Wed' => CalendarUtil::i18n_date('%a', $this->date_counter->addDay()->get()),
125 'Thu' => CalendarUtil::i18n_date('%a', $this->date_counter->addDay()->get()),
126 'Fri' => CalendarUtil::i18n_date('%a', $this->date_counter->addDay()->get()),
127 'Sat' => CalendarUtil::i18n_date('%a', $this->date_counter->addDay()->get())
128 ))->renderWith('NewsLiveCalendarWidget');
129 }
130 }
131
132 class NewsArchive extends Controller {
133
134 static $url_handlers = array(
135 'show/$HolderID/$CurrentMonth/$AnchorStart/$AnchorEnd' => 'handleShow'
136 );
137
138 public function handleShow($request) {
139 $holderID = (int)$request->param('HolderID');
140 if ($holderID) {
141 $calendar = DataObject::get_by_id('NewsHolder', $holderID);
142 } else {
143 $calendar = DataObject::get_one('NewsHolder');
144 }
145 if ($calendar) {
146 $default_view = $request->param('DefaultView') == "1";
147 $c = new NewsLiveCalendarWidget(
148 $calendar,
149 new sfDate(CalendarUtil::getDateFromString($request->param('CurrentMonth'))),
150 null,
151 $default_view
152 );
153 $c->setAnchorStart($request->param('AnchorStart'));
154 $c->setAnchorEnd($request->param('AnchorEnd'));
155 echo $c->forTemplate();
156 }
157 else
158 return false;
159 }
160
161 }
162
[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.
-