datepicker.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /* Copyright (C) phpBSM
  3. * Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2007 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This file is a modified version of datepicker.php from phpBSM to fix some
  8. * bugs, to add new features and to dramatically increase speed.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/core/datepicker.php
  25. * \brief File to manage popup date selector
  26. */
  27. if (!defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // disabled
  28. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  29. if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
  30. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Not disabled cause need to do translations
  31. if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', 1);
  32. if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1);
  33. if (!defined('NOLOGIN')) define('NOLOGIN', 1); // disabled
  34. if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', 1);
  35. if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', 1);
  36. require_once '../main.inc.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  38. if (GETPOST('lang', 'aZ09')) $langs->setDefaultLang(GETPOST('lang', 'aZ09')); // If language was forced on URL by the main.inc.php
  39. // Load translation files required by the page
  40. $langs->loadLangs(array("main", "agenda"));
  41. $right = ($langs->trans("DIRECTION") == 'rtl' ? 'left' : 'right');
  42. $left = ($langs->trans("DIRECTION") == 'rtl' ? 'right' : 'left');
  43. //var_dump($langs->defaultlang);
  44. //var_dump($conf->format_date_short_java);
  45. //var_dump($langs->trans("FormatDateShortJava"));
  46. // URL http://mydolibarr/core/datepicker.php?mode=test&m=10&y=2038 can be used for tests
  47. print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'."\n";
  48. print '<html>'."\n";
  49. print '<head>'."\n";
  50. if (GETPOST('mode') && GETPOST('mode') == 'test')
  51. {
  52. print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js.php"></script>'."\n";
  53. }
  54. else
  55. {
  56. print '<title>'.$langs->trans("Calendar").'</title>';
  57. }
  58. // Define tradMonths javascript array (we define this in datapicker AND in parent page to avoid errors with IE8)
  59. $tradTemp = array(
  60. $langs->trans("January"),
  61. $langs->trans("February"),
  62. $langs->trans("March"),
  63. $langs->trans("April"),
  64. $langs->trans("May"),
  65. $langs->trans("June"),
  66. $langs->trans("July"),
  67. $langs->trans("August"),
  68. $langs->trans("September"),
  69. $langs->trans("October"),
  70. $langs->trans("November"),
  71. $langs->trans("December")
  72. );
  73. print '<script type="text/javascript">';
  74. print 'var tradMonths = [';
  75. foreach ($tradTemp as $val)
  76. {
  77. print '"'.addslashes($val).'",';
  78. }
  79. print '""];';
  80. print '</script>'."\n";
  81. print '</head>'."\n";
  82. print '<body>'."\n";
  83. $qualified = true;
  84. if (!isset($_GET["sd"])) $_GET["sd"] = "00000000";
  85. if (!isset($_GET["m"]) || !isset($_GET["y"])) $qualified = false;
  86. if (isset($_GET["m"]) && isset($_GET["y"]))
  87. {
  88. if ($_GET["m"] < 1 || $_GET["m"] > 12) $qualified = false;
  89. if ($_GET["y"] < 0 || $_GET["y"] > 9999) $qualified = false;
  90. }
  91. // If parameters provided, we show calendar
  92. if ($qualified)
  93. {
  94. //print $_GET["cm"].",".$_GET["sd"].",".$_GET["m"].",".$_GET["y"];exit;
  95. displayBox(GETPOST("sd", 'alpha'), GETPOST("m", 'int'), GETPOST("y", 'int'));
  96. }
  97. else
  98. {
  99. dol_print_error('', 'ErrorBadParameters');
  100. }
  101. print '</body></html>'."\n";
  102. /**
  103. * Convert date to timestamp
  104. *
  105. * @param string $mysqldate Date YYYMMDD
  106. * @return integer Timestamp
  107. */
  108. function xyzToUnixTimestamp($mysqldate)
  109. {
  110. $year = substr($mysqldate, 0, 4);
  111. $month = substr($mysqldate, 4, 2);
  112. $day = substr($mysqldate, 6, 2);
  113. $unixtimestamp = dol_mktime(12, 0, 0, $month, $day, $year);
  114. return $unixtimestamp;
  115. }
  116. /**
  117. * Show box
  118. *
  119. * @param string $selectedDate Date YYYMMDD
  120. * @param int $month Month
  121. * @param int $year Year
  122. * @return void
  123. */
  124. function displayBox($selectedDate, $month, $year)
  125. {
  126. global $langs, $conf;
  127. //print "$selectedDate,$month,$year";
  128. $thedate = dol_mktime(12, 0, 0, $month, 1, $year);
  129. //print "thedate=$thedate";
  130. $today = dol_now();
  131. $todayArray = dol_getdate($today);
  132. if ($selectedDate != "00000000")
  133. {
  134. $selDate = xyzToUnixTimestamp($selectedDate);
  135. $xyz = dol_print_date($selDate, "%Y%m%d");
  136. }
  137. else
  138. {
  139. $selDate = 0;
  140. $xyz = 0;
  141. }
  142. ?>
  143. <table class="dp">
  144. <tr>
  145. <td colspan="6" class="dpHead"><?php
  146. $selectMonth = dol_print_date($thedate, '%m');
  147. $selectYear = dol_print_date($thedate, '%Y');
  148. echo $langs->trans("Month".$selectMonth).", ".$selectYear;
  149. ?></td>
  150. <td class="dpHead">
  151. <button type="button" class="dpInvisibleButtons" id="DPCancel"
  152. onClick="closeDPBox();">X</button>
  153. </td>
  154. </tr>
  155. <tr>
  156. <td class="dpButtons"
  157. onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php echo $month?>','<?php echo $year - 1?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&lt;&lt;</td>
  158. <td class="dpButtons"
  159. onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php if ($month == 1) echo "12"; else echo $month - 1?>','<?php if ($month == 1) echo $year - 1; else echo $year?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&lt;</td>
  160. <td colspan="3" class="dpButtons"
  161. onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php echo (int) dol_print_date($today, '%m')?>','<?php echo $todayArray["year"]?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')"><?php echo '-' ?></td>
  162. <td class="dpButtons"
  163. onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php if ($month == 12) echo "1"; else echo $month + 1?>','<?php if ($month == 12) echo $year + 1; else echo $year; ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&gt;</td>
  164. <td class="dpButtons"
  165. onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php echo $month?>','<?php echo $year + 1?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&gt;&gt;</td>
  166. </tr>
  167. <tr class="dpDayNames">
  168. <?php
  169. $startday = isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1;
  170. $day_names = array('ShortSunday', 'ShortMonday', 'ShortTuesday', 'ShortWednesday', 'ShortThursday', 'ShortFriday', 'ShortSaturday');
  171. for ($i = 0; $i < 7; $i++)
  172. {
  173. echo '<td width="', (int) (($i + 1) * 100 / 7) - (int) ($i * 100 / 7), '%">', $langs->trans($day_names[($i + $startday) % 7]), '</td>', "\n";
  174. }
  175. print '</tr>';
  176. //print "x ".$thedate." y"; // $thedate = first day of month
  177. $firstdate = dol_getdate($thedate);
  178. //var_dump($firstdateofweek);
  179. $mydate = dol_get_first_day_week(1, $month, $year, true); // mydate = cursor date
  180. // Loop on each day of month
  181. $stoploop = 0; $day = 1; $cols = 0;
  182. while (!$stoploop)
  183. {
  184. //print_r($mydate);
  185. if ($mydate < $firstdate) // At first run
  186. {
  187. echo "<tr class=\"dpWeek\">";
  188. //echo $conf->global->MAIN_START_WEEK.' '.$firstdate["wday"].' '.$startday;
  189. $cols = 0;
  190. for ($i = 0; $i < 7; $i++)
  191. {
  192. $w = ($i + $startday) % 7;
  193. if ($w == $firstdate["wday"])
  194. {
  195. $mydate = $firstdate;
  196. break;
  197. }
  198. echo "<td>&nbsp;</td>";
  199. $cols++;
  200. }
  201. }
  202. else
  203. {
  204. if ($mydate["wday"] == $startday)
  205. {
  206. echo "<tr class=\"dpWeek\">";
  207. $cols = 0;
  208. }
  209. }
  210. $dayclass = "dpReg";
  211. if ($thedate == $selDate) $dayclass = "dpSelected";
  212. elseif ($thedate == $today) $dayclass = "dpToday";
  213. if ($langs->trans("FormatDateShortJavaInput") == "FormatDateShortJavaInput")
  214. {
  215. print "ERROR FormatDateShortJavaInput not defined for language ".$langs->defaultlang;
  216. exit;
  217. }
  218. // Sur click dans calendrier, appelle fonction dpClickDay
  219. echo "<td class=\"".$dayclass."\"";
  220. echo " onMouseOver=\"dpHighlightDay(".$mydate["year"].",parseInt('".dol_print_date($thedate, "%m")."',10),".$mydate["mday"].",tradMonths)\"";
  221. echo " onClick=\"dpClickDay(".$mydate["year"].",parseInt('".dol_print_date($thedate, "%m")."',10),".$mydate["mday"].",'".$langs->trans("FormatDateShortJavaInput")."')\"";
  222. echo ">".sprintf("%02s", $mydate["mday"])."</td>";
  223. $cols++;
  224. if (($mydate["wday"] + 1) % 7 == $startday) echo "</TR>\n";
  225. //$thedate=strtotime("tomorrow",$thedate);
  226. $day++;
  227. $thedate = dol_mktime(12, 0, 0, $month, $day, $year);
  228. if ($thedate == '')
  229. {
  230. $stoploop = 1;
  231. }
  232. else
  233. {
  234. $mydate = dol_getdate($thedate);
  235. if ($firstdate["month"] != $mydate["month"]) $stoploop = 1;
  236. }
  237. }
  238. if ($cols < 7)
  239. {
  240. for ($i = 6; $i >= $cols; $i--) echo "<td>&nbsp;</td>";
  241. echo "</tr>\n";
  242. }
  243. ?>
  244. <tr>
  245. <td id="dpExp" class="dpExplanation" colspan="7"><?php
  246. if ($selDate)
  247. {
  248. $tempDate = dol_getdate($selDate);
  249. print $langs->trans("Month".$selectMonth)." ";
  250. print sprintf("%02s", $tempDate["mday"]);
  251. print ", ".$selectYear;
  252. }
  253. else
  254. {
  255. print "Click a Date";
  256. }
  257. ?></td>
  258. </tr>
  259. </table>
  260. <?php
  261. }//end function