dolibarr  9.0.0
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  *
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 
29 if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // disabled
30 //if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
31 if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
32 //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Not disabled cause need to do translations
33 if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK',1);
34 if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1);
35 if (! defined('NOLOGIN')) define('NOLOGIN',1); // disabled
36 if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU',1);
37 if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML',1);
38 
39 require_once '../main.inc.php';
40 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
41 
42 if (GETPOST('lang', 'aZ09')) $langs->setDefaultLang(GETPOST('lang', 'aZ09')); // If language was forced on URL by the main.inc.php
43 
44 // Load translation files required by the page
45 $langs->loadLangs(array("main","agenda"));
46 
47 $right=($langs->trans("DIRECTION")=='rtl'?'left':'right');
48 $left=($langs->trans("DIRECTION")=='rtl'?'right':'left');
49 
50 //var_dump($langs->defaultlang);
51 //var_dump($conf->format_date_short_java);
52 //var_dump($langs->trans("FormatDateShortJava"));
53 
54 
55 // URL http://mydolibarr/core/datepicker.php?mode=test&m=10&y=2038 can be used for tests
56 print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'."\n";
57 print '<html>'."\n";
58 print '<head>'."\n";
59 if (GETPOST('mode') && GETPOST('mode') == 'test')
60 {
61  print '<script type="text/javascript" src="'.DOL_URL_ROOT.'/core/js/lib_head.js.php"></script>'."\n";
62 }
63 else
64 {
65  print '<title>'.$langs->trans("Calendar").'</title>';
66 }
67 
68 // Define tradMonths javascript array (we define this in datapicker AND in parent page to avoid errors with IE8)
69 $tradTemp=array(
70  $langs->trans("January"),
71  $langs->trans("February"),
72  $langs->trans("March"),
73  $langs->trans("April"),
74  $langs->trans("May"),
75  $langs->trans("June"),
76  $langs->trans("July"),
77  $langs->trans("August"),
78  $langs->trans("September"),
79  $langs->trans("October"),
80  $langs->trans("November"),
81  $langs->trans("December")
82 );
83 print '<script type="text/javascript">';
84 print 'var tradMonths = [';
85 foreach($tradTemp as $val)
86 {
87  print '"'.addslashes($val).'",';
88 }
89 print '""];';
90 print '</script>'."\n";
91 print '</head>'."\n";
92 
93 print '<body>'."\n";
94 
95 
96 $qualified=true;
97 
98 if (! isset($_GET["sd"])) $_GET["sd"]="00000000";
99 
100 if (! isset($_GET["m"]) || ! isset($_GET["y"])) $qualified=false;
101 if (isset($_GET["m"]) && isset($_GET["y"]))
102 {
103  if ($_GET["m"] < 1 || $_GET["m"] > 12) $qualified=false;
104  if ($_GET["y"] < 0 || $_GET["y"] > 9999) $qualified=false;
105 }
106 
107 // If parameters provided, we show calendar
108 if ($qualified)
109 {
110  //print $_GET["cm"].",".$_GET["sd"].",".$_GET["m"].",".$_GET["y"];exit;
111  displayBox(GETPOST("sd",'alpha'), GETPOST("m",'int'), GETPOST("y",'int'));
112 }
113 else
114 {
115  dol_print_error('', 'ErrorBadParameters');
116 }
117 
118 
119 print '</body></html>'."\n";
120 
127 function xyzToUnixTimestamp($mysqldate)
128 {
129  $year=substr($mysqldate,0,4);
130  $month=substr($mysqldate,4,2);
131  $day=substr($mysqldate,6,2);
132  $unixtimestamp=dol_mktime(12,0,0,$month,$day,$year);
133  return $unixtimestamp;
134 }
135 
144 function displayBox($selectedDate,$month,$year)
145 {
146  global $langs,$conf;
147 
148  //print "$selectedDate,$month,$year";
149  $thedate=dol_mktime(12,0,0,$month,1,$year);
150  //print "thedate=$thedate";
151  $today=dol_now();
152  $todayArray=dol_getdate($today);
153  if($selectedDate != "00000000")
154  {
155  $selDate=xyzToUnixTimestamp($selectedDate);
156  $xyz=dol_print_date($selDate,"%Y%m%d");
157  }
158  else
159  {
160  $selDate=0;
161  $xyz=0;
162  }
163  ?>
164 <table class="dp">
165  <tr>
166  <td colspan="6" class="dpHead"><?php
167  $selectMonth = dol_print_date($thedate, '%m');
168  $selectYear = dol_print_date($thedate, '%Y');
169  echo $langs->trans("Month".$selectMonth).", ".$selectYear;
170  ?></td>
171  <td class="dpHead">
172  <button type="button" class="dpInvisibleButtons" id="DPCancel"
173  onClick="closeDPBox();">X</button>
174  </td>
175  </tr>
176  <tr>
177  <td class="dpButtons"
178  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>
179  <td class="dpButtons"
180  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>
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) 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>
185  <td class="dpButtons"
186  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>
187  </tr>
188  <tr class="dpDayNames">
189  <?php
190  $startday=isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1;
191  $day_names = array('ShortSunday', 'ShortMonday', 'ShortTuesday', 'ShortWednesday', 'ShortThursday', 'ShortFriday', 'ShortSaturday');
192  for( $i=0; $i < 7; $i++ )
193  {
194  echo '<td width="', (int) (($i+1)*100/7) - (int) ($i*100/7), '%">', $langs->trans($day_names[($i + $startday) % 7]), '</td>', "\n";
195  }
196  print '</tr>';
197  //print "x ".$thedate." y"; // $thedate = first day of month
198  $firstdate=dol_getdate($thedate);
199  //var_dump($firstdateofweek);
200  $mydate=dol_get_first_day_week(1, $month, $year, true); // mydate = cursor date
201 
202  // Loop on each day of month
203  $stoploop=0; $day=1; $cols=0;
204  while (! $stoploop)
205  {
206  //print_r($mydate);
207  if ($mydate < $firstdate) // At first run
208  {
209  echo "<tr class=\"dpWeek\">";
210  //echo $conf->global->MAIN_START_WEEK.' '.$firstdate["wday"].' '.$startday;
211  $cols=0;
212  for ($i = 0; $i < 7; $i++)
213  {
214  $w = ($i + $startday) % 7;
215  if ($w == $firstdate["wday"])
216  {
217  $mydate = $firstdate;
218  break;
219  }
220  echo "<td>&nbsp;</td>";
221  $cols++;
222  }
223  }
224  else
225  {
226  if ($mydate["wday"] == $startday)
227  {
228  echo "<tr class=\"dpWeek\">";
229  $cols=0;
230  }
231  }
232 
233  $dayclass="dpReg";
234  if($thedate==$selDate) $dayclass="dpSelected";
235  elseif($thedate==$today) $dayclass="dpToday";
236 
237  if ($langs->trans("FormatDateShortJavaInput")=="FormatDateShortJavaInput")
238  {
239  print "ERROR FormatDateShortJavaInput not defined for language ".$langs->defaultlang;
240  exit;
241  }
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 
250  if (($mydate["wday"] + 1) % 7 == $startday) 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  {
257  $stoploop=1;
258  }
259  else
260  {
261  $mydate=dol_getdate($thedate);
262  if ($firstdate["month"] != $mydate["month"]) $stoploop=1;
263  }
264  }
265 
266  if ($cols < 7)
267  {
268  for($i=6; $i>=$cols; $i--) echo "<td>&nbsp;</td>";
269  echo "</tr>\n";
270  }
271  ?>
272  <tr>
273  <td id="dpExp" class="dpExplanation" colspan="7"><?php
274  if($selDate)
275  {
276  $tempDate=dol_getdate($selDate);
277  print $langs->trans("Month".$selectMonth)." ";
278  print sprintf("%02s",$tempDate["mday"]);
279  print ", ".$selectYear;
280  }
281  else
282  {
283  print "Click a Date";
284  }
285  ?></td>
286  </tr>
287 </table>
288  <?php
289 }//end function
xyzToUnixTimestamp($mysqldate)
Convert date to timestamp.
Definition: datepicker.php:127
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_get_first_day_week($day, $month, $year, $gm=false)
Return first day of week for a date.
Definition: date.lib.php:495
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
type
Definition: viewcat.php:284
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_getdate($timestamp, $fast=false)
Return an array with locale date info.
displayBox($selectedDate, $month, $year)
Show box.
Definition: datepicker.php:144