egroupware_official/calendar/inc/class.boholiday.inc.php

509 lines
13 KiB
PHP
Raw Normal View History

<?php
2016-05-01 19:47:59 +02:00
/**************************************************************************\
* eGroupWare - Holiday *
* http://www.egroupware.org *
* Written by Mark Peters <skeeter@phpgroupware.org> *
* -------------------------------------------- *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
\**************************************************************************/
use EGroupware\Api;
use EGroupware\Api\Framework;
/* $Id$ */
/**
* Business object for calendar holidays
*
* @package calendar
* @author Mark Peters <skeeter@phpgroupware.org>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
class boholiday
{
var $public_functions = Array(
'add' => True,
'delete_holiday' => True,
'delete_locale' => True,
'accept_holiday' => True,
'read_entries' => True,
'read_entry' => True,
'add_entry' => True,
'update_entry' => True
);
var $debug = false;
var $base_url = '/index.php';
var $ui;
var $so;
var $owner;
var $year;
var $id;
var $total;
var $start;
var $query;
var $sort;
var $locales = Array();
var $holidays;
var $cached_holidays;
function boholiday()
{
require_once(__DIR__.'/class.soholiday.inc.php');
$this->so = new soholiday();
2016-05-01 19:47:59 +02:00
$this->start = (int)$_REQUEST['start'];
$this->query = $_REQUEST['query'];
$this->sort = $_REQUEST['sort'];
$this->order = $_REQUEST['order'];
$this->id = $_REQUEST['id'];
$this->year = (int)(!empty($_REQUEST['year']) ? $_REQUEST['year'] : date('Y'));
$this->locale = $_REQUEST['locale'];
2016-05-01 19:47:59 +02:00
if ($this->locale)
{
$this->locales[] = $this->locale;
}
2016-05-01 19:47:59 +02:00
if($this->debug)
{
echo '<-- Locale = '.$this->locales[0].' -->'."\n";
}
2016-05-01 19:47:59 +02:00
$this->total = $this->so->holiday_total($this->locales[0],$this->query,$this->year);
}
2016-05-01 19:47:59 +02:00
/* Begin Calendar functions */
function read_entry($id=0)
{
if($this->debug)
{
echo "BO : Reading Holiday ID : ".$id."<br>\n";
}
2016-05-01 19:47:59 +02:00
if(!$id)
{
2016-05-01 19:47:59 +02:00
if(!$this->id)
{
2016-05-01 19:47:59 +02:00
return Array();
}
2016-05-01 19:47:59 +02:00
else
{
2016-05-01 19:47:59 +02:00
$id = $this->id;
}
}
2016-05-01 19:47:59 +02:00
return $this->so->read_holiday($id);
}
2016-05-01 19:47:59 +02:00
function delete_holiday($id=0)
{
if(!$id)
{
if($this->id)
{
2016-05-01 19:47:59 +02:00
$id = $this->id;
}
}
2016-05-01 19:47:59 +02:00
$this->ui =& CreateObject('calendar.uiholiday');
if($id)
{
2016-05-01 19:47:59 +02:00
$this->so->delete_holiday($id);
$this->ui->edit_locale();
}
else
{
$this->ui->admin();
}
}
2016-05-01 19:47:59 +02:00
function delete_locale($locale='')
{
if(!$locale)
{
if($this->locales[0])
{
2016-05-01 19:47:59 +02:00
$locale = $this->locales[0];
}
}
2016-05-01 19:47:59 +02:00
if($locale)
{
2016-05-01 19:47:59 +02:00
$this->so->delete_locale($locale);
}
$this->ui =& CreateObject('calendar.uiholiday');
$this->ui->admin();
}
2016-05-01 19:47:59 +02:00
function accept_holiday()
{
$send_back_to = str_replace('submitlocale','holiday_admin',$_SERVER['HTTP_REFERER']);
if(!@$this->locales[0])
{
Header('Location: '.$send_back_to);
}
2016-05-01 19:47:59 +02:00
$send_back_to = str_replace('&locale='.$this->locales[0],'',$send_back_to);
$file = './holidays.'.$this->locales[0];
if(!file_exists($file) && count($_POST['name']))
{
2016-05-01 19:47:59 +02:00
$fp = fopen($file,'w');
fwrite($fp,"charset\t".Api\Translation::charset()."\n");
$holidays = array();
foreach($_POST['name'] as $i => $name)
{
2016-05-01 19:47:59 +02:00
$holiday = array(
'locale' => $_POST['locale'],
'name' => str_replace('\\','',$name),
'day' => $_POST['day'][$i],
'month' => $_POST['month'][$i],
'occurence' => $_POST['occurence'][$i],
'dow' => $_POST['dow'][$i],
'observance' => $_POST['observance'][$i],
);
}
2016-05-01 19:47:59 +02:00
// sort holidays by year / occurence:
usort($holidays,'_holiday_cmp');
2016-05-01 19:47:59 +02:00
$last_year = -1;
foreach($holidays as $holiday)
{
2016-05-01 19:47:59 +02:00
$year = $holiday['occurence'] <= 0 ? 0 : $holiday['occurence'];
if ($year != $last_year)
{
2016-05-01 19:47:59 +02:00
echo "\n".($year ? $year : 'regular (year=0)').":\n";
$last_year = $year;
}
2016-05-01 19:47:59 +02:00
fwrite($fp,"$holiday[locale]\t$holiday[name]\t$holiday[day]\t$holiday[month]\t$holiday[occurence]\t$holiday[dow]\t$holiday[observance_rule]\n");
}
2016-05-01 19:47:59 +02:00
fclose($fp);
}
2016-05-01 19:47:59 +02:00
Header('Location: '.$send_back_to);
}
2016-05-01 19:47:59 +02:00
function get_holiday_list($locale='', $sort='', $order='', $query='', $total='', $year=0)
{
$locale = ($locale?$locale:$this->locales[0]);
$sort = ($sort?$sort:$this->sort);
$order = ($order?$order:$this->order);
$query = ($query?$query:$this->query);
$year = ($$year?$$year:$this->year);
return $this->so->read_holidays($locale,$query,$order,$year);
}
function get_locale_list($sort='', $order='', $query='')
{
$sort = ($sort?$sort:$this->sort);
$order = ($order?$order:$this->order);
$query = ($query?$query:$this->query);
return $this->so->get_locale_list($sort,$order,$query);
}
function prepare_read_holidays($year=0,$owner=0)
{
$this->year = (isset($year) && $year > 0?$year: Api\DateTime::to(time() - $GLOBALS['egw']->datetime->tz_offset,'Y'));
$this->owner = ($owner?$owner:$GLOBALS['egw_info']['user']['account_id']);
if($this->debug)
{
2016-05-01 19:47:59 +02:00
echo 'Setting Year to : '.$this->year.'<br>'."\n";
}
2016-05-01 19:47:59 +02:00
if(@$GLOBALS['egw_info']['user']['preferences']['common']['country'])
{
2016-05-01 19:47:59 +02:00
$this->locales[] = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
}
2016-05-01 19:47:59 +02:00
elseif(@$GLOBALS['egw_info']['user']['preferences']['calendar']['locale'])
{
$this->locales[] = $GLOBALS['egw_info']['user']['preferences']['calendar']['locale'];
}
else
{
2016-05-01 19:47:59 +02:00
$this->locales[] = 'US';
}
2016-05-01 19:47:59 +02:00
if($this->owner != $GLOBALS['egw_info']['user']['account_id'])
{
$owner_pref = new Api\Preferences($owner);
$owner_prefs = $owner_pref->read_repository();
if(@$owner_prefs['common']['country'])
{
2016-05-01 19:47:59 +02:00
$this->locales[] = $owner_prefs['common']['country'];
}
2016-05-01 19:47:59 +02:00
elseif(@$owner_prefs['calendar']['locale'])
{
2016-05-01 19:47:59 +02:00
$this->locales[] = $owner_prefs['calendar']['locale'];
}
2016-05-01 19:47:59 +02:00
unset($owner_pref);
}
if($GLOBALS['egw_info']['server']['auto_load_holidays'] == True && $this->locales)
{
foreach($this->locales as $local)
{
2016-05-01 19:47:59 +02:00
$this->auto_load_holidays($local, $year);
}
2016-05-01 19:47:59 +02:00
}
}
function auto_load_holidays($locale, $year=0)
{
//error_log(__METHOD__."('$locale', $year)");
if (!Api\Cache::getInstance(__CLASS__, $locale.'-'.$year) && // check if autoload has been tried for this locale and year
(!($total_year = $this->so->holiday_total($locale, '', $year)) ||
// automatic try load new holidays, if there are no irregular ones for queried year
$total_year == $this->so->holiday_total($locale, '', 1901)))
{
//error_log(__METHOD__."('$locale', $year) attemption autoload ...");
Api\Cache::setInstance(__CLASS__, $locale.'-'.$year, true, 864000); // do NOT try again for 10 days
@set_time_limit(0);
/* get the file that contains the calendar events for your locale */
/* "http://www.egroupware.org/cal/holidays.US.csv"; */
if(isset($GLOBALS['egw_info']['server']['holidays_url_path']) && $GLOBALS['egw_info']['server']['holidays_url_path'] != 'localhost')
{
2016-05-01 19:47:59 +02:00
$load_from = $GLOBALS['egw_info']['server']['holidays_url_path'];
}
2016-05-01 19:47:59 +02:00
else
{
2016-05-01 19:47:59 +02:00
if ($GLOBALS['egw_info']['server']['webserver_url'][0] == '/')
{
2016-05-01 19:47:59 +02:00
$server_host = ($_SERVER['HTTPS']?'https://':'http://').$_SERVER['HTTP_HOST'].$GLOBALS['egw_info']['server']['webserver_url'];
}
2016-05-01 19:47:59 +02:00
else
{
2016-05-01 19:47:59 +02:00
$server_host = $GLOBALS['egw_info']['server']['webserver_url'];
}
2016-05-01 19:47:59 +02:00
$load_from = $server_host.'/calendar/egroupware.org';
}
2016-05-01 19:47:59 +02:00
// echo 'Loading from: '.$load_from.'/holidays.'.strtoupper($locale).'.csv'."<br>\n";
if($GLOBALS['egw_info']['server']['holidays_url_path'] == 'localhost')
{
2016-05-01 19:47:59 +02:00
$lines = file(EGW_SERVER_ROOT.'/calendar/egroupware.org/holidays.'.strtoupper($locale).'.csv');
}
2016-05-01 19:47:59 +02:00
else
{
$lines = file($url=$load_from.'/holidays.'.strtoupper($locale).'.csv', 0, Framework::proxy_context());
//error_log(__METHOD__."('$locale', $year) file('$url')=".array2string($lines));
}
if (!$lines)
{
return false;
}
// reading the holidayfile from egroupware.org via network::gethttpsocketfile contains all the headers!
foreach($lines as $line)
{
2016-05-01 19:47:59 +02:00
$fields = preg_split("/[\t\n ]+/",$line);
2016-05-01 19:47:59 +02:00
if ($fields[0] == 'charset' && $fields[1])
{
2016-05-01 19:47:59 +02:00
$lines = Api\Translation::convert($lines,$fields[1]);
break;
}
2016-05-01 19:47:59 +02:00
}
foreach ($lines as $line)
{
// echo 'Line #'.$i.' : '.$lines[$i]."<br>\n";
2016-05-01 19:47:59 +02:00
$holiday = explode("\t",$line);
if(count($holiday) == 7)
{
$holiday['locale'] = $holiday[0];
$holiday['name'] = $GLOBALS['egw']->db->db_addslashes($holiday[1]);
$holiday['mday'] = (int)$holiday[2];
$holiday['month_num'] = (int)$holiday[3];
$holiday['occurence'] = (int)$holiday[4];
$holiday['dow'] = (int)$holiday[5];
$holiday['observance_rule'] = (int)$holiday[6];
$holiday['hol_id'] = 0;
$this->so->save_holiday($holiday);
}
}
}
2016-05-01 19:47:59 +02:00
}
2016-05-01 19:47:59 +02:00
function save_holiday($holiday)
{
$this->so->save_holiday($holiday);
}
2016-05-01 19:47:59 +02:00
function add()
{
if(@$_POST['submit'])
{
2016-05-01 19:47:59 +02:00
$holiday = $_POST['holiday'];
2016-05-01 19:47:59 +02:00
if(empty($holiday['mday']))
{
$holiday['mday'] = 0;
}
if(!isset($this->bo->locales[0]) || $this->bo->locales[0]=='')
{
$this->bo->locales[0] = $holiday['locale'];
}
elseif(!isset($holiday['locale']) || $holiday['locale']=='')
{
$holiday['locale'] = $this->bo->locales[0];
}
if(!isset($holiday['hol_id']))
{
$holiday['hol_id'] = $this->bo->id;
}
2016-05-01 19:47:59 +02:00
// some input validation
2016-05-01 19:47:59 +02:00
if (!$holiday['mday'] == !$holiday['occurence'])
{
$errors[] = lang('You need to set either a day or a occurence !!!');
}
if($holiday['year'] && $holiday['occurence'])
{
$errors[] = lang('You can only set a year or a occurence !!!');
}
else
{
$holiday['occurence'] = (int)($holiday['occurence'] ? $holiday['occurence'] : $holiday['year']);
unset($holiday['year']);
}
2016-05-01 19:47:59 +02:00
// Still need to put some validation in here.....
2016-05-01 19:47:59 +02:00
$this->ui =& CreateObject('calendar.uiholiday');
2016-05-01 19:47:59 +02:00
if (is_array($errors))
{
$holiday['month'] = $holiday['month_num'];
$holiday['day'] = $holiday['mday'];
$this->ui->edit_holiday($errors,$holiday);
}
else
{
$this->so->save_holiday($holiday);
$this->ui->edit_locale($holiday['locale']);
}
}
2016-05-01 19:47:59 +02:00
}
2016-05-01 19:47:59 +02:00
function sort_holidays_by_date($holidays)
{
$c_holidays = count($holidays);
for($outer_loop=0;$outer_loop<($c_holidays - 1);$outer_loop++)
{
2016-05-01 19:47:59 +02:00
for($inner_loop=$outer_loop;$inner_loop<$c_holidays;$inner_loop++)
{
2016-05-01 19:47:59 +02:00
if($holidays[$outer_loop]['date'] > $holidays[$inner_loop]['date'])
{
2016-05-01 19:47:59 +02:00
$temp = $holidays[$inner_loop];
$holidays[$inner_loop] = $holidays[$outer_loop];
$holidays[$outer_loop] = $temp;
}
}
}
2016-05-01 19:47:59 +02:00
return $holidays;
}
2016-05-01 19:47:59 +02:00
function set_holidays_to_date($holidays)
{
$new_holidays = Array();
for($i=0;$i<count($holidays);$i++)
{
// echo "Setting Holidays Date : ".date('Ymd',$holidays[$i]['date'])."<br>\n";
2016-05-01 19:47:59 +02:00
$new_holidays[date('Ymd',$holidays[$i]['date'])][] = $holidays[$i];
}
2016-05-01 19:47:59 +02:00
return $new_holidays;
}
2016-05-01 19:47:59 +02:00
function read_holiday()
{
if(isset($this->cached_holidays))
{
2016-05-01 19:47:59 +02:00
return $this->cached_holidays;
}
$holidays = $this->so->read_holidays($this->locales,'','',$this->year);
2016-05-01 19:47:59 +02:00
if(count($holidays) == 0)
{
return $holidays;
}
2016-05-01 19:47:59 +02:00
$temp_locale = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
foreach($holidays as $i => $holiday)
{
if($i == 0 || $holidays[$i]['locale'] != $holidays[$i - 1]['locale'])
{
2016-05-01 19:47:59 +02:00
if(is_object($holidaycalc))
{
2016-05-01 19:47:59 +02:00
unset($holidaycalc);
}
2016-05-01 19:47:59 +02:00
$GLOBALS['egw_info']['user']['preferences']['common']['country'] = $holidays[$i]['locale'];
$holidaycalc =& CreateObject('calendar.holidaycalc');
}
2016-05-01 19:47:59 +02:00
$holidays[$i]['date'] = $holidaycalc->calculate_date($holiday, $holidays, $this->year);
}
2016-05-01 19:47:59 +02:00
unset($holidaycalc);
$this->holidays = $this->sort_holidays_by_date($holidays);
$this->cached_holidays = $this->set_holidays_to_date($this->holidays);
$GLOBALS['egw_info']['user']['preferences']['common']['country'] = $temp_locale;
return $this->cached_holidays;
}
/* End Calendar functions */
2016-05-01 19:47:59 +02:00
function check_admin()
{
if(!@$GLOBALS['egw_info']['user']['apps']['admin'])
{
2016-05-01 19:47:59 +02:00
Header('Location: ' . $GLOBALS['egw']->link('/index.php'));
}
2016-05-01 19:47:59 +02:00
}
2016-05-01 19:47:59 +02:00
function rule_string($holiday)
{
if (!is_array($holiday))
{
2016-05-01 19:47:59 +02:00
return false;
}
$monthnames = array(
'','January','February','March','April','May','June',
'July','August','September','October','November','December'
);
$month = $holiday['month'] ? lang($monthnames[$holiday['month']]) : '';
2016-05-01 19:47:59 +02:00
if (!$holiday['day'])
{
$occ = $holiday['occurence'] == 99 ? lang('last') : $holiday['occurence'].'.';
2016-05-01 19:47:59 +02:00
$dow_str = Array(lang('Sun'),lang('Mon'),lang('Tue'),lang('Wed'),lang('Thu'),lang('Fri'),lang('Sat'));
$dow = $dow_str[$holiday['dow']];
2016-05-01 19:47:59 +02:00
$str = lang('%1 %2 in %3',$occ,$dow,$month);
}
2016-05-01 19:47:59 +02:00
else
{
$arr = array($month,$holiday['day']);
if ($holiday['occurence']>1900) array_unshift ($arr, $holiday['occurence']);
$str = implode('-', $arr);
}
if ($holiday['observance_rule'])
{
$str .= ' ('.lang('Observance Rule').')';
}
return $str;
}
2016-05-01 19:47:59 +02:00
}
2016-05-01 19:47:59 +02:00
function _holiday_cmp($a,$b)
{
if (($year_diff = ($a['occurence'] <= 0 ? 0 : $a['occurence']) - ($b['occurence'] <= 0 ? 0 : $b['occurence'])))
{
2016-05-01 19:47:59 +02:00
return $year_diff;
}
2016-05-01 19:47:59 +02:00
return $a['month'] - $b['month'] ? $a['month'] - $b['month'] : $a['day'] - $b['day'];
}