dolibarr  7.0.0-beta
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@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 
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 $langs->load("main");
45 $langs->load("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($langs->trans("January"),
70 $langs->trans("February"),
71 $langs->trans("March"),
72 $langs->trans("April"),
73 $langs->trans("May"),
74 $langs->trans("June"),
75 $langs->trans("July"),
76 $langs->trans("August"),
77 $langs->trans("September"),
78 $langs->trans("October"),
79 $langs->trans("November"),
80 $langs->trans("December")
81 );
82 print '<script type="text/javascript">';
83 print 'var tradMonths = [';
84 foreach($tradTemp as $val)
85 {
86  print '"'.addslashes($val).'",';
87 }
88 print '""];';
89 print '</script>'."\n";
90 print '</head>'."\n";
91 
92 print '<body>'."\n";
93 
94 
95 $qualified=true;
96 
97 if (! isset($_GET["sd"])) $_GET["sd"]="00000000";
98 
99 if (! isset($_GET["m"])) $qualified=false;
100 if (! isset($_GET["y"])) $qualified=false;
101 if (isset($_GET["m"]) && isset($_GET["y"]))
102 {
103  if ($_GET["m"] < 1) $qualified=false;
104  if ($_GET["m"] > 12) $qualified=false;
105  if ($_GET["y"] < 0) $qualified=false;
106  if ($_GET["y"] > 9999) $qualified=false;
107 }
108 
109 // If parameters provided, we show calendar
110 if ($qualified)
111 {
112  //print $_GET["cm"].",".$_GET["sd"].",".$_GET["m"].",".$_GET["y"];exit;
113  displayBox(GETPOST("sd",'alpha'),GETPOST("m",'int'),GETPOST("y",'int'));
114 }
115 else
116 {
117  dol_print_error('','ErrorBadParameters');
118 }
119 
120 
121 print '</body></html>'."\n";
122 
129 function xyzToUnixTimestamp($mysqldate)
130 {
131  $year=substr($mysqldate,0,4);
132  $month=substr($mysqldate,4,2);
133  $day=substr($mysqldate,6,2);
134  $unixtimestamp=dol_mktime(12,0,0,$month,$day,$year);
135  return $unixtimestamp;
136 }
137 
146 function displayBox($selectedDate,$month,$year)
147 {
148  global $langs,$conf;
149 
150  //print "$selectedDate,$month,$year";
151  $thedate=dol_mktime(12,0,0,$month,1,$year);
152  //print "thedate=$thedate";
153  $today=dol_now();
154  $todayArray=dol_getdate($today);
155  if($selectedDate != "00000000")
156  {
157  $selDate=xyzToUnixTimestamp($selectedDate);
158  $xyz=dol_print_date($selDate,"%Y%m%d");
159  }
160  else
161  {
162  $selDate=0;
163  $xyz=0;
164  }
165  ?>
166 <table class="dp">
167  <tr>
168  <td colspan="6" class="dpHead"><?php
169  $selectMonth = dol_print_date($thedate, '%m');
170  $selectYear = dol_print_date($thedate, '%Y');
171  echo $langs->trans("Month".$selectMonth).", ".$selectYear;
172  ?></td>
173  <td class="dpHead">
174  <button type="button" class="dpInvisibleButtons" id="DPCancel"
175  onClick="closeDPBox();">X</button>
176  </td>
177  </tr>
178  <tr>
179  <td class="dpButtons"
180  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>
181  <td class="dpButtons"
182  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>
183  <td colspan="3" class="dpButtons"
184  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>
185  <td class="dpButtons"
186  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>
187  <td class="dpButtons"
188  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>
189  </tr>
190  <tr class="dpDayNames">
191  <?php
192  $startday=isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1;
193  $day_names = array('ShortSunday', 'ShortMonday', 'ShortTuesday', 'ShortWednesday', 'ShortThursday', 'ShortFriday', 'ShortSaturday');
194  for( $i=0; $i < 7; $i++ )
195  {
196  echo '<td width="', (int) (($i+1)*100/7) - (int) ($i*100/7), '%">', $langs->trans($day_names[($i + $startday) % 7]), '</td>', "\n";
197  }
198  ?>
199  </tr>
200  <?php
201  //print "x ".$thedate." y"; // $thedate = first day of month
202  $firstdate=dol_getdate($thedate);
203  //var_dump($firstdateofweek);
204  $mydate=dol_get_first_day_week(1, $month, $year, true); // mydate = cursor date
205 
206  // Loop on each day of month
207  $stoploop=0; $day=1; $cols=0;
208  while (! $stoploop)
209  {
210  //print_r($mydate);
211  if ($mydate < $firstdate) // At first run
212  {
213  echo "<TR class=\"dpWeek\">";
214  //echo $conf->global->MAIN_START_WEEK.' '.$firstdate["wday"].' '.$startday;
215  $cols=0;
216  for ($i = 0; $i < 7; $i++)
217  {
218  $w = ($i + $startday) % 7;
219  if ($w == $firstdate["wday"])
220  {
221  $mydate = $firstdate;
222  break;
223  }
224  echo "<TD>&nbsp;</TD>";
225  $cols++;
226  }
227  }
228  else
229  {
230  if ($mydate["wday"] == $startday)
231  {
232  echo "<TR class=\"dpWeek\">";
233  $cols=0;
234  }
235  }
236 
237  $dayclass="dpReg";
238  if($thedate==$selDate) $dayclass="dpSelected";
239  elseif($thedate==$today) $dayclass="dpToday";
240 
241  if ($langs->trans("FormatDateShortJavaInput")=="FormatDateShortJavaInput")
242  {
243  print "ERROR FormatDateShortJavaInput not defined for language ".$langs->defaultlang;
244  exit;
245  }
246 
247  // Sur click dans calendrier, appelle fonction dpClickDay
248  echo "<TD class=\"".$dayclass."\"";
249  echo " onMouseOver=\"dpHighlightDay(".$mydate["year"].",parseInt('".dol_print_date($thedate,"%m")."',10),".$mydate["mday"].",tradMonths)\"";
250  echo " onClick=\"dpClickDay(".$mydate["year"].",parseInt('".dol_print_date($thedate,"%m")."',10),".$mydate["mday"].",'".$langs->trans("FormatDateShortJavaInput")."')\"";
251  echo ">".sprintf("%02s",$mydate["mday"])."</TD>";
252  $cols++;
253 
254  if (($mydate["wday"] + 1) % 7 == $startday) echo "</TR>\n";
255 
256  //$thedate=strtotime("tomorrow",$thedate);
257  $day++;
258  $thedate=dol_mktime(12,0,0,$month,$day,$year);
259  if ($thedate == '')
260  {
261  $stoploop=1;
262  }
263  else
264  {
265  $mydate=dol_getdate($thedate);
266  if ($firstdate["month"] != $mydate["month"]) $stoploop=1;
267  }
268  }
269 
270  if ($cols < 7)
271  {
272  for($i=6; $i>=$cols; $i--) echo "<TD>&nbsp;</TD>";
273  echo "</TR>\n";
274  }
275  ?>
276  <tr>
277  <td id="dpExp" class="dpExplanation" colspan="7"><?php
278  if($selDate)
279  {
280  $tempDate=dol_getdate($selDate);
281  print $langs->trans("Month".$selectMonth)." ";
282  print sprintf("%02s",$tempDate["mday"]);
283  print ", ".$selectYear;
284  }
285  else
286  {
287  print "Click a Date";
288  }
289  ?></td>
290  </tr>
291 </table>
292  <?php
293 }//end function
xyzToUnixTimestamp($mysqldate)
Convert date to timestamp.
Definition: datepicker.php:129
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:487
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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).
print
Draft customers invoices.
Definition: index.php:91
dol_getdate($timestamp, $fast=false)
Return an array with locale date info.
displayBox($selectedDate, $month, $year)
Show box.
Definition: datepicker.php:146
type
Definition: viewcat.php:283