datepicker.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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')) {
  28. define('NOREQUIREUSER', '1'); // disabled
  29. }
  30. //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
  31. if (!defined('NOREQUIRESOC')) {
  32. define('NOREQUIRESOC', '1');
  33. }
  34. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Not disabled cause need to do translations
  35. if (!defined('NOCSRFCHECK')) {
  36. define('NOCSRFCHECK', 1);
  37. }
  38. if (!defined('NOTOKENRENEWAL')) {
  39. define('NOTOKENRENEWAL', 1);
  40. }
  41. if (!defined('NOLOGIN')) {
  42. define('NOLOGIN', 1); // disabled
  43. }
  44. if (!defined('NOREQUIREMENU')) {
  45. define('NOREQUIREMENU', 1);
  46. }
  47. if (!defined('NOREQUIREHTML')) {
  48. define('NOREQUIREHTML', 1);
  49. }
  50. require_once '../main.inc.php';
  51. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  52. if (GETPOST('lang', 'aZ09')) {
  53. $langs->setDefaultLang(GETPOST('lang', 'aZ09')); // If language was forced on URL by the main.inc.php
  54. }
  55. // Load translation files required by the page
  56. $langs->loadLangs(array("main", "agenda"));
  57. $right = ($langs->trans("DIRECTION") == 'rtl' ? 'left' : 'right');
  58. $left = ($langs->trans("DIRECTION") == 'rtl' ? 'right' : 'left');
  59. //var_dump($langs->defaultlang);
  60. //var_dump($conf->format_date_short_java);
  61. //var_dump($langs->trans("FormatDateShortJava"));
  62. // URL http://mydolibarr/core/datepicker.php?mode=test&m=10&y=2038 can be used for tests
  63. print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'."\n";
  64. print '<html>'."\n";
  65. print '<head>'."\n";
  66. if (GETPOST('mode') && GETPOST('mode') == 'test') {
  67. print '<script nonce="'.getNonce().'" type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js.php"></script>'."\n";
  68. } else {
  69. print '<title>'.$langs->trans("Calendar").'</title>';
  70. }
  71. // Define tradMonths javascript array (we define this in datapicker AND in parent page to avoid errors with IE8)
  72. $tradTemp = array(
  73. $langs->trans("January"),
  74. $langs->trans("February"),
  75. $langs->trans("March"),
  76. $langs->trans("April"),
  77. $langs->trans("May"),
  78. $langs->trans("June"),
  79. $langs->trans("July"),
  80. $langs->trans("August"),
  81. $langs->trans("September"),
  82. $langs->trans("October"),
  83. $langs->trans("November"),
  84. $langs->trans("December")
  85. );
  86. print '<script nonce="'.getNonce().'" type="text/javascript">';
  87. print 'var tradMonths = [';
  88. foreach ($tradTemp as $val) {
  89. print '"'.addslashes($val).'",';
  90. }
  91. print '""];';
  92. print '</script>'."\n";
  93. print '</head>'."\n";
  94. print '<body>'."\n";
  95. $qualified = true;
  96. if (!isset($_GET["sd"])) {
  97. $_GET["sd"] = "00000000";
  98. }
  99. if (!isset($_GET["m"]) || !isset($_GET["y"])) {
  100. $qualified = false;
  101. }
  102. if (isset($_GET["m"]) && isset($_GET["y"])) {
  103. if ($_GET["m"] < 1 || $_GET["m"] > 12) {
  104. $qualified = false;
  105. }
  106. if ($_GET["y"] < 0 || $_GET["y"] > 9999) {
  107. $qualified = false;
  108. }
  109. }
  110. // If parameters provided, we show calendar
  111. if ($qualified) {
  112. //print $_GET["cm"].",".$_GET["sd"].",".$_GET["m"].",".$_GET["y"];exit;
  113. displayBox(GETPOST("sd", 'alpha'), GETPOST("m", 'int'), GETPOST("y", 'int'));
  114. } else {
  115. dol_print_error('', 'ErrorBadParameters');
  116. }
  117. print '</body></html>'."\n";
  118. /**
  119. * Convert date to timestamp
  120. *
  121. * @param string $mysqldate Date YYYMMDD
  122. * @return integer Timestamp
  123. */
  124. function xyzToUnixTimestamp($mysqldate)
  125. {
  126. $year = substr($mysqldate, 0, 4);
  127. $month = substr($mysqldate, 4, 2);
  128. $day = substr($mysqldate, 6, 2);
  129. $unixtimestamp = dol_mktime(12, 0, 0, $month, $day, $year);
  130. return $unixtimestamp;
  131. }
  132. /**
  133. * Show box
  134. *
  135. * @param string $selectedDate Date YYYMMDD
  136. * @param int $month Month
  137. * @param int $year Year
  138. * @return void
  139. */
  140. function displayBox($selectedDate, $month, $year)
  141. {
  142. global $langs, $conf;
  143. //print "$selectedDate,$month,$year";
  144. $thedate = dol_mktime(12, 0, 0, $month, 1, $year);
  145. //print "thedate=$thedate";
  146. $today = dol_now();
  147. $todayArray = dol_getdate($today);
  148. if ($selectedDate != "00000000") {
  149. $selDate = xyzToUnixTimestamp($selectedDate);
  150. $xyz = dol_print_date($selDate, "%Y%m%d");
  151. } else {
  152. $selDate = 0;
  153. $xyz = 0;
  154. }
  155. ?>
  156. <table class="dp">
  157. <tr>
  158. <td colspan="6" class="dpHead"><?php
  159. $selectMonth = dol_print_date($thedate, '%m');
  160. $selectYear = dol_print_date($thedate, '%Y');
  161. echo $langs->trans("Month".$selectMonth).", ".$selectYear;
  162. ?></td>
  163. <td class="dpHead">
  164. <button type="button" class="dpInvisibleButtons" id="DPCancel"
  165. onClick="closeDPBox();">X</button>
  166. </td>
  167. </tr>
  168. <tr>
  169. <td class="dpButtons"
  170. 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>
  171. <td class="dpButtons"
  172. onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php if ($month == 1) {
  173. echo "12";
  174. } else {
  175. echo $month - 1;
  176. }?>','<?php if ($month == 1) {
  177. echo $year - 1;
  178. } else {
  179. echo $year;
  180. }?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&lt;</td>
  181. <td colspan="3" class="dpButtons"
  182. 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>
  183. <td class="dpButtons"
  184. onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php if ($month == 12) {
  185. echo "1";
  186. } else {
  187. echo $month + 1;
  188. }?>','<?php if ($month == 12) {
  189. echo $year + 1;
  190. } else {
  191. echo $year;
  192. } ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&gt;</td>
  193. <td class="dpButtons"
  194. 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>
  195. </tr>
  196. <tr class="dpDayNames">
  197. <?php
  198. $startday = isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1;
  199. $day_names = array('ShortSunday', 'ShortMonday', 'ShortTuesday', 'ShortWednesday', 'ShortThursday', 'ShortFriday', 'ShortSaturday');
  200. for ($i = 0; $i < 7; $i++) {
  201. echo '<td width="', (int) (($i + 1) * 100 / 7) - (int) ($i * 100 / 7), '%">', $langs->trans($day_names[($i + $startday) % 7]), '</td>', "\n";
  202. }
  203. print '</tr>';
  204. //print "x ".$thedate." y"; // $thedate = first day of month
  205. $firstdate = dol_getdate($thedate);
  206. //var_dump($firstdateofweek);
  207. $mydate = dol_get_first_day_week(1, $month, $year, true); // mydate = cursor date
  208. // Loop on each day of month
  209. $stoploop = 0;
  210. $day = 1;
  211. $cols = 0;
  212. while (!$stoploop) {
  213. //print_r($mydate);
  214. if ($mydate < $firstdate) { // At first run
  215. echo "<tr class=\"dpWeek\">";
  216. //echo $conf->global->MAIN_START_WEEK.' '.$firstdate["wday"].' '.$startday;
  217. $cols = 0;
  218. for ($i = 0; $i < 7; $i++) {
  219. $w = ($i + $startday) % 7;
  220. if ($w == $firstdate["wday"]) {
  221. $mydate = $firstdate;
  222. break;
  223. }
  224. echo "<td>&nbsp;</td>";
  225. $cols++;
  226. }
  227. } else {
  228. if ($mydate["wday"] == $startday) {
  229. echo "<tr class=\"dpWeek\">";
  230. $cols = 0;
  231. }
  232. }
  233. $dayclass = "dpReg";
  234. if ($thedate == $selDate) {
  235. $dayclass = "dpSelected";
  236. } elseif ($thedate == $today) {
  237. $dayclass = "dpToday";
  238. }
  239. if ($langs->trans("FormatDateShortJavaInput") == "FormatDateShortJavaInput") {
  240. print "ERROR FormatDateShortJavaInput not defined for language ".$langs->defaultlang;
  241. exit;
  242. }
  243. // Sur click dans calendrier, appelle fonction dpClickDay
  244. echo "<td class=\"".$dayclass."\"";
  245. echo " onMouseOver=\"dpHighlightDay(".$mydate["year"].",parseInt('".dol_print_date($thedate, "%m")."',10),".$mydate["mday"].",tradMonths)\"";
  246. echo " onClick=\"dpClickDay(".$mydate["year"].",parseInt('".dol_print_date($thedate, "%m")."',10),".$mydate["mday"].",'".$langs->trans("FormatDateShortJavaInput")."')\"";
  247. echo ">".sprintf("%02s", $mydate["mday"])."</td>";
  248. $cols++;
  249. if (($mydate["wday"] + 1) % 7 == $startday) {
  250. echo "</TR>\n";
  251. }
  252. //$thedate=strtotime("tomorrow",$thedate);
  253. $day++;
  254. $thedate = dol_mktime(12, 0, 0, $month, $day, $year);
  255. if ($thedate == '') {
  256. $stoploop = 1;
  257. } else {
  258. $mydate = dol_getdate($thedate);
  259. if ($firstdate["month"] != $mydate["month"]) {
  260. $stoploop = 1;
  261. }
  262. }
  263. }
  264. if ($cols < 7) {
  265. for ($i = 6; $i >= $cols; $i--) {
  266. echo "<td>&nbsp;</td>";
  267. }
  268. echo "</tr>\n";
  269. }
  270. ?>
  271. <tr>
  272. <td id="dpExp" class="dpExplanation" colspan="7"><?php
  273. if ($selDate) {
  274. $tempDate = dol_getdate($selDate);
  275. print $langs->trans("Month".$selectMonth)." ";
  276. print sprintf("%02s", $tempDate["mday"]);
  277. print ", ".$selectYear;
  278. } else {
  279. print "Click a Date";
  280. }
  281. ?></td>
  282. </tr>
  283. </table>
  284. <?php
  285. }//end function