Add file to import google feed feature/googleEventCalendarImport
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 18 Aug 2014 17:23:54 +0000 (13:23 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 18 Aug 2014 17:23:54 +0000 (13:23 -0400)
Using php import the xml feed from google into our Events

Toolkit/Events/googleFeedReader.php [new file with mode: 0644]

diff --git a/Toolkit/Events/googleFeedReader.php b/Toolkit/Events/googleFeedReader.php
new file mode 100644 (file)
index 0000000..44d4605
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+require_once '../../setup.phtml';
+if (!defined('COMMON_APP_BASE')) {
+    define('COMMON_APP_BASE', '/var/www/server/CommonApps/');
+}
+
+define('COMMON_EVENTS_SCHEMA', 'events');
+require_once COMMON_APP_BASE . 'EventCalendar/V1/models/EventMapper.php';
+$eventMapper = new EventMapper(Toolkit_Database::getInstance());
+
+define(
+    'GOOGLE_CALENDAR_FEED',
+//    'https://www.google.com/calendar/feeds/gaslightcalendar%40gmail.com/'
+//    . 'private-d90d9b0557062c92882daff93dc2e581/full'
+    'https://www.google.com/calendar/feeds/keweenawcvb%40gmail.com/public/full'
+);
+
+$feed_url = GOOGLE_CALENDAR_FEED;
+
+libxml_use_internal_errors(TRUE);
+try {
+//    $feed_url .= '?singleevents=true&futureevents=true&orderby=starttime&sortorder=a';
+//    $feed_url .= '?singleevents=true&orderby=starttime&sortorder=a';
+    $feed_url .= '?futureevents=true&orderby=starttime&sortorder=a';
+       // params: url, options, and flag to show first param is a url
+       $feed_xml = new SimpleXMLElement($feed_url, NULL, TRUE);
+} catch(Exception $e) {
+       var_dump($e);
+       exit;
+}
+
+$events = $events2 = array();
+$currentDate = date('m/d/Y');
+foreach ($feed_xml->entry as $event) {
+    // found in attribute of feed node
+       $gd_nodes   = $event->children('http://schemas.google.com/g/2005');
+    // found in attribute of feed node
+       $gcal_nodes = $event->children('http://schemas.google.com/gCal/2005');
+
+    $place = $address = $city = $state = $zip = '';
+
+       $id          = (string) $gcal_nodes->uid->attributes()->value;
+       $title       = (string) $event->title;
+       $description = (string) $event->content;
+       $location    = (string) $gd_nodes->where->attributes()->valueString;
+       $startTime   = (string) $gd_nodes->when->attributes()->startTime;
+       $endTime     = (string) $gd_nodes->when->attributes()->endTime;
+    $btime       = strtotime($startTime);
+    $etime       = strtotime($endTime);
+    $events2[]    = array(
+        'id'          => $id,
+        'title'       => $title,
+        'description' => $description,
+        'location'    => $location,
+        'startTime'   => $startTime,
+        'endTime'     => $endTime
+    );
+    if (strpos($location, ',')) {
+        $loc  = explode(',', $location);
+        foreach ($loc as $key => $value) {
+            $loc[$key] = trim($value);
+        }
+        echo '<pre>'.print_r($loc, true).'</pre>';
+        $size = count($loc);
+        switch ($size) {
+        case 1:
+            $place = $location;
+            break;
+        case 2:
+            $place   = $loc[0];
+            $address = $loc[2];
+            break;
+        case 3:
+            if (in_array($loc[2], array('United States', 'USA'))) {
+                $place   = '';
+                $city = $loc[0];
+                $state   = $loc[1];
+            } else {
+                $place   = $loc[0];
+                $address = $loc[1];
+                $state   = $loc[2];
+            }
+            break;
+        case 5:
+            $place   = $loc[0];
+            $address = $loc[1];
+            $city    = $loc[2];
+            $state   = $loc[3];
+            break;
+        case 6:
+            $place   = $loc[0];
+            $address = $loc[1];
+            $city    = $loc[2];
+            $state   = $loc[4];
+            break;
+        }
+    }
+    $eventData = array(
+        'created'       => $currentDate,
+        'approved'      => $currentDate,
+        'active'        => true,
+        'category'      => null,
+        'starting'      => date('m/d/Y', $btime),
+        'ending'        => date('m/d/Y', $etime),
+        'starthour'     => (date('H:i A', $btime) == '00:00 AM')
+            ? null
+            : date('H:i A', $btime),
+        'endhour'       => (date('H:i A', $etime) == '00:00 AM')
+            ? null
+            : date('H:i A', $etime),
+        'description'   => $description,
+        'header'        => $title,
+        'place'         => $place,
+        'address'       => $address,
+        'city'          => $city,
+        'state'         => $state,
+        'zip'           => $zip
+    );
+    if (MEMBERS_DB) {
+        $event = MemberEvent::createByValues($eventData);
+    } else {
+        $event = Event::createByValues($eventData);
+    }
+    $eventMapper->saveEvent($event);
+    $events[] = $event;
+}
+echo '<pre>'.print_r($events2, true).'</pre>';
+echo '<pre>'.print_r($events, true).'</pre>';
\ No newline at end of file