From 81518c3904b6d6f0418f0ee386fc293c4fe2828f Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 26 Jan 2018 14:58:19 -0500 Subject: [PATCH] Working on an import for Event Manager plugin This imports all events from the plugin Event Manager. --- models/admin/management/events.php | 435 +++++++++++++++++++++++++++++ views/admin/management/events.html | 78 ++++-- 2 files changed, 489 insertions(+), 24 deletions(-) diff --git a/models/admin/management/events.php b/models/admin/management/events.php index 2d7983c..7c536d5 100644 --- a/models/admin/management/events.php +++ b/models/admin/management/events.php @@ -209,6 +209,16 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement $import_result = $this->importFromTimely(); break; + case 'eventsManagerImport': + $import = false; + if (isset($_REQUEST['import'])) { + $import = filter_var( $_REQUEST['import'], FILTER_VALIDATE_BOOLEAN ); + } + $import_result = $this->importFromEventsManager(); + //$categories = get_terms( 'event-categories' ); + //echo '
$categories: ' . print_r( $categories, true ) . '
'; + break; + case 'icalfeed': if ( isset( $_REQUEST['option2'] ) ) { $option2 = filter_var( $_REQUEST['option2'], FILTER_SANITIZE_STRING ); @@ -581,6 +591,41 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement update_option( 'glm-member-event-old-cat-map', serialize( $this->oldCatMap ) ); return "

Adding $category_counter Categories

"; } + public function addEventManagerCategories() { + $category_counter = 0; + $categories = get_terms( 'event-categories' ); + foreach ( $categories as $category ) { + ++$category_counter; + // 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']; + } + } + // Store the oldCatMap for the next pages. + update_option( 'glm-member-event-old-cat-map', serialize( $this->oldCatMap ) ); + return "

Adding $category_counter Categories

"; + } public function getOldCatMap() { return unserialize( get_option('glm-member-event-old-cat-map') ); @@ -1696,6 +1741,396 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement . "&start=$start\">Next

