PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/calendar_form.php

https://github.com/DanGodfrey/Advanced_Event_Registration
PHP | 452 lines | 345 code | 70 blank | 37 comment | 87 complexity | 8fe44f3838bb2736d6679897b39f985a MD5 | raw file
  1. <?php
  2. require_once('includes/tc_calendar.php');
  3. $thispage = $_SERVER['PHP_SELF'];
  4. $sld = (isset($_REQUEST["selected_day"])) ? $_REQUEST["selected_day"] : 0;
  5. $slm = (isset($_REQUEST["selected_month"])) ? (int)$_REQUEST["selected_month"] : 0;
  6. $sly = (isset($_REQUEST["selected_year"])) ? (int)$_REQUEST["selected_year"] : 0;
  7. $year_start = (isset($_REQUEST["year_start"])) ? $_REQUEST["year_start"] : 0;
  8. $year_end = (isset($_REQUEST["year_end"])) ? $_REQUEST["year_end"] : 0;
  9. $startMonday = (isset($_REQUEST["mon"])) ? $_REQUEST["mon"] : 0;
  10. $date_allow1 = (isset($_REQUEST["da1"])) ? $_REQUEST["da1"] : "";
  11. $date_allow2 = (isset($_REQUEST["da2"])) ? $_REQUEST["da2"] : "";
  12. $show_not_allow = (isset($_REQUEST["sna"])) ? $_REQUEST["sna"] : true;
  13. $auto_submit = (isset($_REQUEST["aut"])) ? $_REQUEST["aut"] : false;
  14. $form_name = (isset($_REQUEST["frm"])) ? $_REQUEST["frm"] : "";
  15. $target_url = (isset($_REQUEST["tar"])) ? $_REQUEST["tar"] : "";
  16. $show_input = (isset($_REQUEST["inp"])) ? $_REQUEST["inp"] : true;
  17. $date_format = (isset($_REQUEST["fmt"])) ? $_REQUEST["fmt"] : 'd-M-Y';
  18. //echo("date: $sly-$slm-$sld");
  19. if(isset($_REQUEST["m"]))
  20. $m = $_REQUEST["m"];
  21. else
  22. $m = ($slm) ? $slm : date('m');
  23. //echo("m: ".$m);
  24. if($m < 1 && $m > 12) $m = date('m');
  25. $cyr = ($sly) ? true : false;
  26. if($sly && $sly < $year_start) $sly = $year_start;
  27. if($sly && $sly > $year_end) $sly = $year_end;
  28. if(isset($_REQUEST["y"]))
  29. $y = $_REQUEST["y"];
  30. else
  31. $y = ($cyr) ? $sly : date('Y');
  32. //echo("<br>y: ".$y);
  33. if($y <= 0) $y = date('Y');
  34. $objname = (isset($_REQUEST["objname"])) ? $_REQUEST["objname"] : "";
  35. $dp = (isset($_REQUEST["dp"])) ? $_REQUEST["dp"] : "";
  36. $cobj = new tc_calendar("");
  37. $cobj->startMonday($startMonday);
  38. if(!$year_start || !$year_end){
  39. $year_start = $cobj->year_start; //get default value of year start
  40. $year_end = $cobj->year_end; //get default value of year end
  41. }
  42. //$cobj->setDate($sld, $slm, $sly);
  43. $total_thismonth = $cobj->total_days($m, $y);
  44. if($m == 1){
  45. $previous_month = 12;
  46. $previous_year = $y-1;
  47. }else{
  48. $previous_month = $m-1;
  49. $previous_year = $y;
  50. }
  51. if($m == 12){
  52. $next_month = 1;
  53. $next_year = $y+1;
  54. }else{
  55. $next_month = $m+1;
  56. $next_year = $y;
  57. }
  58. $total_lastmonth = $cobj->total_days($previous_month, $previous_year);
  59. $today = date('Y-m-d');
  60. //$startdate = $cobj->getDayNum(date('D', strtotime($y."-".$m."-1")));
  61. $startdate = date('w', strtotime($y."-".$m."-1"));
  62. if($startMonday)
  63. if($startdate == 0)
  64. $startwrite = $total_lastmonth - 5;
  65. else
  66. $startwrite = $total_lastmonth - ($startdate - 2);
  67. else
  68. $startwrite = $total_lastmonth - ($startdate - 1);
  69. ?>
  70. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  71. <html xmlns="http://www.w3.org/1999/xhtml">
  72. <head>
  73. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  74. <title>TriConsole.com - PHP Calendar Date Picker</title>
  75. <link href="styles.css" rel="stylesheet" type="text/css" />
  76. <script language="javascript">
  77. <!--
  78. function setValue(){
  79. var f = document.calendarform;
  80. var date_selected = padString(f.selected_year.value, 4, "0") + "-" + padString(f.selected_month.value, 2, "0") + "-" + padString(f.selected_day.value, 2, "0");
  81. window.parent.setValue(f.objname.value, date_selected);
  82. }
  83. function selectDay(d){
  84. var f = document.calendarform;
  85. f.selected_day.value = d.toString();
  86. f.selected_month.value = f.m[f.m.selectedIndex].value;
  87. f.selected_year.value = f.y[f.y.selectedIndex].value;
  88. setValue();
  89. this.loading();
  90. f.submit();
  91. submitNow(f.selected_day.value, f.selected_month.value, f.selected_year.value);
  92. }
  93. function hL(E, mo){
  94. //clear last selected
  95. if(document.getElementById("select")){
  96. var selectobj = document.getElementById("select");
  97. selectobj.Id = "";
  98. }
  99. while (E.tagName!="TD"){
  100. E=E.parentElement;
  101. }
  102. E.Id = "select";
  103. }
  104. function selectMonth(m){
  105. var f = document.calendarform;
  106. f.selected_month.value = m;
  107. }
  108. function selectYear(y){
  109. var f = document.calendarform;
  110. f.selected_year.value = y;
  111. }
  112. function move(m, y){
  113. var f = document.calendarform;
  114. f.m.value = m;
  115. f.y.value = y;
  116. this.loading();
  117. f.submit();
  118. }
  119. function closeMe(){
  120. window.parent.toggleCalendar('div_<?php echo($objname); ?>');
  121. }
  122. function submitNow(dvalue, mvalue, yvalue){
  123. <?php
  124. //write auto submit script
  125. if($auto_submit){
  126. echo("if(yvalue>0 && mvalue>0 && dvalue>0){\n");
  127. if($form_name){
  128. //submit value by post form
  129. echo("window.parent.document.".$form_name.".submit();\n");
  130. }elseif($target_url){
  131. //submit value by get method
  132. echo("var date_selected = yvalue + \"-\" + mvalue + \"-\" + dvalue;\n");
  133. echo("window.parent.location.href='".$target_url."?".$objname."='+date_selected;\n");
  134. }
  135. echo("}\n");
  136. }
  137. ?>
  138. }
  139. function padString(stringToPad, padLength, padString) {
  140. if (stringToPad.length < padLength) {
  141. while (stringToPad.length < padLength) {
  142. stringToPad = padString + stringToPad;
  143. }
  144. }else {}
  145. /*
  146. if (stringToPad.length > padLength) {
  147. stringToPad = stringToPad.substring((stringToPad.length - padLength), padLength);
  148. } else {}
  149. */
  150. return stringToPad;
  151. }
  152. function loading(){
  153. document.getElementById('calendar-container').innerHTML = "<div id=\"calendar-body\"><div style=\"padding: 10px 5px 5px 5px;\"><div align=\"center\" style=\"background-color: #ffffff; height: 50px; padding: 25px; 3px 5px 3px;\">Refreshing Calendar...</div></div></div>";
  154. }
  155. function submitCalendar(){
  156. this.loading();
  157. document.calendarform.submit();
  158. }
  159. function getObject(item){
  160. if( window.mmIsOpera ) return(document.getElementById(item));
  161. if(document.all) return(document.all[item]);
  162. if(document.getElementById) return(document.getElementById(item));
  163. if(document.layers) return(document.layers[item]);
  164. return(false);
  165. }
  166. //window.onload = function(){ setValue(); closeMe(); };
  167. //-->
  168. </script>
  169. </head>
  170. <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
  171. <div id="calendar-header" align="center">
  172. <?php if($dp){ ?>
  173. <div align="right" class="closeme"><a href="javascript:closeMe();"><img src="images/close.jpg" border="0" /></a></div>
  174. <?php } ?>
  175. <form id="calendarform" name="calendarform" method="post" action="<?php echo($thispage);?>" style="margin: 0px;">
  176. <table border="0" cellspacing="0" cellpadding="1">
  177. <tr>
  178. <td><select name="m" onchange="javascript:submitCalendar();">
  179. <?php
  180. $monthnames = $cobj->getMonthNames();
  181. for($f=1; $f<=sizeof($monthnames); $f++){
  182. $selected = ($f == (int)$m) ? " selected" : "";
  183. echo("<option value=\"".str_pad($f, 2, "0", STR_PAD_LEFT)."\"$selected>".$monthnames[$f-1]."</option>");
  184. }
  185. ?>
  186. </select> </td>
  187. <td><select name="y" onchange="javascript:submitCalendar();">
  188. <?php
  189. $thisyear = date('Y');
  190. //check year to be select in case of date_allow is set
  191. if(!$show_not_allow && ($date_allow1 || $date_allow2)){
  192. if($date_allow1 && $date_allow2){
  193. $da1Time = strtotime($date_allow1);
  194. $da2Time = strtotime($date_allow2);
  195. if($da1Time < $da2Time){
  196. $year_start = date('Y', $da1Time);
  197. $year_end = date('Y', $da2Time);
  198. }else{
  199. $year_start = date('Y', $da2Time);
  200. $year_end = date('Y', $da1Time);
  201. }
  202. }elseif($date_allow1){
  203. //only date 1 specified
  204. $da1Time = strtotime($date_allow1);
  205. $year_start = date('Y', $da1Time);
  206. }elseif($date_allow2){
  207. //only date 2 specified
  208. $da2Time = strtotime($date_allow2);
  209. $year_end = date('Y', $da2Time);
  210. }
  211. }
  212. //write year options
  213. for($year=$year_start; $year<=$year_end; $year++){
  214. $selected = ($year == $y) ? " selected" : "";
  215. echo("<option value=\"$year\"$selected>$year</option>");
  216. }
  217. ?>
  218. </select> </td>
  219. </tr>
  220. </table>
  221. <input name="selected_day" type="hidden" id="selected_day" value="<?php echo($sld);?>" />
  222. <input name="selected_month" type="hidden" id="selected_month" value="<?php echo($slm);?>" />
  223. <input name="selected_year" type="hidden" id="selected_year" value="<?php echo($sly);?>" />
  224. <input name="year_start" type="hidden" id="year_start" value="<?php echo($year_start);?>" />
  225. <input name="year_end" type="hidden" id="year_end" value="<?php echo($year_end);?>" />
  226. <input name="objname" type="hidden" id="objname" value="<?php echo($objname);?>" />
  227. <input name="dp" type="hidden" id="dp" value="<?php echo($dp);?>" />
  228. <input name="mon" type="hidden" id="mon" value="<?php echo($startMonday);?>" />
  229. <input name="da1" type="hidden" id="da1" value="<?php echo($date_allow1);?>" />
  230. <input name="da2" type="hidden" id="da2" value="<?php echo($date_allow2);?>" />
  231. <input name="sna" type="hidden" id="sna" value="<?php echo($show_not_allow);?>" />
  232. <input name="aut" type="hidden" id="aut" value="<?php echo($auto_submit);?>" />
  233. <input name="frm" type="hidden" id="frm" value="<?php echo($form_name);?>" />
  234. <input name="tar" type="hidden" id="tar" value="<?php echo($target_url);?>" />
  235. <input name="inp" type="hidden" id="inp" value="<?php echo($show_input);?>" />
  236. <input name="fmt" type="hidden" id="fmt" value="<?php echo($date_format);?>" />
  237. </form>
  238. </div>
  239. <div id="calendar-container">
  240. <div id="calendar-body">
  241. <table border="0" cellspacing="1" cellpadding="3" align="center" class="bg">
  242. <?php
  243. $day_headers = array_values($cobj->getDayHeaders());
  244. echo("<tr>");
  245. //write calendar day header
  246. foreach($day_headers as $dh){
  247. echo("<td align=\"center\" class=\"header\">".$dh."</td>");
  248. }
  249. echo("</tr>");
  250. echo("<tr>");
  251. $dayinweek_counter = 0;
  252. $row_count = 0;
  253. //write previous month
  254. for($day=$startwrite; $day<=$total_lastmonth; $day++){
  255. echo("<td align=\"center\" class=\"othermonth\">$day</td>");
  256. $dayinweek_counter++;
  257. }
  258. $pvMonthTime = strtotime($previous_year."-".$previous_month."-".$total_lastmonth);
  259. //check lastmonth is on allowed date
  260. if($date_allow1 && !$show_not_allow){
  261. if($pvMonthTime >= strtotime($date_allow1)){
  262. $show_previous = true;
  263. }else $show_previous = false;
  264. }else $show_previous = true; //always show when not set
  265. //$date_num = $cobj->getDayNum(date('D', strtotime($previous_year."-".$previous_month."-".$total_lastmonth)));
  266. $date_num = date('w', $pvMonthTime);
  267. if((!$startMonday && $date_num == 6) || ($startMonday && $date_num == 0)){
  268. echo("</tr><tr>");
  269. $row_count++;
  270. }
  271. //write current month
  272. for($day=1; $day<=$total_thismonth; $day++){
  273. //$date_num = $cobj->getDayNum(date('D', strtotime($y."-".$m."-".$day)));
  274. $date_num = date('w', strtotime($y."-".$m."-".$day));
  275. $currentTime = strtotime($y."-".$m."-".$day);
  276. $htmlClass = array();
  277. $is_today = $currentTime - strtotime($today);
  278. $htmlClass[] = ($is_today == 0) ? "today" : "general";
  279. $is_selected = strtotime($y."-".$m."-".$day) - strtotime($sly."-".$slm."-".$sld);
  280. if($is_selected == 0) $htmlClass[] = "select";
  281. //check date allowed
  282. if($date_allow1 || $date_allow2){
  283. //at least one date allowed specified
  284. $dateLink = true;
  285. if($date_allow1 && $date_allow2){
  286. //both date specified
  287. $da1Time = strtotime($date_allow1);
  288. $da2Time = strtotime($date_allow2);
  289. if($da1Time < $da2Time){
  290. $dateLink = ($da1Time <= $currentTime && $da2Time >= $currentTime) ? true : false;
  291. }else{
  292. $dateLink = ($da1Time >= $currentTime && $da2Time <= $currentTime) ? true : false;
  293. }
  294. }elseif($date_allow1){
  295. //only date 1 specified
  296. $da1Time = strtotime($date_allow1);
  297. $dateLink = ($currentTime >= $da1Time) ? true : false;
  298. }elseif($date_allow2){
  299. //only date 2 specified
  300. $da2Time = strtotime($date_allow2);
  301. $dateLink = ($currentTime <= $da2Time) ? true : false;
  302. }
  303. }else{
  304. //no date allow specified, assume show all
  305. $dateLink = true;
  306. }
  307. if($dateLink){
  308. //write date with link
  309. $class = implode(" ", $htmlClass);
  310. if($class) $class = " class=\"$class\"";
  311. echo("<td align=\"center\"$class><a href=\"javascript:selectDay('".str_pad($day, 2, "0", STR_PAD_LEFT)."');\">$day</a></td>");
  312. }else{
  313. $htmlClass[] = "disabledate";
  314. $class = implode(" ", $htmlClass);
  315. if($class) $class = " class=\"$class\"";
  316. //write date without link
  317. echo("<td align=\"center\"$class>$day</td>");
  318. }
  319. if((!$startMonday && $date_num == 6) || ($startMonday && $date_num == 0)){
  320. echo("</tr>");
  321. if($day < $total_thismonth) echo("<tr>");
  322. $row_count++;
  323. $dayinweek_counter = 0;
  324. }else $dayinweek_counter++;
  325. }
  326. //write next other month
  327. $write_end_days = (6-$dayinweek_counter)+1;
  328. if($write_end_days > 0){
  329. for($day=1; $day<=$write_end_days; $day++){
  330. echo("<td class=\"othermonth\" align=\"center\">$day</td>");
  331. }
  332. echo("</tr>");
  333. $row_count++;
  334. }
  335. //write fulfil row to 6 rows
  336. for($day=$row_count; $day<=5; $day++){
  337. echo("<tr>");
  338. $tmpday = $write_end_days+1;
  339. for($f=$tmpday; $f<=($tmpday+6); $f++){
  340. echo("<td class=\"othermonth\" align=\"center\">$f</td>");
  341. }
  342. $write_end_days += 6;
  343. echo("</tr>");
  344. }
  345. //check next month is on allowed date
  346. if($date_allow2 && !$show_not_allow){
  347. $nxMonthTime = strtotime($next_year."-".$next_month."-1");
  348. if($nxMonthTime <= strtotime($date_allow2)){
  349. $show_next = true;
  350. }else $show_next = false;
  351. }else $show_next = true; //always show when not set
  352. ?>
  353. </table>
  354. </div>
  355. <?php
  356. if(($previous_year >= $year_start || $next_year <= $year_end) && ($show_previous || $show_next)){
  357. ?>
  358. <div id="calendar-footer">
  359. <div class="btn">
  360. <div style="float: left; width: 50%;">
  361. <?php
  362. if($previous_year >= $year_start && $show_previous){
  363. ?><a href="javascript:move('<?php echo(str_pad($previous_month, 2, "0", STR_PAD_LEFT));?>', '<?php echo($previous_year);?>');">&lt; Previous</a><?php
  364. }else echo("&nbsp;");
  365. ?>
  366. </div>
  367. <div style="float: left; width: 50%; text-align: right;">
  368. <?php
  369. if($next_year <= $year_end && $show_next){
  370. ?><a href="javascript:move('<?php echo(str_pad($next_month, 2, "0", STR_PAD_LEFT));?>', '<?php echo($next_year);?>');">Next &gt;</a><?php
  371. }else echo("&nbsp;");
  372. ?>
  373. </div>
  374. <div style="clear: both;"></div>
  375. </div>
  376. </div>
  377. <?php
  378. }
  379. ?>
  380. </div>
  381. </body>
  382. </html>