22
Mär/10
0

ICS Klasse, generiert Kalender

(iframe AG, S-Node)

Mein Ziel war es eine Klasse für das S-Node CMS der Firma iframe AG zu schreiben, welche aus einem zweidimensionalen Array (zb. ein Datenbankresultset) einen Kalenderim ics bzw. ical Format generiert.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
<?php
 
/**
*
* Distributed under the GNU Lesser General Public License (LGPL v3)
* (http://www.gnu.org/licenses/lgpl.html)
* This program is distributed in the hope that it will be useful -
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @author Dominik Zogg <dominik.zogg@gmail.com>
* @copyright Copyright (c) 2011, Dominik Zogg
*
*/
 
class ics
{
/**
* @var array $_arrCalendar the calendar array
*/
protected $_arrCalendar = array();
/**
* @var string $_strIcsHeader the header of the calendar
*/
protected $_strIcsHeader = "BEGIN:VCALENDAR\r\nPRODID:-//php/ics\r\nVERSION:2.0\r\nMETHOD:PUBLISH\r\n";
/**
* @var string $_strIcsFooter the footer of the calendar
*/
protected $_strIcsFooter = 'END:VCALENDAR';
 
/**
* @var string $_strIcs ics string
*/
protected $_strIcs = '';
 
/**
* __construct
* @param array $arrCalendar the calendar as an array
*/
public function __construct(array $arrCalendar)
{
$this->_arrCalendar = $arrCalendar;
$this->_strIcs = $this->_strIcsHeader;
foreach($this->_arrCalendar as $arrEvent)
{
$this->_strIcs .= self::generateEventString($arrEvent);
}
$this->_strIcs .= $this->_strIcsFooter;
}
/**
* getFile
* @param string $strFilename the names of the file
*/
public function getFile($strFilename) {
ob_start();
header("Content-type: text/calendar");
header('Content-Disposition: attachment; filename="' . $strFilename . '"');
echo $this->_strIcs;
ob_flush();
die();
}
/**
* getString
* @return string ics string
*/
public function getString()
{
return $this->_strIcs;
}
 
/**
*
* generateEventString
* @param array $arrEvent
* @return string event as ics string
*/
public static function generateEventString(array $arrEvent)
{
$strReturn = "BEGIN:VEVENT\r\n";
$arrEventParts = array();
// set uid
if(isset($arrEvent['id']))
{
$arrEventParts['UID'] = md5($arrEvent['id'] . "@" . $_SERVER['SERVER_NAME']);
}
 
// set creation date
if(isset($arrEvent['creation_date']))
{
$arrEventParts['DTSTAMP'] = gmstrftime("%Y%m%dT%H%M00Z", $arrEvent['creation_date']);
}
elseif(isset($arrEvent['from_date']))
{
$arrEventParts['DTSTAMP'] = gmstrftime("%Y%m%dT%H%M00Z", $arrEvent['from_date']);
}
 
// set start time of the event
if(isset($arrEvent['from_date']))
{
$arrEventParts['DTSTART'] = gmstrftime("%Y%m%dT%H%M00Z", $arrEvent['from_date']);
}
// set end time of the event
if(isset($arrEvent['end_date']))
{
$arrEventParts['DTEND'] = gmstrftime("%Y%m%dT%H%M00Z", $arrEvent['end_date']);
}
 
// set summary
if(isset($arrEvent['title']))
{
$arrEventParts['SUMMARY'] = self::cleanString($arrEvent['title']);
}
 
// set description
if(isset($arrEvent['text']))
{
$arrEventParts['DESCRIPTION'] = self::cleanString($arrEvent['text']);
}
 
// set location
if(isset($arrEvent['location']))
{
$arrEventParts['LOCATION'] = self::cleanString($arrEvent['location']);
}
 
// check if all needed values are set if not throw exception
if(!isset($arrEventParts['UID']) ||
!isset($arrEventParts['DTSTAMP']) ||
!isset($arrEventParts['DTSTART']) ||
!isset($arrEventParts['DTEND']) ||
!isset($arrEventParts['SUMMARY']))
{
throw new Exception('at least one missing value');
}
// add event parts to return string
foreach($arrEventParts as $strKey => $strValue)
{
$strReturn .= $strKey . ":" . $strValue . "\r\n";
}
// add end to return string
$strReturn .= "END:VEVENT" . "\r\n";
// return event string
return($strReturn);
}
 
/**
* cleanString
* @param string $strDirtyString the dirty input string
* @return string cleaned string
*/
public static function cleanString($strDirtyString)
{
$arrBadSigns = array('<br />', '<br/>', '<br>', "\r\n", "\r", "\n", "\t", '"');
$arrGoodSigns = array('\n', '\n', '\n', '', '', '', ' ', '\"');
return(trim(str_replace($arrBadSigns, $arrGoodSigns, strip_tags($strDirtyString, '<br>'))));
}
}
view raw ics.class.php This Gist brought to you by GitHub.

Zurück

Einen Kommentar schreiben