<?php
/***************************************************************************
* InfoFrame (image generator for digital picture frames)
* Copyright (C) 2009 Tobias Kolb
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
***************************************************************************/
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() {
// get calendar entries
try
{
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$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();
// delete all old calendar entries
mysql_query("START TRANSACTION", $this->dbconn);
$query = "Delete from if_calendar";
mysql_query($query, $this->dbconn) or die('Error, delete query failed');
// read calendars
foreach ($calendarList as $calendar)
{
// skip unselected calendars
if (!$calendar->selected->getValue())
continue;
// calendar color
$color = $calendar->color;
// get calendar id
$cal_id = substr(strrchr($calendar->id, '/'), 1 );
// set query parameter
$startDate=strftime( '%Y-%m-%d', strtotime("-7 day", time() ));
$endDate = strftime( '%Y-%m-%d', strtotime("+34 day", time() ) );
$query = $cal->newEventQuery();
$query->setUser($cal_id); // set calendar 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)
{
// get entry data
$title = $entry->title;
$begin = strtotime($entry->when[0]->startTime);
$end = strtotime($entry->when[0]->endTime);
$location = $entry->where[0]->valueString;
// add to database
$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()) UNION SELECT * FROM `if_wkw` WHERE (`end` > NOW()) ORDER BY `begin`,`id` ASC";
$result = mysql_query($query, $this->dbconn);
if (mysql_num_rows($result) > 0) {
// define styles
$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,
);
// print header
imagettftextboxopt($image, 18, 0, 50, $yoffset-5, $style['textcolor'], $style['font'], "Nächste Termine", $opt_header);
$icon = ImageCreateFromPNG ( 'resources/icons/clock.png' );
ImageCopy($image, $icon, 20, $yoffset-8, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
$yoffset += 21;
// print calendar
$counter = 0;
$formatDate = "%d.%m.%Y";
$formatTime = "%H:%M";
$today = strftime( $formatDate );
$tomorrow = strftime( $formatDate, strtotime("+1 day", time() ) );
$currently_displayed_day = 0;
$result_arr = array();
while ($row = mysql_fetch_assoc($result)) {
$result_arr[] = array($row['id'], $row['color'],$row['begin'], $row['end'],$row['title'], $row['location']);
$counter++;
// if end of screen is reached and more than one items left -> cut off and show hint "x more appointments..."
$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;
}
//print_r ($result_arr);
if (strtotime($result_arr[$counter-1][2])> strtotime("+".$this->config['number_of_days']." day"))
{
$yoffset += 20;
break;
}
//echo strtotime($result_arr[1][2]);
//echo 'Result: '.strtotime($result_arr[$counter-1][2]);
//echo ' 3Day: '.strtotime("+3 day").' ';
// if ((($row['color']) != "#AB8B00") && (($row['color']) != "#1B887A"))
// {
// ----------------------------------------- DAYNAME --------------------------------------------
// print day name
$begin_date = strftime( $formatDate, strtotime($row['begin']));
// if begin day is before today set begin to today
if (strtotime($begin_date) < strtotime($today))
$begin_date = $today;
// now if begin is after $currently_displayed_day update $currently_displayed_day
if ((strtotime($begin_date) > $currently_displayed_day)) {
if ($counter!=1) $yoffset += 22;
if ((($result_arr[$counter-2][1]) != "#A32929")&&(($result_arr[$counter-2][1]) != "birth")&&($counter!=1))
$yoffset -= 18;
$abst=0; $mull=0;$multiday=0;
// update currently_displayed_day
$currently_displayed_day = strtotime($begin_date);
// print day name
if (($row['color']) == "#1B887A") {$feiert=', '.$row['title'];
$colorArray = htmlColorToRgb($row['color']);
$color = ImageColorAllocate ($image, $colorArray[0], $colorArray[1], $colorArray[2]);
} else {$feiert=''; $color=$style['textcolor'];}
if( $begin_date == $today )
$dayname = "Heute".$feiert;
else if( $begin_date == $tomorrow )
$dayname = "Morgen".$feiert;
else
$dayname = strftime("%A, ".$formatDate, strtotime($begin_date)).$feiert;
imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $color, $style['fontb'], $dayname, $opt_day);
}
// ------------------------------------------------------------------------------------------------
// Ersten Tag darstellen
// if ((strtotime($result_arr[$counter-2][2]) < $currently_displayed_day)&&(strtotime($result_arr[$counter-2][2]) > 0))
//{$yoffset += 22; imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $style['textcolor'], $style['fontb'], $dayname, $opt_day);}
// ---------------------------------------------- Muellabfuhr ---------------------------------
// Icon für Müllabfuhr
if (($row['color']) == "#A32929")
{
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'], 'Gelbe S') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/gelber_sack.png' );
if (strpos($row['title'], 'Restm') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/tonne_schwarz.png' );
if ($mull==0) ImageCopy($image, $icon, 25, $yoffset+1, 0, 0, imagesx($icon), imagesy($icon));
else ImageCopy($image, $icon, 9, $yoffset+1, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
$mull++;
// if (($rest==0)||((strtotime($result_arr[$counter-2][2]) < $currently_displayed_day)&&(strtotime($result_arr[$counter-2][2]) > 0))) {imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $style['textcolor'], $style['fontb'], $dayname, $opt_day); $yoffset += 22;}
$multiday=0;
continue;
}
// -----------------------------------------------------------------------------------------------
// ------------------------------------------- Geburtstag ----------------------------------------
if (($row['color']) == "birth")
{
$tbreite = imagettfbbox ( 14, 0, $style['fontb'], $dayname);
//echo 'last:'.strtotime($result_arr[$counter-2][3]).' ';
//echo 'cur:'.$currently_displayed_day.' ';
//echo 'date:'.$row['end'].' ';
if ((strtotime($result_arr[$counter-2][2])) == ($currently_displayed_day)) {
$a=imagettfbbox ( 11, 0, $style['font'], $result_arr[$counter-2][4]);
$abst=$abst+$a[2]+10;} else {$abst=0;}
$colorArray = htmlColorToRgb('#757575');
$color = ImageColorAllocate ($image, $colorArray[0], $colorArray[1], $colorArray[2]);
imagettftextboxopt($image, 11, 0, $tbreite[2]+60+$abst, $yoffset+5, $color, $style['font'], $row['title'], $opt_entry);
//imagettftextboxopt($image, 8, 0, 50, 100+$yoffset+(12*$counter), $style['textcolor'], $style['font'], 'if('.strtotime($result_arr[$counter-2][2]).'<'.$currently_displayed_day.'_'.$row['title'].'_'.$daypr, $opt_day);
/* if ((strtotime($result_arr[$counter-2][2]) < $currently_displayed_day)&&($daypr==0)&&(strtotime($result_arr[$counter-2][2]) > 0)) {
imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $style['textcolor'], $style['fontb'], $dayname, $opt_day);
$yoffset += 22; $daypr=1; $abst=0;}
else
$daypr=0;
$birth++;*/
$multiday=0;
continue;
}
// ---------------------------------------------------------------------------------------------
/* if ((strtotime($result_arr[$counter-2][2]) < $currently_displayed_day)&&(strtotime($result_arr[$counter-2][2]) > 0))
{$yoffset += 22; imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $style['textcolor'], $style['fontb'], $dayname, $opt_day);}
if ($counter==1) $yoffset += 22;
$mull=0;*/
//}
// ---------------------------------------- TERMIN ----------------------------------------------
if ($multiday!=1) $yoffset += 22;
//$multiday=0;
// build appointment time text
$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")) {
// all day event (substract 1 second to get the real end date at 23:59)
$end_date = strftime( $formatDate, strtotime($row['end'])-1);
// if all day event is longer than one day -> print end date
if (strtotime($end_date) != $currently_displayed_day)
$text = $text." (bis ".$end_date.")";
} else {
// normal event with start and end time
$text = $text." (".$begin_time." - ";
// if event ends not this day -> display end date additionally to time
if (strtotime($end_date) != $currently_displayed_day)
{$text = $text.$end_date.", ";//$multiday=1;
}
$text = $text.$end_time.")";
}
$multiday=1;
// determine button color
$colorArray = htmlColorToRgb($row['color']);
$color = ImageColorAllocate ($image, $colorArray[0], $colorArray[1], $colorArray[2]);
// print appointment text and button
$tbreite = imagettfbbox ( 14, 0, $style['font'], $text);
// drawGlassButton($image, $color, 32+$tbreite[2]/2, $yoffset+8, $tbreite[2]+40);
drawGlassButton($image, $color, 51, $yoffset+10);
//drawGlassButton($image, $color, 15+$tbreite[2], $yoffset+10);
//drawGlassButton($image, $color, 65, $yoffset+10);
//imagefilledrectangle($image, 51, $yoffset+2, $tbreite[2]+15, $yoffset+18, $color);
for ($i = 51; $i <= $tbreite[2]+15; $i++)
{
drawGlassButton($image, $color, $i, $yoffset+10);
}
imagettftextboxopt($image, 12, 0, 50, $yoffset, $style['textcolor'], $style['font'], $text, $opt_entry);
$yoffset += 18;
// ----------------------------------------------------------------------------------------
/*
} elseif (($row['color']) != "#1B887A") {
// print day name
$begin_date = strftime( $formatDate, strtotime($row['begin']));
// if begin day is before today set begin to today
if (strtotime($begin_date) < strtotime($today))
$begin_date = $today;
// now if begin is after $currently_displayed_day update $currently_displayed_day
if (strtotime($begin_date) > $currently_displayed_day) {
// update currently_displayed_day
$currently_displayed_day = strtotime($begin_date);
// print day name
if( $begin_date == $today )
$dayname = "Heute, ".$row['title'];
else if( $begin_date == $tomorrow )
$dayname = "Morgen, ".$row['title'];
else
$dayname = strftime("%A, ".$formatDate, strtotime($begin_date)).", ".$row['title'];
// determine Day color
$colorArray = htmlColorToRgb($row['color']);
$color = ImageColorAllocate ($image, $colorArray[0], $colorArray[1], $colorArray[2]);
imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $color, $style['fontb'], $dayname, $opt_day);
$yoffset += 24;
}
}
else {
// print day name
$begin_date = strftime( $formatDate, strtotime($row['begin']));
// if begin day is before today set begin to today
if (strtotime($begin_date) < strtotime($today))
$begin_date = $today;
// now if begin is after $currently_displayed_day update $currently_displayed_day
if (strtotime($begin_date) > $currently_displayed_day) {
// update currently_displayed_day
$currently_displayed_day = strtotime($begin_date);
// print day name
if( $begin_date == $today )
$dayname = "Heute, ".$row['title'];
else if( $begin_date == $tomorrow )
$dayname = "Morgen, ".$row['title'];
else
$dayname = strftime("%A, ".$formatDate, strtotime($begin_date)).", ".$row['title'];
// determine Day color
$colorArray = htmlColorToRgb($row['color']);
$color = ImageColorAllocate ($image, $colorArray[0], $colorArray[1], $colorArray[2]);
if (($row['color']) == "#A32929")
{
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'], 'Gelbe S') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/gelber_sack.png' );
if (strpos($row['title'], 'Mülltonne') !== false)
$icon = ImageCreateFromPNG ( 'resources/icons/tonne_schwarz.png' );
ImageCopy($image, $icon, 25, $yoffset+1, 0, 0, imagesx($icon), imagesy($icon));
ImageDestroy($icon);
imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $color, $style['fontb'], $dayname, $opt_day);
$yoffset += 24;
continue;
}
imagettftextboxopt($image, 14, 0, 50, $yoffset+4, $color, $style['fontb'], $dayname, $opt_day);
$yoffset += 24;
}
}*/
}
//$yoffset += 22;
}
if ((($result_arr[$counter][1]) == "#A32929")||(($result_arr[$counter][1]) == "birth"))
$yoffset += 18;
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('Error, insert query failed: '.mysql_error());
}
}