Posted By

alexmglover on 02/12/11


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

czehner
luxorweb


Event Calendar


 / Published in: PHP
 

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * Event Calendar Class
  4.  *
  5.  * This class enables the creation of event calendar
  6.  *
  7.  * @author Alex Glover <[email protected]>
  8.  */
  9. class EventCalendar {
  10.  
  11. var $CI;
  12. var $show_next_prev = TRUE;
  13. var $next_prev_url = '';
  14. var $day_url = '';
  15. var $events_url = '';
  16.  
  17. /**
  18.   * Constructor
  19.   *
  20.   * @access public
  21.   */
  22. function EventCalendar($config = array()) {
  23.  
  24. if(count($config) > 0) {
  25. $this->initialize($config);
  26. }
  27. }
  28.  
  29. /**
  30.   * Initializes the configuration
  31.   *
  32.   * @access public
  33.   * @param array config array
  34.   * @return void
  35.   */
  36. function initialize($config = array())
  37. {
  38. foreach ($config as $key => $val)
  39. {
  40. if (isset($this->$key))
  41. {
  42. $this->$key = $val;
  43. }
  44. }
  45. }
  46.  
  47.  
  48. /**
  49.   * Generates the event calendar
  50.   *
  51.   * @access public
  52.   * @param array config array
  53.   * @param array events data array
  54.   * @return void
  55.   */
  56. function generate($config = NULL, $events = NULL)
  57. {
  58.  
  59. $date = $config['month'] . ' ' . $config['year'];
  60.  
  61. //Tests if the offset is set, which is how many months away from current month the calendar is displaying
  62. if(isset($config['offset'])) {
  63. $offset = $config['offset'];
  64. $prev = $offset - 1;
  65. $next = $offset + 1;
  66. } else {
  67. $prev = -1;
  68. $next = 1;
  69. }
  70.  
  71. //prints the calendar name and navigation arrows
  72. echo '<span id="calendar-month">';
  73.  
  74. echo anchor($this->next_prev_url . $prev . "#calendar-month", '<img src="'.base_url().'assets/images/calendar-left.png" id="calendar-left" class="calendar-nav"/>', 'rel="nofollow"') . date('F Y', strtotime($date)) . anchor($this->next_prev_url . $next . '#calendar-month', '<img src="'.base_url().'assets/images/calendar-right.png" id="calendar-right" class="calendar-nav"/>', 'rel="nofollow"') . '</span>';
  75.  
  76.  
  77. echo '
  78. <table cellpadding="0" cellspacing="0" border="1" class="volunteer-calendar">
  79. <thead>
  80. <tr>
  81. <th>Sunday</th>
  82. <th>Monday</th>
  83. <th>Tuesday</th>
  84. <th>Wednesday</th>
  85. <th>Thursday</th>
  86. <th>Friday</th>
  87. <th>Saturday</th>
  88. </tr>
  89. </thead>
  90.  
  91. <tbody>';
  92.  
  93. $first = $this->getFirstWeekDay($config['month'] . $config['year']);
  94. $total_days = $this->getTotalDaysinMonth($date);
  95.  
  96. $count = 0;
  97.  
  98. echo '<tr>';
  99.  
  100. //displays empty days
  101. while($count < $first) {
  102. echo '<td class="empty">&nbsp;</td>' . "\n";
  103. $count++;
  104. }
  105.  
  106. //count used to display the date
  107. $day_count = 1;
  108.  
  109. //loops through as long as day_countis less than the last day of the month
  110. while($day_count <= $total_days) {
  111.  
  112. $daily_events = $this->getDailyEvents(date('Y-m-' . $day_count, strtotime($config['month'] . ' ' . $config['year'])), $events);
  113.  
  114. $class = '';
  115.  
  116. //if there are events on the specific day
  117. if(isset($daily_events)) {
  118. $print_events = '<ul class="events">' . $daily_events . '</ul>';
  119. }
  120.  
  121. if(date('Y-m-' . $day_count, strtotime($config['month'] .' ' . $config['year'])) == date('Y-m-d')) {
  122. $class .= 'today';
  123. }
  124.  
  125. //displays the day's number on the calendar
  126. $day_num = ($daily_events != '') ? '<a href="'.site_url().$this->day_url.date('Y-m-' . $day_count, strtotime($config['month'] .' ' . $config['year'])).'" rel="nofollow">' . $day_count . '</a>' : $day_count;
  127.  
  128. echo '<td class="'.$class.'"><span class="day">' . $day_num . '</span>' .
  129. $print_events
  130. . '</td>';
  131.  
  132. if($count == 6) {
  133. $count = -1;
  134. echo '</tr>'. "\n";
  135. echo '<tr>';
  136. }
  137. $day_count++;
  138. $count++;
  139. }
  140.  
  141.  
  142.  
  143. while($count <= 6) {
  144. echo '<td class="empty">&nbsp;</td>' . "\n";
  145. $count++;
  146. }
  147. echo '</tr>';
  148. echo '</tbody>';
  149. echo '</table>';
  150. }
  151.  
  152. /**
  153.   * Gets the first day of the month in week number format (0-7 returned)
  154.   *
  155.   * @access public
  156.   * @param date
  157.   */
  158. function getFirstWeekDay($date) {
  159. return date('w', strtotime($date));
  160. }
  161.  
  162. /**
  163.   * Gets the total number of days in the given month
  164.   *
  165.   * @access public
  166.   * @param date
  167.   */
  168. function getTotalDaysinMonth($date) {
  169. return date('t', strtotime($date));
  170. }
  171.  
  172. /**
  173.   * Gets the events from the events array that match the given date
  174.   *
  175.   * @access public
  176.   * @param String date
  177.   * @param array events
  178.   * @return String
  179.   */
  180. function getDailyEvents($date, $events) {
  181.  
  182. if(!empty($events)) {
  183.  
  184. $theevents = '';
  185. foreach($events as $event) {
  186.  
  187. if($event->start_date >= date('Y-m-d 00:00:00', strtotime($date)) && $event->start_date <= date('Y-m-d 23:59:59', strtotime($date))) {
  188.  
  189. $theevents .= '<li>' . anchor($this->events_url . $event->id, $event->title , 'rel="nofollow"') . '</li>';
  190. }
  191. }
  192.  
  193. }
  194.  
  195. return $theevents;
  196. }
  197.  
  198.  
  199. }
  200.  
  201. ?>

Report this snippet  

You need to login to post a comment.