[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/etemplate/inc/ -> class.date_widget.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare - eTemplate Extension - Date Widget                           *
   4      * http://www.egroupware.org                                                *
   5      * Written by Ralf Becker <RalfBecker@outdoor-training.de>                  *
   6      * --------------------------------------------                             *
   7      *  This program is free software; you can redistribute it and/or modify it *
   8      *  under the terms of the GNU General Public License as published by the   *
   9      *  Free Software Foundation; either version 2 of the License, or (at your  *
  10      *  option) any later version.                                              *
  11      \**************************************************************************/
  12  
  13      /* $Id: class.date_widget.inc.php 22216 2006-08-12 06:15:32Z ralfbecker $ */
  14  
  15      /**
  16       * eTemplate extension to input or display date and/or time values
  17       *
  18       * Contains the following widgets: Date, Date+Time, Time, Hour, Duration
  19       *
  20       * Supported attributes: format[,options]
  21       *  format: ''=timestamp, or eg. 'Y-m-d H:i' for 2002-12-31 23:59
  22       *  options: &1 = year is int-input not selectbox, &2 = show a [Today] button, (html-UI always uses jscal and dont care for &1+&2)
  23       *           &4 = 1min steps for time (default is 5min, with fallback to 1min if value is not in 5min-steps),
  24       *           &8 = dont show time for readonly and type date-time if time is 0:00, 
  25       *           &16 = prefix r/o display with dow
  26       *
  27       * This widget is independent of the UI as it only uses etemplate-widgets and has therefor no render-function.
  28       * Uses the adodb datelibary to overcome the windows-limitation to not allow dates before 1970
  29       *
  30       * @package etemplate
  31       * @subpackage extensions
  32       * @author RalfBecker-AT-outdoor-training.de
  33       * @license GPL
  34       */
  35      class date_widget
  36      {
  37          /** 
  38           * exported methods of this class
  39           * @var array
  40           */
  41          var $public_functions = array(
  42              'pre_process' => True,
  43              'post_process' => True
  44          );
  45          /**
  46           * availible extensions and there names for the editor
  47           * @var array
  48           */
  49          var $human_name = array(
  50              'date'      => 'Date',        // just a date, no time
  51              'date-time' => 'Date+Time',    // date + time
  52              'date-timeonly' => 'Time',    // time
  53              'date-houronly' => 'Hour',    // hour
  54              'date-duration' => 'Duration', // duration
  55          );
  56          var $dateformat;    // eg. Y-m-d, d-M-Y
  57          var $timeformat;    // 12 or 24
  58  
  59          /**
  60           * Constructor of the extension
  61           *
  62           * @param string $ui '' for html
  63           */
  64  		function date_widget($ui)
  65          {
  66              if ($ui == 'html')
  67              {
  68                  if (!is_object($GLOBALS['egw']->jscalendar))
  69                  {
  70                      $GLOBALS['egw']->jscalendar =& CreateObject('phpgwapi.jscalendar');
  71                  }
  72                  $this->jscal =& $GLOBALS['egw']->jscalendar;
  73              }
  74              $this->timeformat = $GLOBALS['egw_info']['user']['preferences']['common']['timeformat'];
  75              $this->dateformat = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat'];
  76          }
  77  
  78          /**
  79           * pre-processing of the extension
  80           *
  81           * This function is called before the extension gets rendered
  82           *
  83           * @param string $name form-name of the control
  84           * @param mixed &$value value / existing content, can be modified
  85           * @param array &$cell array with the widget, can be modified for ui-independent widgets 
  86           * @param array &$readonlys names of widgets as key, to be made readonly
  87           * @param mixed &$extension_data data the extension can store persisten between pre- and post-process
  88           * @param object &$tmpl reference to the template we belong too
  89           * @return boolean true if extra label is allowed, false otherwise
  90           */
  91  		function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl)
  92          {
  93              $type = $cell['type'];
  94              if ($type == 'date-duration')
  95              {
  96                  return $this->pre_process_duration($name,$value,$cell,$readonlys,$extension_data,$tmpl);
  97              }
  98              list($data_format,$options,$options2) = explode(',',$cell['size']);
  99              if ($type == 'date-houronly' && empty($data_format)) $data_format = 'H';
 100              
 101              $readonly = $cell['readonly'] || $readonlys;
 102  
 103              if (!$readonly)    // dont set extension-data on readonly, it's not needed and can conflict with other widgets
 104              {
 105                  $extension_data = array(
 106                      'type'            => $type,
 107                      'data_format'    => $data_format,
 108                  );
 109              }
 110              if (!$value)
 111              {
 112                  $value = array(
 113                      'Y' => '',
 114                      'm' => '',
 115                      'd' => '',
 116                      'H' => '',
 117                      'i' => '',
 118                  );
 119              }
 120              elseif ($data_format != '')
 121              {
 122                  $date = split('[- /.:,]',$value);
 123                  //echo "date=<pre>"; print_r($date); echo "</pre>";
 124                  $mdy  = split('[- /.:,]',$data_format);
 125                  $value = array();
 126                  foreach ($date as $n => $dat)
 127                  {
 128                      switch($mdy[$n])
 129                      {
 130                          case 'Y': $value['Y'] = $dat; break;
 131                          case 'm': $value['m'] = $dat; break;
 132                          case 'd': $value['d'] = $dat; break;
 133                          case 'H': $value['H'] = $dat; break;
 134                          case 'i': $value['i'] = $dat; break;
 135                      }
 136                  }
 137              }
 138              else
 139              {
 140                  // for the timeformats we use only seconds, no timezone conversation between server-time and UTC
 141                  if (substr($type,-4) == 'only') $value -= adodb_date('Z',0);
 142  
 143                  $value = array(
 144                      'Y' => adodb_date('Y',$value),
 145                      'm' => adodb_date('m',$value),
 146                      'M' => substr(lang(adodb_date('F',$value)),0,3),
 147                      'd' => adodb_date('d',$value),
 148                      'H' => adodb_date('H',$value),
 149                      'i' => adodb_date('i',$value)
 150                  );
 151              }
 152              $time_0h0 = !(int)$value['H'] && !(int)$value['i'];
 153  
 154              $timeformat = array(3 => 'H', 4 => 'i');
 155              if ($this->timeformat == '12' && $readonly && $value['H'] !== '')
 156              {
 157                  $value['a'] = $value['H'] < 12 ? 'am' : 'pm';
 158                  $value['H'] = $value['H'] % 12 ?  $value['H'] % 12 : 12;    // no leading 0 and 0h => 12am
 159                  $timeformat += array(5 => 'a');
 160              }
 161              $format = split('[/.-]',$this->dateformat);
 162              
 163              // no time also if $options&8 and readonly and time=0h0
 164              if ($type != 'date' && !($readonly && ($options & 8) && $time_0h0))
 165              {
 166                  $format += $timeformat;
 167              }
 168              if ($readonly)    // is readonly
 169              {
 170                  if ($value['H'] === '') unset($value['a']);    // no am/pm if no hour set
 171  
 172                  $sep = array(
 173                      1 => $this->dateformat[1],
 174                      2 => $this->dateformat[1],
 175                      3 => ' ',
 176                      4 => ':'
 177                  );
 178                  for ($str='',$n = substr($type,-4) == 'only' ? 3 : 0; $n < count($format); ++$n)
 179                  {
 180                      if ($value[$format[$n]])
 181                      {
 182                          if (!$n && $options & 16 )
 183                          {
 184                              $str = lang(adodb_date('l',adodb_mktime(12,0,0,$value['m'],$value['d'],$value['Y']))).' ';
 185                          }
 186                          $str .= ($str != '' ? $sep[$n] : '') . $value[$format[$n]];
 187                      }
 188                      if ($type == 'date-houronly') ++$n;    // no minutes
 189                  }
 190                  $value = $str;
 191                  $cell['type'] = 'label';
 192                  if (!$cell['no_lang'])
 193                  {
 194                      $cell['no_lang'] = True;
 195                      $cell['label'] = strlen($cell['label']) > 1 ? lang($cell['label']) : $cell['label'];
 196                  }
 197                  unset($cell['size']);
 198                  return True;
 199              }
 200              if ($cell['needed'])
 201              {
 202                  $GLOBALS['egw_info']['etemplate']['to_process'][$name] = array(
 203                      'type' => 'ext-'.$type,
 204                      'needed' => $cell['needed'],
 205                  );
 206              }
 207              $tpl =& new etemplate;
 208              $tpl->init('*** generated fields for date','','',0,'',0,0);    // make an empty template
 209              // keep the editor away from the generated tmpls
 210              $tpl->no_onclick = true;
 211  
 212              $types = array(
 213                  'Y' => ($options&1 ? 'int' : 'select-year'),    // if options&1 set, show an int-field
 214                  'm' => 'select-month',
 215                  'M' => 'select-month',
 216                  'd' => 'select-day',
 217                  'H' => 'select-hour',
 218                  'i' => 'select-number'
 219              );
 220              $opts = array(
 221                  'H' => $this->timeformat == '12' ? ',0,12' : ',0,23,01',
 222                  'i' => $value['i'] % 5 || $options & 4 ? ',0,59,01' : ',0,59,05' // 5min steps, if ok with value
 223              );
 224              $help = array(
 225                  'Y' => 'Year',
 226                  'm' => 'Month',
 227                  'M' => 'Month',
 228                  'd' => 'Day',
 229                  'H' => 'Hour',
 230                  'i' => 'Minute'
 231              );
 232              $row = array();
 233              for ($i=0,$n= substr($type,-4) == 'only' ? 3 : 0; $n < ($type == 'date' ? 3 : 5); ++$n,++$i)
 234              {
 235                  $dcell = $tpl->empty_cell();
 236                  if ($cell['tabindex']) $dcell['tabindex'] = $cell['tabindex'];
 237                  if (!$i && $cell['accesskey']) $dcell['accesskey'] = $cell['accesskey'];
 238                  
 239                  // test if we can use jsCalendar
 240                  if ($n == 0 && $this->jscal && $tmpl->java_script())
 241                  {
 242                      $dcell['type'] = 'html';
 243                      $dcell['name'] = 'str';
 244                      $value['str'] = $this->jscal->input($name.'[str]',False,$value['Y'],$value['m'],$value['d'],lang($cell['help']));
 245                      $n = 2;                // no other fields
 246                      $options &= ~2;        // no set-today button
 247                  }
 248                  else
 249                  {
 250                      $dcell['type'] = $types[$format[$n]];
 251                      $dcell['size'] = $opts[$format[$n]];
 252                      $dcell['name'] = $format[$n];
 253                      $dcell['help'] = lang($help[$format[$n]]).': '.lang($cell['help']);    // note: no lang on help, already done
 254                  }
 255                  if ($n == 4)
 256                  {
 257                      $dcell['label'] = ':';    // put a : between hour and minute
 258                  }
 259                  $dcell['no_lang'] = 2;
 260                  $row[$tpl->num2chrs($i)] = &$dcell;
 261                  unset($dcell);
 262                  
 263                  if ($n == 2 && ($options & 2))    // Today button
 264                  {
 265                      $dcell = $tpl->empty_cell();
 266                      if ($cell['tabindex']) $dcell['tabindex'] = $cell['tabindex'];
 267                      $dcell['name'] = 'today';
 268                      $dcell['label'] = 'Today';
 269                      $dcell['help'] = 'sets today as date';
 270                      $dcell['no_lang'] = True;
 271                      if (($js = $tmpl->java_script()))
 272                      {
 273                          $dcell['needed'] = True;    // to get a button
 274                          $dcell['onchange'] = "this.form.elements['$name"."[Y]'].value='".adodb_date('Y')."'; this.form.elements['$name"."[m]'].value='".adodb_date('n')."';this.form.elements['$name"."[d]'].value='".(0+adodb_date('d'))."'; return false;";
 275                      }
 276                      $dcell['type'] = $js ? 'button' : 'checkbox';
 277                      $row[$tpl->num2chrs(++$i)] = &$dcell;
 278                      unset($dcell);
 279                  }
 280                  if ($n == 2 && $type == 'date-time')    // insert some space between date+time
 281                  {
 282                      $dcell = $tpl->empty_cell();
 283                      $dcell['type'] = 'html';
 284                      $dcell['name'] = 'space';
 285                      $value['space'] = ' &nbsp; &nbsp; ';
 286                      $row[$tpl->num2chrs(++$i)] = &$dcell;
 287                      unset($dcell);
 288                  }
 289                  if ($type == 'date-houronly') $n++;    // no minutes
 290              }
 291              $tpl->data[0] = array();
 292              $tpl->data[1] = &$row;
 293              $tpl->set_rows_cols();
 294              $tpl->size = ',,,,0';
 295  
 296              $cell['size'] = $cell['name'];
 297              $cell['type'] = 'template';
 298              $cell['name'] = $tpl->name;
 299              $cell['obj'] = &$tpl;
 300  
 301              return True;    // extra Label is ok
 302          }
 303  
 304          /**
 305           * pre-processing of the duration extension
 306           *
 307           * Options contain $data_format,$input_format,$hours_per_day,$empty_not_0,$short_labels
 308           *  1. data_format: d = days, h = hours, m = minutes, default minutes
 309           *    2. input_format: d = days, h = hours, m = minutes,  default hours+days (selectbox), optional % = allow to enter a percent value (no conversation)
 310           *    3. hours_per_day: default 8 (workday)
 311           *  4. should the widget differ between 0 and empty, which get then returned as NULL
 312           *  5. short_labels use d/h/m instead of day/hour/minute
 313           *
 314           * @param string $name form-name of the control
 315           * @param mixed &$value value / existing content, can be modified
 316           * @param array &$cell array with the widget, can be modified for ui-independent widgets 
 317           * @param array &$readonlys names of widgets as key, to be made readonly
 318           * @param mixed &$extension_data data the extension can store persisten between pre- and post-process
 319           * @param object &$tmpl reference to the template we belong too
 320           * @return boolean true if extra label is allowed, false otherwise
 321           */
 322  		function pre_process_duration($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl)
 323          {
 324              //echo "<p>pre_process_duration($name,$value,...) cell[size]='$cell[size]'</p>\n";
 325              $readonly = $readonlys || $cell['readonly'];
 326              list($data_format,$input_format,$hours_per_day,$empty_not_0,$short_labels) = explode(',',$cell['size']);
 327              if (!$hours_per_day) $hours_per_day = 8; // workday is 8 hours
 328              if (($percent_allowed = strstr($input_format,'%') !== false))
 329              {
 330                  $input_format = str_replace('%','',$input_format);
 331              }
 332              if (!in_array($input_format,array('d','h','dh','m','hm','dhm'))) $input_format = 'dh'; // hours + days
 333              
 334              if (!$readonly)    // dont set extension-data on readonly, it's not needed and can conflict with other widgets
 335              {
 336                  $extension_data = array(
 337                      'type'            => $cell['type'],
 338                      'data_format'    => $data_format,
 339                      'unit'          => ($unit = $input_format == 'd' ? 'd' : 'h'),
 340                      'input_format'  => $input_format,
 341                      'hours_per_day' => $hours_per_day,
 342                      'percent_allowed'=> $percent_allowed,
 343                      'empty_not_0'   => $empty_not_0,
 344                  );
 345              }
 346              if ($value)
 347              {
 348                  switch($data_format)
 349                  {
 350                      case 'd':
 351                          $value *= $hours_per_day;
 352                          // fall-through
 353                      case 'h': case 'H':
 354                          $value *= 60;
 355                          break;
 356                  }
 357              }            
 358              $cell['type'] = 'text';
 359              $cell['size'] = '4,,/^-?[0-9]*[,.]?[0-9]*'.($percent_allowed ? '%?' : '').'$/';
 360              $cell_name = $cell['name'];
 361              $cell['name'] .= '[value]';
 362              
 363              if (strstr($input_format,'m') && $value && $value < 60)
 364              {
 365                  $unit = 'm';
 366              }
 367              elseif (strstr($input_format,'d') && $value >= 60*$hours_per_day)
 368              {
 369                  $unit = 'd';
 370              }
 371              $value = $empty_not_0 && (string) $value === '' || !$empty_not_0 && !$value ? '' : 
 372                  ($unit == 'm' ? (int) $value : round($value / 60 / ($unit == 'd' ? $hours_per_day : 1),3));
 373  
 374              if (!$readonly && strlen($input_format) > 1) // selectbox to switch between hours and days
 375              {
 376                  $value = array(
 377                      'value' => $value,
 378                      'unit'  => $unit,
 379                  );
 380                  $tpl =& new etemplate;
 381                  $tpl->init('*** generated fields for duration','','',0,'',0,0);    // make an empty template
 382                  // keep the editor away from the generated tmpls
 383                  $tpl->no_onclick = true;
 384                  
 385                  $selbox =& $tpl->empty_cell('select',$cell_name.'[unit]');
 386                  if (strstr($input_format,'m')) $selbox['sel_options']['m'] = $short_labels ? 'm' : 'minutes';
 387                  if (strstr($input_format,'h')) $selbox['sel_options']['h'] = $short_labels ? 'h' : 'hours';
 388                  if (strstr($input_format,'d')) $selbox['sel_options']['d'] = $short_labels ? 'd' : 'days';
 389                  if ($cell['tabindex']) $selbox['tabindex'] = $cell['tabindex'];
 390                  
 391                  $tpl->data[0] = array();
 392                  $tpl->data[1] =array(
 393                      'A' => $cell,
 394                      'B' => $selbox,
 395                  );
 396                  $tpl->set_rows_cols();
 397                  $tpl->size = ',,,,0';
 398      
 399                  unset($cell['size']);
 400                  $cell['type'] = 'template';
 401                  $cell['name'] = $tpl->name;
 402                  unset($cell['label']);
 403                  $cell['obj'] = &$tpl;
 404              }
 405              elseif (!$readonly || $value)
 406              {
 407                  $cell['no_lang'] = 2;
 408                  $cell['label'] .= ($cell['label'] ? ' ' : '') . '%s ';
 409                  switch($unit)
 410                  {
 411                      case 'm': $cell['label'] .= $short_labels ? 'm' : lang('minutes'); break;
 412                      case 'h': $cell['label'] .= $short_labels ? 'h' : lang('hours'); break;
 413                      case 'd': $cell['label'] .= $short_labels ? 'd' : lang('days'); break;
 414                  }
 415              }
 416              return True;    // extra Label is ok
 417          }
 418  
 419          /**
 420           * postprocessing method, called after the submission of the form
 421           *
 422           * It has to copy the allowed/valid data from $value_in to $value, otherwise the widget
 423           * will return no data (if it has a preprocessing method). The framework insures that
 424           * the post-processing of all contained widget has been done before.
 425           *
 426           * Only used by select-dow so far
 427           *
 428           * @param string $name form-name of the widget
 429           * @param mixed &$value the extension returns here it's input, if there's any
 430           * @param mixed &$extension_data persistent storage between calls or pre- and post-process
 431           * @param boolean &$loop can be set to true to request a re-submision of the form/dialog
 432           * @param object &$tmpl the eTemplate the widget belongs too
 433           * @param mixed &value_in the posted values (already striped of magic-quotes)
 434           * @return boolean true if $value has valid content, on false no content will be returned!
 435           */
 436  		function post_process($name,&$value,&$extension_data,&$loop,&$tmpl,$value_in)
 437          {
 438              //echo "<p>date_widget::post_process('$name','$extension_data[type]','$extension_data[data_format]') value="; print_r($value); echo ", value_in="; print_r($value_in); echo "</p>\n";
 439              if (!isset($value) && !isset($value_in))
 440              {
 441                  return False;
 442              }
 443              if ($extension_data['type'] == 'date-duration')
 444              {
 445                  if (is_array($value))    // template with selectbox
 446                  {
 447                      $unit = $value['unit'];
 448                      $value = $value['value'];
 449                  }
 450                  elseif (!preg_match('/^-?[0-9]*[,.]?[0-9]*'.($extension_data['percent_allowed'] ? '%?' : '').'$/',$value_in))
 451                  {
 452                      $GLOBALS['egw_info']['etemplate']['validation_errors'][$name] = lang("'%1' is not a valid floatingpoint number !!!",$value_in);
 453                      return false;
 454                  }
 455                  else
 456                  {
 457                      $value = $value_in;
 458                      $unit = $extension_data['unit'];
 459                  }
 460                  if ($extension_data['percent_allowed'] && substr($value,-1) == '%')
 461                  {
 462                      return true;
 463                  }
 464                  if ($value === '' && $extension_data['empty_not_0'])    // we differ between 0 and empty, which get returned as null
 465                  {
 466                      $value = null;
 467                      return true;
 468                  }
 469                  $value = (int) round(str_replace(',','.',$value) * ($unit == 'm' ? 1 : (60 * ($unit == 'd' ? $extension_data['hours_per_day'] : 1))));
 470  
 471                  switch($extension_data['data_format'])
 472                  {
 473                      case 'd':
 474                          $value /= (float) $extension_data['hours_per_day'];
 475                          // fall-through
 476                      case 'h': case 'H':
 477                          $value /= 60.0;
 478                          break;
 479                  }
 480                  return true;
 481              }
 482              $no_date = substr($extension_data['type'],-4) == 'only';
 483  
 484              if ($value['today'])
 485              {
 486                  $set = array('Y','m','d');
 487                  foreach($set as $d)
 488                  {
 489                      $value[$d] = adodb_date($d);
 490                  }
 491              }
 492              if (isset($value_in['str']) && !empty($value_in['str']))
 493              {
 494                  if (!is_array($value))
 495                  {
 496                      $value = array();
 497                  }
 498                  $value += $this->jscal->input2date($value_in['str'],False,'d','m','Y');
 499              }
 500              if ($value['d'] || $no_date && 
 501                  (isset($value['H']) && $value['H'] !== '' || isset($value['i']) && $value['i'] !== ''))
 502              {
 503                  if ($value['d'])
 504                  {
 505                      if (!$value['m'])
 506                      {
 507                          $value['m'] = adodb_date('m');
 508                      }
 509                      if (!$value['Y'])
 510                      {
 511                          $value['Y'] = adodb_date('Y');
 512                      }
 513                      elseif ($value['Y'] < 100)
 514                      {
 515                          $value['Y'] += $value['Y'] < 30 ? 2000 : 1900;
 516                      }
 517                  }
 518                  else    // for the timeonly field
 519                  {
 520                      $value['d'] = $value['m'] = 1;
 521                      $value['Y'] = 1970;
 522                  }
 523                  // checking the date is a correct one
 524                  if (!checkdate($value['m'],$value['d'],$value['Y']))
 525                  {
 526                      $GLOBALS['egw_info']['etemplate']['validation_errors'][$name] .= lang("'%1' is not a valid date !!!",
 527                          $GLOBALS['egw']->common->dateformatorder($value['Y'],$value['m'],$value['d'],true));
 528                  }
 529                  $data_format = $extension_data['data_format'];
 530                  if (empty($data_format))
 531                  {
 532                      // for time or hour format we use just seconds (and no timezone correction between server-time and UTC)
 533                      $value = $no_date ? 3600 * (int) $value['H'] + 60 * (int) $value['i'] :
 534                          adodb_mktime((int) $value['H'],(int) $value['i'],0,$value['m'],$value['d'],$value['Y']);
 535                  }
 536                  else
 537                  {
 538                      for ($n = 0,$str = ''; $n < strlen($data_format); ++$n)
 539                      {
 540                          if (strstr('YmdHi',$c = $data_format[$n]))
 541                          {
 542                              $str .= sprintf($c=='Y'?'%04d':'%02d',$value[$c]);
 543                          }
 544                          else
 545                          {
 546                              $str .= $c;
 547                          }
 548                      }
 549                      $value = $str;
 550                  }
 551              }
 552              else
 553              {
 554                  $value = '';
 555              }
 556              return True;
 557          }
 558      }


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7