Working on ical feed for event detail
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 18 May 2016 14:54:35 +0000 (10:54 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 18 May 2016 14:55:22 +0000 (10:55 -0400)
adding categories
for member location and location
geo
location
location address for either only if hide address is not checked.

models/front/events/icalFeed.php

index 011980c..10587f3 100644 (file)
@@ -47,7 +47,7 @@ class GLmMembersFront_events_icalFeed extends GlmMembersFront_events_baseAction
             $this->postAddRecurrences = true;
             $event = $this->getModelEventData($eventId);
             $this->postAddRecurrences = false;
-            echo '<pre>$event: ' . print_r($event, true) . '</pre>';
+            //echo '<pre>$event: ' . print_r($event, true) . '</pre>';
 $icalHeader = <<<EOD
 BEGIN:VCALENDAR
 PRODID:-//Gaslight Media Inc//Gaslight Events Calendar v2.0/EN
@@ -79,9 +79,9 @@ EOD;
             $timeStampFormat = 'Ymd\THis';
             $weekdays        =
                 array(1 => 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
-            //header('Content-type: text/calendar; charset=utf-8');
-            //header('Content-Disposition: inline; filename=event-' . $eventId . '.ics');
-            $output .= '<pre>';
+            header('Content-type: text/calendar; charset=utf-8');
+            header('Content-Disposition: inline; filename=event-' . $eventId . '.ics');
+            //$output .= '<pre>';
             $output .= $icalHeader;
             // clean description
             $description = 'DESCRIPTION:' . str_replace("\n", "\\n", strip_tags($event['descr']));
@@ -99,9 +99,107 @@ EOD;
                 $event['name']
             );
             $output .= $eventDataStart;
+            // geo location (lat lon)
+            if (   $event['use_member_location']['value'] 
+                && $event['member']
+            ) {
+                if ( $event['member']['lat'] && $event['member']['lon'] ) {
+                    $output .= "\nGEO:" . $event['member']['lat']
+                        . ';' . $event['member']['lon'];
+                }
+                // location (address city state zip)
+                $output .= "\nLOCATION:" . $event['member']['member_name'] . ' - ';
+                if ( !$event['hide_address']['value'] ) {
+                    if ( $event['member']['addr1'] ) {
+                        $output .= "{$event['member']['addr1']}";
+                    }
+                    if ( $event['member']['city'] ) {
+                        $output .= "\,{$event['member']['city']}";
+                    }
+                    if ( $event['member']['state']['value'] ) {
+                        $output .= "\,{$event['member']['state']['value']}";
+                    }
+                    if ( $event['member']['zip'] ) {
+                        $output .= "\,{$event['member']['zip']}";
+                    }
+                }
+            } else if ( !empty( $event['locations'] ) ) {
+                // location (address city state zip)
+                $output .= "\nLOCATION:" 
+                    . $event['locations']['name'] . ' - ';
+                if ( !$event['hide_address']['value'] ) {
+                    if ( $event['locations']['address'] ) {
+                        $output .= "{$event['locations']['address']}";
+                    }
+                    if ( $event['locations']['city'] ) {
+                        $output .= "\,{$event['locations']['city']['name']}";
+                    }
+                    if ( $event['locations']['state']['value'] ) {
+                        $output .= "\,{$event['locations']['state']['value']}";
+                    }
+                    if ( $event['locations']['zip'] ) {
+                        $output .= "\,{$event['locations']['zip']}";
+                    }
+                }
+            }
+            // contact info
+            if (   $event['contact_name'] 
+                || $event['contact_email'] 
+                || $event['contact_phone'] 
+            ) {
+                $output .= "\nCONTACT:" . $event['contact_email'] . '\;;' 
+                    . $this->phoneFrmt( $event['contact_phone'] ) . '\;'
+                    . $event['contact_name'];
+            }
+            // Categories
+            if ( !empty($event['categories']) ) {
+                $output .= "\nCATEGORIES:";
+                $eventCategories = array();
+                foreach ( $event['categories'] as $eCategory ) {
+                    $eventCategories[] = strtoupper( $eCategory['name'] );
+                }
+                $output .= implode( ',', $eventCategories);
+            }
+            // frequency (recurring data)
+            if ( $event['recurrences'][0]['recurring']['value'] ) {
+                if ( $event['recurrences'][0]['day_of_week']['names'] ) {
+                    $daysOfWeek = array();
+                    foreach ( $event['recurrences'][0]['day_of_week']['names'] as $dow ) {
+                        $freq = 'FREQ=WEEKLY';
+                        switch ( $dow ) {
+                        case 'Sunday':
+                            $daysOfWeek[] = 'Sun';
+                            break;
+                        case 'Monday':
+                            $daysOfWeek[] = 'Mon';
+                            break;
+                        case 'Tuesday':
+                            $daysOfWeek[] = 'Tue';
+                            break;
+                        case 'Wednesday':
+                            $daysOfWeek[] = 'Wed';
+                            break;
+                        case 'Thursday':
+                            $daysOfWeek[] = 'Thu';
+                            break;
+                        case 'Friday':
+                            $daysOfWeek[] = 'Fri';
+                            break;
+                        case 'Saturday':
+                            $daysOfWeek[] = 'Sat';
+                            break;
+                        }
+                    }
+                    if (!empty($daysOfWeek)) {
+                        $daysInWeek = implode(',', $daysOfWeek);
+                    }
+                    $output .=  "\nRRULE:" . $freq .";BYDAY=" . $daysInWeek . ";UNTIL="
+                        . $this->icalDateFormat($event['ending_timestamp']);
+                }
+            }
             $output .= $eventTemplateEnd;
             $output .= $icalFooter;
-            $output .= '</pre>';
+            //$output .= '</pre>';
             echo $output;
             exit;
             break;
@@ -111,8 +209,39 @@ EOD;
         }
     }
 
-    function icalDateFormat($datetime)
+    /**
+     * Returns a date formatted for the iCal feed
+     *
+     * @param $datetime A timestamp of the date and time
+     *
+     * @access public
+     * @return string
+     */
+    public function icalDateFormat($datetime)
     {
         return date('Ymd\THis', $datetime);
     }
+
+    /**
+     * Returns the phone number in (XXX) XXX-XXXX format
+     *
+     * @param $number Phone number
+     *
+     * @access public
+     * @return string
+     */
+    public 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;
+
+        }
+    }
 }