Created
July 18, 2014 20:52
-
-
Save matthewstokeley/356b3816a4ee46695ed8 to your computer and use it in GitHub Desktop.
parse google calendar xml feed incomplete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Parsing google calendar xml feed **/ | |
class GoogleCal { | |
protected $feed_url; | |
protected $data; | |
public $dates = array(); | |
public $line = array(); | |
function __construct($feed_url) { | |
$this->setFeedUrl($feed_url); | |
$this->setData($this->getRawData()); | |
} | |
public function setFeedUrl($feed_url) | |
{ | |
$this->feed_url = $feed_url; | |
return $this; | |
} | |
public function getFeedUrl() | |
{ | |
return $this->feed_url; | |
} | |
public function setData($data) | |
{ | |
$this->data = $data; | |
return $this; | |
} | |
public function getData() | |
{ | |
return $this->data; | |
} | |
public function setLine(array $line) | |
{ | |
$this->line = $line; | |
return $this; | |
} | |
public function getLine() | |
{ | |
return $this->line; | |
} | |
public function setDates(array $dates) | |
{ | |
$this->dates = $dates; | |
return $this; | |
} | |
public function getDates() | |
{ | |
return $this->dates; | |
} | |
protected function get_string_between($string, $start, $end) | |
{ | |
$string = " ".$string; | |
$ini = strpos($string,$start); | |
if ($ini == 0) return ""; | |
$ini += strlen($start); | |
$len = strpos($string,$end,$ini) - $ini; | |
return substr($string,$ini,$len); | |
} | |
protected function getFeedContents() { | |
return file_get_contents($this->getFeedUrl()); | |
} | |
protected function getRawData() { | |
return new SimpleXMLElement($this->getFeedContents()); | |
} | |
protected function formatDate($content) { | |
$time_string = $this->get_string_between($content, 'When:', ', 2014'); | |
$time_string = $time_string . ' 2014'; | |
$time_string = strtotime($time_string); | |
return date("m-d-Y", $time_string); | |
} | |
protected function formatHtml($title, $url) { | |
return '<a href="'.$url.'">'.$title.'</a>'; | |
} | |
protected function formatEndLine($ii, $len) { | |
if ($ii == $len) { | |
return ''; | |
} else { | |
return ', '; | |
} | |
} | |
protected function pushDate($date) { | |
if(!in_array($this->dates)) { | |
array_push($this->dates, $date); | |
} | |
} | |
protected function pushLine($line) { | |
array_push($this->line, $line); | |
} | |
protected function formatLine() { | |
$dates = array_unique($this->dates); | |
} | |
protected function combineHtml($html) { | |
} | |
protected function iterate() { | |
$ii = 0; | |
$len = count($this->data->entry) - 1; | |
foreach ($this->data->entry as $key => $val) { | |
$date = $this->formatDate($val->summary); | |
$html = $this->formatHtml($val->title, $val->link['href']); | |
$line = array( | |
'date' => $date, | |
'html' => $html, | |
'end' => $this->formatEndLine($ii, $len) | |
); | |
$this->pushDate($date); | |
$this->pushLine($line); | |
$ii++; | |
} | |
} | |
public function display() { | |
$this->iterate(); | |
$this->formatLine(); | |
} | |
public function openTags() { | |
return '<script type="text/javascript"> | |
var codropsEvents = {'; | |
} | |
public function closingTags() { | |
return '}; | |
</script>'; | |
} | |
} | |
// implementation | |
$feed_url = ''; | |
$cal = new GoogleCal($feed_url); | |
echo $cal->openTags(); | |
echo $cal->display(); | |
echo $cal->closingTags(); | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment