PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Calendar.php

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