Updates on ical feed
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 5 Aug 2016 16:00:47 +0000 (12:00 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 5 Aug 2016 16:00:47 +0000 (12:00 -0400)
Adding the image to the feed and the URL.
This is for work also on the event import of these fields.

models/front/events/icalFeed.php

index 311f80f..bc03aae 100644 (file)
@@ -38,11 +38,11 @@ class GlmMembersFront_events_icalFeed extends GlmMembersFront_events_baseAction
         if ( isset($_REQUEST['eventId']) && $eventId = filter_var($_REQUEST['eventId'], FILTER_VALIDATE_INT)) {
             $search = true;
             $action = 'event-ical';
-        } 
+        }
 
         if ( !isset($eventId) || !$eventId ) {
             $action = 'event-list-ical';
-        } 
+        }
 
         $output = '';
 
@@ -55,8 +55,20 @@ class GlmMembersFront_events_icalFeed extends GlmMembersFront_events_baseAction
             exit;
             break;
         case 'event-list-ical':
+        $icalHeader = <<<EOD
+\r\nBEGIN:VCALENDAR
+PRODID:-//Gaslight Media Inc//Gaslight Events Calendar v2.0/EN
+VERSION:2.0
+CALSCALE:GREGORIAN
+METHOD:PUBLISH
+EOD;
+
+        $icalFooter = <<<EOD
+\nEND:VCALENDAR
+EOD;
+
             $eventSql = "
-              SELECT id 
+              SELECT id
                 FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX. "events
                WHERE id IN (
                         SELECT event
@@ -65,12 +77,14 @@ class GlmMembersFront_events_icalFeed extends GlmMembersFront_events_baseAction
                            AND " . $this->dateRange . "
                     )
             ORDER BY id";
+            $output .= $icalHeader;
             $events = $this->wpdb->get_results( $eventSql, ARRAY_A );
             if ( isset($events) && !empty($events) ) {
                 foreach ( $events as $key => $event ) {
                     $output .= $this->getEventOutput( $event['id'] );
                 }
             }
+            $output .= $icalFooter;
             header('Content-type: text/calendar; charset=utf-8');
             header('Content-Disposition: inline; filename=events.ics');
             echo $output;
@@ -87,22 +101,10 @@ class GlmMembersFront_events_icalFeed extends GlmMembersFront_events_baseAction
         $this->postAddRecurrences = true;
         $event = $this->getModelEventData($eventId);
         $this->postAddRecurrences = false;
-        $icalHeader = <<<EOD
-\r\nBEGIN:VCALENDAR
-PRODID:-//Gaslight Media Inc//Gaslight Events Calendar v2.0/EN
-VERSION:2.0
-CALSCALE:GREGORIAN
-METHOD:PUBLISH
-EOD;
-
-        $icalFooter = <<<EOD
-\nEND:VCALENDAR
-EOD;
-
         $eventTemplateStart = <<<EOD
 \r\nBEGIN:VEVENT
 UID:%d@%s
-ORGANIZER:CN=%s
+ORGANIZER;CN="%s"
 CREATED:%s
 DTSTAMP:%s
 DTSTART:%s
@@ -119,12 +121,36 @@ EOD;
         $timeStampFormat = 'Ymd\THis';
         $weekdays        =
             array(1 => 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
-        $output .= $icalHeader;
         // clean description
         $description = 'DESCRIPTION:' . str_replace("\n", "\\n", strip_tags($event['descr']));
         $description = str_replace("\r", '', $description);
         $urlParts    = parse_url(get_bloginfo('url'), PHP_URL_HOST);
         $urlHost     = str_replace('www.', '', $urlParts);
+        if ( $event['image'] ) {
+            // get file extension
+            $extension = end( explode( '.', $event['image'] ) );
+            if ( $extension ) {
+                switch ( strtolower( $extension )  ) {
+                case 'jpeg':
+                case 'jpg':
+                    $description .= "\nATTACH;FMTTYPE=image/jpeg:" . GLM_MEMBERS_PLUGIN_MEDIA_URL . "/images/large/{$event['image']}";
+                    break;
+                case 'gif':
+                    $description .= "\nATTACH;FMTTYPE=image/gif:" . GLM_MEMBERS_PLUGIN_MEDIA_URL . "/images/large/{$event['image']}";
+                    break;
+                case 'png':
+                    $description .= "\nATTACH;FMTTYPE=image/png:" . GLM_MEMBERS_PLUGIN_MEDIA_URL . "/images/large/{$event['image']}";
+                    break;
+                }
+            }
+        }
+        if ( $event['url'] ) {
+            $http
+                = (trim($event['url']) && !preg_match('%http[s]?://%i', $event['url']))
+                ? 'http://'
+                : '';
+            $description .= "\nURL:{$http}{$event['url']}";
+        }
         $eventDataStart = sprintf($eventTemplateStart,
             $event['id'],
             $urlHost,
@@ -138,7 +164,7 @@ EOD;
         );
         $output .= $eventDataStart;
         // geo location (lat lon)
-        if (   $event['use_member_location']['value'] 
+        if (   $event['use_member_location']['value']
             && $event['member']
         ) {
             if ( $event['member']['lat'] && $event['member']['lon'] ) {
@@ -163,7 +189,7 @@ EOD;
             }
         } else if ( !empty( $event['locations'] ) ) {
             // location (address city state zip)
-            $output .= "\nLOCATION:" 
+            $output .= "\nLOCATION:"
                 . $event['locations']['name'] . ' - ';
             if ( !$event['hide_address']['value'] ) {
                 if ( $event['locations']['address'] ) {
@@ -181,11 +207,11 @@ EOD;
             }
         }
         // contact info
-        if (   $event['contact_name'] 
-            || $event['contact_email'] 
-            || $event['contact_phone'] 
+        if (   $event['contact_name']
+            || $event['contact_email']
+            || $event['contact_phone']
         ) {
-            $output .= "\nCONTACT:" . $event['contact_email'] . '\;;' 
+            $output .= "\nCONTACT:" . $event['contact_email'] . '\;;'
                 . $this->phoneFrmt( $event['contact_phone'] ) . '\;'
                 . $event['contact_name'];
         }
@@ -206,25 +232,25 @@ EOD;
                     $freq = 'FREQ=WEEKLY';
                     switch ( $dow ) {
                     case 'Sunday':
-                        $daysOfWeek[] = 'Sun';
+                        $daysOfWeek[] = 'SU';
                         break;
                     case 'Monday':
-                        $daysOfWeek[] = 'Mon';
+                        $daysOfWeek[] = 'MO';
                         break;
                     case 'Tuesday':
-                        $daysOfWeek[] = 'Tue';
+                        $daysOfWeek[] = 'TU';
                         break;
                     case 'Wednesday':
-                        $daysOfWeek[] = 'Wed';
+                        $daysOfWeek[] = 'WE';
                         break;
                     case 'Thursday':
-                        $daysOfWeek[] = 'Thu';
+                        $daysOfWeek[] = 'TH';
                         break;
                     case 'Friday':
-                        $daysOfWeek[] = 'Fri';
+                        $daysOfWeek[] = 'FR';
                         break;
                     case 'Saturday':
-                        $daysOfWeek[] = 'Sat';
+                        $daysOfWeek[] = 'SA';
                         break;
                     }
                 }
@@ -236,7 +262,6 @@ EOD;
             }
         }
         $output .= $eventTemplateEnd;
-        $output .= $icalFooter;
         return $output;
     }