egroupware/calendar/inc/class.soholiday.inc.php

212 lines
5.4 KiB
PHP
Raw Normal View History

<?php
2008-03-15 15:10:20 +01:00
/**
* eGroupWare - Calendar Holidays
*
* @link http://www.egroupware.org
* @package calendar
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @author Mark Peters <skeeter@phpgroupware.org>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
/**
* Storage layer for calendar holidays
*
* Maintained and further developed by RalfBecker@outdoor-training.de
* Originaly written by Mark Peters <skeeter@phpgroupware.org>
*/
class soholiday
{
var $debug = False;
/**
2008-03-15 15:10:20 +01:00
* Reference to the global db-object
*
2008-03-15 15:10:20 +01:00
* @var egw_db
*/
2008-03-15 15:10:20 +01:00
var $db;
var $table = 'egw_cal_holidays';
function soholiday()
{
2008-03-15 15:10:20 +01:00
$this->db = $GLOBALS['egw']->db;
}
2008-03-15 15:10:20 +01:00
/* Begin Holiday functions */
function save_holiday($holiday)
{
// observance_rule is either "True" or unset !
$holiday['observance_rule'] = @$holiday['observance_rule'] ? 1 : 0;
$holiday['locale'] = strtoupper($holiday['locale']);
2008-03-15 15:10:20 +01:00
foreach($holiday as $name => $val)
{
2008-03-15 15:10:20 +01:00
if (substr($name,0,4) != 'hol_')
{
2008-03-15 15:10:20 +01:00
if (!is_numeric($name))
{
$holiday['hol_'.$name] = $val;
}
2008-03-15 15:10:20 +01:00
unset($holiday[$name]);
}
2008-03-15 15:10:20 +01:00
}
$hol_id = $holiday['hol_id'];
unset($holiday['hol_id']);
unset($holiday['hol_locales']);
2008-03-15 15:10:20 +01:00
if ($hol_id)
{
if($this->debug)
{
2008-03-15 15:10:20 +01:00
echo "Updating LOCALE='".$holiday['locale']."' NAME='".$holiday['name']."' extra=(".$holiday['mday'].'/'.$holiday['month_num'].'/'.$holiday['occurence'].'/'.$holiday['dow'].'/'.$holiday['observance_rule'].")<br>\n";
}
2008-03-15 15:10:20 +01:00
$this->db->update($this->table,$holiday,array('hol_id' => $hol_id),__LINE__,__FILE__,'calendar');
}
else
{
if($this->debug)
{
2008-03-15 15:10:20 +01:00
echo "Inserting LOCALE='".$holiday['locale']."' NAME='".$holiday['name']."' extra=(".$holiday['mday'].'/'.$holiday['month_num'].'/'.$holiday['occurence'].'/'.$holiday['dow'].'/'.$holiday['observance_rule'].")<br>\n";
}
// delete evtl. existing rules with same name, year (occurence) and local
$this->db->delete($this->table, array(
'hol_name' => $holiday['hol_name'],
'hol_occurence' => $holiday['hol_occurence'],
'hol_locale' => $holiday['hol_locale'],
), __LINE__, __FILES__, 'calendar');
2008-03-15 15:10:20 +01:00
$this->db->insert($this->table,$holiday,False,__LINE__,__FILE__,'calendar');
}
2008-03-15 15:10:20 +01:00
}
2008-03-15 15:10:20 +01:00
function store_to_array(&$holidays,$rs)
{
foreach($rs as $row)
{
2008-03-15 15:10:20 +01:00
$holidays[] = Array(
'index' => $row['hol_id'],
'locale' => $row['hol_locale'],
'name' => $GLOBALS['egw']->strip_html($row['hol_name']),
'day' => (int)$row['hol_mday'],
'month' => (int)$row['hol_month_num'],
'occurence' => (int)$row['hol_occurence'],
'dow' => (int)$row['hol_dow'],
'observance_rule' => $row['hol_observance_rule']
);
if($this->debug)
{
2008-03-15 15:10:20 +01:00
echo 'Holiday ID: '.$row['hol_id'].'<br>'."\n";
}
}
2008-03-15 15:10:20 +01:00
}
function read_holidays($locales='',$query='',$order='',$year=0)
{
$holidays = Array();
2008-03-15 15:10:20 +01:00
if($locales == '')
{
2008-03-15 15:10:20 +01:00
return $holidays;
}
2008-03-15 15:10:20 +01:00
$where = $this->_build_where($locales,$query,$order,$year);
2008-03-15 15:10:20 +01:00
if($this->debug)
{
echo 'Read Holidays : '.$where.'<br>'."\n";
}
2008-03-15 15:10:20 +01:00
$rs = $this->db->select($this->table,'*',$where,__LINE__,__FILE__,false,'','calendar');
$this->store_to_array($holidays,$rs);
2008-03-15 15:10:20 +01:00
return $holidays;
}
2008-03-15 15:10:20 +01:00
function read_holiday($id)
{
$holidays = Array();
if($this->debug)
{
2008-03-15 15:10:20 +01:00
echo 'Reading Holiday ID : '.$id.'<br>'."\n";
}
2008-03-15 15:10:20 +01:00
$rs = $this->db->select($this->table,'*',array('hol_id'=>$id),__LINE__,__FILE__,false,'','calendar');
$this->store_to_array($holidays,$rs);
return $holidays[0];
}
function delete_holiday($id)
{
$this->db->delete($this->table,array('hol_id' => $id),__LINE__,__FILE__,'calendar');
}
2008-03-15 15:10:20 +01:00
function delete_locale($locale)
{
$this->db->delete($this->table,array('hol_locale' => $locale),__LINE__,__FILE__,'calendar');
}
2008-03-15 15:10:20 +01:00
/* Private functions */
function _build_where($locales,$query='',$order='',$year=0,$add_order_by=True)
{
$querymethod = 'hol_locale';
if (is_array($locales))
{
$querymethod .= ' IN ('.$this->db->column_data_implode(',',$locales,False).')';
}
else
{
$querymethod .= '='.$this->db->quote($locales);
}
if($query)
{
$querymethod .= " AND hol_name LIKE ".$this->db->quote('%'.$query.'%');
}
if ($year > 1900)
{
$querymethod .= " AND (hol_occurence < 1900 OR hol_occurence = ".(int)$year.")";
}
if ($add_order_by)
{
2008-03-15 15:10:20 +01:00
$querymethod .= ' ORDER BY '.(preg_match('/^[a-zA-Z0-9_,]+$/',$order) ? $order : 'hol_month_num,hol_mday');
}
2008-03-15 15:10:20 +01:00
return $querymethod;
}
2008-03-15 15:10:20 +01:00
function get_locale_list($sort='', $order='', $query='')
{
$querymethod = '';
if($query)
{
2008-03-15 15:10:20 +01:00
$querymethod = 'hol_locale LIKE '.$this->db->quote('%'.$query.'%');
}
2008-03-15 15:10:20 +01:00
if(!preg_match('/^[a-zA-Z0-9_,]+$/',$order))
{
$order = 'hol_locale';
}
if (strtoupper($sort) != 'DESC') $sort = 'ASC';
if (strpos($order, ',') === false) $order .= ' '.$sort;
foreach($this->db->select($this->table,'DISTINCT hol_locale',$querymethod,__LINE__,__FILE__,false,'ORDER BY '.$order,'calendar') as $row)
2008-03-15 15:10:20 +01:00
{
$locale[] = $row['hol_locale'];
}
return $locale;
}
2008-03-15 15:10:20 +01:00
function holiday_total($locale,$query='',$year=0)
{
$where = $this->_build_where($locale,$query,'',$year,False);
2008-03-15 15:10:20 +01:00
if($this->debug)
{
2008-03-15 15:10:20 +01:00
echo 'HOLIDAY_TOTAL : '.$where.'<br>'."\n";
}
$retval = $this->db->select($this->table,'count(*)',$where,__LINE__,__FILE__,false,'','calendar')->fetchColumn();
2008-03-15 15:10:20 +01:00
if($this->debug)
{
echo 'Total Holidays for : '.$locale.' : '.$retval."<br>\n";
}
2008-03-15 15:10:20 +01:00
return $retval;
}
2008-03-15 15:10:20 +01:00
}