PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/system/codeigniter/system/libraries/Calendar.php

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