Calendar.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter Calendar Class
  19. *
  20. * This class enables the creation of calendars
  21. *
  22. * @package CodeIgniter
  23. * @subpackage Libraries
  24. * @category Libraries
  25. * @author EllisLab Dev Team
  26. * @link http://codeigniter.com/user_guide/libraries/calendar.html
  27. */
  28. class CI_Calendar {
  29. var $CI;
  30. var $lang;
  31. var $local_time;
  32. var $template = '';
  33. var $start_day = 'sunday';
  34. var $month_type = 'long';
  35. var $day_type = 'abr';
  36. var $show_next_prev = FALSE;
  37. var $next_prev_url = '';
  38. /**
  39. * Constructor
  40. *
  41. * Loads the calendar language file and sets the default time reference
  42. */
  43. public function __construct($config = array())
  44. {
  45. $this->CI =& get_instance();
  46. if ( ! in_array('calendar_lang.php', $this->CI->lang->is_loaded, TRUE))
  47. {
  48. $this->CI->lang->load('calendar');
  49. }
  50. $this->local_time = time();
  51. if (count($config) > 0)
  52. {
  53. $this->initialize($config);
  54. }
  55. log_message('debug', "Calendar Class Initialized");
  56. }
  57. // --------------------------------------------------------------------
  58. /**
  59. * Initialize the user preferences
  60. *
  61. * Accepts an associative array as input, containing display preferences
  62. *
  63. * @access public
  64. * @param array config preferences
  65. * @return void
  66. */
  67. function initialize($config = array())
  68. {
  69. foreach ($config as $key => $val)
  70. {
  71. if (isset($this->$key))
  72. {
  73. $this->$key = $val;
  74. }
  75. }
  76. }
  77. // --------------------------------------------------------------------
  78. /**
  79. * Generate the calendar
  80. *
  81. * @access public
  82. * @param integer the year
  83. * @param integer the month
  84. * @param array the data to be shown in the calendar cells
  85. * @return string
  86. */
  87. function generate($year = '', $month = '', $data = array())
  88. {
  89. // Set and validate the supplied month/year
  90. if ($year == '')
  91. $year = date("Y", $this->local_time);
  92. if ($month == '')
  93. $month = date("m", $this->local_time);
  94. if (strlen($year) == 1)
  95. $year = '200'.$year;
  96. if (strlen($year) == 2)
  97. $year = '20'.$year;
  98. if (strlen($month) == 1)
  99. $month = '0'.$month;
  100. $adjusted_date = $this->adjust_date($month, $year);
  101. $month = $adjusted_date['month'];
  102. $year = $adjusted_date['year'];
  103. // Determine the total days in the month
  104. $total_days = $this->get_total_days($month, $year);
  105. // Set the starting day of the week
  106. $start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
  107. $start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];
  108. // Set the starting day number
  109. $local_date = mktime(12, 0, 0, $month, 1, $year);
  110. $date = getdate($local_date);
  111. $day = $start_day + 1 - $date["wday"];
  112. while ($day > 1)
  113. {
  114. $day -= 7;
  115. }
  116. // Set the current month/year/day
  117. // We use this to determine the "today" date
  118. $cur_year = date("Y", $this->local_time);
  119. $cur_month = date("m", $this->local_time);
  120. $cur_day = date("j", $this->local_time);
  121. $is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;
  122. // Generate the template data array
  123. $this->parse_template();
  124. // Begin building the calendar output
  125. $out = $this->temp['table_open'];
  126. $out .= "\n";
  127. $out .= "\n";
  128. $out .= $this->temp['heading_row_start'];
  129. $out .= "\n";
  130. // "previous" month link
  131. if ($this->show_next_prev == TRUE)
  132. {
  133. // Add a trailing slash to the URL if needed
  134. $this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->next_prev_url);
  135. $adjusted_date = $this->adjust_date($month - 1, $year);
  136. $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
  137. $out .= "\n";
  138. }
  139. // Heading containing the month/year
  140. $colspan = ($this->show_next_prev == TRUE) ? 5 : 7;
  141. $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);
  142. $this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)."&nbsp;".$year, $this->temp['heading_title_cell']);
  143. $out .= $this->temp['heading_title_cell'];
  144. $out .= "\n";
  145. // "next" month link
  146. if ($this->show_next_prev == TRUE)
  147. {
  148. $adjusted_date = $this->adjust_date($month + 1, $year);
  149. $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
  150. }
  151. $out .= "\n";
  152. $out .= $this->temp['heading_row_end'];
  153. $out .= "\n";
  154. // Write the cells containing the days of the week
  155. $out .= "\n";
  156. $out .= $this->temp['week_row_start'];
  157. $out .= "\n";
  158. $day_names = $this->get_day_names();
  159. for ($i = 0; $i < 7; $i ++)
  160. {
  161. $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
  162. }
  163. $out .= "\n";
  164. $out .= $this->temp['week_row_end'];
  165. $out .= "\n";
  166. // Build the main body of the calendar
  167. while ($day <= $total_days)
  168. {
  169. $out .= "\n";
  170. $out .= $this->temp['cal_row_start'];
  171. $out .= "\n";
  172. for ($i = 0; $i < 7; $i++)
  173. {
  174. $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
  175. if ($day > 0 AND $day <= $total_days)
  176. {
  177. if (isset($data[$day]))
  178. {
  179. // Cells with content
  180. $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
  181. $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
  182. }
  183. else
  184. {
  185. // Cells with no content
  186. $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
  187. $out .= str_replace('{day}', $day, $temp);
  188. }
  189. }
  190. else
  191. {
  192. // Blank cells
  193. $out .= $this->temp['cal_cell_blank'];
  194. }
  195. $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];
  196. $day++;
  197. }
  198. $out .= "\n";
  199. $out .= $this->temp['cal_row_end'];
  200. $out .= "\n";
  201. }
  202. $out .= "\n";
  203. $out .= $this->temp['table_close'];
  204. return $out;
  205. }
  206. // --------------------------------------------------------------------
  207. /**
  208. * Get Month Name
  209. *
  210. * Generates a textual month name based on the numeric
  211. * month provided.
  212. *
  213. * @access public
  214. * @param integer the month
  215. * @return string
  216. */
  217. function get_month_name($month)
  218. {
  219. if ($this->month_type == 'short')
  220. {
  221. $month_names = array('01' => 'cal_jan', '02' => 'cal_feb', '03' => 'cal_mar', '04' => 'cal_apr', '05' => 'cal_may', '06' => 'cal_jun', '07' => 'cal_jul', '08' => 'cal_aug', '09' => 'cal_sep', '10' => 'cal_oct', '11' => 'cal_nov', '12' => 'cal_dec');
  222. }
  223. else
  224. {
  225. $month_names = array('01' => 'cal_january', '02' => 'cal_february', '03' => 'cal_march', '04' => 'cal_april', '05' => 'cal_mayl', '06' => 'cal_june', '07' => 'cal_july', '08' => 'cal_august', '09' => 'cal_september', '10' => 'cal_october', '11' => 'cal_november', '12' => 'cal_december');
  226. }
  227. $month = $month_names[$month];
  228. if ($this->CI->lang->line($month) === FALSE)
  229. {
  230. return ucfirst(str_replace('cal_', '', $month));
  231. }
  232. return $this->CI->lang->line($month);
  233. }
  234. // --------------------------------------------------------------------
  235. /**
  236. * Get Day Names
  237. *
  238. * Returns an array of day names (Sunday, Monday, etc.) based
  239. * on the type. Options: long, short, abrev
  240. *
  241. * @access public
  242. * @param string
  243. * @return array
  244. */
  245. function get_day_names($day_type = '')
  246. {
  247. if ($day_type != '')
  248. $this->day_type = $day_type;
  249. if ($this->day_type == 'long')
  250. {
  251. $day_names = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
  252. }
  253. elseif ($this->day_type == 'short')
  254. {
  255. $day_names = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
  256. }
  257. else
  258. {
  259. $day_names = array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
  260. }
  261. $days = array();
  262. foreach ($day_names as $val)
  263. {
  264. $days[] = ($this->CI->lang->line('cal_'.$val) === FALSE) ? ucfirst($val) : $this->CI->lang->line('cal_'.$val);
  265. }
  266. return $days;
  267. }
  268. // --------------------------------------------------------------------
  269. /**
  270. * Adjust Date
  271. *
  272. * This function makes sure that we have a valid month/year.
  273. * For example, if you submit 13 as the month, the year will
  274. * increment and the month will become January.
  275. *
  276. * @access public
  277. * @param integer the month
  278. * @param integer the year
  279. * @return array
  280. */
  281. function adjust_date($month, $year)
  282. {
  283. $date = array();
  284. $date['month'] = $month;
  285. $date['year'] = $year;
  286. while ($date['month'] > 12)
  287. {
  288. $date['month'] -= 12;
  289. $date['year']++;
  290. }
  291. while ($date['month'] <= 0)
  292. {
  293. $date['month'] += 12;
  294. $date['year']--;
  295. }
  296. if (strlen($date['month']) == 1)
  297. {
  298. $date['month'] = '0'.$date['month'];
  299. }
  300. return $date;
  301. }
  302. // --------------------------------------------------------------------
  303. /**
  304. * Total days in a given month
  305. *
  306. * @access public
  307. * @param integer the month
  308. * @param integer the year
  309. * @return integer
  310. */
  311. function get_total_days($month, $year)
  312. {
  313. $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  314. if ($month < 1 OR $month > 12)
  315. {
  316. return 0;
  317. }
  318. // Is the year a leap year?
  319. if ($month == 2)
  320. {
  321. if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
  322. {
  323. return 29;
  324. }
  325. }
  326. return $days_in_month[$month - 1];
  327. }
  328. // --------------------------------------------------------------------
  329. /**
  330. * Set Default Template Data
  331. *
  332. * This is used in the event that the user has not created their own template
  333. *
  334. * @access public
  335. * @return array
  336. */
  337. function default_template()
  338. {
  339. return array (
  340. 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
  341. 'heading_row_start' => '<tr>',
  342. 'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
  343. 'heading_title_cell' => '<th colspan="{colspan}">{heading}</th>',
  344. 'heading_next_cell' => '<th><a href="{next_url}">&gt;&gt;</a></th>',
  345. 'heading_row_end' => '</tr>',
  346. 'week_row_start' => '<tr>',
  347. 'week_day_cell' => '<td>{week_day}</td>',
  348. 'week_row_end' => '</tr>',
  349. 'cal_row_start' => '<tr>',
  350. 'cal_cell_start' => '<td>',
  351. 'cal_cell_start_today' => '<td>',
  352. 'cal_cell_content' => '<a href="{content}">{day}</a>',
  353. 'cal_cell_content_today' => '<a href="{content}"><strong>{day}</strong></a>',
  354. 'cal_cell_no_content' => '{day}',
  355. 'cal_cell_no_content_today' => '<strong>{day}</strong>',
  356. 'cal_cell_blank' => '&nbsp;',
  357. 'cal_cell_end' => '</td>',
  358. 'cal_cell_end_today' => '</td>',
  359. 'cal_row_end' => '</tr>',
  360. 'table_close' => '</table>'
  361. );
  362. }
  363. // --------------------------------------------------------------------
  364. /**
  365. * Parse Template
  366. *
  367. * Harvests the data within the template {pseudo-variables}
  368. * used to display the calendar
  369. *
  370. * @access public
  371. * @return void
  372. */
  373. function parse_template()
  374. {
  375. $this->temp = $this->default_template();
  376. if ($this->template == '')
  377. {
  378. return;
  379. }
  380. $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today');
  381. foreach (array('table_open', 'table_close', 'heading_row_start', 'heading_previous_cell', 'heading_title_cell', 'heading_next_cell', 'heading_row_end', 'week_row_start', 'week_day_cell', 'week_row_end', 'cal_row_start', 'cal_cell_start', 'cal_cell_content', 'cal_cell_no_content', 'cal_cell_blank', 'cal_cell_end', 'cal_row_end', 'cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today') as $val)
  382. {
  383. if (preg_match("/\{".$val."\}(.*?)\{\/".$val."\}/si", $this->template, $match))
  384. {
  385. $this->temp[$val] = $match['1'];
  386. }
  387. else
  388. {
  389. if (in_array($val, $today, TRUE))
  390. {
  391. $this->temp[$val] = $this->temp[str_replace('_today', '', $val)];
  392. }
  393. }
  394. }
  395. }
  396. }
  397. // END CI_Calendar class
  398. /* End of file Calendar.php */
  399. /* Location: ./system/libraries/Calendar.php */