Update for feed develop
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 29 Sep 2015 18:19:24 +0000 (14:19 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 29 Sep 2015 18:19:24 +0000 (14:19 -0400)
correcting category part
format of phone number
correction in description line breaks

index.php

index e1a5e40..20883de 100644 (file)
--- a/index.php
+++ b/index.php
@@ -33,10 +33,30 @@ function getHourArrayFromTime($time)
         'A' => $dateTime->format('A')
     );
 }
+function phoneFrmt($number)
+{
+    $num = preg_replace('%[^0-9]%', '', trim($number));
+    if (strlen($num) == 10) {
+        preg_match('%(\d{3})(\d{3})(\d{4})%', $num, $part);
+        return '('. $part[1] . ') ' . $part[2] . '-' . $part[3];
+    } else if (strlen($num) == 7) {
+        preg_match('%(\d{3})(\d{4})%', $num, $part);
+        return $part[1] . '-' . $part[2];
+    } else {
+        return $num;
+
+    }
+}
+
+if (!$dbh) {
+    $dbh = Toolkit_Database::getInstance();
+}
 $icalHeader = <<<EOD
 BEGIN:VCALENDAR
+PRODID:-//Gaslight Media Inc//Gaslight Events Calendar v2.0/EN
 VERSION:2.0
-PRODID:~//Gaslight Media Inc//Gaslight Events Calendar v2.0/EN
+CALSCALE:GREGORIAN
+METHOD:PUBLISH
 EOD;
 
 $icalFooter = <<<EOD
@@ -50,7 +70,7 @@ CREATED:%s
 DTSTAMP:%s
 DTSTART:%s
 DTEND:%s
-DESCRIPTION:%s
+%s
 SUMMARY:%s
 STATUS:CONFIRMED
 EOD;
@@ -68,6 +88,8 @@ try {
     $sql = "
       SELECT *
         FROM event
+       WHERE edate >= '01/01/2014'::DATE
+         AND visable = true
     ORDER BY bdate,edate";
     $stmt = $dbh->query($sql);
     $events = $stmt->fetchAll(PDO::FETCH_ASSOC);
@@ -88,8 +110,9 @@ $weekdays        =
     array(1 => 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
 foreach ($events as $event) {
     // clean description
-    $description = str_replace("\n", '\n', strip_tags($event['descr']));
+    $description = 'DESCRIPTION:' . str_replace("\n", "\\n", strip_tags($event['descr']));
     $description = str_replace("\r", '', $description);
+    //$description = wordwrap($description, 75, "\\n");
     $eventDataStart = sprintf($eventTemplateStart,
         $event['id'],
         'emmetcounty.org',
@@ -103,9 +126,49 @@ foreach ($events as $event) {
         $event['header']
     );
     $output .= $eventDataStart;
+    // If event has Location data
+    if ($event['lat'] || $event['lon']) {
+        $output .= "\nGEO:" . $event['lat'] . ';' . $event['lon'];
+    }
+    $hasLocation = ($event['loc']
+        || $event['address']
+        || $event['city']
+        || $event['state']
+        || $event['zip']
+    );
+    if ($hasLocation) {
+        $output .= "\nLOCATION:" . $event['loc'].' - ';
+        if ($event['address']) {
+            $output .= "{$event['address']}";
+        }
+        if ($event['city']) {
+            $output .= "\,{$event['city']}";
+        }
+        if ($event['state']) {
+            $output .= "\,{$event['state']}";
+        }
+        if ($event['zip']) {
+            $output .= "\,{$event['zip']}";
+        }
+    }
     // if event has contact data
     if ( $event['contact'] || $event['email'] || $event['phone']) {
-        $output .= "\nCONTACT:" . $event['contact'] . '\,' .$event['email'] .'\,'.$event['phone'];
+        $output .= "\nCONTACT:" . $event['email'] . '\;;' . phoneFrmt($event['phone']) . '\;'.$event['contact'];
+    }
+    if ($event['topicid']) {
+        $getTopic->bindParam(':id', $event['topicid'], PDO::PARAM_INT);
+        $getTopic->execute();
+        $categoryName = $getTopic->fetchColumn();
+        $output .= "\nCATEGORIES:" . $categoryName;
+    }
+    if ($event['cost']) {
+        $output .= "\nX-COST:" . $event['cost'];
+    }
+    if ($event['url']) {
+        if (!preg_match('%http://|https://%', $event['url'])) {
+            $event['url'] = 'http://' . $event['url'];
+        }
+        $output .= "\nX-TICKETS-URL:" . $event['url'];
     }
     // if event is recur
     if ($event['reacur']) {
@@ -150,12 +213,6 @@ foreach ($events as $event) {
             if (!empty($eventDays)) {
                 $daysInWeek = implode(',', $eventDays);
             }
-            if ($event['topicid']) {
-                $getTopic->bindParam(':id', $event['topicid'], PDO::PARAM_INT);
-                $getTopic->execute();
-                $categoryName = $getTopic->fetchColumn();
-                $output .= "\nCATEGORIES:" . $categoryName;
-            }
             $output .= "\nRRULE:" .$freq . ';BYDAY=' . $daysInWeek . ';UNTIL=' . icalDateFormat($event['edate'], getHourArrayFromTime($event['etime']));
         }
     }