"; return $return_string; } + public function importFromEventsManager() { + $limit = 100; + $debug = false; + if ( isset( $_REQUEST['start'] ) ) { + $start = filter_var( $_REQUEST['start'], FILTER_VALIDATE_INT ); + } else { + $start = 0; + } + if ( $start === 0 ) { + // clear the events tables first. + $this->clearData(); + // Add the Categories from Time.ly. + $return_string = $this->addEventManagerCategories(); + } else { + $this->oldCatMap = $this->getOldCatMap(); + } + + $yearly_events = + $daily_events = + $weekly_events = + $interval_events = + $monthly_events = + $custom_date_events = 0; + + $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 * + FROM " . $this->wpdb->prefix . "posts + WHERE post_type = 'event' + AND post_status = 'publish' + AND ID IN ( + SELECT distinct post_id + FROM " . $this->wpdb->prefix . "em_events + -- WHERE event_end_date >= '2017-06-01' + WHERE event_end_date >= '2018-01-26' + )"; + $sql .= " LIMIT $limit OFFSET $start"; + $results = $this->wpdb->get_results( $sql, ARRAY_A ); + //'
$results: ' . print_r( $results, true ) . '
'; + + $event_counter = 0; + + if ( !empty( $results ) ) { + foreach ( $results as $post ) { + // Initialize some variables + $serialized_custom_times = null; + if ( $debug ) { + echo '
'; + } + ++$event_counter; + $sql = " + SELECT * + FROM " . $this->wpdb->prefix . "em_events + WHERE post_id = {$post['ID']}"; + $event_data = $this->wpdb->get_row( $sql, ARRAY_A ); + // echo '
$event_data: ' . print_r( $event_data, true ) . '
'; + $att_data = unserialize( $event_data['event_attributes'] ); + $cost = $att_data['Event Cost']; + + // use the posts table data + $image = wp_get_attachment_image_src( $thumbnail_id, 'large' ); + if ( $image ) { + //echo '
$thumb_src: ' . print_r($thumb_src, true) . '
'; + $res = $image_upload->storeImage($thumb_src[0]); + //echo '
$res: ' . print_r($res, true) . '
'; + if ( $res['newFileName'] ) { + $image = $res['newFileName']; + } + } + + // Need the user data for admin and contact name phone and email + $user = get_user_by( 'id', $event_data['event_owner'] ); + // echo '
$user: ' . print_r( $user, true ) . '
'; + + $ref_dest = null; + // See if the user has a member then use this for ref_dest + if ( $user->data->user_login ) { + $ref_dest = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE old_member_id = %s", + $user->data->user_login + ) + ); + } + + // Insert the event data + $event_import_data = array( + 'status' => $this->config['status_numb']['Active'], + 'created' => $post['post_date'], + 'updated' => $post['post_modified'], + 'approved' => $post['post_modified'], + 'name' => $post['post_title'], + 'name_slug' => $post['post_name'], + 'intro' => mb_substr( strip_tags( $post['post_content'] ), 0, 80 ), + 'descr' => $post['post_content'], + 'image' => $image, + 'file1' => null, + 'file1_descr' => null, + 'cost' => $cost, + 'free' => 0, + 'url' => '', + 'old_event_id' => $post['ID'], + 'ref_type' => 10, + 'ref_dest' => $ref_dest, + 'admin_name' => $user->data->display_name, + 'admin_org' => null, + 'admin_email' => $user->data->user_email, + 'admin_phone' => '', + 'contact_email' => $user->data->user_email, + 'contact_name' => $user->data->display_name, + 'contact_phone' => '', + 'notes' => null, + 'hide_address' => null, + 'use_member_location' => 0, + ); + $event_data_format = array( + '%d', // Status + '%s', // Created + '%s', // Updated + '%s', // Approved + '%s', // Name + '%s', // Name Slug + '%s', // Intro + '%s', // Descr + '%s', // Image + '%s', // File1 + '%s', // File1 Desc + '%s', // Cost + '%s', // Free + '%s', // Url + '%s', // Old_event_id + '%s', // Ref Type + '%s', // Ref Dest + '%s', // Admin Name + '%s', // Admin Org + '%s', // Admin Email + '%s', // Admin Phone + '%s', // Contact Email + '%s', // Contact Name + '%s', // Contact Phone + '%s', // Notes + '%s', // Hide Address + '%s', // Use Member Location + ); + $is_insert = $this->wpdb->insert( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events', + $event_import_data, + $event_data_format + ); + if ( $debug ) { + var_dump( $is_insert ); + } + $event_id = $this->wpdb->insert_id; + if ( $debug ) { + var_dump( $event_id ); + } + if ( !$event_id ) { + $this->wpdb->print_error(); + echo '
$event_import_data: ' . print_r($event_import_data, true) . '
'; + echo '
SQL Error: ' . $this->wpdb->last_error . '
'; + echo '
SQL Error: ' . $this->wpdb->print_error() . '
'; + die('something is wrong no event_id'); + } + // get the post categories + $categories = array(); + $post_terms = wp_get_post_terms( $post['ID'], 'event-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' + ) + ); + } + if ( $debug ) { + echo '
$event_data: ' . print_r($event_data, true) . '
'; + echo '
$categories: ' . print_r($categories, true) . '
'; + } + + // Stop processing this event + // return false; + // continue; + + $by_day_of_month = + $last_day_of_month = null; + + $recurring_event = + $start_time_only = + $recurring_event = 0; + + $weekly = + $daily = false; + + $day_of_week = 127; + $month_of_year = 4095; + $week_of_month = 63; + + $custom_times = array(); + $curZone = date_default_timezone_get(); + if ( $debug ) { + echo '
$curZone: ' . print_r($curZone, true) . '
'; + } + // if ( $event_data['timezone_name'] ) { + // date_default_timezone_set( $event_data['timezone_name'] ); + // } + $curZone = date_default_timezone_get(); + if ( $debug ) { + echo '
$curZone: ' . print_r($curZone, true) . '
'; + } + + $from_date = $event_data['event_start_date']; + if ( $debug ) { + echo '
$from_date: ' . print_r($from_date, true) . '
'; + } + $to_date = $event_data['event_end_date']; + // check if the $to_date has 0 hr + // if ( date( 'H', $event_data['event_end_date'] ) === '00' ) { + // $ending_datetime = new DateTime( $to_date ); + // $ending_datetime->modify('- 1 day'); + // $to_date = $ending_datetime->format( 'Y-m-d' ); + // } + if ( $debug ) { + echo '
$to_date: ' . print_r($to_date, true) . '
'; + } + $btime = $event_data['event_start_time']; + if ( $debug ) { + echo '
$btime: ' . print_r($btime, true) . '
'; + } + $etime = $event_data['event_end_time']; + if ( $debug ) { + echo '
$etime: ' . print_r($etime, true) . '
'; + } + + + if ($recurring_event && $from_date == $to_date && empty( $custom_times ) ) { + // Add two years to the recurring event if dates match + $starting_datetime = new DateTime( $from_date ); + $ending_date = $starting_datetime->modify('2 years'); + $to_date = $ending_date->format( 'Y-m-d' ); + } + $all_day = $event_data['event_all_day']; + + if ( $btime == $etime ) { + $start_time_only = true; + } + + // 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, + 'day_of_month' => $day_of_month, + 'by_day_of_month' => $by_day_of_month, + 'last_day_of_month' => $last_day_of_month, + 'specific_dates' => $serialized_custom_times + ); + if ( $debug ) { + echo '
$recur_data: ' . print_r($recur_data, true) . '
'; + } + $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', + '%d', + '%s' + ) + ); + $recurr_id = $this->wpdb->insert_id; + $Recurrences->createRecurrenceTimesEntries( $recurr_id, true, true ); + // location for the event + $has_location = $event_data['location_id']; + $location_data = $location_format = array(); + if ( $has_location ) { + // Get the record for location + $location = $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT * + FROM " . $this->wpdb->prefix . "em_locations + WHERE location_id = %d", + $has_location + ), + ARRAY_A + ); + // echo '
$location: ' . print_r( $location, true ) . '
'; + $location_data['event'] = $event_id; + $location_format[] = '%d'; + if ($location['location_name']) { + $location_data['name'] = $location['location_name']; + $location_format[] = '%s'; + } + if ($location['location_address']) { + $location_data['address'] = $location['location_address']; + $location_format[] = '%s'; + } + if ($location['location_town']) { + $location_data['city'] = $this->getCityId( $location['location_town'] ); + $location_format[] = '%s'; + } + if ($location['location_state']) { + // need to lookup the state abbreviation + $state_abbr = array_search( $location['location_state'], $this->config['states'] ); + if ( $state_abbr !== false ) { + $location_data['state'] = $state_abbr; + $location_format[] = '%s'; + } + } else { + $location_data['state'] = 'MI'; + $location_format[] = '%s'; + } + if ($location['location_postalcode']) { + $location_data['zip'] = $location['location_postalcode']; + $location_format[] = '%s'; + } + if ($location['location_country']) { + $location_data['country'] = $location['location_country']; + $location_format[] = '%s'; + } + if ($location['location_latitude']) { + $location_data['lat'] = $location['location_latitude']; + $location_format[] = '%s'; + } + if ($location['location_longitude']) { + $location_data['lon'] = $location['location_longitude']; + $location_format[] = '%s'; + } + // echo '
$location_data: ' . print_r( $location_data, true ) . '
'; + // echo '
$location_format: ' . print_r( $location_format, true ) . '
'; + $this->wpdb->insert( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations', + $location_data, + $location_format + ); + $location_id = $this->wpdb->insert_id; + if ( !$location_id ) { + if ( $debug ) { + echo '
$location_data: ' . print_r($location_data, true) . '
'; + } + die('no return id for location'); + } + } + if ( $debug ) { + echo '
'; + } + } + $return_string .= "

Adding $event_counter Events

"; + } + echo $this->config['states']; + $return_string .= "
+            Start: $start
+        
"; + $start += $limit; + $return_string .= "

Next

"; + return $return_string; + } } ?> diff --git a/views/admin/management/events.html b/views/admin/management/events.html index a8c3bc3..509e52c 100644 --- a/views/admin/management/events.html +++ b/views/admin/management/events.html @@ -5,6 +5,7 @@ iCal Feeds Import Timely Import + Events Manager Import @@ -312,6 +313,7 @@ {/if} + {if $importResult} @@ -338,37 +340,65 @@ {/if} +
Return to Time.ly Import
+ + {if $importResult} + + + + + {else} + + + + + + + {/if} +
Return to Events Manager Import
+ {$importResult} +

Import Event Manager Events

+
+ + + + +
+ +
+
+
- + if('{$eventsSettings.fieldData.calendar_view}'){ + $("#calendar-view").val('{$eventsSettings.fieldData.calendar_view}'); + } else { + $("#calendar-view").val("agenda"); + } + }); + -- 2.17.1