Updated icalfeed import script.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 12 May 2017 12:58:36 +0000 (08:58 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 12 May 2017 12:58:36 +0000 (08:58 -0400)
Worked on importing feed from chx library evanced event export.

classes/icalReader.php
models/admin/management/events.php
models/front/events/icalFeedImport.php

index 6bc73c3..661c12f 100644 (file)
@@ -186,6 +186,10 @@ class ICal
      */
     public function iCalDateToUnixTimestamp($icalDate)
     {
+        $zulu = false;
+        if ( strpos( $icalDate, 'Z' ) ) {
+            $zulu = true;
+        }
         $icalDate = str_replace('T', '', $icalDate);
         $icalDate = str_replace('Z', '', $icalDate);
 
@@ -198,7 +202,7 @@ class ICal
         preg_match($pattern, $icalDate, $date);
 
         // Unix timestamp can't represent dates before 1970
-        if ($date[1] <= 1970) {
+        if (isset($date[1]) && $date[1] <= 1970) {
             return false;
         }
         // Unix timestamps after 03:14:07 UTC 2038-01-19 might cause an overflow
@@ -209,6 +213,10 @@ class ICal
                             (int)$date[2],
                             (int)$date[3],
                             (int)$date[1]);
+        // add offset in seconds if zulu
+        if ( $zulu ) {
+            $timestamp += date('Z');
+        }
         return  $timestamp;
     }
 
index b9231d6..4d6d63b 100644 (file)
@@ -248,8 +248,11 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                     if ( !wp_next_scheduled( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, $feed_id ) ) {
                         wp_schedule_event( time(), GLM_MEMBERS_EVENTS_PLUGIN_CRON_RECURRANCE, GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( $feed_id ) );
                     }
+                } else {
+                    $icalFeedRelust = 'Not a valid url';
                 }
 
+                $event_settings = $this->editEntry(1);
                 break;
 
             case 'eventImagesImport':
index 70e064d..676d970 100644 (file)
@@ -41,12 +41,13 @@ class GlmMembersFront_events_icalFeedImport
     }
     public function getCategoryId( $category )
     {
+        $category_id = null;
         $category_id = $this->wpdb->get_var(
             $this->wpdb->prepare(
                 "SELECT id
                    FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "categories
-                  WHERE lower(name) = %d",
-                strtolower($category)
+                  WHERE name = %s",
+                trim($category)
             )
         );
         if ( $category_id ) {
@@ -55,7 +56,7 @@ class GlmMembersFront_events_icalFeedImport
             $this->wpdb->insert(
                 GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'categories',
                 array(
-                    'name'   => $category,
+                    'name'   => trim($category),
                     'parent' => 0
                 ),
                 array(
@@ -73,6 +74,7 @@ class GlmMembersFront_events_icalFeedImport
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "recurrences" );
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "times" );
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "event_categories" );
+        $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "categories" );
         $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "locations" );
     }
     public function importIcalFeed( $feed_id )
@@ -119,6 +121,9 @@ class GlmMembersFront_events_icalFeedImport
         if ( $debug ) {
              $out .= '<pre>$events: ' . print_r( $events, true ) . '</pre>';
         }
