";
private $view_head_sunday = "
Sunday |
Monday |
Tuesday |
Wednedsday |
Thursday |
Friday |
Saturday |
";
private $view_head_monday = "
Monday |
Tuesday |
Wednedsday |
Thursday |
Friday |
Saturday |
Sunday |
";
private $view_head_day = "
__DAY__ |
";
function __construct($root, $start_hour, $end_hour, $start_sunday = FALSE) {
self::$start_sunday = $start_sunday;
self::$root = $root;
self::$start_hour = $start_hour;
self::$end_hour = $end_hour;
}
protected function applyLegend() {
$calenders = $_SESSION['user_settings']->getCalendars();
$legend = '';
foreach ($calenders as $calender) {
$legend .= '[ :' . $calender->name . ']';
}
$legend .= '
';
$legend .= <<<_HELP
Events outside configured range is only displayed
when Day or Week view is active
_HELP;
return $legend;
}
protected function getViewHead($kindOf) {
switch ($kindOf) {
case 'month':
$return = $this->heading.'Week | ';
if (self::$start_sunday)
$return .= $this->view_head_sunday;
else
$return .= $this->view_head_monday;
return $return;
break;
case 'week':
if (self::$start_sunday) {
$head = str_replace('', '
__DATE__',
$this->view_head_sunday);
}
else {
$head = str_replace('', '
__DATE__',
$this->view_head_monday);
}
$head = 'Time | ' . $head;
return $this->heading . $head;
break;
case 'day':
$return = $this->heading . 'Time | ';
return $return . $this->view_head_day;
break;
default: trigger_error("$kindOf: ['month', 'week', 'day']", E_USER_ERROR);
}
}
/*
* Function get_week_number which returns the week number of the
* given date according to ISO 8601-1988
* http://www.php.happycodings.com/Date_Time/code22.html
*/
protected function is_leap_year($year) {
if ((($year % 4) == 0 and ($year % 100)!=0) or ($year % 400)==0) {
return 1;
} else {
return 0;
}
}
/* define ISO_WEEK_START_WDAY 1 // Monday
* define ISO_WEEK1_WDAY 4 // Thursday
* define YDAY_MINIMUM (-366)
* int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7;
* return (yday - (yday - wday + ISO_WEEK1_WDAY +
* big_enough_multiple_of_7) % 7 + ISO_WEEK1_WDAY -
* ISO_WEEK_START_WDAY);
*/
private function iso_week_days($yday, $wday) {
return $yday - (($yday - $wday + 382) % 7) + 3;
}
protected function get_week_number($timestamp) {
$d = getdate($timestamp);
$days = $this->iso_week_days($d[ "yday"], $d[ "wday"]);
if ($days < 0) {
$d[ "yday"] += 365 + $this->is_leap_year(--$d[ "year"]);
$days = $this->iso_week_days($d[ "yday"], $d[ "wday"]);
} else {
$d[ "yday"] -= 365 + $this->is_leap_year($d[ "year"]);
$d2 = $this->iso_week_days($d[ "yday"], $d[ "wday"]);
if (0 <= $d2) {
/* $d["year"]++; */
$days = $d2;
}
}
$adjust = 1;
$day = date('w');
if ($_SESSION['user_settings']->getStartWeek() === true && $day == 0)
$adjust = 2;
return (int)($days / 7) + $adjust;
}
protected function getStartDay($year, $month) {
/* date('w') returns start day counting from Sunday */
$start = date('w', mktime(0,0,0,$month,1,$year));
return $start;
}
protected function isToDay($year, $month, $day) {
return ($year == date('Y') && $month == date('n') && $day == date('j'));
}
protected function lastDayInWeek($year, $week) {
$weekEndDay = 0; // 0 = Sunday.
$dayOfYear = 4 + (($week - 1) * 7);
$date = mktime(0, 0, 0, 1, $dayOfYear, $year);
// Find the last day of this week.
$dayOfWeek = date("w", $date);
$daysToAdd = ($weekEndDay - $dayOfWeek + 7) % 7;
$date += $daysToAdd * 24*60*60;
return (self::$start_sunday) ? $date - (24*60*60) : $date;
}
protected function dateFromDayInYear($year, $day) {
$now = mktime(0, 0, 0, 1, 1, $year);
return date('l, j F', strtotime("+$day days", $now));
}
protected function makeSummary($infos) {
$summary = 'Title: ';
$summary .= (isset($infos['summary']) && ! empty($infos['summary'])) ?
$infos['summary'] : 'Untitled';
$summary .= "\n";
$summary .= 'Date: ';
$start = strtotime($infos['dates'][0]['start']);
$date = date("D j M Y", $start);
$end = strtotime($infos['dates'][0]['end']);
$summary .= $date . ' ' . date("G:i", $start) . ' - ' . date("G:i", $end);
return $summary;
}
protected function makeEvent($cell, $uri, $infos) {
$history = $_SERVER['PHP_SELF'];
$summary = (isset($infos['summary']) && ! empty($infos['summary'])) ?
$infos['summary'] : 'Untitled';
$history .= (empty($_SERVER['QUERY_STRING'])) ? '' : '?'.$_SERVER['QUERY_STRING'];
//$url = WEB_ROOT . 'events/edit_event.php?etag='. $uri;
$url = 'etag='. $uri.'&referer='.$history.'&cal='.$infos['cal'];
$url = WEB_ROOT . 'events/edit_event.php?' . urlencode($url);
$c = ''.$summary.'';
return ($cell != '') ? "$cell
$c" : $c;
}
protected function getEvents($range) {
$list = array();
$all_events = array();
$cals = $_SESSION['user_settings']->getCalendars();
//file_put_contents('/tmp/davical.log',
// __FILE__ . ": " . var_export($cals, TRUE), FILE_APPEND);
if (! $cals)
return NULL;
$start = CaldavRessource::timestamp2ICal($range['start'], TRUE);
if ($_SESSION['user_settings']->getStartWeek() === FALSE) {
$datetime = explode('T', $start);
//print_r($datetime);
$day = $datetime[0] + 1;
$start = $day.'T'.$datetime[1].'Z';
}
$end = CaldavRessource::timestamp2ICal($range['end'], TRUE);
$datetime = explode('T', $end);
$end = $datetime[0].'T'.'235959Z';
//print "$start:$end
";
foreach ($cals as $cal) {
//var_dump($cal);
$conf = implode_cal(decode($cal->config));
//var_dump($conf);
$events = new Calendar($conf['url'], $conf['uid'], $conf['pwd']);
//print_r($events);
$events->getComponents($start, $end);
//print_r($events);
$all_events[$cal->name] = $events;
foreach ($events as $event) {
$dates = $event->getActiveDates();
//file_put_contents('/tmp/davical.log',
//__FILE__ . ": " . var_export($dates, TRUE), FILE_APPEND);
//var_dump($dates);
$comp = $event->getBaseComponent();
//print_r($comp);
$summary = $comp->GetPValue('SUMMARY');
$item = array();
$dtend = explode('T', $comp->GetPValue('DTEND'));
//print_r($comp->GetPValue('DTEND'));
//print "
";
//print_r($dtend);
//print "
";
//print_r($dates);
//print "
--------------------------
";
if (count($dates) > 0) {
foreach ($dates as $date) {
//print_r($date);
//print "
--------------------------
";
//$datetime = explode('T', $date);
//print_r($dtend);
//print "
--------------------------
";
//if (count($datetime) > 1) {
if (count($dtend) > 1) {
//if (count($dtend) < 2)
// array_push($dtend, '235959');
//$endtime = $datetime[0].'T'.$dtend[1];
$endtime = $dtend[0].'T'.$dtend[1];
}
else {
//$endtime = $datetime[0];
$day = strtotime($dtend[0]);
$day = strtotime("-1 day", $day);
$endtime = date("Ymd\T235959", $day);
//$endtime = $endtime."T235959";
//$endtime = $dtend[0]."T000000";
}
//print "$date:$endtime
";
array_push($item, array('start' => $date, 'end' => $endtime));
}
$list[$event->getEtag()] = array('color' => $cal->color,
'summary' => $summary, 'dates' => $item, 'cal' => $cal->name);
}
}
}
$_SESSION['all_events'] = $all_events;
//var_dump($list);
return $list;
}
protected abstract function parseDate();
protected abstract function getHead();
public abstract function getView($year = NULL, $other = NULL);
}
class PageView extends View {
const DAY = 0;
const WEEK = 1;
const MONTH = 2;
function __construct($root, $start_hour, $end_hour, $start_sunday = FALSE) {
parent::__construct($root, $start_hour, $end_hour, $start_sunday);
}
public function createView($view) {
switch ($view) {
case (self::DAY): return new DayView(); break;
case (self::WEEK): return new WeekView(); break;
case (self::MONTH): return new MonthView(); break;
default: trigger_error("$view: ['MONTH', 'WEEK', 'DAY']", E_USER_ERROR);
}
}
protected function parseDate() {
throw new Exception("Function can only be called from a subclass");
}
protected function getHead() {
throw new Exception("Function can only be called from a subclass");
}
public function getView($year = NULL, $other = NULL) {
throw new Exception("Function can only be called from a subclass");
}
}