<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
class CalendarPlugin implements IPlugin
{
private $dbconn = NULL;
private $config = NULL;
public function __construct($dbconn, $config) {
$this->dbconn = $dbconn;
$this->config = $config;
}
public function doUpdate() {
try
{
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($this->config['user'], $this->config['password'],$service);
}
catch (Exception $e)
{
die ('Folgender Fehler trat auf: ' . $e->getMessage());
}
$cal = new Zend_Gdata_Calendar($client);
$calendarList = $cal->getCalendarListFeed();
mysql_query("START TRANSACTION", $this->dbconn);
$query = "Delete from if_calendar";
mysql_query($query, $this->dbconn) or die('Error, delete query failed');
foreach ($calendarList as $calendar)
{
if (!$calendar->selected->getValue())
continue;
$color = $calendar->color;
$cal_id = substr(strrchr($calendar->id, '/'), 1 );
$startDate=strftime( '%Y-%m-%d');
$endDate = strftime( '%Y-%m-%d', strtotime("+".$this->config['number_of_days']." day", time() ) );
$query = $cal->newEventQuery();
$query->setUser($cal_id);
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setStartMin($startDate);
$query->setStartMax($endDate);
$query->setSortOrder('ascending');
$query->setSingleEvents(true);
$entries = $cal->getCalendarEventFeed($query);
foreach ($entries as $entry)
{
$title = $entry->title;
$begin = strtotime($entry->when[0]->startTime);
$end = strtotime($entry->when[0]->endTime);
$location = $entry->where[0]->valueString;
$this->addCalendar($color, $begin, $end, $title, $location);
}
}
mysql_query("COMMIT", $this->dbconn);
}
public function doOutput($image, $style, $updateData, &$yoffset) {
if ($updateData)
$this->doUpdate();
$query = "SELECT * FROM `if_calendar` WHERE (`end` > NOW()) ORDER BY `begin` ASC";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) > 0) {
$opt_header = array(
'width' => imagesx($image)-290,
'line_height' => 18,
'align' => ALIGN_LEFT
);
$opt_day = array(
'width' => imagesx($image)-290,
'height' => 14,
'line_height' => 14,
'align' => ALIGN_LEFT
);
$opt_entry = array(
'width' => imagesx($image)-290,
'height' => 12,
'line_height' => 12,
'align' => ALIGN_LEFT,
'word_wrap_hyphen' => '...',
'aggressive_word_wrap' => true,
);
imagettftextboxopt($image, 18, 0, 50, $yoffset, $style['textcolor'], $style['font'], "Nächste Termine", $opt_header);
$icon = ImageCreateFromPNG ( 'resources/icons/clock.png' );
ImageCopy($image, $icon, 20, $yoffset-3, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
$yoffset += 26;
$counter = 0;
$formatDate = "%d.%m.%Y";
$formatTime = "%H:%M";
$today = strftime( $formatDate );
$tomorrow = strftime( $formatDate, strtotime("+1 day", time() ) );
$currently_displayed_day = 0;
while ($row = mysql_fetch_assoc($result)) {
$counter++;
$rest = (mysql_num_rows($result) - $counter) + 1;
if (($yoffset >= (imagesy($image) - 36)) && ($rest > 1)) {
$text = "... $rest weitere Termine";
imagettftextboxopt($image, 14, 0, 24, $yoffset, $style['textcolor'], $style['fontb'], $text, $opt_entry);
$yoffset += 20;
break;
}
$begin_date = strftime( $formatDate, strtotime($row['begin']));
if (strtotime($begin_date) < strtotime($today))
$begin_date = $today;
if (strtotime($begin_date) > $currently_displayed_day) {
$currently_displayed_day = strtotime($begin_date);
if( $begin_date == $today )
$dayname = "Heute";
else if( $begin_date == $tomorrow )
$dayname = "Morgen";
else
$dayname = datumDeutsch(strftime("%A, ".$formatDate, strtotime($begin_date)));
imagettftextboxopt($image, 14, 0, 50, $yoffset-1, $style['textcolor'], $style['fontb'], $dayname, $opt_day);
$yoffset += 22;
}
$end_date = strftime( $formatDate, strtotime($row['end']));
$begin_time = strftime( $formatTime, strtotime($row['begin']));
$end_time = strftime( $formatTime, strtotime($row['end']));
$text = $row['title'];
if ($row['location'] != null)
$text = $text . ", " . $row['location'];
if (($begin_time == "00:00") && ($end_time == "00:00")) {
$end_date = strftime( $formatDate, strtotime($row['end'])-1);
if (strtotime($end_date) != $currently_displayed_day)
$text = $text." (bis ".$end_date.")";
} else {
$text = $text." (".$begin_time." - ";
if (strtotime($end_date) != $currently_displayed_day)
$text = $text.$end_date.", ";
$text = $text.$end_time.")";
}
if (($row['color']) == "#711616") {
if (strpos($row['title'], 'Blaue') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/tonne_blau.png' );
if (strpos($row['title'], 'Biotonne') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/tonne_gruen.png' );
if (strpos($row['title'], 'Gelber Sack') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/gelber_sack.png' );
if (strpos($row['title'], 'Restabfall') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/tonne_schwarz.png' );
ImageCopy($image, $icon, $xoffset+25, $yoffset-15, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
}else{
if (($row['color']) == "#711616") {
if (strpos($row['title'], 'Geburtstag') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/birth.png' );
ImageCopy($image, $icon, $xoffset+30, $yoffset+0, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
}else{
if (($row['color']) == "#711616") {
$icon = ImageCreateFromPNG ( 'resources/icons/name.png' );
ImageCopy($image, $icon, $xoffset+27, $yoffset+1, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
}else{
$colorArray = htmlColorToRgb($row['color']);
$color = ImageColorAllocate ($image, $colorArray[0], $colorArray[1], $colorArray[2]);
drawGlassButton($image, $color, $xoffset+36, $yoffset+8);
}
}
}
imagettftextboxopt($image, 12, 0, $xoffset+50, $yoffset-6, $style['textcolor'], $style['font'], $text, $opt_entry);
$yoffset += 16;
$letzter_eintrag = $currently_displayed_day;
}
$yoffset += 6;
}
mysql_free_result($result);
}
private function addCalendar($color, $begin, $end, $title, $location) {
$query = "INSERT INTO `if_calendar` (`id`, `color`, `begin`, `end`, `title`, `location`)
VALUES (NULL,
'".mysql_real_escape_string($color)."', FROM_UNIXTIME($begin), FROM_UNIXTIME($end),
'".mysql_real_escape_string($title)."',
'".mysql_real_escape_string($location)."'
)";
mysql_query($query, $this->dbconn) or die('CalendarPlugin: '.mysql_error());
}
}