dolibarr  20.0.0-alpha
datepicker.php
Go to the documentation of this file.
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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7  *
8  * This file is a modified version of datepicker.php from phpBSM to fix some
9  * bugs, to add new features and to dramatically increase speed.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
30 if (!defined('NOREQUIREUSER')) {
31  define('NOREQUIREUSER', '1'); // disabled
32 }
33 //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
34 if (!defined('NOREQUIRESOC')) {
35  define('NOREQUIRESOC', '1');
36 }
37 //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Not disabled cause need to do translations
38 if (!defined('NOCSRFCHECK')) {
39  define('NOCSRFCHECK', 1);
40 }
41 if (!defined('NOTOKENRENEWAL')) {
42  define('NOTOKENRENEWAL', 1);
43 }
44 if (!defined('NOLOGIN')) {
45  define('NOLOGIN', 1); // disabled
46 }
47 if (!defined('NOREQUIREMENU')) {
48  define('NOREQUIREMENU', 1);
49 }
50 if (!defined('NOREQUIREHTML')) {
51  define('NOREQUIREHTML', 1);
52 }
53 
54 require_once '../main.inc.php';
55 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
56 
57 if (GETPOST('lang', 'aZ09')) {
58  $langs->setDefaultLang(GETPOST('lang', 'aZ09')); // If language was forced on URL by the main.inc.php
59 }
60 
61 // Load translation files required by the page
62 $langs->loadLangs(array("main", "agenda"));
63 
64 $right = ($langs->trans("DIRECTION") == 'rtl' ? 'left' : 'right');
65 $left = ($langs->trans("DIRECTION") == 'rtl' ? 'right' : 'left');
66 
67 //var_dump($langs->defaultlang);
68 //var_dump($conf->format_date_short_java);
69 //var_dump($langs->trans("FormatDateShortJava"));
70 
71 
72 // URL http://mydolibarr/core/datepicker.php?mode=test&m=10&y=2038 can be used for tests
73 print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'."\n";
74 print '<html>'."\n";
75 print '<head>'."\n";
76 if (GETPOST('mode') && GETPOST('mode') == 'test') {
77  print '<script nonce="'.getNonce().'" type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js.php"></script>'."\n";
78 } else {
79  print '<title>'.$langs->trans("Calendar").'</title>';
80 }
81 
82 // Define tradMonths javascript array (we define this in datapicker AND in parent page to avoid errors with IE8)
83 $tradTemp = array(
84  $langs->trans("January"),
85  $langs->trans("February"),
86  $langs->trans("March"),
87  $langs->trans("April"),
88  $langs->trans("May"),
89  $langs->trans("June"),
90  $langs->trans("July"),
91  $langs->trans("August"),
92  $langs->trans("September"),
93  $langs->trans("October"),
94  $langs->trans("November"),
95  $langs->trans("December")
96 );
97 print '<script nonce="'.getNonce().'" type="text/javascript">';
98 print 'var tradMonths = [';
99 foreach ($tradTemp as $val) {
100  print '"'.addslashes($val).'",';
101 }
102 print '""];';
103 print '</script>'."\n";
104 print '</head>'."\n";
105 
106 print '<body>'."\n";
107 
108 
109 $qualified = true;
110 
111 // TODO Replace with GETPOST
112 if (!isset($_GET["sd"])) {
113  $_GET["sd"] = "00000000";
114 }
115 if (!isset($_GET["m"]) || !isset($_GET["y"])) {
116  $qualified = false;
117 }
118 if (isset($_GET["m"]) && isset($_GET["y"])) {
119  if ($_GET["m"] < 1 || $_GET["m"] > 12) {
120  $qualified = false;
121  }
122  if ($_GET["y"] < 0 || $_GET["y"] > 9999) {
123  $qualified = false;
124  }
125 }
126 
127 // If parameters provided, we show calendar
128 if ($qualified) {
129  displayBox(GETPOSTINT("sd"), GETPOSTINT("m"), GETPOSTINT("y"));
130 } else {
131  dol_print_error(null, 'ErrorBadParameters');
132 }
133 
134 
135 print '</body></html>'."\n";
136 
143 function xyzToUnixTimestamp($mysqldate)
144 {
145  $year = substr($mysqldate, 0, 4);
146  $month = substr($mysqldate, 4, 2);
147  $day = substr($mysqldate, 6, 2);
148  $unixtimestamp = dol_mktime(12, 0, 0, $month, $day, $year);
149  return $unixtimestamp;
150 }
151 
160 function displayBox($selectedDate, $month, $year)
161 {
162  global $langs, $conf;
163 
164  //print "$selectedDate,$month,$year";
165  $thedate = dol_mktime(12, 0, 0, $month, 1, $year);
166  //print "thedate=$thedate";
167  $today = dol_now();
168  $todayArray = dol_getdate($today);
169  if ($selectedDate != "00000000") {
170  $selDate = xyzToUnixTimestamp($selectedDate);
171  $xyz = dol_print_date($selDate, "%Y%m%d");
172  } else {
173  $selDate = 0;
174  $xyz = 0;
175  } ?>
176 <table class="dp">
177  <tr>
178  <td colspan="6" class="dpHead"><?php
179  $selectMonth = dol_print_date($thedate, '%m');
180  $selectYear = dol_print_date($thedate, '%Y');
181  echo $langs->trans("Month".$selectMonth).", ".$selectYear; ?></td>
182  <td class="dpHead">
183  <button type="button" class="dpInvisibleButtons" id="DPCancel"
184  onClick="closeDPBox();">X</button>
185  </td>
186  </tr>
187  <tr>
188  <td class="dpButtons"
189  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>
190  <td class="dpButtons"
191  onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php if ($month == 1) {
192  echo "12";
193  } else {
194  echo $month - 1;
195  } ?>','<?php if ($month == 1) {
196  echo $year - 1;
197  } else {
198  echo $year;
199  } ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&lt;</td>
200  <td colspan="3" class="dpButtons"
201  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>
202  <td class="dpButtons"
203  onClick="loadMonth('<?php echo DOL_URL_ROOT.'/core/' ?>','<?php if ($month == 12) {
204  echo "1";
205  } else {
206  echo $month + 1;
207  } ?>','<?php if ($month == 12) {
208  echo $year + 1;
209  } else {
210  echo $year;
211  } ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&gt;</td>
212  <td class="dpButtons"
213  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>
214  </tr>
215  <tr class="dpDayNames">
216  <?php
217  $startday = isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1;
218  $day_names = array('ShortSunday', 'ShortMonday', 'ShortTuesday', 'ShortWednesday', 'ShortThursday', 'ShortFriday', 'ShortSaturday');
219  for ($i = 0; $i < 7; $i++) {
220  echo '<td width="', (int) (($i + 1) * 100 / 7) - (int) ($i * 100 / 7), '%">', $langs->trans($day_names[($i + $startday) % 7]), '</td>', "\n";
221  }
222  print '</tr>';
223  //print "x ".$thedate." y"; // $thedate = first day of month
224  $firstdate = dol_getdate($thedate);
225  //var_dump($firstdateofweek);
226  $mydate = dol_get_first_day_week(1, $month, $year, true); // mydate = cursor date
227 
228  // Loop on each day of month
229  $stoploop = 0;
230  $day = 1;
231  $cols = 0;
232  while (!$stoploop) {
233  //print_r($mydate);
234  if ($mydate < $firstdate) { // At first run
235  echo "<tr class=\"dpWeek\">";
236  //echo $conf->global->MAIN_START_WEEK.' '.$firstdate["wday"].' '.$startday;
237  $cols = 0;
238  for ($i = 0; $i < 7; $i++) {
239  $w = ($i + $startday) % 7;
240  if ($w == $firstdate["wday"]) {
241  $mydate = $firstdate;
242  break;
243  }
244  echo "<td>&nbsp;</td>";
245  $cols++;
246  }
247  } else {
248  if ($mydate["wday"] == $startday) {
249  echo "<tr class=\"dpWeek\">";
250  $cols = 0;
251  }
252  }
253 
254  $dayclass = "dpReg";
255  if ($thedate == $selDate) {
256  $dayclass = "dpSelected";
257  } elseif ($thedate == $today) {
258  $dayclass = "dpToday";
259  }
260 
261  if ($langs->trans("FormatDateShortJavaInput") == "FormatDateShortJavaInput") {
262  print "ERROR FormatDateShortJavaInput not defined for language ".$langs->defaultlang;
263  exit;
264  }
265 
266  // Sur click dans calendrier, appelle fonction dpClickDay
267  echo "<td class=\"".$dayclass."\"";
268  echo " onMouseOver=\"dpHighlightDay(".$mydate["year"].",parseInt('".dol_print_date($thedate, "%m")."',10),".$mydate["mday"].",tradMonths)\"";
269  echo " onClick=\"dpClickDay(".$mydate["year"].",parseInt('".dol_print_date($thedate, "%m")."',10),".$mydate["mday"].",'".$langs->trans("FormatDateShortJavaInput")."')\"";
270  echo ">".sprintf("%02d", $mydate["mday"])."</td>";
271  $cols++;
272 
273  if (($mydate["wday"] + 1) % 7 == $startday) {
274  echo "</TR>\n";
275  }
276 
277  //$thedate=strtotime("tomorrow",$thedate);
278  $day++;
279  $thedate = dol_mktime(12, 0, 0, $month, $day, $year);
280  if ($thedate == '') {
281  $stoploop = 1;
282  } else {
283  $mydate = dol_getdate($thedate);
284  if ($firstdate["month"] != $mydate["month"]) {
285  $stoploop = 1;
286  }
287  }
288  }
289 
290  if ($cols < 7) {
291  for ($i = 6; $i >= $cols; $i--) {
292  echo "<td>&nbsp;</td>";
293  }
294  echo "</tr>\n";
295  } ?>
296  <tr>
297  <td id="dpExp" class="dpExplanation" colspan="7"><?php
298  if ($selDate) {
299  $tempDate = dol_getdate($selDate);
300  print $langs->trans("Month".$selectMonth)." ";
301  print sprintf("%02d", $tempDate["mday"]);
302  print ", ".$selectYear;
303  } else {
304  print "Click a Date";
305  } ?></td>
306  </tr>
307 </table>
308  <?php
309 }//end function
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition: date.lib.php:669
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
treeview li table
No Email.
table tableforfield button
0 = Do not include form tag and submit button -1 = Do not include form tag but include submit button
Definition: style.css.php:886
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:122