On branch master
[event_calendar_module.git] / mod_eventcalendar.php
blob5c175f7ca23df8a8ad13f13668aac1bc7c61bc07
1 <?php
3 /*
4 * Event Calendar (module) for Elxis CMS 2008.x and 2009.x+
6 * @version 1.0
7 * @package Event Calendar (module)
8 * @author Apostolos Koutsoulelos <akoutsoulelos@yahoo.gr>
9 * @copyright Copyright (C) 2009-2010 Apostolos Koutsoulelos. All rights reserved.
10 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
11 * @link
14 // Prevent direct inclusion of this file
15 defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
17 // Get globals
18 global $lang;
20 // Check if Event Calendar (component) is installed
21 if ( file_exists($mosConfig_absolute_path.'/components/com_eventcalendar/eventcalendar.class.php') ) {
22 require_once( $mosConfig_absolute_path.'/components/com_eventcalendar/eventcalendar.class.php' );
23 } else {
24 echo 'Event Calendar (component) not found!<br />';
25 return;
28 // Includes
29 if ( file_exists($mosConfig_absolute_path.'/modules/mod_eventcalendar/language/'.$lang.'.php') ) {
30 require_once( $mosConfig_absolute_path.'/modules/mod_eventcalendar/language/'.$lang.'.php' );
31 } else {
32 require_once( $mosConfig_absolute_path.'/modules/mod_eventcalendar/language/english.php' );
36 // Check if module is already declared
37 if (!class_exists('clsEventCalendarMOD')) {
38 class clsEventCalendarMOD {
40 // Itialize variables
41 public $ec_year = '';
42 public $ec_month = '';
43 public $ec_day = '';
44 public $ec_date = '';
46 public $ec_modParams = null;
47 public $ec_comParams = null;
48 public $ec_comItemid = '';
50 public $ec_weeknrs = array();
51 public $ec_calendar = array();
52 public $ec_events = array();
54 /****************************/
55 /* The class' constructor */
56 /****************************/
57 public function __construct($params, $database) {
58 global $mainframe, $my, $Itemid;
60 // Load params from Event Calendar (module)
61 $this->ec_modParams = $params;
63 // Load params from Event Calendar (component)
64 $database->setQuery("SELECT params FROM #__components WHERE link='option=com_eventcalendar'", '#__', 1, 0);
65 $result = $database->loadResult();
66 $this->ec_comParams = new mosParameters($result);
68 // Get MENUITEM for links to Event Calendar (component)
69 $this->ec_comItemid = $Itemid;
70 if ($mainframe->getCfg('sef') != 2) {
71 $access = !$mainframe->getCfg('shownoauth');
72 $query = "SELECT id FROM #__menu WHERE link='index.php?option=com_eventcalendar' AND published='1'"
73 ."\n AND ((language IS NULL) OR (language LIKE '%".$lang."%'))"
74 .($access ? "\n AND access IN (".$my->allowed.")" : "");
75 $database->setQuery($query, '#__', 1, 0);
76 $this->ec_comItemid = intval($database->loadResult());
77 if (!$this->ec_comItemid) { $this->ec_comItemid = $Itemid; }
80 // Set variables
81 $this->ec_year = (string)date("Y", time());
82 $this->ec_month = (string)date("m", time());
83 $this->ec_day = (string)date("d", time());
84 $this->ec_date = strtotime($this->ec_year."-".$this->ec_month."-".$this->ec_day);
87 /******************************/
88 /* The class' main function */
89 /******************************/
90 public function main() {
91 // standard functionality
92 $this->loadCalendar();
93 $this->drawCalendar();
96 /***************************************/
97 /* Prepare to display calendar table */
98 /***************************************/
99 protected function loadCalendar() {
100 global $fmanager;
102 $week_startingday = $this->ec_comParams->get('week_startingday' , 0);
104 // Get timestamps for first and last day of month
105 $firstDay_stamp = mktime( 0, 0, 0, $this->ec_month, 1, $this->ec_year );
106 $lastDay_stamp = mktime( 0, 0, 0, $this->ec_month + 1, 1, $this->ec_year );
107 $lastDay_stamp = strtotime("yesterday", $lastDay_stamp);
109 // Get how many days will be empty in the view before current month starts
110 $prevMonth_days = intval( strftime("%w", $firstDay_stamp) ) - $week_startingday;
111 $prevMonth_days = ($prevMonth_days < 0)?$prevMonth_days + 7:$prevMonth_days;
113 // Get how many days will be empty in the view after current month ends
114 $nextMonth_days = intval( 6 - strftime("%w", $lastDay_stamp) + $week_startingday);
115 $nextMonth_days = ($nextMonth_days == 7)?0:$nextMonth_days;
117 // Loading weeknumbers
118 $working_date = $firstDay_stamp;
119 // Get the number of weeks in the view
120 for($i = 0; $i < ( $prevMonth_days + strftime("%d", $lastDay_stamp) + $nextMonth_days ) / 7 ; $i++ ) {
121 $this->ec_weeknrs[] = ($fmanager->iswin) ? strftime("%U", $working_date) : strftime("%V", $working_date);
122 $working_date = strtotime("+ 1 week", $working_date);
125 // Create calender matrix of dates for days of month
126 $working_date = strtotime( "- ".$prevMonth_days." days", $firstDay_stamp);
127 // Fill previous month days
128 for ($i = 0; $i < $prevMonth_days; $i++) {
129 $this->ec_calendar[] = $working_date;
130 $working_date = strtotime( "+ 1 day", $working_date);
132 // Fill current month days
133 for ($i = 0; $i < strftime("%d", $lastDay_stamp); $i++) {
134 $this->ec_calendar[] = $working_date;
135 $working_date = strtotime( "+ 1 day", $working_date);
137 // Fill next month days
138 for ($i = 0; $i < $nextMonth_days; $i++) {
139 $this->ec_calendar[] = $working_date;
140 $working_date = strtotime( "+ 1 day", $working_date);
143 // Calculate events
144 $this->calcEvents();
147 /*************************/
148 /* Draw calendar table */
149 /*************************/
150 protected function drawCalendar() {
151 global $mainframe, $mosConfig_absolute_path, $lang;
153 // Load core HTML library
154 mosCommonHTML::loadOverlib();
156 // Display month table
157 $thisMonth = mktime( 0, 0, 0, $this->ec_month, 1, $this->ec_year );
158 $lastMonth = strtotime( "1 month ago", $thisMonth );
159 $nextMonth = strtotime( "+ 1 month", $thisMonth );?>
161 <table>
162 <!-- Calendar table -->
163 <tr>
164 <!-- Weeknum empty -->
165 <?php if ($this->ec_modParams->get('show_weeknumber', true)) { ?>
166 <td class="weeknum_empty" rowspan="2"></td>
167 <?php } ?>
168 <!-- Navigation -->
169 <td class="navprev" colspan="2">
170 <?php
171 if (($this->ec_month - 1) <= 0) {
172 $month = '12';
173 $year = $this->ec_year - 1;
174 } else {
175 $month = $this->ec_month - 1;
176 $month = (strlen($month) == 1) ? ("0".$month) : $month;
177 $year = $this->ec_year;
180 <a href="<?php echo sefRelToAbs("index.php?option=com_eventcalendar&task=monthview&year=".$year."&month=".$month."&Itemid=".$this->ec_comItemid, EVCALBASE."/all/".$year."/".$month."/") ?>"> << </a>
181 </td>
182 <td class="nav" colspan="3">
183 <a href="<?php echo sefRelToAbs("index.php?option=com_eventcalendar&task=monthview&year=".date("Y", $thisMonth)."&month=".date("m", $thisMonth)."&Itemid=".$this->ec_comItemid, EVCALBASE."/all/".date("Y", $thisMonth)."/".date("m", $thisMonth)."/") ?>">
184 <?php echo strftime( "%B", $thisMonth ) . " " . strftime("%Y", $thisMonth) ?>
185 </a>
186 </td>
187 <td class="navnext" colspan="2">
188 <?php
189 if (($this->ec_month + 1) > 12) {
190 $month = '01';
191 $year = $this->ec_year + 1;
192 } else {
193 $month = $this->ec_month + 1;
194 $month = (strlen($month) == 1) ? ("0".$month) : $month;
195 $year = $this->ec_year;
198 <a href="<?php echo sefRelToAbs("index.php?option=com_eventcalendar&task=monthview&year=".$year."&month=".$month."&Itemid=".$this->ec_comItemid, EVCALBASE."/all/".$year."/".$month."/") ?>"> >> </a>
199 </td>
200 </tr>
201 <tr>
202 <!-- Week names -->
203 <?php for($dcount = 0; $dcount < 7; $dcount++) {?>
204 <td class="weeknames"><?php echo strftime("%a", $this->ec_calendar[$dcount]) ?></td>
205 <?php }?>
206 </tr>
207 <?php for ($wcount = 1; $wcount <= count($this->ec_weeknrs); $wcount++) { ?>
208 <tr>
209 <?php
210 if ($this->ec_modParams->get('show_weeknumber', true)) { ?>
211 <!-- Week num -->
212 <td class="weeknum">
213 <?php
214 if ($this->ec_modParams->get('week_number_links', true)) {
216 <a href="<?php echo sefRelToAbs( "/index.php?option=com_eventcalendar&task=weekview&year=".$this->ec_year."&week=".$this->ec_weeknrs[$wcount-1]."&Itemid=".$this->ec_comItemid, EVCALBASE."/all/".$this->ec_year."/".$this->ec_weeknrs[$wcount-1].".html") ?>">
217 <?php echo $this->ec_weeknrs[$wcount-1]; ?>
218 </a>
219 <?php } else {
220 echo $this->ec_weeknrs[$wcount-1];
221 } ?>
222 </td>
223 <?php
225 for ($wdcount = 1; $wdcount <= 7; $wdcount++) {
226 $working_day = $this->ec_calendar[(($wcount * 7) - 7) + ($wdcount - 1)];
227 if (isset($working_day)) {
228 if (date("m", $working_day) < date("m", $this->ec_date)) { ?>
229 <!-- Last month -->
230 <td class="othermonth">
231 <span><?php echo strftime("%b", $working_day) ?></span>
232 <?php } elseif (date("m", $working_day) > date("m", $this->ec_date)) { ?>
233 <!-- Next month -->
234 <td class="othermonth">
235 <span><?php echo strftime("%b", $working_day) ?></span>
236 <?php } else { ?>
237 <?php
238 $marked = false;
239 foreach ($this->ec_events as $day_event) {
240 // Calculate recursion
241 if ($this->calcRecursion($working_day, $day_event)) {
242 // Calculate exception days
243 $exceptions = $this->calcExceptions( $day_event );
244 if (array_search($working_day, $exceptions) === false) {
245 $marked = true;
249 if ($marked) { ?>
250 <td class="<?php echo (strftime("%d", $working_day) == strftime("%d", time()))?'cur_':''; ?>day_event">
251 <span>
252 <a href="<?php echo sefRelToAbs("index.php?option=com_eventcalendar&task=dayview&year=".$this->ec_year."&month=".$this->ec_month."&day=".strftime("%d", $working_day)."&Itemid=".$this->ec_comItemid, EVCALBASE."/all/".$this->ec_year."/".$this->ec_month."/".strftime("%d", $working_day).".html") ?>">
253 <?php
254 echo (date("Y-m-d", $working_day) == strftime("Y-m-d", time()))?'<b>':'';
255 echo strftime("%d", $working_day);
256 echo (date("Y-m-d", $working_day) == strftime("Y-m-d", time()))?'</b>':'';
258 </a>
259 </span>
260 <?php } else { ?>
261 <td class="<?php echo (strftime("%d", $working_day) == strftime("%d", time()))?'cur_':''; ?>day">
262 <span>
263 <?php
264 echo (date("Y-m-d", $working_day) == strftime("Y-m-d", time()))?'<b>':'';
265 echo strftime("%d", $working_day);
266 echo (date("Y-m-d", $working_day) == strftime("Y-m-d", time()))?'</b>':'';
268 </span>
269 <?php } ?>
270 </td>
271 <?php }
274 </tr>
275 <?php }
277 // Forth-coming table
278 if ($this->ec_modParams->get('view_forth') == '1') { ?>
279 <tr><td class="spacer"></td></tr>
280 <tr>
281 <td class="subtitle" colspan="<?php echo ($this->ec_modParams->get('show_weeknumber', true))?'8':'7'; ?>">
282 <h5><?php echo CX_EVCALMOD_FORTH_EVENTS; ?>:</h5>
283 </td>
284 </tr><tr>
285 <td class="forth_events" colspan="<?php echo ($this->ec_modParams->get('show_weeknumber', true))?'8':'7'; ?>">
286 <?php
287 $forth_count = 0;
288 $displist = array();
289 $working_day = strtotime( "+1 day", mktime(0, 0, 0, date("m"), date("d"), date("Y")) );
290 for ($i = 1; $i <= $this->ec_modParams->get('view_forth_days'); $i++) {
291 foreach ($this->ec_events as $day_event) {
292 if ($forth_count <= $this->ec_modParams->get('view_forth_num')) {
293 // Calculate recursion
294 if ($this->calcRecursion($working_day, $day_event)) {
295 // Calculate exception days
296 $exceptions = $this->calcExceptions( $day_event );
297 if (array_search($working_day, $exceptions) === false) {
298 // Prevent events been displayed more than once
299 if (array_search($day_event->id, $displist) === false) { ?>
300 <li><a href="<?php echo sefRelToAbs("index.php?option=com_eventcalendar&task=eventview&eventid=".$day_event->id."&Itemid=".$this->ec_comItemid, EVCALBASE."/".$day_event->seotitle.".html") ?>"><?php echo $day_event->title; ?></a></li>
301 <?php $forth_count++;
302 $displist[] = $day_event->id;
308 $working_day = strtotime("+1 day", $working_day);
310 if ($forth_count == 0) { ?>
311 <p><?php echo CX_EVCALMOD_FORTH_NONE; ?></p>
312 <?php } ?>
313 </td>
314 </tr>
315 <?php } ?>
318 <?php // Day info table
319 if ($this->ec_modParams->get('view_info') == '1') {
320 require_once( $mosConfig_absolute_path.'/modules/mod_eventcalendar/sun/sun.php' );
321 require_once( $mosConfig_absolute_path.'/modules/mod_eventcalendar/sun/moon-phase.cls.php' );
322 if ( file_exists($mosConfig_absolute_path.'/modules/mod_eventcalendar/anniversaries/'.$lang.'.php') ) {
323 require_once( $mosConfig_absolute_path.'/modules/mod_eventcalendar/anniversaries/'.$lang.'.php' );
324 $ann = 'true';
327 $objMoon = new moonPhase(-1);
328 $moon = round( $objMoon->getPeriodInDays() - $objMoon->getDaysUntilNextNewMoon(), 0);
329 $moon_name = $objMoon->getPhaseName();
331 if ( $moon_name == "New Moon" or $moon == 0 ) {
332 $moon_name = CX_EVCALMOD_INFO_NEWMOON;
333 } elseif ( $moon_name == "First Quarter Moon" ) {
334 $moon_name = CX_EVCALMOD_INFO_FQMOON;
335 } elseif ( $moon_name == "Full Moon" ) {
336 $moon_name = CX_EVCALMOD_INFO_FULLMOON;
337 } elseif ( $moon_name == "Third Quarter Moon" ) {
338 $moon_name = CX_EVCALMOD_INFO_TQMOON;
339 } else {
340 $moon_name = '';
343 if ( $moon_name ) {
344 $moon_text = CX_EVCALMOD_INFO_MOONPRE . ":<br/>" . $moon_name;
345 } else {
346 if ( $moon == 1 ) {
347 $moon_post = CX_EVCALMOD_INFO_MOONPOSTSING;
348 } else {
349 $moon_post = CX_EVCALMOD_INFO_MOONPOSTPLUR;
351 $moon_text = CX_EVCALMOD_INFO_MOONPRE . "<br />" . $moon . "&nbsp;" . $moon_post;
352 } ?>
354 <tr><td class="spacer"></td></tr>
355 <tr>
356 <td class="subtitle" colspan="<?php echo ($this->ec_modParams->get('show_weeknumber', true))?'8':'7'; ?>">
357 <h5><?php echo CX_EVCALMOD_INFO_TODAY.', '.$this->ec_year.'/'.$this->ec_month.'/'.$this->ec_day; ?></h5>
358 </td>
359 </tr>
360 <?php if ($this->ec_modParams->get('view_info_place')) { ?>
361 <tr>
362 <td class="place" colspan="<?php echo ($this->ec_modParams->get('show_weeknumber', true))?'8':'7'; ?>">
363 <?php echo $this->ec_modParams->get('view_info_place'); ?>
364 </td>
365 </tr>
366 <?php } ?>
367 <tr>
368 <td class="sun-moon" colspan="<?php echo ($this->ec_modParams->get('show_weeknumber', true))?'8':'7'; ?>">
369 <table><tr>
370 <td class="sun">
372 <?php echo CX_EVCALMOD_INFO_SUNRISE; ?>:&nbsp;<?php echo date("H:i", sun::rise($this->ec_date, $this->ec_modParams->get('view_info_timezone'), $this->ec_modParams->get('view_info_latitude'), $this->ec_modParams->get('view_info_longitude'), $this->ec_modParams->get('view_info_zenith') )); ?><br/>
373 <?php echo CX_EVCALMOD_INFO_SUNSET; ?>:&nbsp;<?php echo date("H:i", sun::set($this->ec_date, $this->ec_modParams->get('view_info_timezone'), $this->ec_modParams->get('view_info_latitude'), $this->ec_modParams->get('view_info_longitude'), $this->ec_modParams->get('view_info_zenith') )); ?>
374 </p>
375 </td><td class="moon">
376 <p><?php echo $moon_text; ?></p>
377 </td>
378 </tr></table>
379 </td>
380 </tr>
382 <!-- Nameday table -->
383 <?php if ($ann) {
384 calcSpecialNamedays($this->ec_day, $this->ec_month, $namedays_long, $namedays_short, $moving_holidays); ?>
385 <tr><td class="spacer"></td></tr>
386 <tr><td class="namedays" colspan="<?php echo ($this->ec_modParams->get('show_weeknumber', true))?'8':'7'; ?>">
387 <p><?php echo $namedays_long[$this->ec_day.'/'.$this->ec_month] ?><br/><?php echo isset($namedays_short[$this->ec_day.'/'.$this->ec_month]) ? '('.$namedays_short[$this->ec_day.'/'.$this->ec_month].')' : '' ?></p>
388 </td></tr>
390 <!-- Worldday table -->
391 <tr><td class="spacer"></td></tr>
392 <tr><td class="worlddays" colspan="<?php echo ($this->ec_modParams->get('show_weeknumber', true))?'8':'7'; ?>">
393 <p><?php echo getWorldday($this->ec_day.'/'.$this->ec_month, $worlddays); ?></p>
394 </td></tr>
395 <?php }
396 } ?>
397 </table>
398 <?php
401 /********************/
402 /* Calculate events */
403 /********************/
404 protected function calcEvents() {
405 global $database, $my;
407 // Load events from database
408 $query = "SELECT e.id, e.start_date, e.end_date FROM #__eventcalendar e"
409 . "\n LEFT JOIN #__categories c ON c.id = e.catid"
410 . "\n WHERE e.published = '1' AND c.published = '1' AND c.access IN (".$my->allowed.")"
411 . "\n ORDER BY e.start_date ASC";
412 $database->setQuery( $query );
413 $results = $database->loadObjectList();
415 // Calculate events
416 foreach ($results as $row_event) {
417 if ( (strtotime($row_event->start_date) <= $this->ec_calendar[count($this->ec_calendar)-1]) || (strtotime($row_event->end_date) >= $this->ec_calendar[0]) ) {
418 $load_event = new mosEventCalendar_Event($database);
419 $load_event->load($row_event->id);
420 $this->ec_events[] = clone( $load_event );
425 /************************/
426 /* Calculate exceptions */
427 /************************/
428 public function calcExceptions( $event ) {
429 $events = split("\n", $event->recur_except);
430 for ($i = 0; $i <= (count($events)-1); $i++) {
431 $events[$i] = strtotime($events[$i]);
433 return $events;
436 /***********************/
437 /* Calculate recursion */
438 /***********************/
439 public function calcRecursion ( $date, $event ) {
441 // Check if date is in event publishing range
442 if ( ((strtotime(date("Y-m-d", strtotime($event->start_date)))) <= $date) && ((strtotime(date("Y-m-d", strtotime($event->end_date)))) >= $date) ) {
444 switch ($event->recur_type) {
445 case 'week':
446 return ( date("w", $date) == $event->recur_week );
447 break;
448 case 'month':
449 return ( $event->recur_month == date("j", $date) );
450 break;
451 case 'year':
452 return ( ($event->recur_year_d."/".$event->recur_year_m ) == date("j/n", $date) );
453 break;
454 case 'day':
455 return true;
456 break;
457 default:
458 return false;
459 break;
464 } // end class
465 } // end if class_exists
467 // Start module
468 $objEventCalendarMOD = new clsEventCalendarMOD($params, $database);
469 $objEventCalendarMOD->main();
470 unset($objEventCalendarMOD);