<?php
/*
* @name: Punkt . 16 PHP Calendar
* @author: Florian Palme
* @date: 21.12.2009
* @class: Punkt16_Calendar
* @file: calendar.class.php
* @package: Punkt16
* @subpackage: Calendar
* @version: 1.1.1
*/
class Punkt16_Calendar
{
protected $now;
protected $temp;
protected $temp2;
protected $daysLong = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
protected $daysShort = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
protected $monthsLong = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
protected $monthsShort = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
protected $classes = array('table' => 'calendar', 'tableTitle' => 'title', 'monthBackwardA' => 'monthBackwardA', 'monthForwardA' => 'monthForwardA', 'monthYear' => 'monthYear', 'dayA' => 'dayA', 'dayTodayA' => 'dayTodayA', 'dayOther' => 'dayOther', 'dayName' => 'dayName');
protected $inserts = array('monthBackward' => '«', 'monthForward' => '»');
protected $n = "\n";
protected $getParams = array('month' => 'm', 'year' => 'y');
protected $holidays = array(
array('day' => '01.1.*', 'name' => 'Neujahrstag', 'class' => 'holiday', 'url' => ''),
array('day' => '06.1.*', 'name' => 'Heilige Drei Könige [BW, BY, ST]', 'class' => 'holiday', 'url' => ''),
array('day' => '05.4.*', 'name' => 'Gründonnerstag [BW]', 'class' => 'holiday', 'url' => ''),
array('day' => '06.4.*', 'name' => 'Karfreitag', 'class' => 'holiday', 'url' => ''),
array('day' => '09.4.*', 'name' => 'Ostermontag', 'class' => 'holiday', 'url' => ''),
array('day' => '01.5.*', 'name' => 'Tag der Arbeit', 'class' => 'holiday', 'url' => ''),
array('day' => '17.5.*', 'name' => 'Christi Himmelfahrt', 'class' => 'holiday', 'url' => ''),
array('day' => '28.5.*', 'name' => 'Pfingstmontag', 'class' => 'holiday', 'url' => ''),
array('day' => '07.6.*', 'name' => 'Fronleichnam [BW, BY, HE, NW, RP, SL]', 'class' => 'holiday', 'url' => ''),
array('day' => '15.8.*', 'name' => 'Mariä Himmelfahrt [BY, SL]', 'class' => 'holiday', 'url' => ''),
array('day' => '03.10.*', 'name' => 'Tag der Deutschen Einheit', 'class' => 'holiday', 'url' => ''),
array('day' => '31.10.*', 'name' => 'Reformationstag [BW, BB, MV, SN, ST, TH]', 'class' => 'holiday', 'url' => ''),
array('day' => '01.11.*', 'name' => 'Allerheiligen [BW, BY, NW, RP, SL]', 'class' => 'holiday', 'url' => ''),
array('day' => '21.11.*', 'name' => 'Buß- und Bettag [BY, SN]', 'class' => 'holiday', 'url' => ''),
array('day' => '24.12.*', 'name' => 'Weihnachten', 'class' => 'christmas', 'url' => ''),
array('day' => '25.12.*', 'name' => '1. Weihnachtstag', 'class' => 'holiday', 'url' => ''),
array('day' => '26.12.*', 'name' => '2. Weihnachtstag', 'class' => 'holiday', 'url' => '')
);
protected $showHolidays = true;
protected $showAllUrls = true;
protected $timer = array('start' => 0, 'end' => 0, 'time' => 0);
protected $holiSep = ' - ';
/*
* @access: public
* @param: array params
*/
public function __construct($params = array())
{
$timerStart = explode(' ', microtime());
$this->timer['start'] = $timerStart[0] + $timerStart[1];
$this->now = time();
foreach($params as $param => $paramval)
{
if(is_array($paramval))
{
foreach($paramval as $paramvalparam => $paramvalparamval)
{
$theArray = $this->$param;
$theArray[$paramvalparam] = $paramvalparamval;
$this->$param = $theArray;
}
}
else
{
$this->$param = $paramval;
}
}
if(isset($_GET[$this->getParams['month']]))
{
$month = intval($_GET[$this->getParams['month']]);
if($month <= 12 && $month > 0)
{
$this->setCalendarMonth($month);
}
}
if(isset($_GET[$this->getParams['year']]))
{
$year = intval($_GET[$this->getParams['year']]);
if($year >= 1970)
{
$this->setCalendarYear($year);
}
}
}
/*
* @access: public
*/
public function getCalendar()
{
$this->temp = $this->createTempTimestamp($this->now);
$this->temp2 = $this->temp;
$date = array();
$date['month'] = $this->getMonth($this->now);
$date['monthName'] = $this->getMonthName($this->now);
$date['year'] = $this->getYear($this->now);
$date['firstDayNum'] = $this->getFirstDayNum($this->now);
$calendarOutput = $this->htmlTop($date['monthName'], $date['year']);
$itsToday = date('d.n.Y', time());
for($t = 0; $t < $date['firstDayNum']; $t++)
{
$this->temp2 = $this->removeDay($this->temp2);
}
$calendarOutput .= $this->htmlOpenRow();
for($b = 0; $b < 7; $b++)
{
$calendarOutput .= $this->htmlDayName($this->daysShort[$b]);
}
$calendarOutput .= $this->htmlCloseRow();
$isFirstRow = true;
for($i = 0; $i < 6; $i++)
{
$calendarOutput .= $this->htmlOpenRow();
for($b = 0; $b < 7; $b++)
{
/* Prüfen, ob dieser Tag ein Feiertag ist! */
$holiArray = array();
if($this->showHolidays == true)
{
$theDay = $this->getDay($this->temp2);
$theMonth = $this->getMonth($this->temp2);
$theYear = $this->getYear($this->temp2);
$holidays = array();
$holidays['full'] = $theDay . '.' . $theMonth . '.' . $theYear;
$holidays['day'] = '*.' . $theMonth . '.' . $theYear;
$holidays['month'] = $theDay . '.*.' . $theYear;
$holidays['year'] = $theDay . '.' . $theMonth . '.*';
$holidays['dayMonth'] = '*.*.' . $theYear;
$holidays['dayYear'] = '*.' . $theMonth . '.*';
$holidays['monthYear'] = $theDay . '.*.*';
foreach($this->holidays as $holiday)
{
if(in_array($holiday['day'], $holidays))
{
$holiArray[] = array('name' => $holiday['name'], 'class' => $holiday['class'], 'url' => $holiday['url']);
}
}
}
/* ENDE der Überprüfung */
if($isFirstRow == true)
{
if($b < $date['firstDayNum'])
{
$calendarOutput .= $this->htmlDay($this->getDay($this->temp2), $this->classes['dayOther'], $holiArray);
$this->temp2 = $this->addDay($this->temp2);
}
else
{
$theDay = $this->getDay($this->temp);
$theMonth = $this->getMonth($this->temp);
$cl = ($itsToday == $theDay . '.' . $theMonth . '.' . $date['year']) ? $this->classes['dayTodayA'] : '';
$calendarOutput .= $this->htmlDay($theDay, $cl, $holiArray);
$this->temp = $this->addDay($this->temp);
$this->temp2 = $this->addDay($this->temp2);
}
}
else
{
$theDay = $this->getDay($this->temp);
$theMonth = $this->getMonth($this->temp);
$cl = ($itsToday == $theDay . '.' . $theMonth . '.' . $date['year']) ? $this->classes['dayTodayA'] : '';
$cl = ($date['month'] == $theMonth) ? $cl : $this->classes['dayOther'];
$calendarOutput .= $this->htmlDay($theDay, $cl, $holiArray);
$this->temp = $this->addDay($this->temp);
$this->temp2 = $this->addDay($this->temp2);
}
}
$calendarOutput .= $this->htmlCloseRow();
$isFirstRow = false;
}
$calendarOutput .= $this->htmlBottom();
return $calendarOutput;
}
/*
* @access: public
* @params: int month
*/
public function setCalendarMonth($month)
{
$month = intval($month);
$thisYear = date('Y', $this->now);
$newTimestamp = mktime(0, 0, 0, $month, 1, $thisYear);
$this->now = $newTimestamp;
}
/*
* @access: public
* @params: int year
*/
public function setCalendarYear($year)
{
$year = intval($year);
$thisMonth = date('n', $this->now);
$newTimestamp = mktime(0, 0, 0, $thisMonth, 1, $year);
$this->now = $newTimestamp;
}
/*
* @access: private
* @param: string dayname, string day, string month, string year
*/
private function htmlTop($month, $year)
{
return '<table class="' . $this->classes['table'] . '" border="0" cellpadding="0" cellspacing="5">' . $this->n .
' <tbody>' . $this->n .
' <tr>' . $this->n .
' <td colspan="7">' . $this->n .
' <table class="' . $this->classes['tableTitle'] . '" border="0" cellpadding="0" cellspaing="0">' . $this->n .
' <tbody>' . $this->n .
' <tr>' . $this->n .
' <td class="' . $this->classes['monthBackwardA'] . '">' . $this->n .
' <a href="' . $this->createPrevMonthUrl($this->now) . '">' . $this->n .
' ' . $this->inserts['monthBackward'] . $this->n .
' </a>' . $this->n .
' </td>' . $this->n .
' <td class="' . $this->classes['monthYear'] . '">' . $this->n .
' ' . $month . ' ' . $year . $this->n .
' </td>' . $this->n .
' <td class="' . $this->classes['monthForwardA'] . '">' . $this->n .
' <a href="' . $this->createNextMonthUrl($this->now) . '">' . $this->n .
' ' . $this->inserts['monthForward'] . $this->n .
' </a>' . $this->n .
' </td>' . $this->n .
' </tr>' . $this->n .
' </tbody>' . $this->n .
' </table>' . $this->n .
' </td>' . $this->n .
' </tr>' . $this->n;
}
/*
* @access: private
*/
private function htmlOpenRow()
{
return '<tr>' . $this->n;
}
/*
* @access: private
*/
private function htmlCloseRow()
{
return '</tr>' . $this->n;
}
/*
* @access: private
* @param: string val (, string css, array holiday)
*/
private function htmlDay($val, $css = '', $holidays = array())
{
$css = ($css == '') ? '' : ' ' . $css;
$holiday = array('name' => '', 'class' => '', 'url' => '', 'url2' => '');
foreach($holidays as $holi)
{
$holiday['name'] .= $holi['name'] . $this->holiSep;
$holiday['class'] .= $holi['class'] . ' ';
$holiday['url'] = $holi['url'];
$holiday['url2'] .= $holi['url'] . $this->holiSep;
}
$holiday['name'] = substr($holiday['name'], 0, strlen($holiday['name']) - strlen($this->holiSep));
$holiday['class'] = substr($holiday['class'], 0, strlen($holiday['class']) - 1);
$holiday['url2'] = substr($holiday['url2'], 0, strlen($holiday['url2']) - strlen($this->holiSep));
if($holiday['name'] != '')
{
$return = ' <td class="' . $this->classes['dayA'] . $css . '">' . $this->n;
if($holiday['url'] != '')
{
$longdesc = 'longdesc="';
if($this->showAllUrls == true)
{
$longdesc .= $holiday['url2'];
}
$longdesc .= '"';
$return .= ' <a href="' . $holiday['url'] . '" ' . $longdesc . ' title="' . $holiday['name'] . '" class="' . $holiday['class'] . '">' . $this->n .
' ' . $val . $this->n .
' </a>' . $this->n;
}
else
{
$return .= ' <span title="' . $holiday['name'] . '" class="' . $holiday['class'] . '">' . $this->n .
' ' . $val . $this->n .
' </span>';
}
$return .= '</td>'. $this->n;
return $return;
}
else
{
return ' <td class="' . $this->classes['dayA'] . $css . '">' . $this->n .
' ' . $val . $this->n .
' </td>' . $this->n;
}
}
/*
* @access: private
* @param: string day
*/
private function htmlDayName($day)
{
return ' <td class="' . $this->classes['dayName'] . '">' . $this->n .
' ' . $day . $this->n .
' </td>' . $this->n;
}
/*
* @access: private
*/
private function htmlBottom()
{
$timerEnd = explode(' ', microtime());
$this->timer['end'] = $timerEnd[0] + $timerEnd[1];
$this->timer['time'] = round($this->timer['end'] - $this->timer['start'], 6);
return ' </tbody>' . $this->n .
'</table>' . $this->n .
'<!-- Punkt16 PHP Calendar www.punkt16.de // Time: ' . $this->timer['time'] . ' -->' . $this->n . $this->n;
}
/*
* @access: private
* @params: int timestamp
*/
private function getRealDay($timestamp)
{
$day = intval(date('w', $timestamp)) - 1;
$day = ($day == -1) ? 6 : $day;
return $day;
}
/*
* @access: private
* @params: int timestamp
*/
private function getDayName($timestamp)
{
$day = $this->getRealDay($timestamp);
return $this->daysLong[$day];
}
/*
* @access: private
* @params: int timestamp
*/
private function getDay($timestamp)
{
return date('d', $timestamp);
}
/*
* @access: private
* @params: int timestamp
*/
private function getMonth($timestamp)
{
return date('n', $timestamp);
}
/*
* @access: private
* @params: int timestamp
*/
private function getMonthName($timestamp)
{
$month = date('n', $timestamp) - 1;
return $this->monthsLong[$month];
}
/*
* @access: private
* @params: int timestamp
*/
private function getYear($timestamp)
{
return date('Y', $timestamp);
}
/*
* @access: private
* @params: int timestamp
*/
private function getFirstDayNum($timestamp)
{
$thisMonth = date('n', $timestamp);
$thisYear = date('Y', $timestamp);
$realTime = mktime(0, 0, 0, $thisMonth, 1, $thisYear);
$day = $this->getRealDay($realTime);
return $day;
}
/*
* @access: private
* @params: int timestamp
*/
private function addDay($timestamp)
{
/*
* 60 Seconds = 1 Minute
* 1 Hour = 60 Minutes @ 60 * 60
* 1 Day = 24 Hours @ 60 * 60 * 25
*/
return $timestamp + (60 * 60 * 24);
}
/*
* @access: private
* @params: int timestamp
*/
private function removeDay($timestamp)
{
return $timestamp - (60 * 60 * 24);
}
/*
* @access: private
* @params: int timestamp
*/
private function createTempTimestamp($timestamp)
{
$thisMonth = date('n', $timestamp);
$thisYear = date('Y', $timestamp);
$realTime = mktime(0, 0, 0, $thisMonth, 1, $thisYear);
return $realTime;
}
/*
* @access: private
* @params: int timestamp
*/
private function createPrevMonthUrl($timestamp)
{
$thisMonth = date('n', $timestamp);
$thisYear = date('Y', $timestamp);
$prevMonth = $thisMonth - 1;
$prevYear = $thisYear;
if($prevMonth == 0)
{
$prevMonth = 12;
$prevYear = $thisYear - 1;
}
$prevTimestamp = mktime(0, 0, 0, $prevMonth, 1, $prevYear);
return '?' . $this->getParams['month'] . '=' . date('n', $prevTimestamp) . '&' . $this->getParams['year'] . '=' . date('Y', $prevTimestamp);
}
/*
* @access: private
* @params: int timestamp
*/
private function createNextMonthUrl($timestamp)
{
$thisMonth = date('n', $timestamp);
$thisYear = date('Y', $timestamp);
$nextMonth = $thisMonth + 1;
$nextYear = $thisYear;
if($nextMonth == 13)
{
$nextMonth = 1;
$nextYear = $thisYear + 1;
}
$nextTimestamp = mktime(0, 0, 0, $nextMonth, 1, $nextYear);
return '?' . $this->getParams['month'] . '=' . date('n', $nextTimestamp) . '&' . $this->getParams['year'] . '=' . date('Y', $nextTimestamp);
}
/*
* @access: private
* @params: string var, array array
*/
private function setArray($var, $array)
{
foreach($array as $param => $val)
{
$theArray = $this->$var;
$theArray[$param] = $val;
$this->$var = $theArray;
}
}
/* GET a SET */
public function getNow()
{
return $this->now;
}
public function setNow($param)
{
$this->now = $param;
}
public function getDaysLong()
{
return $this->daysLong;
}
public function setDaysLong($param)
{
$this->daysLong = $param;
}
public function getDaysShort()
{
return $this->daysShort;
}
public function setDaysShort($param)
{
$this->daysShort = $param;
}
public function getMonthLong()
{
return $this->monthLong;
}
public function setMontzLong($param)
{
$this->monthLong = $param;
}
public function getMonthShort()
{
return $this->monthShort;
}
public function setMonthShort($param)
{
$this->monthShort = $param;
}
public function getparams()
{
return $this->getParams;
}
public function setparams($param)
{
$this->setArray('setparams', $param);
}
public function getClasses()
{
return $this->getClasses;
}
public function setClasses($param)
{
$this->setArray('classes', $param);
}
public function getInserts()
{
return $this->inserts;
}
public function setInserts($param)
{
$this->setArray('inserts', $param);
}
public function getShowHolidays()
{
return $this->showHolidays;
}
public function setShowHolidays($param)
{
$this->showHolidays = $param;
}
public function getShowAllUrls()
{
return $this->showAllUrls;
}
public function setShowAllUrls($param)
{
$this->showAllUrls = $param;
}
public function getHoliSep()
{
return $this->holiSep();
}
/*
* @access: public
* @params: string name, string date (, string url, string class)
*/
public function addHoliday($name, $date, $url = '', $class = 'holiday')
{
$this->holidays[] = array('name' => $name, 'day' => $date, 'class' => $class, 'url' => $url);
}
/*
* @access: public
* @params: string day
*/
public function removeHoliday($day)
{
for($i = 0; $i < count($this->holidays); $i++)
{
if($this->holidays[$i]['day'] == $day)
{
unset($this->holidays[$i]);
}
}
$this->holidays = $this->array_nindex($this->holidays);
}
/*
* @access: public
* @params: string from, string to
*/
public function addVacation($from, $to, $name, $url = '', $class = '')
{
$fromArray = explode('.', $from);
$toArray = explode('.', $to);
$fromTimestamp = mktime(0, 0, 0, $fromArray[1], $fromArray[0], $fromArray[2]);
$toTimestamp = mktime(0, 0, 0, $toArray[1], $toArray[0], $toArray[2]);
$timestamp = $toTimestamp - $fromTimestamp;
$days = $timestamp / 60 / 60 / 24;
for($i = 0; $i <= $days; $i++)
{
$this->holidays[] = array('day' => date('d.m.Y', $fromTimestamp), 'name' => $name, 'url' => $url, 'class' => $class);
$fromTimestamp = $this->addDay($fromTimestamp);
}
}
/*
* @access: public
* @params: string name
*/
public function removeVacation($name)
{
$count = count($this->holidays);
for($c = 0; $c < $count; $c++)
{
if($this->holidays[$c]['name'] == $name)
{
unset($this->holidays[$c]);
}
}
$this->holidays = $this->array_nindex($this->holidays);
}
/*
* @access: private
* @params: array array
*/
private function array_nindex($array)
{
$newArray = array();
foreach($array as $elem)
{
$newArray[] = $elem;
}
return $newArray;
}
}
?>