Now importing the data feature/timelyImport
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 1 Jun 2016 19:55:14 +0000 (15:55 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 1 Jun 2016 19:55:14 +0000 (15:55 -0400)
Importing images, recurrence data, location data

models/admin/management/events.php

index eff01d8..2d3bcef 100644 (file)
@@ -559,6 +559,36 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                return $return;
        }
 
+       public function addTimelyCategories() {
+               $categories = get_terms( 'events_categories' );
+               foreach ( $categories as $category ) {
+                       // first check to see if the category is already there
+                       $sql = "
+                       SELECT id
+                         FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "categories
+                        WHERE name = '" . esc_sql( trim($category->name) ) . "'";
+                       $found   = $this->wpdb->get_row($sql, ARRAY_A);
+                       if ( !isset($found) ) {
+                               $this->wpdb->insert(
+                                       GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'categories',
+                                       array(
+                                               'name'   => trim($category->name),
+                                               'parent' => $category->parent
+                                       ),
+                                       array(
+                                               '%s',
+                                               '%d'
+                                       )
+                               );
+                               $catid = $this->wpdb->insert_id;
+                               $this->categories[$catid] = trim($category->name);
+                               $this->oldCatMap[$category->term_id] = $catid;
+                       } else {
+                               $this->categories[$found['id']] = trim($category->name);
+                               $this->oldCatMap[$category->term_id] = $found['id'];
+                       }
+               }
+       }
        /**
         * getTime
         *
@@ -1030,7 +1060,12 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                $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 . "locations" );
+       
+               $return = $this->addTimelyCategories();
+
                $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config);
+               require_once(GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php');
+               $image_upload = new GlmMembersAdmin_ajax_imageUpload($this->wpdb, $this->config);
 
                $sql = "
                SELECT * 
@@ -1050,15 +1085,21 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                                 WHERE post_id = {$post['ID']}";
                                $event_data = $this->wpdb->get_row( $sql, ARRAY_A );
                                echo '<pre>$event_data: ' . print_r($event_data, true) . '</pre>';
-                               $cost_data  = unserialize( $event_data );
+                               $cost_data  = unserialize( $event_data['cost'] );
                                $cost       = $cost_data['cost'];
                                $is_free    = $cost_data['is_free'];
 
                                // use the posts table data 
+                               $image = '';
                                if ( has_post_thumbnail( $post['ID'] ) ) {
                                        $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
                                        $thumb_src    = wp_get_attachment_image_src( $thumbnail_id, 'large' );
-                                       //echo '<pre>$thumbnail: ' . print_r($thumb_src, true) . '</pre>';
+                                       echo '<pre>$thumb_src: ' . print_r($thumb_src, true) . '</pre>';
+                                       $res          = $image_upload->storeImage($thumb_src[0]);
+                                       echo '<pre>$res: ' . print_r($res, true) . '</pre>';
+                                       if ( $res['newFileName'] ) {
+                                               $image = $res['newFileName'];
+                                       }
                                }
                                // Insert the event data
                                $event_import_data = array(
@@ -1067,7 +1108,8 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                                        'updated'                         => $post['post_modified'],
                                        'approved'                        => $post['post_modified'],
                                        'name'                            => $post['post_title'],
-                                       'intro'                           => $intro,
+                                       'name_slug'                       => $post['post_name'],
+                                       'intro'                           => substr( strip_tags( $post['post_content'] ), 0, 250 ),
                                        'descr'                           => $post['post_content'],
                                        'image'                           => $image,
                                        'file1'                           => null,
@@ -1078,10 +1120,10 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                                        'old_event_id'            => $post['ID'],
                                        'ref_type'                        => null,
                                        'ref_dest'                        => null,
-                                       'admin_name'              => null,
+                                       'admin_name'              => $event_data['contact_name'],
                                        'admin_org'                       => null,
-                                       'admin_email'             => null,
-                                       'admin_phone'             => null,
+                                       'admin_email'             => $event_data['contact_email'],
+                                       'admin_phone'             => $event_data['contact_phone'],
                                        'contact_email'           => $event_data['contact_email'],
                                        'contact_name'            => $event_data['contact_name'],
                                        'contact_phone'           => $event_data['contact_phone'],
@@ -1122,12 +1164,211 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement
                                        $event_import_data,
                                        $event_data_format
                                );
-                               $eventId  = $this->wpdb->insert_id;
-                               var_dump( $eventId );
-                               if ( !$eventId ) {
+                               $event_id  = $this->wpdb->insert_id;
+                               var_dump( $event_id );
+                               if ( !$event_id ) {
                                        echo '<pre>SQL Error: ' . $this->wpdb->last_error . '</pre>';
                                        echo '<pre>SQL Error: ' . $this->wpdb->print_error() . '</pre>';
-                                       die('something is wrong no eventId');
+                                       die('something is wrong no event_id');
+                               }
+                               // get the post categories
+                               $categories = array();
+                               $post_terms = wp_get_post_terms( $post['ID'], 'events_categories', 'term_id' );
+                               foreach ( $post_terms as $term ) {
+                                       $this->wpdb->insert(
+                                               GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'event_categories',
+                                               array(
+                                                       'event'    => $event_id,
+                                                       'category' => $this->oldCatMap[$term->term_id]
+                                               ),
+                                               array(
+                                                       '%d',
+                                                       '%d'
+                                               )
+                                       );
+                               }
+                               echo '<pre>$categories: ' . print_r($categories, true) . '</pre>';
+
+                               $day_of_week =
+                               $month_of_year =
+                               $week_of_month =
+                               $by_bay_of_month =
+                               $last_day_of_month = null;
+
+                               $recurring_event =
+                               $start_time_only =
+                               $recurringEvent  = 0;
+
+                               $weekly = 
+                               $daily  = false;
+
+                               $day_of_week   = 127;
+                               $month_of_year = 4095;
+                               $week_of_month = 63;
+
+                               $from_date = date( 'Y-m-d', $event_data['start'] );
+                               echo '<pre>$from_date: ' . print_r($from_date, true) . '</pre>';
+                               $to_date   = date( 'Y-m-d', $event_data['end'] );
+                               echo '<pre>$to_date: ' . print_r($to_date, true) . '</pre>';
+                               $btime     = date( 'H:i', $event_data['start'] );
+                               echo '<pre>$btime: ' . print_r($btime, true) . '</pre>';
+                               $etime     = date( 'H:i', $event_data['end'] );
+                               echo '<pre>$etime: ' . print_r($etime, true) . '</pre>';
+
+                               if ( $event_data['recurrence_rules'] ) {
+                                       $recurring_event = 1;
+                                       $recurrence_rules = explode( ';', $event_data['recurrence_rules'] );
+                                       echo '<pre>$recurrence_rules: ' . print_r($recurrence_rules, true) . '</pre>';
+                                       foreach ( $recurrence_rules as $rule ) {
+                                               if ( preg_match( ';FREQ=(.*);', $rule, $matches ) ) {
+                                                       echo '<pre>$matches: ' . print_r($matches, true) . '</pre>';
+                                                       switch ( $matches[1] ) {
+                                                               case "WEEKLY":
+                                                                       $weekly = true;
+                                                                       // check for BYDAY
+                                                                       break;
+                                                               case "DAILY":
+                                                                       $daily = true;
+                                                                       break;
+                                                       }
+                                               } else if ( preg_match( ';COUNT=(.*);', $rule, $matches ) ) {
+                                                       echo '<pre>$matches: ' . print_r($matches, true) . '</pre>';
+                                                       // set the end date to COUNT days from start
+                                                       try {
+                                                               $number_of_days    = (int)$matches[1] - 1;
+                                                               $starting_datetime = new DateTime( $from_date );
+                                                               $add               = ( $number_of_days === 1 ) ? '1 day': $number_of_days . ' days';
+                                                               $ending_date       = $starting_datetime->modify($add);
+                                                               $to_date           = $ending_date->format( 'Y-m-d' );
+                                                               $recurring_event   = 0;
+                                                       } catch (Exception $e) {
+                                                               echo '<pre>$e: ' . print_r($e, true) . '</pre>';
+                                                               die();
+                                                       }
+                                               } else if ( preg_match( ';UNTIL=(.*);', $rule, $matches ) ) {
+                                                       $to_date = date( 'Y-m-d', strtotime( $matches[1] ) );
+                                               } else if ( preg_match( ';BYDAY=(.*);', $rule, $matches ) ) {
+                                                       echo '<pre>$matches: ' . print_r($matches, true) . '</pre>';
+                                                       $days = explode( ',', $matches[1] );
+                                                       $day_of_week = 0;
+                                                       foreach ( $days as $day ) {
+                                                               switch ( $day ) {
+                                                                       case "SU":
+                                                                               $day_of_week += pow(2, 0);
+                                                                               break;
+                                                                       case "MO":
+                                                                               $day_of_week += pow(2, 1);
+                                                                               break;
+                                                                       case "TU":
+                                                                               $day_of_week += pow(2, 2);
+                                                                               break;
+                                                                       case "WE":
+                                                                               $day_of_week += pow(2, 3);
+                                                                               break;
+                                                                       case "TH":
+                                                                               $day_of_week += pow(2, 4);
+                                                                               break;
+                                                                       case "FR":
+                                                                               $day_of_week += pow(2, 5);
+                                                                               break;
+                                                                       case "SA":
+                                                                               $day_of_week += pow(2, 6);
+                                                                               break;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+
+                               $all_day       = $event_data['allday'];
+                               // for the recurrences part
+                               $recur_data = array(
+                                       'event'                         => $event_id,
+                                       'name'                          => 'Imported',
+                                       'start_time'            => $btime,
+                                       'end_time'                      => $etime,
+                                       'start_time_only'       => $start_time_only,
+                                       'from_date'                     => $from_date,
+                                       'to_date'                       => $to_date,
+                                       'all_day'                       => $all_day,
+                                       'recurring'                     => $recurring_event,
+                                       'month_of_year'         => $month_of_year,
+                                       'week_of_month'         => $week_of_month,
+                                       'day_of_week'           => $day_of_week,
+                                       'by_day_of_month'       => $by_bay_of_month,
+                                       'last_day_of_month' => $last_day_of_month,
+                               );
+                               echo '<pre>$recur_data: ' . print_r($recur_data, true) . '</pre>';
+                               $this->wpdb->insert(
+                                       GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'recurrences',
+                                       $recur_data,
+                                       array(
+                                                '%d',
+                                                '%s',
+                                                '%s',
+                                                '%s',
+                                                '%s',
+                                                '%s',
+                                                '%s',
+                                                '%d',
+                                                '%d',
+                                                '%d',
+                                                '%d',
+                                                '%d',
+                                                '%d',
+                                                '%d',
+                                       )
+                               );
+                               $recurr_id      = $this->wpdb->insert_id;
+                               $Recurrences->createRecurrenceTimesEntries( $recurr_id, true, true );
+                               // location for the event
+                               $has_location = ( $event_data['address'] || $event_data['venue'] );
+                               if ( $has_location ) {
+                                       $location_data = $location_format = array();
+                                       $location_data['event'] = $event_id;
+                                       $location_format[] = '%d';
+                                       if ($event_data['venue']) {
+                                               $location_data['name'] = $event_data['venue'];
+                                               $location_format[] = '%s';
+                                       }
+                                       if ($event_data['address']) {
+                                               $location_data['address'] = $event_data['address'];
+                                               $location_format[] = '%s';
+                                       }
+                                       if ($event_data['city']) {
+                                               $location_data['city'] = $this->getCityId( $event_data['city'] );
+                                               $location_format[] = '%s';
+                                       }
+                                       if ($event_data['province']) {
+                                               // need to lookup the state abbreviation
+                                               $state_abbr = array_search( $event_data['province'], $this->config['states'] );
+                                               if ( $state_abbr !== false ) {
+                                                       $location_data['state'] = $state_abbr;
+                                                       $location_format[] = '%s';
+                                               }
+                                       }
+                                       if ($event_data['postal_code']) {
+                                               $location_data['zip'] = $event_data['postal_code'];
+                                               $location_format[] = '%s';
+                                       }
+                                       if ($event_data['latitude']) {
+                                               $location_data['lat'] = $event_data['latitude'];
+                                               $location_format[] = '%s';
+                                       }
+                                       if ($event_data['longitude']) {
+                                               $location_data['lon'] = $event_data['longitude'];
+                                               $location_format[] = '%s';
+                                       }
+                                       $this->wpdb->insert(
+                                               GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations',
+                                               $location_data,
+                                               $location_format
+                                       );
+                                       $location_id = $this->wpdb->insert_id;
+                                       if ( !$location_id ) {
+                                               echo '<pre>$location_data: ' . print_r($location_data, true) . '</pre>';
+                                               die('no return id for location');
+                                       }
                                }
                        }
                }