PageRenderTime 78ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/system/plugins/js_calendar_pi.php

https://github.com/xupisco/Xare-a-Link
PHP | 604 lines | 371 code | 88 blank | 145 comment | 102 complexity | fd8e15de26bda7ee9c4b0812757c2b56 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 Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.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. ob_start();
  114. ?>
  115. <script type="text/javascript">
  116. <!--
  117. var form_name = "<?php echo $form_name; ?>";
  118. var format = 'us'; // eu or us
  119. var days = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
  120. var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  121. var last_click = new Array();
  122. var current_month = '';
  123. var current_year = '';
  124. var last_date = '';
  125. function calendar(id, d, highlight, adjusted)
  126. {
  127. if (adjusted == undefined)
  128. {
  129. var d = new Date(d * 1000);
  130. }
  131. this.id = id;
  132. this.highlight = highlight;
  133. this.date_obj = d;
  134. this.write = build_calendar;
  135. this.total_days = total_days;
  136. this.month = d.getMonth();
  137. this.date = d.getDate();
  138. this.day = d.getDay();
  139. this.year = d.getFullYear();
  140. this.hours = d.getHours();
  141. this.minutes = d.getMinutes();
  142. this.seconds = d.getSeconds();
  143. this.date_str = date_str;
  144. if (highlight == false)
  145. {
  146. this.selected_date = '';
  147. }
  148. else
  149. {
  150. this.selected_date = this.year + '' + this.month + '' + this.date;
  151. }
  152. // Set the "selected date"
  153. d.setDate(1);
  154. this.firstDay = d.getDay();
  155. //then reset the date object to the correct date
  156. d.setDate(this.date);
  157. }
  158. // Build the body of the calendar
  159. function build_calendar()
  160. {
  161. var str = '';
  162. // Calendar Heading
  163. str += '<div id="cal' + this.id + '">';
  164. str += '<table class="calendar" cellspacing="0" cellpadding="0" border="0" >';
  165. str += '<tr>';
  166. str += '<td class="calnavleft" onClick="change_month(-1, \'' + this.id + '\')">&lt;&lt;<\/td>';
  167. str += '<td colspan="5" class="calheading">' + months[this.month] + ' ' + this.year + '<\/td>';
  168. str += '<td class="calnavright" onClick="change_month(1, \'' + this.id + '\')">&gt;&gt;<\/td>';
  169. str += '<\/tr>';
  170. // Day Names
  171. str += '<tr>';
  172. for (i = 0; i < 7; i++)
  173. {
  174. str += '<td class="caldayheading">' + days[i] + '<\/td>';
  175. }
  176. str += '<\/tr>';
  177. // Day Cells
  178. str += '<tr>';
  179. selDate = (last_date != '') ? last_date : this.date;
  180. for (j = 0; j < 42; j++)
  181. {
  182. var displayNum = (j - this.firstDay + 1);
  183. if (j < this.firstDay) // leading empty cells
  184. {
  185. str += '<td class="calblanktop">&nbsp;<\/td>';
  186. }
  187. else if (displayNum == selDate && this.highlight == true) // Selected date
  188. {
  189. str += '<td id="' + this.id +'selected" class="caldayselected" onClick="set_date(this,\'' + this.id + '\')">' + displayNum + '<\/td>';
  190. }
  191. else if (displayNum > this.total_days())
  192. {
  193. str += '<td class="calblankbot">&nbsp;<\/td>'; // trailing empty cells
  194. }
  195. else // Unselected days
  196. {
  197. 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>';
  198. }
  199. if (j % 7 == 6)
  200. {
  201. str += '<\/tr><tr>';
  202. }
  203. }
  204. str += '<\/tr>';
  205. str += '<\/table>';
  206. str += '<\/div>';
  207. return str;
  208. }
  209. // Total number of days in a month
  210. function total_days()
  211. {
  212. switch(this.month)
  213. {
  214. case 1: // Check for leap year
  215. if (( this.date_obj.getFullYear() % 4 == 0
  216. && this.date_obj.getFullYear() % 100 != 0)
  217. || this.date_obj.getFullYear() % 400 == 0)
  218. return 29;
  219. else
  220. return 28;
  221. case 3:
  222. return 30;
  223. case 5:
  224. return 30;
  225. case 8:
  226. return 30;
  227. case 10:
  228. return 30
  229. default:
  230. return 31;
  231. }
  232. }
  233. // Highlight Cell on Mouseover
  234. function cell_highlight(td, num, cal)
  235. {
  236. cal = eval(cal);
  237. if (last_click[cal.id] != num)
  238. {
  239. td.className = "caldaycellhover";
  240. }
  241. }
  242. // Reset Cell on MouseOut
  243. function cell_reset(td, num, cal)
  244. {
  245. cal = eval(cal);
  246. if (last_click[cal.id] == num)
  247. {
  248. td.className = "caldayselected";
  249. }
  250. else
  251. {
  252. td.className = "caldaycells";
  253. }
  254. }
  255. // Clear Field
  256. function clear_field(id)
  257. {
  258. eval("document." + form_name + "." + id + ".value = ''");
  259. document.getElementById(id + "selected").className = "caldaycells";
  260. document.getElementById(id + "selected").id = "";
  261. cal = eval(id);
  262. cal.selected_date = '';
  263. }
  264. // Set date to specified time
  265. function set_to_time(id, raw)
  266. {
  267. if (document.getElementById(id + "selected"))
  268. {
  269. document.getElementById(id + "selected").className = "caldaycells";
  270. document.getElementById(id + "selected").id = "";
  271. }
  272. document.getElementById('cal' + id).innerHTML = '<div id="tempcal'+id+'">&nbsp;<'+'/div>';
  273. var nowDate = new Date();
  274. nowDate.setTime = raw * 1000;
  275. current_month = nowDate.getMonth();
  276. current_year = nowDate.getFullYear();
  277. current_date = nowDate.getDate();
  278. oldcal = eval(id);
  279. oldcal.selected_date = current_year + '' + current_month + '' + current_date;
  280. cal = new calendar(id, nowDate, true, true);
  281. cal.selected_date = current_year + '' + current_month + '' + current_date;
  282. last_date = cal.date;
  283. document.getElementById('tempcal'+id).innerHTML = cal.write();
  284. insert_date(cal);
  285. }
  286. // Set date to what is in the field
  287. var lastDates = new Array();
  288. function update_calendar(id, dateValue)
  289. {
  290. if (lastDates[id] == dateValue) return;
  291. lastDates[id] = dateValue;
  292. var fieldString = dateValue.replace(/\s+/g, ' ');
  293. while (fieldString.substring(0,1) == ' ')
  294. {
  295. fieldString = fieldString.substring(1, fieldString.length);
  296. }
  297. var dateString = fieldString.split(' ');
  298. var dateParts = dateString[0].split('-')
  299. if (dateParts.length < 3) return;
  300. var newYear = dateParts[0];
  301. var newMonth = dateParts[1];
  302. var newDay = dateParts[2];
  303. if (isNaN(newDay) || newDay < 1 || (newDay.length != 1 && newDay.length != 2)) return;
  304. if (isNaN(newYear) || newYear < 1 || newYear.length != 4) return;
  305. if (isNaN(newMonth) || newMonth < 1 || (newMonth.length != 1 && newMonth.length != 2)) return;
  306. if (newMonth > 12) newMonth = 12;
  307. if (newDay > 28)
  308. {
  309. switch(newMonth - 1)
  310. {
  311. case 1: // Check for leap year
  312. if ((newYear % 4 == 0 && newYear % 100 != 0) || newYear % 400 == 0)
  313. {
  314. if (newDay > 29) newDay = 29;
  315. }
  316. else
  317. {
  318. if (newDay > 28) newDay = 28;
  319. }
  320. case 3:
  321. if (newDay > 30) newDay = 30;
  322. case 5:
  323. if (newDay > 30) newDay = 30;
  324. case 8:
  325. if (newDay > 30) newDay = 30;
  326. case 10:
  327. if (newDay > 30) newDay = 30;
  328. default:
  329. if (newDay > 31) newDay = 31;
  330. }
  331. }
  332. if (document.getElementById(id + "selected"))
  333. {
  334. document.getElementById(id + "selected").className = "caldaycells";
  335. document.getElementById(id + "selected").id = "";
  336. }
  337. document.getElementById('cal' + id).innerHTML = '<div id="tempcal'+id+'">&nbsp;<'+'/div>';
  338. var nowDate = new Date();
  339. nowDate.setDate(newDay);
  340. nowDate.setMonth(newMonth - 1);
  341. nowDate.setYear(newYear);
  342. nowDate.setHours(12);
  343. current_month = nowDate.getMonth();
  344. current_year = nowDate.getFullYear();
  345. cal = new calendar(id, nowDate, true, true);
  346. document.getElementById('tempcal'+id).innerHTML = cal.write();
  347. }
  348. // Set the date
  349. function set_date(td, cal)
  350. {
  351. cal = eval(cal);
  352. // If the user is clicking a cell that is already
  353. // selected we'll de-select it and clear the form field
  354. if (last_click[cal.id] == td.firstChild.nodeValue)
  355. {
  356. td.className = "caldaycells";
  357. last_click[cal.id] = '';
  358. remove_date(cal);
  359. cal.selected_date = '';
  360. return;
  361. }
  362. // Onward!
  363. if (document.getElementById(cal.id + "selected"))
  364. {
  365. document.getElementById(cal.id + "selected").className = "caldaycells";
  366. document.getElementById(cal.id + "selected").id = "";
  367. }
  368. td.className = "caldayselected";
  369. td.id = cal.id + "selected";
  370. cal.selected_date = cal.date_obj.getFullYear() + '' + cal.date_obj.getMonth() + '' + cal.date;
  371. cal.date_obj.setDate(td.firstChild.nodeValue);
  372. cal = new calendar(cal.id, cal.date_obj, true, true);
  373. cal.selected_date = cal.date_obj.getFullYear() + '' + cal.date_obj.getMonth() + '' + cal.date;
  374. last_date = cal.date;
  375. //cal.date
  376. last_click[cal.id] = cal.date;
  377. // Insert the date into the form
  378. insert_date(cal);
  379. }
  380. // Insert the date into the form field
  381. function insert_date(cal)
  382. {
  383. cal = eval(cal);
  384. fval = eval("document." + form_name + "." + cal.id);
  385. if (fval.value == '')
  386. {
  387. fval.value = cal.date_str('y');
  388. }
  389. else
  390. {
  391. time = fval.value.substring(10);
  392. new_date = cal.date_str('n') + time;
  393. fval.value = new_date;
  394. }
  395. }
  396. // Remove the date from the form field
  397. function remove_date(cal)
  398. {
  399. cal = eval(cal);
  400. fval = eval("document." + form_name + "." + cal.id);
  401. fval.value = '';
  402. }
  403. // Change to a new month
  404. function change_month(mo, cal)
  405. {
  406. cal = eval(cal);
  407. if (current_month != '')
  408. {
  409. cal.date_obj.setMonth(current_month);
  410. cal.date_obj.setYear(current_year);
  411. current_month = '';
  412. current_year = '';
  413. }
  414. var newMonth = cal.date_obj.getMonth() + mo;
  415. var newDate = cal.date_obj.getDate();
  416. if (newMonth == 12)
  417. {
  418. cal.date_obj.setYear(cal.date_obj.getFullYear() + 1)
  419. newMonth = 0;
  420. }
  421. else if (newMonth == -1)
  422. {
  423. cal.date_obj.setYear(cal.date_obj.getFullYear() - 1)
  424. newMonth = 11;
  425. }
  426. if (newDate > 28)
  427. {
  428. var newYear = cal.date_obj.getFullYear();
  429. switch(newMonth)
  430. {
  431. case 1: // Check for leap year
  432. if ((newYear % 4 == 0 && newYear % 100 != 0) || newYear % 400 == 0)
  433. {
  434. if (newDate > 29) newDate = 29;
  435. }
  436. else
  437. {
  438. if (newDate > 28) newDate = 28;
  439. }
  440. case 3:
  441. if (newDate > 30) newDate = 30;
  442. case 5:
  443. if (newDate > 30) newDate = 30;
  444. case 8:
  445. if (newDate > 30) newDate = 30;
  446. case 10:
  447. if (newDate > 30) newDate = 30;
  448. default:
  449. if (newDate > 31) newDate = 31;
  450. }
  451. }
  452. cal.date_obj.setDate(newDate);
  453. cal.date_obj.setMonth(newMonth);
  454. new_mdy = cal.date_obj.getFullYear() + '' + cal.date_obj.getMonth() + '' + cal.date;
  455. highlight = (cal.selected_date == new_mdy) ? true : false;
  456. cal = new calendar(cal.id, cal.date_obj, highlight, true);
  457. document.getElementById('cal' + cal.id).innerHTML = cal.write();
  458. }
  459. // Finalize the date string
  460. function date_str(time)
  461. {
  462. var month = this.month + 1;
  463. if (month < 10)
  464. month = '0' + month;
  465. var day = (this.date < 10) ? '0' + this.date : this.date;
  466. var minutes = (this.minutes < 10) ? '0' + this.minutes : this.minutes;
  467. if (format == 'us')
  468. {
  469. var hours = (this.hours > 12) ? this.hours - 12 : this.hours;
  470. var ampm = (this.hours > 11) ? 'PM' : 'AM'
  471. }
  472. else
  473. {
  474. var hours = this.hours;
  475. var ampm = '';
  476. }
  477. if (time == 'y')
  478. {
  479. return this.year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ' ' + ampm;
  480. }
  481. else
  482. {
  483. return this.year + '-' + month + '-' + day;
  484. }
  485. }
  486. //-->
  487. </script>
  488. <?php
  489. $r = ob_get_contents();
  490. ob_end_clean();
  491. return $r;
  492. }
  493. function js_calendar_write($field_id, $time = '', $highlight = TRUE)
  494. {
  495. if ($time == '')
  496. $time = time();
  497. return
  498. '<script type="text/javascript">
  499. var '.$field_id.' = new calendar("'.$field_id.'", '.$time.', '.(($highlight == TRUE) ? 'true' : 'false').');
  500. document.write('.$field_id.'.write());
  501. </script>';
  502. }
  503. ?>