+        $origTimeZone = date_default_timezone_get();
+        $wpTimeZone = get_option('timezone_string');
+        date_default_timezone_set( $wpTimeZone );
         if ( $events ) {
             foreach ( $events as $event ) {
                 if ( $debug ) {
@@ -126,9 +131,10 @@ class GlmMembersFront_events_icalFeedImport
                 }
                 $contact = array();
                 $image   = '';
-                $event['DESCRIPTION']
-                    = str_replace( '\n', "<br>", $event['DESCRIPTION'] );
-                $intro        = substr( strip_tags( $event['DESCRIPTION'] ), 0 ,150);
+                $event['DESCRIPTION'] = str_replace( '\n', "<br>", $event['DESCRIPTION'] );
+                $event['DESCRIPTION'] = str_replace( 'ENCODING=QUOTED-PRINTABLE:', "", $event['DESCRIPTION'] );
+                $event['SUMMARY'] = str_replace( 'ENCODING=QUOTED-PRINTABLE:', "", $event['SUMMARY'] );
+                $intro = substr( strip_tags( $event['DESCRIPTION'] ), 0 ,150);
                 if ( isset( $event['CONTACT'] ) ) {
                     // Remove the backslashes
                     $eventContact = str_replace( '\\', '', $event['CONTACT'] );
@@ -201,8 +207,8 @@ class GlmMembersFront_events_icalFeedImport
                 $event_data   = array(
                     'status'        => $this->config['status_numb']['Active'],
                     'ref_type'      => 10,
-                    'created'       => $ical->iCalDateToUnixTimestamp( $event['CREATED'] ),
-                    'updated'       => $ical->iCalDateToUnixTimestamp( $event['CREATED'] ),
+                    'created'       => ( isset( $event['CREATED'] ) ? $ical->iCalDateToUnixTimestamp( $event['CREATED'] ) : time() ),
+                    'updated'       => ( isset( $event['CREATED'] ) ? $ical->iCalDateToUnixTimestamp( $event['CREATED'] ) : time() ),
                     'approved'      => null,
                     'ical_uid'      => $event['UID'],
                     'name'          => $event['SUMMARY'],
@@ -233,25 +239,21 @@ class GlmMembersFront_events_icalFeedImport
                     '%s',
                     '%s',
                 );
-                if ( !$debug ) {
-                    if ( $event_id ) {
-                        $this->wpdb->update(
-                            GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
-                            $event_data,
-                            array( 'id' => $event_id ),
-                            $event_data_format,
-                            '%d'
-                        );
-                    } else {
-                        $this->wpdb->insert(
-                            GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
-                            $event_data,
-                            $event_data_format
-                        );
-                        $event_id  = $this->wpdb->insert_id;
-                    }
+                if ( $event_id ) {
+                    $this->wpdb->update(
+                        GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
+                        $event_data,
+                        array( 'id' => $event_id ),
+                        $event_data_format,
+                        '%d'
+                    );
                 } else {
-                    continue;
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
+                        $event_data,
+                        $event_data_format
+                    );
+                    $event_id  = $this->wpdb->insert_id;
                 }
                 if ( !$event_id ) {
                     continue;
@@ -268,6 +270,8 @@ class GlmMembersFront_events_icalFeedImport
                     '%d'
                 );
                 // Categories
+                $category_data = null;
+                $categoryId    = null;
                 if ( isset( $event['CATEGORIES'] ) ) {
                     $category_data = explode( ',', $event['CATEGORIES'] );
                     foreach ( $category_data as $category ) {
@@ -282,16 +286,19 @@ class GlmMembersFront_events_icalFeedImport
                                 '%d'
                             );
                         }
+                        unset( $categoryId );
                     }
                 }
                 // Location Data
                 if ( isset( $event['LOCATION'] ) ) {
                     $location_data = explode( ',', str_replace( '\\', '', $event['LOCATION'] ) );
-                    $place         = ( isset( $location_data[0] ) ? $location_data[0]: '');
-                    $address       = ( isset( $location_data[1] ) ? $location_data[1]: '');
-                    $city          = ( isset( $location_data[2] ) ? $location_data[2]: '');
-                    $state         = ( isset( $location_data[3] ) ? $location_data[3]: '');
-                    $zip           = ( isset( $location_data[4] ) ? $location_data[4]: '');
+                    // Place will be before the first dash
+                    $placeParts = explode( ' - ', $location_data[0] );
+                    $place         = ( isset( $placeParts[0] ) ? $placeParts[0]: '');
+                    $address       = ( isset( $placeParts[1] ) ? $placeParts[1]: '');
+                    $city          = ( isset( $location_data[1] ) ? $location_data[1]: '');
+                    $state         = ( isset( $location_data[2] ) ? $location_data[2]: '');
+                    $zip           = ( isset( $location_data[3] ) ? $location_data[3]: '');
                     $location      = array(
                         'event'   => $event_id,
                         'name'    => $place,
@@ -353,6 +360,7 @@ class GlmMembersFront_events_icalFeedImport
                 $to_date           = date( 'Y-m-d', $ending );
                 $btime             = date( 'H:i', $starting );
                 $etime             = date( 'H:i', $ending );
+
                 $freq = $byday = $until = '';
                 if ( isset( $event['RRULE'] ) ) {
                     $recurring_event = 1;
@@ -495,6 +503,7 @@ class GlmMembersFront_events_icalFeedImport
             '%d',
             '%d'
         );
+        date_default_timezone_set( $origTimeZone );
 
         return $out;
     }