Updating event ical import
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 14 Aug 2018 15:18:07 +0000 (11:18 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 14 Aug 2018 15:18:29 +0000 (11:18 -0400)
Deal with google location

models/front/events/icalFeedImport.php

index 7022835..5aee493 100644 (file)
@@ -79,6 +79,8 @@ class GlmMembersFront_events_icalFeedImport
     }
     public function importIcalFeed( $feed_id )
     {
+        $state_zip_reg = '%([A-Z]{2})([ ]{1,2})?([0-9]*)?%';
+        $fake_city_reg = '%([0-9]+)%';
         $start_timer   = time();
         $debug         = false;
         $clear_data    = false;
@@ -275,6 +277,9 @@ class GlmMembersFront_events_icalFeedImport
                 }
                 ++$number_events;
                 // generate the slug name for this new event
+                if ( $number_events > 30 ) {
+                    // die('Stopping here');
+                }
                 $event_abstract = new GlmDataEvents( $this->wpdb, $this->config );
                 $event_abstract->updateSlug( $event_id );
 
@@ -306,15 +311,129 @@ class GlmMembersFront_events_icalFeedImport
                 }
                 // Location Data
                 if ( isset( $event['LOCATION'] ) ) {
-                    $location_data = explode( ',', str_replace( '\\', '', $event['LOCATION'] ) );
-                    // 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(
+                    // Check to see if there's a dash (venue - address) normal
+                    // if not then everything is separated by \,
+                    if ( strpos( $event['LOCATION'], ' - ' ) ) {
+                        $location_data = explode( ',', str_replace( '\\', '', $event['LOCATION'] ) );
+                        // 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]: '');
+                    } else {
+                        // Remove USA or United States from the ending
+                        $event['LOCATION'] = str_replace('\\, USA', '', $event['LOCATION'] );
+                        $event['LOCATION'] = str_replace('\\, United States', '', $event['LOCATION'] );
+                        $event['LOCATION'] = str_replace('\\, United States of America', '', $event['LOCATION'] );
+                        $location_data = explode( ',', str_replace( '\\', '', $event['LOCATION'] ) );
+                        $place = $address = $city = $state = $zip = '';
+
+                        // echo '<pre>$location_data: ' . print_r( $location_data, true ) . '</pre>';
+                        $loc_count = count( $location_data );
+                        switch ( $loc_count ) {
+                        case 1:
+                            $place = ( isset( $location_data[0] ) ? $location_data[0]: '');
+                            break;
+                        case 2:
+                            $place = ( isset( $location_data[0] ) ? $location_data[0]: '');
+                            $city  = ( isset( $location_data[1] ) ? $location_data[1]: '');
+                            // Check if they stuck the state and zip together (MI 49707)
+                            if ( $city && preg_match( $state_zip_reg, $city, $matches ) ) {
+                                // echo '<pre>city $matches: ' . print_r( $matches, true ) . '</pre>';
+                                $city    = '';
+                                $address = '';
+                                $state   = $matches[1];
+                                $zip     = $matches[3];
+                            } else if ( $city && preg_match( $fake_city_reg, $city, $matched ) ) {
+                                // If we find numbers in the city then this should goto the address field.
+                                // echo '<pre>$matched: ' . print_r( $matched, true ) . '</pre>';
+                                $address = $city;
+                                $city = '';
+                            }
+                            break;
+                        case 3:
+                            $place   = ( isset( $location_data[0] ) ? $location_data[0]: '');
+                            $address = ( isset( $location_data[1] ) ? $location_data[1]: '');
+                            $city    = ( isset( $location_data[2] ) ? $location_data[2]: '');
+                            // Check if they stuck the state and zip together (MI 49707)
+                            if ( $city && preg_match( $state_zip_reg, $city, $matches ) ) {
+                                // echo '<pre>$matches: ' . print_r( $matches, true ) . '</pre>';
+                                $city    = $address;
+                                $address = '';
+                                $state   = $matches[1];
+                                $zip     = $matches[3];
+                            } else if ( $city && preg_match( $fake_city_reg, $city, $matched ) ) {
+                            // If we find numbers in the city then this should goto the address field.
+                                // echo '<pre>$matched: ' . print_r( $matched, true ) . '</pre>';
+                                $address = $city;
+                                $city = '';
+                            }
+                            break;
+                        case 4:
+                            $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]: '');
+
+                            // Check if they stuck the state and zip together (MI 49707)
+                            if ( $city && preg_match( $state_zip_reg, $city, $matches ) ) {
+                                // echo '<pre>$matches: ' . print_r( $matches, true ) . '</pre>';
+                                $city    = '';
+                                $address = '';
+                                $state   = $matches[1];
+                                $zip     = $matches[3];
+                            } else if ( $city && preg_match( $fake_city_reg, $city, $matched ) ) {
+                                // If we find numbers in the city then this should goto the address field.
+                                // echo '<pre>$matched: ' . print_r( $matched, true ) . '</pre>';
+                                $address = $city;
+                                $city = '';
+                            }
+                            // Check if they stuck the state and zip together (MI 49707)
+                            if ( $state && preg_match( $state_zip_reg, $state, $matches ) ) {
+                                // echo '<pre>$matches: ' . print_r( $matches, true ) . '</pre>';
+                                $state = $matches[1];
+                                $zip   = $matches[3];
+                            }
+                            break;
+                        case 5:
+                            echo '<p>5</p>';
+                            $place   = ( isset( $location_data[0] ) ? $location_data[0]: '');
+                            $place  .= ( isset( $location_data[1] ) ? ' ' . $location_data[1]: '');
+                            $address = ( isset( $location_data[2] ) ? $location_data[2]: '');
+                            $city    = ( isset( $location_data[3] ) ? $location_data[3]: '');
+                            $state   = ( isset( $location_data[4] ) ? $location_data[4]: '');
+
+                            // echo '<pre>$place: ' . print_r( $place, true ) . '</pre>';
+                            // echo '<pre>$address: ' . print_r( $address, true ) . '</pre>';
+                            // echo '<pre>$city: ' . print_r( $city, true ) . '</pre>';
+                            // echo '<pre>$state: ' . print_r( $state, true ) . '</pre>';
+
+                            // Check if they stuck the state and zip together (MI 49707)
+                            if ( $city && preg_match( $state_zip_reg, $city, $matches ) ) {
+                                // echo '<pre>$matches: ' . print_r( $matches, true ) . '</pre>';
+                                $city    = '';
+                                $address = '';
+                                $state   = $matches[1];
+                                $zip     = $matches[3];
+                            } else if ( $city && preg_match( $fake_city_reg, $city, $city_matches ) ) {
+                                // If we find numbers in the city then this should goto the address field.
+                                // echo '<pre>$city_matches: ' . print_r( $city_matches, true ) . '</pre>';
+                                $address = $city;
+                                $city = '';
+                            }
+                            // Check if they stuck the state and zip together (MI 49707)
+                            if ( $state && preg_match( $state_zip_reg, $state, $matches ) ) {
+                                // echo '<pre>$matches: ' . print_r( $matches, true ) . '</pre>';
+                                $state = $matches[1];
+                                $zip   = $matches[3];
+                            }
+                            break;
+                        }
+                    }
+
+                    $location = array(
                         'event'   => $event_id,
                         'name'    => $place,
                         'address' => $address,
@@ -322,6 +441,7 @@ class GlmMembersFront_events_icalFeedImport
                         'state'   => $state,
                         'zip'     => $zip,
                     );
+                    // echo '<pre>$location: ' . print_r( $location, true ) . '</pre>';
                     $location_format = array(
                         '%d', '%s', '%s', '%d', '%s', '%s'
                     );