[ Index ]
 

Code source de eGroupWare 1.2.106-2

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

title

Body

[fermer]

/calendar/inc/ -> class.boholiday.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare - Holiday                                                     *
   4      * http://www.egroupware.org                                                *
   5      * Written by Mark Peters <skeeter@phpgroupware.org>                        *
   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.boholiday.inc.php 20561 2006-03-15 06:44:09Z ralfbecker $ */
  14  
  15      /**
  16       * Business object for calendar holidays
  17       *
  18       * @package calendar
  19       * @author Mark Peters <skeeter@phpgroupware.org>
  20       * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  21       */
  22      class boholiday
  23      {
  24          var $public_functions = Array(
  25              'add'        => True,
  26              'delete_holiday'    => True,
  27              'delete_locale'    => True,
  28              'accept_holiday'    => True,
  29              
  30              'read_entries'    => True,
  31              'read_entry'    => True,
  32              'add_entry'    => True,
  33              'update_entry'    => True
  34          );
  35  
  36          var $debug = false;
  37          var $base_url = '/index.php';
  38  
  39          var $ui;
  40          var $so;
  41          var $owner;
  42          var $year;
  43  
  44          var $id;
  45          var $total;
  46          var $start;
  47          var $query;
  48          var $sort;
  49  
  50          var $locales = Array();
  51          var $holidays;
  52          var $cached_holidays;
  53  
  54  		function boholiday()
  55          {
  56              $this->so =& CreateObject('calendar.soholiday');
  57  
  58              $this->start  = (int)get_var('start',array('POST','GET'));
  59              $this->query  = get_var('query',array('POST','GET'));
  60              $this->sort   = get_var('sort',array('POST','GET'));
  61              $this->order  = get_var('order',array('POST','GET'));
  62              $this->id     = get_var('id',array('POST','GET'));
  63              $this->year   = get_var('year',array('POST','GET'),date('Y'));
  64              $this->locale = get_var('locale',array('POST','GET'));
  65              if ($this->locale)
  66              {
  67                  $this->locales[] = $this->locale;
  68              }
  69  
  70              if($this->debug)
  71              {
  72                  echo '<-- Locale = '.$this->locales[0].' -->'."\n";
  73              }
  74  
  75              $this->total = $this->so->holiday_total($this->locales[0],$this->query,$this->year);
  76          }
  77  
  78          /* Begin Calendar functions */
  79  		function read_entry($id=0)
  80          {
  81              if($this->debug)
  82              {
  83                  echo "BO : Reading Holiday ID : ".$id."<br>\n";
  84              }
  85  
  86              if(!$id)
  87              {
  88                  if(!$this->id)
  89                  {
  90                      return Array();
  91                  }
  92                  else
  93                  {
  94                      $id = $this->id;
  95                  }
  96              }
  97  
  98              return $this->so->read_holiday($id);
  99          }
 100  
 101  		function delete_holiday($id=0)
 102          {
 103              if(!$id)
 104              {
 105                  if($this->id)
 106                  {
 107                      $id = $this->id;
 108                  }
 109              }
 110  
 111              $this->ui =& CreateObject('calendar.uiholiday');
 112              if($id)
 113              {
 114                  $this->so->delete_holiday($id);
 115                  $this->ui->edit_locale();
 116              }
 117              else
 118              {
 119                  $this->ui->admin();
 120              }
 121          }
 122  
 123  		function delete_locale($locale='')
 124          {
 125              if(!$locale)
 126              {
 127                  if($this->locales[0])
 128                  {
 129                      $locale = $this->locales[0];
 130                  }
 131              }
 132  
 133              if($locale)
 134              {
 135                  $this->so->delete_locale($locale);
 136              }
 137              $this->ui =& CreateObject('calendar.uiholiday');
 138              $this->ui->admin();
 139          }
 140  
 141  		function accept_holiday()
 142          {
 143              $send_back_to = str_replace('submitlocale','holiday_admin',$_SERVER['HTTP_REFERER']);
 144              if(!@$this->locales[0])
 145              {
 146                  Header('Location: '.$send_back_to);
 147              }
 148  
 149              $send_back_to = str_replace('&locale='.$this->locales[0],'',$send_back_to);
 150              $file = './holidays.'.$this->locales[0];
 151              if(!file_exists($file) && count($_POST['name']))
 152              {
 153                  $fp = fopen($file,'w');
 154                  fwrite($fp,"charset\t".$GLOBALS['egw']->translation->charset()."\n");
 155  
 156                  $holidays = array();
 157                  foreach($_POST['name'] as $i => $name)
 158                  {
 159                      $holiday = array(
 160                          'locale' => $_POST['locale'],
 161                          'name'   => str_replace('\\','',$name),
 162                          'day'    => $_POST['day'][$i],
 163                          'month'  => $_POST['month'][$i],
 164                          'occurence' => $_POST['occurence'][$i],
 165                          'dow'    => $_POST['dow'][$i],
 166                          'observance' => $_POST['observance'][$i],
 167                      );
 168                  }
 169                  // sort holidays by year / occurence:
 170                  usort($holidays,'_holiday_cmp');
 171                  
 172                  $last_year = -1;
 173                  foreach($holidays as $holiday)
 174                  {
 175                      $year = $holiday['occurence'] <= 0 ? 0 : $holiday['occurence'];
 176                      if ($year != $last_year)
 177                      {
 178                          echo "\n".($year ? $year : 'regular (year=0)').":\n";
 179                          $last_year = $year;
 180                      }
 181                      fwrite($fp,"$holiday[locale]\t$holiday[name]\t$holiday[day]\t$holiday[month]\t$holiday[occurence]\t$holiday[dow]\t$holiday[observance_rule]\n");
 182                  }
 183                  fclose($fp);
 184              }
 185              Header('Location: '.$send_back_to);
 186          }
 187  
 188  		function get_holiday_list($locale='', $sort='', $order='', $query='', $total='', $year=0)
 189          {
 190              $locale = ($locale?$locale:$this->locales[0]);
 191              $sort = ($sort?$sort:$this->sort);
 192              $order = ($order?$order:$this->order);
 193              $query = ($query?$query:$this->query);
 194              $year = ($$year?$$year:$this->year);
 195              return $this->so->read_holidays($locale,$query,$order,$year);
 196          }
 197  
 198  		function get_locale_list($sort='', $order='', $query='')
 199          {
 200              $sort = ($sort?$sort:$this->sort);
 201              $order = ($order?$order:$this->order);
 202              $query = ($query?$query:$this->query);
 203              return $this->so->get_locale_list($sort,$order,$query);
 204          }
 205  
 206  		function prepare_read_holidays($year=0,$owner=0)
 207          {
 208              $this->year = (isset($year) && $year > 0?$year:$GLOBALS['egw']->common->show_date(time() - $GLOBALS['egw']->datetime->tz_offset,'Y'));
 209              $this->owner = ($owner?$owner:$GLOBALS['egw_info']['user']['account_id']);
 210  
 211              if($this->debug)
 212              {
 213                  echo 'Setting Year to : '.$this->year.'<br>'."\n";
 214              }
 215  
 216              if(@$GLOBALS['egw_info']['user']['preferences']['common']['country'])
 217              {
 218                  $this->locales[] = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
 219              }
 220              elseif(@$GLOBALS['egw_info']['user']['preferences']['calendar']['locale'])
 221              {
 222                  $this->locales[] = $GLOBALS['egw_info']['user']['preferences']['calendar']['locale'];
 223              }
 224              else
 225              {
 226                  $this->locales[] = 'US';
 227              }
 228  
 229              if($this->owner != $GLOBALS['egw_info']['user']['account_id'])
 230              {
 231                  $owner_pref =& CreateObject('phpgwapi.preferences',$owner);
 232                  $owner_prefs = $owner_pref->read_repository();
 233                  if(@$owner_prefs['common']['country'])
 234                  {
 235                      $this->locales[] = $owner_prefs['common']['country'];
 236                  }
 237                  elseif(@$owner_prefs['calendar']['locale'])
 238                  {
 239                      $this->locales[] = $owner_prefs['calendar']['locale'];
 240                  }
 241                  unset($owner_pref);
 242              }
 243  
 244              if($GLOBALS['egw_info']['server']['auto_load_holidays'] == True && $this->locales)
 245              {
 246                  foreach($this->locales as $local)
 247                  {
 248                      $this->auto_load_holidays($local);
 249                  }
 250              }
 251          }
 252  
 253  		function auto_load_holidays($locale)
 254          {
 255              if($this->so->holiday_total($locale) == 0)
 256              {
 257                  @set_time_limit(0);
 258  
 259                  /* get the file that contains the calendar events for your locale */
 260                  /* "http://www.egroupware.org/cal/holidays.US.csv";                 */
 261                  $network =& CreateObject('phpgwapi.network');
 262                  if(isset($GLOBALS['egw_info']['server']['holidays_url_path']) && $GLOBALS['egw_info']['server']['holidays_url_path'] != 'localhost')
 263                  {
 264                      $load_from = $GLOBALS['egw_info']['server']['holidays_url_path'];
 265                  }
 266                  else
 267                  {
 268                      $pos = strpos(' '.$GLOBALS['egw_info']['server']['webserver_url'],$_SERVER['HTTP_HOST']);
 269                      if($pos == 0)
 270                      {
 271                          switch($_SERVER['SERVER_PORT'])
 272                          {
 273                              case 80:
 274                                  $http_protocol = 'http://';
 275                                  break;
 276                              case 443:
 277                                  $http_protocol = 'https://';
 278                                  break;
 279                          }
 280                          $server_host = $http_protocol.$_SERVER['HTTP_HOST'].$GLOBALS['egw_info']['server']['webserver_url'];
 281                      }
 282                      else
 283                      {
 284                          $server_host = $GLOBALS['egw_info']['server']['webserver_url'];
 285                      }
 286                      $load_from = $server_host.'/calendar/egroupware.org';
 287                  }
 288  //                echo 'Loading from: '.$load_from.'/holidays.'.strtoupper($locale).'.csv'."<br>\n";
 289                  if($GLOBALS['egw_info']['server']['holidays_url_path'] == 'localhost')
 290                  {
 291                      $lines = @file(EGW_SERVER_ROOT.'/calendar/egroupware.org/holidays.'.strtoupper($locale).'.csv');
 292                  }
 293                  else
 294                      $lines = $network->gethttpsocketfile($load_from.'/holidays.'.strtoupper($locale).'.csv');
 295  
 296                  if (!$lines)
 297                  {
 298                      return false;
 299                  }
 300                  $charset = split("[\t\n ]+",$lines[0]);        // give a bit flexibility in the syntax AND remove the lineend (\n)
 301                  if (strstr($charset[0],'charset') && $charset[1])
 302                  {
 303                      $lines = $GLOBALS['egw']->translation->convert($lines,$charset[1]);
 304                  }
 305                  $c_lines = count($lines);
 306                  foreach ($lines as $i => $line)
 307                  {
 308  //                    echo 'Line #'.$i.' : '.$lines[$i]."<br>\n";
 309                      $holiday = explode("\t",$line);
 310                      if(count($holiday) == 7)
 311                      {
 312                          $holiday['locale'] = $holiday[0];
 313                          $holiday['name'] = $GLOBALS['egw']->db->db_addslashes($holiday[1]);
 314                          $holiday['mday'] = (int)$holiday[2];
 315                          $holiday['month_num'] = (int)$holiday[3];
 316                          $holiday['occurence'] = (int)$holiday[4];
 317                          $holiday['dow'] = (int)$holiday[5];
 318                          $holiday['observance_rule'] = (int)$holiday[6];
 319                          $holiday['hol_id'] = 0;
 320                          $this->so->save_holiday($holiday);
 321                      }
 322                  }
 323              }
 324          }
 325  
 326  		function save_holiday($holiday)
 327          {
 328              $this->so->save_holiday($holiday);
 329          }
 330  
 331  		function add()
 332          {
 333              if(@$_POST['submit'])
 334              {
 335                  $holiday = $_POST['holiday'];
 336  
 337                  if(empty($holiday['mday']))
 338                  {
 339                      $holiday['mday'] = 0;
 340                  }
 341                  if(!isset($this->bo->locales[0]) || $this->bo->locales[0]=='')
 342                  {
 343                      $this->bo->locales[0] = $holiday['locale'];
 344                  }
 345                  elseif(!isset($holiday['locale']) || $holiday['locale']=='')
 346                  {
 347                      $holiday['locale'] = $this->bo->locales[0];
 348                  }
 349                  if(!isset($holiday['hol_id']))
 350                  {
 351                      $holiday['hol_id'] = $this->bo->id;
 352                  }
 353  
 354                  // some input validation
 355  
 356                  if (!$holiday['mday'] == !$holiday['occurence'])
 357                  {
 358                      $errors[] = lang('You need to set either a day or a occurence !!!');
 359                  }
 360                  if($holiday['year'] && $holiday['occurence'])
 361                  {
 362                      $errors[] = lang('You can only set a year or a occurence !!!');
 363                  }
 364                  else
 365                  {
 366                      $holiday['occurence'] = (int)($holiday['occurence'] ? $holiday['occurence'] : $holiday['year']);
 367                      unset($holiday['year']);
 368                  }
 369  
 370      // Still need to put some validation in here.....
 371  
 372                  $this->ui =& CreateObject('calendar.uiholiday');
 373  
 374                  if (is_array($errors))
 375                  {
 376                      $holiday['month'] = $holiday['month_num'];
 377                      $holiday['day']   = $holiday['mday'];
 378                      $this->ui->edit_holiday($errors,$holiday);
 379                  }
 380                  else
 381                  {
 382                      $this->so->save_holiday($holiday);
 383                      $this->ui->edit_locale($holiday['locale']);
 384                  }
 385              }
 386          }
 387  
 388  		function sort_holidays_by_date($holidays)
 389          {
 390              $c_holidays = count($holidays);
 391              for($outer_loop=0;$outer_loop<($c_holidays - 1);$outer_loop++)
 392              {
 393                  for($inner_loop=$outer_loop;$inner_loop<$c_holidays;$inner_loop++)
 394                  {
 395                      if($holidays[$outer_loop]['date'] > $holidays[$inner_loop]['date'])
 396                      {
 397                          $temp = $holidays[$inner_loop];
 398                          $holidays[$inner_loop] = $holidays[$outer_loop];
 399                          $holidays[$outer_loop] = $temp;
 400                      }
 401                  }
 402              }
 403              return $holidays;
 404          }
 405  
 406  		function set_holidays_to_date($holidays)
 407          {
 408              $new_holidays = Array();
 409              for($i=0;$i<count($holidays);$i++)
 410              {
 411  //    echo "Setting Holidays Date : ".date('Ymd',$holidays[$i]['date'])."<br>\n";
 412                  $new_holidays[date('Ymd',$holidays[$i]['date'])][] = $holidays[$i];
 413              }
 414              return $new_holidays;
 415          }
 416  
 417  		function read_holiday()
 418          {
 419              if(isset($this->cached_holidays))
 420              {
 421                  return $this->cached_holidays;
 422              }
 423              $holidays = $this->so->read_holidays($this->locales,'','',$this->year);
 424  
 425              if(count($holidays) == 0)
 426              {
 427                  return $holidays;
 428              }
 429  
 430              $temp_locale = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
 431              foreach($holidays as $i => $holiday)
 432              {
 433                  if($i == 0 || $holidays[$i]['locale'] != $holidays[$i - 1]['locale'])
 434                  {
 435                      if(is_object($holidaycalc))
 436                      {
 437                          unset($holidaycalc);
 438                      }
 439                      $GLOBALS['egw_info']['user']['preferences']['common']['country'] = $holidays[$i]['locale'];
 440                      $holidaycalc =& CreateObject('calendar.holidaycalc');
 441                  }
 442                  $holidays[$i]['date'] = $holidaycalc->calculate_date($holiday, $holidays, $this->year);
 443              }
 444              unset($holidaycalc);
 445              $this->holidays = $this->sort_holidays_by_date($holidays);
 446              $this->cached_holidays = $this->set_holidays_to_date($this->holidays);
 447              $GLOBALS['egw_info']['user']['preferences']['common']['country'] = $temp_locale;
 448              return $this->cached_holidays;
 449          }
 450          /* End Calendar functions */
 451  
 452  		function check_admin()
 453          {
 454              if(!@$GLOBALS['egw_info']['user']['apps']['admin'])
 455              {
 456                  Header('Location: ' . $GLOBALS['egw']->link('/index.php'));
 457              }
 458          }
 459  
 460  		function rule_string($holiday)
 461          {
 462              if (!is_array($holiday))
 463              {
 464                  return false;
 465              }
 466              $sbox =& CreateObject('phpgwapi.sbox');
 467              $month = $holiday['month'] ? lang($sbox->monthnames[$holiday['month']]) : '';
 468              unset($sbox);
 469  
 470              if (!$holiday['day'])
 471              {
 472                  $occ = $holiday['occurence'] == 99 ? lang('last') : $holiday['occurence'].'.';
 473  
 474                  $dow_str = Array(lang('Sun'),lang('Mon'),lang('Tue'),lang('Wed'),lang('Thu'),lang('Fri'),lang('Sat'));
 475                  $dow = $dow_str[$holiday['dow']];
 476                  
 477                  $str = lang('%1 %2 in %3',$occ,$dow,$month);
 478              }
 479              else
 480              {
 481                  $str = $GLOBALS['egw']->common->dateformatorder($holiday['occurence']>1900?$holiday['occurence']:'',$month,$holiday[day]);
 482              }
 483              if ($holiday['observance_rule'])
 484              {
 485                  $str .= ' ('.lang('Observance Rule').')';
 486              }
 487              return $str;
 488          }
 489      }
 490  
 491  	function _holiday_cmp($a,$b)
 492      {
 493          if (($year_diff = ($a['occurence'] <= 0 ? 0 : $a['occurence']) - ($b['occurence'] <= 0 ? 0 : $b['occurence'])))
 494          {
 495              return $year_diff;
 496          }
 497          return $a['month'] - $b['month'] ? $a['month'] - $b['month'] : $a['day'] - $b['day'];
 498      }


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