datepicker.php 9.0 KB

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