|
__YEAR_AND_WEEK__ |
|
";
private $bottom;
function __construct(Calendar $calendar = NULL) {
$this->url = parent::$root.'/navigate/show_week.php';
$this->calendar = $calendar;
$this->range = array();
}
protected function parseDate() {
if ($this->year && $this->week) {
$week = $this->week;
if ($week < 0 || $week > 53) {
$week = $this->get_week_number(time());
}
$year = $this->year;
}
else {
$week = $this->get_week_number(time());
$year = date('Y');
}
$this->privious = $week - 1;
$this->p_year = $year;
if ($this->privious == 0) {
$this->p_year -= 1;
if (date("w", strtotime($this->p_year."/12/31")) > 3)
$this->privious = 53;
else
$this->privious = 52;
}
$this->next = $week + 1;
$this->n_year = $year;
if ($this->next > 51) {
if ($this->get_week_number(strtotime("$year/12/31")) == 1 ||
$this->next > 53) {
$this->next = 1;
$this->n_year += 1;
}
}
$this->year_and_week = "$year Week $week";
$this->year = $year;
$this->week = $week;
}
protected function getHead() {
$this->parseDate();
$head = str_replace('__URL__', $this->url, $this->top);
$head = str_replace('__P_YEAR__', $this->p_year, $head);
$head = str_replace('__PRIVIOUS__', $this->privious, $head);
$head = str_replace('__WEB_ROOT__', parent::$root, $head);
$head = str_replace('__YEAR_AND_WEEK__', $this->year_and_week, $head);
$head = str_replace('__N_YEAR__', $this->n_year, $head);
$head = str_replace('__NEXT__', $this->next, $head);
$this->bottom = $head;
return $this->applyLegend() . $head;
}
private function addDates($head) {
$timestamp = $this->lastDayInWeek($this->year, $this->week);
$i = 7;
$this->range['end'] = $timestamp;
do {
$date = date('Y/m/d', $timestamp);
$timestamp = strtotime('-1 day', $timestamp);
$head = substr_replace($head, $date, strrpos($head, '__DATE__'), 8);
$i--;
} while ($i > 0);
$this->range['start'] = $timestamp;
return $head;
}
private function getCell($events, $day, $progress, $excluded = FALSE) {
$cell = '';
//$etags = array();
if ($events) {
//print var_export($events,true)."
";
$dateFormat = new dateFormat();
foreach ($events as $etag =>$infos) {
foreach ($infos['dates'] as $time) {
//print "{$time['start']}:{$time['end']}
";
//print $time['end']."
";
CaldavRessource::fix_allday_event($time['start'],$time['end']);
//print $time['end']."
";
//print "{$time['start']}:{$time['end']}
";
$time['start'] = $dateFormat->UTC2Client($time['start']);
$time['end'] = $dateFormat->UTC2Client($time['end']);
//print $time['end']."
";
if ($excluded) {
$ts = strtotime("+$day day +".parent::$start_hour." hour",
$this->range['start']);
$ical_start = CaldavRessource::timestamp2ICal($ts);
//print parent::$end_hour;
$end = (parent::$end_hour == 24) ? 23 : parent::$end_hour;
$ts = strtotime("+$day day +".$end." hour",
$this->range['start']);
$ical_end = CaldavRessource::timestamp2ICal($ts);
$time_start = (strrchr($time['start'], 'Z')) ?
trim($time['start'], "Z") : $time['start'];
$time_end = (strrchr($time['end'], 'Z')) ?
trim($time['end'], "Z") : $time['end'];
//print $time_end."\n";
$parts = explode("T", $time_end);
if (substr($parts[1], 0, 2) == "00") {
$parts[1] = "24" . substr($parts[1], 2, 4);
$time_end = $parts[0] . "T" . $parts[1];
}
//print "b[$time_start : $time_end]
";
//print "a[$ical_start : $ical_end] [$time_start : $time_end]
";
if ((CaldavRessource::cmptime(
$ical_start, $time_start) > 0 &&
CaldavRessource::cmpdate($ical_start, $time_end) == 0)
||
(CaldavRessource::cmptime(
$ical_end, $time_end) < 0 &&
CaldavRessource::cmpdate($ical_end, $time_start) == 0)) {
//print "b[$ical_start : $ical_end] [$time_start : $time_end]
";
if (! in_array($etag, $this->etags)) {
array_push($this->etags, $etag);
$cell = $this->makeEvent($cell, $etag, $infos);
//print "$cell
";
//print "exc: cell: $etag
";
//print "exc: ".var_export($this->etags, true)."
";
}
}
}
else {
$ts = strtotime("+$day day +$progress hour",
$this->range['start']);
$icalDate = CaldavRessource::timestamp2ICal($ts, TRUE);
//print "$icalDate {$time['start']}:{$time['end']}
";
$s_cmp = CaldavRessource::datecmp($icalDate, $time['start']);
$e_cmp = CaldavRessource::datecmp($icalDate, $time['end']);
//print "$s_cmp:$e_cmp
";
if ($s_cmp >= 0 && $e_cmp < 0) {
//if (! in_array($etag, $this->etags)) {
$cell = $this->makeEvent($cell, $etag, $infos);
//print "$cell
";
//print "inc: cell: $etag
";
//print "inc: ".var_export($this->etags, true)."
";
//}
}
}
}
}
}
return ($cell == '') ? ' ' : $cell;
}
public function getView($year = NULL, $week = NULL) {
$this->year = $year;
$this->week = $week;
$calendar = $this->getHead();
$calendar .= '
';
$calendar .= $this->addDates($this->getViewHead('week'));
if ($this->get_week_number(time()) == $week || $week == NULL) {
$day = date('w');
if ($_SESSION['user_settings']->getStartWeek() === FALSE) {
if ($day == 0)
$day = 6;
else
$day--;
}
}
else
$day = -1;
$events = $this->getEvents($this->range);
//var_dump($events);
//var_dump($_SESSION['all_events']);
$calendar .= "Excluded | \n";
$excluded = array();
for ($d = 0; $d < 7; $d++) {
$cell = $this->getCell($events, $d + 1, 0, TRUE);
if ($d == $day && ($year == date('Y') || $year == NULL)) {
$calendar .= "$cell | \n";
}
else
$calendar .= "$cell | \n";
//print_r($cell);
//if ($d < 6) {
// print "clean etags
";
// $this->etags = array();
//}
array_push($excluded, $this->etags);
$this->etags = array();
}
$calendar .= '
';
for ($i = parent::$start_hour; $i < parent::$end_hour; $i++) {
$s_time = ($i < 10) ? "0$i" : $i;
$e_time = ($i < 9) ? '0'.($i + 1) : ($i + 1);
$calendar .= "$s_time - $e_time | \n";
for ($d = 0; $d < 7; $d++) {
$this->etags = $excluded[$d];
$progress = $i;
$cell = $this->getCell($events, $d + 1, $progress);
//print_r($cell);
if ($d == $day && ($year == date('Y') || $year == NULL)) {
$calendar .= "$cell | \n";
}
else
$calendar .= "$cell | \n";
}
$calendar .= '
';
}
$calendar .= '';
$calendar .= '
';
$calendar .= $this->bottom;
//file_put_contents('/tmp/davical.log', $calendar);
return $calendar;
}
function get_week_number($timestamp) {
return parent::get_week_number($timestamp);
}
}