PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Calendar.php

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