Skip to content

Commit

Permalink
basic iCal file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Apr 2, 2011
1 parent 1921607 commit de3e482
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/CreepyCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteDayOfWeekData.php';
$dataManager->writeData(new WriteDayOfWeekData(array()));

require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteICalData.php';
$dataManager->writeData(new WriteICalData(array('file'=>'out.ical')));


55 changes: 55 additions & 0 deletions src/WriteICalData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright (C) 2011 James (james at jarofgreen dot co dot uk)
*
* 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/>.
**/


class WriteICalData extends BaseWriteClass {

public function write() {

if (!$handle = fopen($this->configData['file'], 'w')) {
throw new Exception("Cannot open file");
}

$this->fwrite($handle,"BEGIN:VCALENDAR\r\n");
$this->fwrite($handle,"VERSION:2.0\r\n");
$this->fwrite($handle,"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n");

foreach($this->dataManager->getData() as $i=>$item) {
$this->fwrite($handle,"BEGIN:VEVENT\r\n");
$this->fwrite($handle,"UID:uid".$i."@example.com\r\n");
$this->fwrite($handle,"SUMMARY: Event\r\n");
$this->fwrite($handle,"DTSTAMP:".$item->getDateTimeAs("Ymd\THis\Z")."\r\n");
$this->fwrite($handle,"DTSTART:".$item->getDateTimeAs("Ymd\THis\Z")."\r\n");
$this->fwrite($handle,"DTEND:".$item->getDateTimeAs("Ymd\THis\Z")."\r\n");
$this->fwrite($handle,"END:VEVENT\r\n");
}

$this->fwrite($handle,"END:VCALENDAR\r\n");

fclose($handle);

}

private function fwrite($handle,$data) {
if (fwrite($handle, $data) === FALSE) {
throw new Exception("Cannot write to file");
}
}

}

0 comments on commit de3e482

Please sign in to comment.