datepicker.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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@capnetworks.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 <http://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')) $langs->setDefaultLang(GETPOST('lang')); // If language was forced on URL by the main.inc.php
  39. $langs->load("main");
  40. $langs->load("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($langs->trans("January"),
  60. $langs->trans("February"),
  61. $langs->trans("March"),
  62. $langs->trans("April"),
  63. $langs->trans("May"),
  64. $langs->trans("June"),
  65. $langs->trans("July"),
  66. $langs->trans("August"),
  67. $langs->trans("September"),
  68. $langs->trans("October"),
  69. $langs->trans("November"),
  70. $langs->trans("December")
  71. );
  72. print '<script type="text/javascript">';
  73. print 'var tradMonths = [';
  74. foreach($tradTemp as $val)
  75. {
  76. print '"'.addslashes($val).'",';
  77. }
  78. print '""];';
  79. print '</script>'."\n";
  80. print '</head>'."\n";
  81. print '<body>'."\n";
  82. $qualified=true;
  83. if (! isset($_GET["sd"])) $_GET["sd"]="00000000";
  84. if (! isset($_GET["m"])) $qualified=false;
  85. if (! isset($_GET["y"])) $qualified=false;
  86. if (isset($_GET["m"]) && isset($_GET["y"]))
  87. {
  88. if ($_GET["m"] < 1) $qualified=false;
  89. if ($_GET["m"] > 12) $qualified=false;
  90. if ($_GET["y"] < 0) $qualified=false;
  91. if ($_GET["y"] > 9999) $qualified=false;
  92. }
  93. // If parameters provided, we show calendar
  94. if ($qualified)
  95. {
  96. //print $_GET["cm"].",".$_GET["sd"].",".$_GET["m"].",".$_GET["y"];exit;
  97. displayBox($_GET["sd"],$_GET["m"],$_GET["y"]);
  98. }
  99. else
  100. {
  101. dol_print_error('','ErrorBadParameters');
  102. }
  103. print '</body></html>'."\n";
  104. /**
  105. * Convert date to timestamp
  106. *
  107. * @param string $mysqldate Date YYYMMDD
  108. * @return integer Timestamp
  109. */
  110. function xyzToUnixTimestamp($mysqldate)
  111. {
  112. $year=substr($mysqldate,0,4);
  113. $month=substr($mysqldate,4,2);
  114. $day=substr($mysqldate,6,2);
  115. $unixtimestamp=dol_mktime(12,0,0,$month,$day,$year);
  116. return $unixtimestamp;
  117. }
  118. /**
  119. * Show box
  120. *
  121. * @param string $selectedDate Date YYYMMDD
  122. * @param int $month Month
  123. * @param int $year Year
  124. * @return void
  125. */
  126. function displayBox($selectedDate,$month,$year)
  127. {
  128. global $langs,$conf;
  129. //print "$selectedDate,$month,$year";
  130. $thedate=dol_mktime(12,0,0,$month,1,$year);
  131. //print "thedate=$thedate";
  132. $today=dol_now();
  133. $todayArray=dol_getdate($today);
  134. if($selectedDate != "00000000")
  135. {
  136. $selDate=xyzToUnixTimestamp($selectedDate);
  137. $xyz=dol_print_date($selDate,"%Y%m%d");
  138. }
  139. else
  140. {
  141. $selDate=0;
  142. $xyz=0;
  143. }
  144. ?>
  145. <table class="dp">
  146. <tr>
  147. <td colspan="6" class="dpHead"><?php
  148. $selectMonth = dol_print_date($thedate, '%m');
  149. $selectYear = dol_print_date($thedate, '%Y');
  150. echo $langs->trans("Month".$selectMonth).", ".$selectYear;
  151. ?></td>
  152. <td class="dpHead">
  153. <button type="button" class="dpInvisibleButtons" id="DPCancel"
  154. onClick="closeDPBox();">X</button>
  155. </td>
  156. </tr>
  157. <tr>
  158. <td class="dpButtons"
  159. 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>
  160. <td class="dpButtons"
  161. 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>
  162. <td colspan="3" class="dpButtons"
  163. 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>
  164. <td class="dpButtons"
  165. 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>
  166. <td class="dpButtons"
  167. 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>
  168. </tr>
  169. <tr class="dpDayNames">
  170. <?php
  171. $startday=isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1;
  172. $day_names = array('ShortSunday', 'ShortMonday', 'ShortTuesday', 'ShortWednesday', 'ShortThursday', 'ShortFriday', 'ShortSaturday');
  173. for( $i=0; $i < 7; $i++ )
  174. {
  175. echo '<td width="', (int) (($i+1)*100/7) - (int) ($i*100/7), '%">', $langs->trans($day_names[($i + $startday) % 7]), '</td>', "\n";
  176. }
  177. ?>
  178. </tr>
  179. <?php
  180. //print "x ".$thedate." y"; // $thedate = first day of month
  181. $firstdate=dol_getdate($thedate);
  182. //var_dump($firstdateofweek);
  183. $mydate=dol_get_first_day_week(1, $month, $year, true); // mydate = cursor date
  184. // Loop on each day of month
  185. $stoploop=0; $day=1; $cols=0;
  186. while (! $stoploop)
  187. {
  188. //print_r($mydate);
  189. if ($mydate < $firstdate) // At first run
  190. {
  191. echo "<TR class=\"dpWeek\">";
  192. //echo $conf->global->MAIN_START_WEEK.' '.$firstdate["wday"].' '.$startday;
  193. $cols=0;
  194. for ($i = 0; $i < 7; $i++)
  195. {
  196. $w = ($i + $startday) % 7;
  197. if ($w == $firstdate["wday"])
  198. {
  199. $mydate = $firstdate;
  200. break;
  201. }
  202. echo "<TD>&nbsp;</TD>";
  203. $cols++;
  204. }
  205. }
  206. else
  207. {
  208. if ($mydate["wday"] == $startday)
  209. {
  210. echo "<TR class=\"dpWeek\">";
  211. $cols=0;
  212. }
  213. }
  214. $dayclass="dpReg";
  215. if($thedate==$selDate) $dayclass="dpSelected";
  216. elseif($thedate==$today) $dayclass="dpToday";
  217. if ($langs->trans("FormatDateShortJavaInput")=="FormatDateShortJavaInput")
  218. {
  219. print "ERROR FormatDateShortJavaInput not defined for language ".$langs->defaultlang;
  220. exit;
  221. }
  222. // Sur click dans calendrier, appelle fonction dpClickDay
  223. echo "<TD class=\"".$dayclass."\"";
  224. echo " onMouseOver=\"dpHighlightDay(".$mydate["year"].",parseInt('".dol_print_date($thedate,"%m")."',10),".$mydate["mday"].",tradMonths)\"";
  225. echo " onClick=\"dpClickDay(".$mydate["year"].",parseInt('".dol_print_date($thedate,"%m")."',10),".$mydate["mday"].",'".$langs->trans("FormatDateShortJavaInput")."')\"";
  226. echo ">".sprintf("%02s",$mydate["mday"])."</TD>";
  227. $cols++;
  228. if (($mydate["wday"] + 1) % 7 == $startday) echo "</TR>\n";
  229. //$thedate=strtotime("tomorrow",$thedate);
  230. $day++;
  231. $thedate=dol_mktime(12,0,0,$month,$day,$year);
  232. if ($thedate == '')
  233. {
  234. $stoploop=1;
  235. }
  236. else
  237. {
  238. $mydate=dol_getdate($thedate);
  239. if ($firstdate["month"] != $mydate["month"]) $stoploop=1;
  240. }
  241. }
  242. if ($cols < 7)
  243. {
  244. for($i=6; $i>=$cols; $i--) echo "<TD>&nbsp;</TD>";
  245. echo "</TR>\n";
  246. }
  247. ?>
  248. <tr>
  249. <td id="dpExp" class="dpExplanation" colspan="7"><?php
  250. if($selDate)
  251. {
  252. $tempDate=dol_getdate($selDate);
  253. print $langs->trans("Month".$selectMonth)." ";
  254. print sprintf("%02s",$tempDate["mday"]);
  255. print ", ".$selectYear;
  256. }
  257. else
  258. {
  259. print "Click a Date";
  260. }
  261. ?></td>
  262. </tr>
  263. </table>
  264. <?php
  265. }//end function