/halogy/plugins/js_calendar_pi.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 629 lines · 378 code · 86 blank · 165 comment · 100 complexity · ef9a218b4a8058b37f8464ca7c593c8f 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. Instructions:
  18. Load the plugin using:
  19. $this->load->plugin('js_calendar');
  20. Once loaded you'll add the calendar script to the <head> of your page like this:
  21. <?php echo js_calendar_script('my_form'); ?>
  22. The above function will be passed the name of your form.
  23. Then to show the actual calendar you'll do this:
  24. <?php echo js_calendar_write('entry_date', time(), true);?>
  25. <form name="my_form">
  26. <input type="text" name="entry_date" value="" onblur="update_calendar(this.name, this.value);" />
  27. <p><a href="javascript:void(0);" onClick="set_to_time('entry_date', '<?php echo time();?>')" >Today</a></p>
  28. </form>
  29. Note: The first parameter is the name of the field containing your date, the second parameter contains the "now" time,
  30. and the third tells the calendar whether to highlight the current day or not.
  31. Lastly, you'll need some CSS for your calendar:
  32. .calendar {
  33. border: 1px #6975A3 solid;
  34. background-color: transparent;
  35. }
  36. .calheading {
  37. background-color: #7C8BC0;
  38. color: #fff;
  39. font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
  40. font-size: 11px;
  41. font-weight: bold;
  42. text-align: center;
  43. }
  44. .calnavleft {
  45. background-color: #7C8BC0;
  46. font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
  47. font-size: 10px;
  48. font-weight: bold;
  49. color: #fff;
  50. padding: 4px;
  51. cursor: pointer;
  52. }
  53. .calnavright {
  54. background-color: #7C8BC0;
  55. font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
  56. font-size: 10px;
  57. font-weight: bold;
  58. color: #fff;
  59. text-align: right;
  60. padding: 4px;
  61. cursor: pointer;
  62. }
  63. .caldayheading {
  64. background-color: #000;
  65. color: #fff;
  66. font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
  67. font-size: 10px;
  68. text-align: center;
  69. padding: 6px 2px 6px 2px;
  70. }
  71. .caldaycells{
  72. color: #000;
  73. background-color: #D1D7E6;
  74. font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
  75. font-size: 11px;
  76. text-align: center;
  77. padding: 4px;
  78. border: 1px #E0E5F1 solid;
  79. cursor: pointer;
  80. }
  81. .caldaycellhover{
  82. color: #fff;
  83. background-color: #B3BCD4;
  84. font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
  85. font-size: 11px;
  86. text-align: center;
  87. padding: 4px;
  88. border: 1px #B3BCD4 solid;
  89. cursor: pointer;
  90. }
  91. .caldayselected{
  92. background-color: #737FAC;
  93. color: #fff;
  94. font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
  95. font-size: 11px;
  96. font-weight: bold;
  97. text-align: center;
  98. border: 1px #566188 solid;
  99. padding: 3px;
  100. cursor: pointer;
  101. }
  102. .calblanktop {
  103. background-color: #fff;
  104. padding: 4px;
  105. }
  106. .calblankbot {
  107. background-color: #fff;
  108. padding: 4px;
  109. }
  110. */
  111. function js_calendar_script($form_name = 'entryform')
  112. {
  113. $CI =& get_instance();
  114. $CI->load->language('calendar');
  115. ob_start();
  116. ?>
  117. <script type="text/javascript">
  118. <!--
  119. var form_name = "<?php echo $form_name; ?>";
  120. var format = 'us'; // eu or us
  121. var days = new Array(
  122. '<?php echo $CI->lang->line('cal_su');?>', // Sunday, short name
  123. '<?php echo $CI->lang->line('cal_mo');?>', // Monday, short name
  124. '<?php echo $CI->lang->line('cal_tu');?>', // Tuesday, short name
  125. '<?php echo $CI->lang->line('cal_wed');?>', // Wednesday, short name
  126. '<?php echo $CI->lang->line('cal_thu');?>', // Thursday, short name
  127. '<?php echo $CI->lang->line('cal_fri');?>', // Friday, short name
  128. '<?php echo $CI->lang->line('cal_sat');?>' // Saturday, short name
  129. );
  130. var months = new Array(
  131. '<?php echo $CI->lang->line('cal_january');?>',
  132. '<?php echo $CI->lang->line('cal_february');?>',
  133. '<?php echo $CI->lang->line('cal_march');?>',
  134. '<?php echo $CI->lang->line('cal_april');?>',
  135. '<?php echo $CI->lang->line('cal_mayl');?>',
  136. '<?php echo $CI->lang->line('cal_june');?>',
  137. '<?php echo $CI->lang->line('cal_july');?>',
  138. '<?php echo $CI->lang->line('cal_august');?>',
  139. '<?php echo $CI->lang->line('cal_september');?>',
  140. '<?php echo $CI->lang->line('cal_october');?>',
  141. '<?php echo $CI->lang->line('cal_november');?>',
  142. '<?php echo $CI->lang->line('cal_december');?>'
  143. );
  144. var last_click = new Array();
  145. var current_month = '';
  146. var current_year = '';
  147. var last_date = '';
  148. function calendar(id, d, highlight, adjusted)
  149. {
  150. if (adjusted == undefined)
  151. {
  152. var d = new Date(d * 1000);
  153. }
  154. this.id = id;
  155. this.highlight = highlight;
  156. this.date_obj = d;
  157. this.write = build_calendar;
  158. this.total_days = total_days;
  159. this.month = d.getMonth();
  160. this.date = d.getDate();
  161. this.day = d.getDay();
  162. this.year = d.getFullYear();
  163. this.hours = d.getHours();
  164. this.minutes = d.getMinutes();
  165. this.seconds = d.getSeconds();
  166. this.date_str = date_str;
  167. if (highlight == false)
  168. {
  169. this.selected_date = '';
  170. }
  171. else
  172. {
  173. this.selected_date = this.year + '' + this.month + '' + this.date;
  174. }
  175. // Set the "selected date"
  176. d.setDate(1);
  177. this.firstDay = d.getDay();
  178. //then reset the date object to the correct date
  179. d.setDate(this.date);
  180. }
  181. // Build the body of the calendar
  182. function build_calendar()
  183. {
  184. var str = '';
  185. // Calendar Heading
  186. str += '<div id="cal' + this.id + '">';
  187. str += '<table class="calendar" cellspacing="0" cellpadding="0" border="0" >';
  188. str += '<tr>';
  189. str += '<td class="calnavleft" onClick="change_month(-1, \'' + this.id + '\')">&lt;&lt;<\/td>';
  190. str += '<td colspan="5" class="calheading">' + months[this.month] + ' ' + this.year + '<\/td>';
  191. str += '<td class="calnavright" onClick="change_month(1, \'' + this.id + '\')">&gt;&gt;<\/td>';
  192. str += '<\/tr>';
  193. // Day Names
  194. str += '<tr>';
  195. for (i = 0; i < 7; i++)
  196. {
  197. str += '<td class="caldayheading">' + days[i] + '<\/td>';
  198. }
  199. str += '<\/tr>';
  200. // Day Cells
  201. str += '<tr>';
  202. selDate = (last_date != '') ? last_date : this.date;
  203. for (j = 0; j < 42; j++)
  204. {
  205. var displayNum = (j - this.firstDay + 1);
  206. if (j < this.firstDay) // leading empty cells
  207. {
  208. str += '<td class="calblanktop">&nbsp;<\/td>';
  209. }
  210. else if (displayNum == selDate && this.highlight == true) // Selected date
  211. {
  212. str += '<td id="' + this.id +'selected" class="caldayselected" onClick="set_date(this,\'' + this.id + '\')">' + displayNum + '<\/td>';
  213. }
  214. else if (displayNum > this.total_days())
  215. {
  216. str += '<td class="calblankbot">&nbsp;<\/td>'; // trailing empty cells
  217. }
  218. else // Unselected days
  219. {
  220. str += '<td id="" class="caldaycells" onClick="set_date(this,\'' + this.id + '\'); return false;" onMouseOver="javascript:cell_highlight(this,\'' + displayNum + '\',\'' + this.id + '\');" onMouseOut="javascript:cell_reset(this,\'' + displayNum + '\',\'' + this.id + '\');" >' + displayNum + '<\/td>';
  221. }
  222. if (j % 7 == 6)
  223. {
  224. str += '<\/tr><tr>';
  225. }
  226. }
  227. str += '<\/tr>';
  228. str += '<\/table>';
  229. str += '<\/div>';
  230. return str;
  231. }
  232. // Total number of days in a month
  233. function total_days()
  234. {
  235. switch(this.month)
  236. {
  237. case 1: // Check for leap year
  238. if (( this.date_obj.getFullYear() % 4 == 0
  239. && this.date_obj.getFullYear() % 100 != 0)
  240. || this.date_obj.getFullYear() % 400 == 0)
  241. return 29;
  242. else
  243. return 28;
  244. case 3:
  245. return 30;
  246. case 5:
  247. return 30;
  248. case 8:
  249. return 30;
  250. case 10:
  251. return 30
  252. default:
  253. return 31;
  254. }
  255. }
  256. // Highlight Cell on Mouseover
  257. function cell_highlight(td, num, cal)
  258. {
  259. cal = eval(cal);
  260. if (last_click[cal.id] != num)
  261. {
  262. td.className = "caldaycellhover";
  263. }
  264. }
  265. // Reset Cell on MouseOut
  266. function cell_reset(td, num, cal)
  267. {
  268. cal = eval(cal);
  269. if (last_click[cal.id] == num)
  270. {
  271. td.className = "caldayselected";
  272. }
  273. else
  274. {
  275. td.className = "caldaycells";
  276. }
  277. }
  278. // Clear Field
  279. function clear_field(id)
  280. {
  281. eval("document." + form_name + "." + id + ".value = ''");
  282. document.getElementById(id + "selected").className = "caldaycells";
  283. document.getElementById(id + "selected").id = "";
  284. cal = eval(id);
  285. cal.selected_date = '';
  286. }
  287. // Set date to specified time
  288. function set_to_time(id, raw)
  289. {
  290. if (document.getElementById(id + "selected"))
  291. {
  292. document.getElementById(id + "selected").className = "caldaycells";
  293. document.getElementById(id + "selected").id = "";
  294. }
  295. document.getElementById('cal' + id).innerHTML = '<div id="tempcal'+id+'">&nbsp;<'+'/div>';
  296. var nowDate = new Date();
  297. nowDate.setTime = raw * 1000;
  298. current_month = nowDate.getMonth();
  299. current_year = nowDate.getFullYear();
  300. current_date = nowDate.getDate();
  301. oldcal = eval(id);
  302. oldcal.selected_date = current_year + '' + current_month + '' + current_date;
  303. cal = new calendar(id, nowDate, true, true);
  304. cal.selected_date = current_year + '' + current_month + '' + current_date;
  305. last_date = cal.date;
  306. document.getElementById('tempcal'+id).innerHTML = cal.write();
  307. insert_date(cal);
  308. }
  309. // Set date to what is in the field
  310. var lastDates = new Array();
  311. function update_calendar(id, dateValue)
  312. {
  313. if (lastDates[id] == dateValue) return;
  314. lastDates[id] = dateValue;
  315. var fieldString = dateValue.replace(/\s+/g, ' ');
  316. while (fieldString.substring(0,1) == ' ')
  317. {
  318. fieldString = fieldString.substring(1, fieldString.length);
  319. }
  320. var dateString = fieldString.split(' ');
  321. var dateParts = dateString[0].split('-')
  322. if (dateParts.length < 3) return;
  323. var newYear = dateParts[0];
  324. var newMonth = dateParts[1];
  325. var newDay = dateParts[2];
  326. if (isNaN(newDay) || newDay < 1 || (newDay.length != 1 && newDay.length != 2)) return;
  327. if (isNaN(newYear) || newYear < 1 || newYear.length != 4) return;
  328. if (isNaN(newMonth) || newMonth < 1 || (newMonth.length != 1 && newMonth.length != 2)) return;
  329. if (newMonth > 12) newMonth = 12;
  330. if (newDay > 28)
  331. {
  332. switch(newMonth - 1)
  333. {
  334. case 1: // Check for leap year
  335. if ((newYear % 4 == 0 && newYear % 100 != 0) || newYear % 400 == 0)
  336. {
  337. if (newDay > 29) newDay = 29;
  338. }
  339. else
  340. {
  341. if (newDay > 28) newDay = 28;
  342. }
  343. case 3:
  344. if (newDay > 30) newDay = 30;
  345. case 5:
  346. if (newDay > 30) newDay = 30;
  347. case 8:
  348. if (newDay > 30) newDay = 30;
  349. case 10:
  350. if (newDay > 30) newDay = 30;
  351. default:
  352. if (newDay > 31) newDay = 31;
  353. }
  354. }
  355. if (document.getElementById(id + "selected"))
  356. {
  357. document.getElementById(id + "selected").className = "caldaycells";
  358. document.getElementById(id + "selected").id = "";
  359. }
  360. document.getElementById('cal' + id).innerHTML = '<div id="tempcal'+id+'">&nbsp;<'+'/div>';
  361. var nowDate = new Date();
  362. nowDate.setDate(newDay);
  363. nowDate.setMonth(newMonth - 1);
  364. nowDate.setYear(newYear);
  365. nowDate.setHours(12);
  366. current_month = nowDate.getMonth();
  367. current_year = nowDate.getFullYear();
  368. cal = new calendar(id, nowDate, true, true);
  369. document.getElementById('tempcal'+id).innerHTML = cal.write();
  370. }
  371. // Set the date
  372. function set_date(td, cal)
  373. {
  374. cal = eval(cal);
  375. // If the user is clicking a cell that is already
  376. // selected we'll de-select it and clear the form field
  377. if (last_click[cal.id] == td.firstChild.nodeValue)
  378. {
  379. td.className = "caldaycells";
  380. last_click[cal.id] = '';
  381. remove_date(cal);
  382. cal.selected_date = '';
  383. return;
  384. }
  385. // Onward!
  386. if (document.getElementById(cal.id + "selected"))
  387. {
  388. document.getElementById(cal.id + "selected").className = "caldaycells";
  389. document.getElementById(cal.id + "selected").id = "";
  390. }
  391. td.className = "caldayselected";
  392. td.id = cal.id + "selected";
  393. cal.selected_date = cal.date_obj.getFullYear() + '' + cal.date_obj.getMonth() + '' + cal.date;
  394. cal.date_obj.setDate(td.firstChild.nodeValue);
  395. cal = new calendar(cal.id, cal.date_obj, true, true);
  396. cal.selected_date = cal.date_obj.getFullYear() + '' + cal.date_obj.getMonth() + '' + cal.date;
  397. last_date = cal.date;
  398. //cal.date
  399. last_click[cal.id] = cal.date;
  400. // Insert the date into the form
  401. insert_date(cal);
  402. }
  403. /*
  404. // Insert the date into the form field
  405. function insert_date(cal)
  406. {
  407. cal = eval(cal);
  408. fval = eval("document." + form_name + "." + cal.id);
  409. if (fval.value == '')
  410. {
  411. fval.value = cal.date_str('y');
  412. }
  413. else
  414. {
  415. time = fval.value.substring(10);
  416. new_date = cal.date_str('n') + time;
  417. fval.value = new_date;
  418. }
  419. }
  420. */
  421. // Remove the date from the form field
  422. function remove_date(cal)
  423. {
  424. cal = eval(cal);
  425. fval = eval("document." + form_name + "." + cal.id);
  426. fval.value = '';
  427. }
  428. // Change to a new month
  429. function change_month(mo, cal)
  430. {
  431. cal = eval(cal);
  432. if (current_month != '')
  433. {
  434. cal.date_obj.setMonth(current_month);
  435. cal.date_obj.setYear(current_year);
  436. current_month = '';
  437. current_year = '';
  438. }
  439. var newMonth = cal.date_obj.getMonth() + mo;
  440. var newDate = cal.date_obj.getDate();
  441. if (newMonth == 12)
  442. {
  443. cal.date_obj.setYear(cal.date_obj.getFullYear() + 1)
  444. newMonth = 0;
  445. }
  446. else if (newMonth == -1)
  447. {
  448. cal.date_obj.setYear(cal.date_obj.getFullYear() - 1)
  449. newMonth = 11;
  450. }
  451. if (newDate > 28)
  452. {
  453. var newYear = cal.date_obj.getFullYear();
  454. switch(newMonth)
  455. {
  456. case 1: // Check for leap year
  457. if ((newYear % 4 == 0 && newYear % 100 != 0) || newYear % 400 == 0)
  458. {
  459. if (newDate > 29) newDate = 29;
  460. }
  461. else
  462. {
  463. if (newDate > 28) newDate = 28;
  464. }
  465. case 3:
  466. if (newDate > 30) newDate = 30;
  467. case 5:
  468. if (newDate > 30) newDate = 30;
  469. case 8:
  470. if (newDate > 30) newDate = 30;
  471. case 10:
  472. if (newDate > 30) newDate = 30;
  473. default:
  474. if (newDate > 31) newDate = 31;
  475. }
  476. }
  477. cal.date_obj.setDate(newDate);
  478. cal.date_obj.setMonth(newMonth);
  479. new_mdy = cal.date_obj.getFullYear() + '' + cal.date_obj.getMonth() + '' + cal.date;
  480. highlight = (cal.selected_date == new_mdy) ? true : false;
  481. cal = new calendar(cal.id, cal.date_obj, highlight, true);
  482. document.getElementById('cal' + cal.id).innerHTML = cal.write();
  483. }
  484. // Finalize the date string
  485. function date_str(time)
  486. {
  487. var month = this.month + 1;
  488. if (month < 10)
  489. month = '0' + month;
  490. var day = (this.date < 10) ? '0' + this.date : this.date;
  491. var minutes = (this.minutes < 10) ? '0' + this.minutes : this.minutes;
  492. if (format == 'us')
  493. {
  494. var hours = (this.hours > 12) ? this.hours - 12 : this.hours;
  495. var ampm = (this.hours > 11) ? 'PM' : 'AM'
  496. }
  497. else
  498. {
  499. var hours = this.hours;
  500. var ampm = '';
  501. }
  502. if (time == 'y')
  503. {
  504. return this.year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ' ' + ampm;
  505. }
  506. else
  507. {
  508. return this.year + '-' + month + '-' + day;
  509. }
  510. }
  511. //-->
  512. </script>
  513. <?php
  514. $r = ob_get_contents();
  515. ob_end_clean();
  516. return $r;
  517. }
  518. function js_calendar_write($field_id, $time = '', $highlight = TRUE)
  519. {
  520. if ($time == '')
  521. $time = time();
  522. return
  523. '<script type="text/javascript">
  524. var '.$field_id.' = new calendar("'.$field_id.'", '.$time.', '.(($highlight == TRUE) ? 'true' : 'false').');
  525. document.write('.$field_id.'.write());
  526. </script>';
  527. }
  528. /* End of file js_calendar_pi.php */
  529. /* Location: ./system/plugins/js_calendar_pi.php */