first version of new calendar SO class:

- so far it only reads (multiple) events
- bocal::search used now socal::read to read all events in one go, this should give a far better performance
This commit is contained in:
Ralf Becker 2004-11-07 14:38:00 +00:00
parent f8ee85dc81
commit 4e4b0c0bea
2 changed files with 191 additions and 45 deletions

View File

@ -15,7 +15,7 @@
/**
* Class to access and manipulate all calendar data
*
* At the moment this class partialy uses the "old" bocalendar class.
* At the moment this class partialy uses the "old" socalendar class.
* As the calendar rewrite proceeds, these references will be removed.
*
* Note: All new code should only access this class and not bocalendar!!!
@ -61,7 +61,7 @@ class bocal
* 4 = function-calls to exported conversation-functions like date2ts, date2array, ...
* 5 = function-calls to private functions
*/
var $debug=False;
var $debug=false;
/**
* @var int $tz_offset_s offset in secconds between user and server-time,
@ -112,8 +112,8 @@ class bocal
if ($this->debug > 0) $this->debug_message('bocal::bocal() started',True,$param);
foreach(array(
// 'old_bo' => 'calendar.bocalendar',
'old_so' => 'calendar.socalendar',
'so' => 'calendar.socal',
'datetime' => 'phpgwapi.datetime',
) as $my => $app_class)
{
@ -121,6 +121,7 @@ class bocal
if (!is_object($GLOBALS['phpgw']->$class))
{
//echo "<p>calling CreateObject($app_class)</p>\n".str_repeat(' ',4096);
$GLOBALS['phpgw']->$class = CreateObject($app_class);
}
$this->$my = &$GLOBALS['phpgw']->$class;
@ -237,19 +238,21 @@ class bocal
// If daywise is True each event need to be added in the array of each day it's running.
// Please note: as we use the old SO class, all returned dates are already in user-time !!!
$events = $recur_exceptions = $recur_events = Array();
$events = $recur_exceptions = Array();
$events_sorted = True;
if(count($event_ids))
if(count($event_ids) || count($rep_event_ids))
{
foreach($event_ids as $id)
//foreach($event_ids+$rep_event_ids as $id)
foreach((array) $this->read($event_ids+$rep_event_ids,true) as $id => $event)
{
$event = $this->read($id,True); // = no ACL check, as other entries dont get reported !!!
//$event = $this->read($id,True); // = no ACL check, as other entries dont get reported !!!
// recuring events are handled later, remember them for later use, no new read necessary
if ($event['recur_type'])
{
$recur_events[$id] = $event;
$this->insert_all_repetitions($event,$start,$end,$events,$recur_exceptions[$id]);
$events_sorted = False;
continue;
}
// recur-exceptions have a reference to the original event
@ -266,25 +269,9 @@ class bocal
}
if ($this->debug && ($this->debug > 2 || $this->debug == 'search'))
{
$this->debug_message('socalendar::search processed event_ids=%1, events=%2',False,$event_ids,$events);
$this->debug_message('socalendar::search processed event_ids=%1, events=%2',False,$event_ids+$rep_event_ids,$events);
}
}
if (count($rep_event_ids))
{
foreach($rep_event_ids as $id)
{
$event = isset($recur_events[$id]) ? $recur_events[$id] : $this->read($id,True);
$this->insert_all_repetitions($event,$start,$end,$events,$recur_exceptions[$id]);
$events_sorted = False;
}
if ($this->debug && ($this->debug > 2 || $this->debug == 'search'))
{
$this->debug_message('socalendar::search processed rep_event_ids=%1, events=%2',False,$rep_event_ids,$events);
}
}
if (!$events_sorted)
{
// sort the events by start-date
@ -333,39 +320,41 @@ class bocal
/**
* Reads a calendar-entry
*
* @param $id int the id of the entry
* @param $ignore_acl boolean should we ignore the acl, default False
* @return array with the event or False if the acl-check went wrong
* @param $ids array/int id(s) of the entry
* @param $ignore_acl boolean should we ignore the acl, default False for a single id, true for multiple id's
* @return boolean/array event or array of id => event pairs or False if the acl-check went wrong
*/
function read($id,$ignore_acl=False)
function read($ids,$ignore_acl=False)
{
$event = False;
if($ignore_acl || $this->check_perms(PHPGW_ACL_READ,$id))
if($ignore_acl || is_array($ids) || $this->check_perms(PHPGW_ACL_READ,$ids))
{
// some minimal cacheing to re-use the event already read in check_perms
static $event = array();
if (!isset($event['id']) || $event['id'] != $id)
{
$event = $this->old_so->read_entry($id);
if (!$event['recur_enddate']['year'] && !$event['recur_enddate']['month'] && !$event['recur_enddate']['day'])
if (is_array($ids) || !isset($event['id']) || $event['id'] != $ids)
{
$events = $this->so->read($ids);
foreach($events as $id => $event)
{
$event['recur_enddate'] = False; // for easier checking
}
// we run all dates through date2array, to get the new keys (day,minute,second,full,raw)
foreach(array('start','end','modtime','recur_enddate') as $date)
{
// The dates are already in user-time, because of the old so-class !!!
$event[$date] = $event[$date] ? $this->date2array($event[$date]) : $event[$date];
// we run all dates through date2array, to get the new keys (day,minute,second,full,raw)
foreach(array('starttime' => 'start','endtime' => 'end','modified' => 'modtime','recur_enddate' => 'recur_enddate') as $ts => $date)
{
unset($events[$id][$ts]);
// we convert here from the server-time timestamps to an array in user-time!
$events[$id][$date] = $event[$ts] ? $this->date2array((int) $event[$ts],true) : false;
}
}
if (!is_array($ids)) $event = $events[$ids];
}
}
if ($this->debug && ($this->debug > 1 || $this->debug == 'read'))
{
$this->debug_message('bocal::read(%1,%2)=%3',True,$id,$ignore_acl,$event);
$this->debug_message('bocal::read(%1,%2)=%3',True,$ids,$ignore_acl,is_array($ids) ? $events : $event);
}
return $event;
return is_array($ids) ? $events : $event;
}
/**
@ -658,9 +647,15 @@ class bocal
{
$date_in = $date;
switch(gettype($date))
{
case 'string': // YYYYMMDD or iso8601 YYYY-MM-DDThh:mm:ss string
if (is_numeric($date) && $date > 21000000)
{
$date = (int) $date; // this is already as timestamp
break;
}
if ($date[10] == 'T')
{
$date = array(
@ -727,8 +722,10 @@ class bocal
if (!is_array($date) || count($date) < 8 || $server2user) // do we need a conversation
{
$date = $this->date2ts($date);
if (!is_int($date))
{
$date = $this->date2ts($date);
}
if ($server2user)
{
$date += $this->tz_offset_s;

View File

@ -0,0 +1,149 @@
<?php
/**************************************************************************\
* eGroupWare - Calendar's "new" SO-layer (storage-object) *
* http://www.egroupware.org *
* Written and (c) 2004 by Ralf Becker <RalfBecker@outdoor-training.de> *
* -------------------------------------------- *
* 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. *
\**************************************************************************/
/* $Id$ */
/**
* Class to store all calendar data
*
* At the moment this class is used together with the "old" socalendar class.
* As the calendar rewrite proceeds, the old class will be removed.
*
* The new UI, BO and SO classes have a strikt definition, in which time-zone they operate:
* UI only operates in user-time, so there have to be no conversation at all !!!
* BO's functions take and return user-time only (!), they convert internaly everything to servertime, because
* SO operates only in server-time (please note, this is not the case with the socalendar*-classes used atm.)
*
* @package calendar
* @author RalfBecker@outdoor-training.de
* @license GPL
*/
class socal
{
/**
* name of the main calendar table and prefix for all other calendar tables
*/
var $cal_table = 'phpgw_cal';
var $extra_table,$holidays_table,$repeats_table,$user_table;
/**
* internal copy of the global db-object
*/
var $db;
/**
* Constructor of the socal class
*/
function socal()
{
foreach(array(
//'old_so' => 'calendar.socalendar',
'async' => 'phpgwapi.asyncservice',
) as $my => $app_class)
{
list(,$class) = explode('.',$app_class);
if (!is_object($GLOBALS['phpgw']->$class))
{
//echo "<p>calling CreateObject($app_class)</p>\n".str_repeat(' ',4096);
$GLOBALS['phpgw']->$class = CreateObject($app_class);
}
$this->$my = &$GLOBALS['phpgw']->$class;
}
$this->db = $GLOBALS['phpgw']->db;
$this->db->set_app('calendar');
foreach(array('extra','holidays','repeats','user') as $name)
{
$vname = $name.'_table';
$this->$vname = $this->cal_table.'_'.$name;
}
}
/**
* reads one or more calendar entries
*
* All times (start, end and modified) are returned as timesstamps in servertime!
*
* @param $ids int/array id(s) of the entries to read
* @return array/boolean array with id => data pairs or false if entry not found
*/
function read($ids)
{
$this->db->select($this->cal_table,'*',array('cal_id'=>$ids),__LINE__,__FILE__);
$events = false;
$recur_ids = array();
while (($row = $this->db->row(true,'cal_')))
{
if ($row['type'] == 'M') $recur_ids[] = $row['id'];
unset($row['type']);
$row['alarm'] = array();
$events[$row['id']] = $row;
}
if (!$events) return false;
// recur details
if (count($recur_ids))
{
$this->db->select($this->repeats_table,'*',array('cal_id'=>$recur_ids),__LINE__,__FILE__);
while(($row = $this->db->row(true)))
{
$row['recur_exception'] = $row['recur_exception'] ? explode(',',$row['recur_exception']) : array();
$cal_id = $row['cal_id'];
unset($row['cal_id']);
$events[$cal_id] += $row;
}
}
// participants
$this->db->select($this->user_table,'*',array('cal_id'=>$ids),__LINE__,__FILE__);
while (($row = $this->db->row(true)))
{
// if the type is not an ordinary user (eg. contact or resource) prefix the id with the type
if ($row['cal_user_type'] && $row['cal_user_type'] != 'u')
{
$user_id = $row['cal_user_type'].$row['cal_user_id'];
}
else
{
$user_id = (int) $row['cal_user_id'];
}
$events[$row['cal_id']]['participants'][$user_id] = $row['cal_status'];
}
// custom fields
$this->db->select($this->extra_table,'*',array('cal_id'=>$ids),__LINE__,__FILE__);
while (($row = $this->db->row(true)))
{
$events[$row['cal_id']]['#'.$row['cal_extra_name']] = $row['cal_extra_value'];
}
// alarms, atm. we read all alarms in the system, as this can be done in a single query
foreach((array)$this->async->read('cal'.(is_array($ids) ? '' : ':'.(int)$ids).':%') as $id => $job)
{
list(,$cal_id) = explode(':',$id);
if (!isset($events[$cal_id])) continue; // not needed
$alarm = $job['data']; // text, enabled
$alarm['id'] = $id;
$alarm['time'] = $job['next'];
$events[$cal_id]['alarm'][$id] = $alarm;
}
//echo "<p>socal::read(".print_r($ids,true).")=<pre>".print_r($events,true)."</pre>\n";
return $events;
}
}