From 72048d9fc3f78960f7e0cd9e0aa87afdb9e760fb Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 12 Aug 2016 13:22:19 -0400 Subject: [PATCH] New db version for events Adding two new fields for the ical_feed table. Field: duration to store how long the import of the events is taking. Field: events to store how many events were fetched in the last fetch. --- index.php | 2 +- models/admin/ajax/icalFeedImport.php | 60 +++ models/front/events/icalFeedImport.php | 453 ++++++++++++++++++ ...0.0.19.sql => create_database_V0.0.20.sql} | 2 + setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.20.sql | 14 + setup/frontHooks.php | 13 +- setup/validActions.php | 9 +- views/admin/management/events.html | 2 +- 9 files changed, 548 insertions(+), 8 deletions(-) create mode 100644 models/admin/ajax/icalFeedImport.php create mode 100644 models/front/events/icalFeedImport.php rename setup/databaseScripts/{create_database_V0.0.19.sql => create_database_V0.0.20.sql} (98%) create mode 100644 setup/databaseScripts/update_database_V0.0.20.sql diff --git a/index.php b/index.php index aa1579b..62c6dcf 100644 --- a/index.php +++ b/index.php @@ -39,7 +39,7 @@ * version from this plugin. */ define('GLM_MEMBERS_EVENTS_PLUGIN_VERSION', '1.3.0'); -define('GLM_MEMBERS_EVENTS_PLUGIN_DB_VERSION', '0.0.19'); +define('GLM_MEMBERS_EVENTS_PLUGIN_DB_VERSION', '0.0.20'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_EVENTS_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '1.0.58'); diff --git a/models/admin/ajax/icalFeedImport.php b/models/admin/ajax/icalFeedImport.php new file mode 100644 index 0000000..ddc79a7 --- /dev/null +++ b/models/admin/ajax/icalFeedImport.php @@ -0,0 +1,60 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +require_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/models/front/events/icalFeedImport.php'; +/** + * Steve Note... + * + * You can get to this using the following URL. + * + * {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=pdfOutput&mystuff=THIS + * + * You should be able to do this as POST or GET and should be able to add and read additional parameters. + * I added a "mystuff" parameter to the URL above and it does output from the code in the + * modelAction() function below. + * + * To add another model under models/admin/ajax all you need to do is create it and add it to the + * setup/validActions.php file. + * + */ + +// Load Members data abstract +// require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataImages.php'); + +/** + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_icalFeedImport + extends GlmMembersFront_events_icalFeedImport +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + +} diff --git a/models/front/events/icalFeedImport.php b/models/front/events/icalFeedImport.php new file mode 100644 index 0000000..79d3761 --- /dev/null +++ b/models/front/events/icalFeedImport.php @@ -0,0 +1,453 @@ + + * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt} + */ +class GlmMembersFront_events_icalFeedImport +{ + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + public function modelAction( $actionData = false ) { + if ( $id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ) ) { + echo $this->importIcalFeed( $id ); + } + exit; + } + public function clearData() + { + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events" ); + $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 . "locations" ); + } + public function importIcalFeed( $feed_id ) + { + $debug = false; + $clear_data = false; + $number_events = 0; + if ( $clear_data ) { + $this->clearData(); + } + $feed_content = $out = ''; + $event_data = array(); + + $feed_url = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT feed_url + FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX. "feed_import + WHERE id = %d", + $feed_id + ) + ); + // Set new updated date for feed + $this->wpdb->update( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'feed_import', + array( 'updated' => current_time( 'mysql' ) ), + array( 'id' => $feed_id ), + '%s', + '%d' + ); + + $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); + + // Get the feed data + $filename = $this->fetchIcalFile( $feed_url ); + require GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/classes/icalReader.php'; + $ical = new ical($filename); + $events = $ical->events(); + if ( $debug ) { + $out .= '
$events: ' . print_r( $events, true ) . '
'; + } + if ( $events ) { + foreach ( $events as $event ) { + $contact = array(); + $image = ''; + $event['DESCRIPTION'] + = str_replace( '\n', "
", $event['DESCRIPTION'] ); + $intro = substr( strip_tags( $event['DESCRIPTION'] ), 0 ,150); + if ( isset( $event['CONTACT'] ) ) { + $contact_data = explode( ',', str_replace( '\\', '', $event['CONTACT'] ) ); + if ( $debug ) { + //$out .= '
$contact_data: ' . print_r( $contact_data, true ) . '
'; + } + $contact = array( + 'name' => $contact_data[0], + 'email' => $contact_data[1], + 'phone' => $contact_data[2], + ); + } else { + $contact = array( + 'name' => '', + 'email' => '', + 'phone' => '', + ); + } + // check for this event record first to see if it can be + // updated instead of insert + $event_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "events + WHERE ical_uid = %s", + $event['UID'] + ) + ); + $old_image = ''; + if ( isset( $event['ATTACH'] ) ) { + // See if we already have this image. + $img_url = preg_replace( '%FMTTYPE=image/(jpeg|gif|png)[:]%', '', $event['ATTACH'] ); + if ( $img_url ) { + if ( $event_id ) { + $old_image = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT image + FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . " + WHERE id = %d", + $event_id + ) + ); + $image = $old_image; + } + if ( !$old_image ) { + $res = $image_upload->storeImage( $img_url ); + $image = $res['newFileName']; + } + } + } + $event_data = array( + 'status' => $this->config['status_numb']['Active'], + 'ref_type' => 10, + 'created' => $ical->iCalDateToUnixTimestamp( $event['CREATED'] ), + 'updated' => $ical->iCalDateToUnixTimestamp( $event['CREATED'] ), + 'approved' => null, + 'ical_uid' => $event['UID'], + 'name' => $event['SUMMARY'], + 'intro' => $intro, + 'descr' => $event['DESCRIPTION'], + 'image' => $image, + 'url' => ( isset( $event['URL'] ) ? $event['URL']: '' ), + 'contact_email' => $contact['name'], + 'contact_name' => $contact['email'], + 'contact_phone' => $contact['phone'], + ); + if ( $debug ) { + //$out .= '
$event_data: ' . print_r( $event_data, true ) . '
'; + } + $event_data_format = array( + '%d', + '%d', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + ); + 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 ) { + die('something is wrong no event_id'); + } + // generate the slug name for this new event + $event_abstract = new GlmDataEvents( $this->wpdb, $this->config ); + $event_abstract->updateSlug( $event_id ); + // 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]: ''); + $location = array( + 'event' => $event_id, + 'name' => $place, + 'address' => $address, + 'city' => $this->getCityId( $city ), + 'state' => $state, + 'zip' => $zip, + ); + $location_format = array( + '%d', '%s', '%s', '%d', '%s', '%s' + ); + if ( isset( $event['GEO'] ) ) { + $geo_data = explode( ';', $event['GEO'] ); + $location['lat'] = $geo_data[0]; + $location['lon'] = $geo_data[1]; + $location_format[] = '%s'; + $location_format[] = '%s'; + } + // Remove any old location data for this event_id. + $this->wpdb->delete( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations', + array( 'event' => $event_id ), + '%d' + ); + $this->wpdb->insert( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations', + $location, + $location_format + ); + } + // Remove any old recurrences data for this event_id. + $this->wpdb->delete( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'recurrences', + array( 'event' => $event_id ), + '%d' + ); + // Remove any old times data for this event_id. + $this->wpdb->delete( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'times', + array( 'event' => $event_id ), + '%d' + ); + $serialized_times = null; + $day_of_week = + $day_of_month = + $month_of_year = + $week_of_month = + $by_day_of_month = + $last_day_of_month = null; + $start_time_only = + $all_day = + $recurring_event = 0; + $day_of_week = 127; + $month_of_year = 4095; + $week_of_month = 63; + $starting = $ical->iCalDateToUnixTimestamp( $event['DTSTART'] ); + $ending = $ical->iCalDateToUnixTimestamp( $event['DTEND'] ); + $from_date = date( 'Y-m-d', $starting ); + $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; + $rrule_data = explode( ';', $event['RRULE'] ); + //echo '
$rrule_data: ' . print_r( $rrule_data, true ) . '
'; + if ( $rrule_data ) { + foreach ( $rrule_data as $rule ) { + if ( preg_match( '%FREQ=(.*)%', $rule, $matches ) ) { + $freq = trim( $matches[1] ); + } + if ( preg_match( '%BYDAY=(.*)%', $rule, $matches ) ) { + $byday = trim( $matches[1] ); + } + if ( preg_match( '%UNTIL=(.*)%', $rule, $matches ) ) { + $until = trim( $matches[1] ); + } + } + } + if ( $freq === 'WEEKLY' && $byday ) { + // separated by commas + $days = explode( ',',$byday ); + //echo '
$days: ' . print_r( $days, true ) . '
'; + if ( $days ) { + $day_of_week = 0; + foreach ( $days as $day ) { + if ( preg_match( '%([0-9])?([A-Z]{2})%', $day, $d_matches ) ) { + //echo '
$d_matches: ' . print_r( $d_matches, true ) . '
'; + if ( $d_matches[1] ) { + $week_of_month = pow(2, $d_matches[1] ); + } + switch ( $d_matches[2] ) { + 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; + } + } + } + } + } + if ( $until != '' ) { + //echo '
$until: ' . print_r( $until, true ) . '
'; + $test_date = $ical->iCalDateToUnixTimestamp( $until ); + if ( $test_date ) { + $to_date = date( 'Y-m-d', $test_date ); + } + //echo '
$test_date: ' . print_r( date( 'm/d/Y', $test_date), true ) . '
'; + } + } + if ( $btime === $etime ) { + if ( $btime === '00:00' ) { + $all_day = true; + } else { + $start_time_only = true; + } + } + if ( $btime && $etime && $etime === '00:00' ) { + $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_times + ); + $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 ); + ++$number_events; + } + } + if ( !$debug ) { + $out = $number_events . ' events imported'; + } + return $out; + } + public function fetchIcalFile( $url ) + { + $path = '/tmp'; + $filename = tempnam( $path, "ICAL" ); + $fp = fopen( $filename, 'w+' ); + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_TIMEOUT, 50 ); + curl_setopt( $ch, CURLOPT_FILE, $fp ); + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); + $fileData = curl_exec( $ch ); + $httpCode = curl_getinfo( $ch ); + curl_close( $ch ); + fclose( $fp ); + // Set correct file permissions + $oldUmask = umask( 0 ); + chmod( $filename, 0660 ); + umask( $oldUmask ); + return $filename; + } + /** + * getCityId + * + * Given a city name find and return the id of the city. + * If a city cannot be found then create city with that name. + * + * @param mixed $city_name Name of the city + * + * @access public + * @return id of city + */ + public function getCityId($city_name) + { + if ( !$city_name ) { + return false; + } + $sql = " + SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities + WHERE name like '" . esc_sql( trim( $city_name ) ) . "'"; + $cityId = $this->wpdb->get_row($sql, ARRAY_A); + if ( !$cityId ) { + // then add the city to the city table + $this->wpdb->insert( + GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities', + array( 'name' => trim( $city_name ) ), + array( '%s' ) + ); + return $this->wpdb->insert_id; + } else { + return $cityId['id']; + } + } + + +} diff --git a/setup/databaseScripts/create_database_V0.0.19.sql b/setup/databaseScripts/create_database_V0.0.20.sql similarity index 98% rename from setup/databaseScripts/create_database_V0.0.19.sql rename to setup/databaseScripts/create_database_V0.0.20.sql index 56d1eba..96f0160 100644 --- a/setup/databaseScripts/create_database_V0.0.19.sql +++ b/setup/databaseScripts/create_database_V0.0.20.sql @@ -192,5 +192,7 @@ CREATE TABLE {prefix}feed_import ( feed_url TEXT NOT NULL, -- The ical feed url to import created DATETIME NULL, -- The date this feed was created updated DATETIME NULL, -- Last time this feed was updated + duration INT NULL, -- The time it took to fetch the feed + events INT NULL, -- The number of events last fetched PRIMARY KEY (id) ); diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index c49c6d0..a64504a 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -31,5 +31,6 @@ $glmMembersEventsDbVersions = array( '0.0.17' => array('version' => '0.0.17', 'tables' => 7, 'date' => '5/5/2016'), '0.0.18' => array('version' => '0.0.18', 'tables' => 7, 'date' => '5/26/2016'), '0.0.19' => array('version' => '0.0.19', 'tables' => 8, 'date' => '8/05/2016'), + '0.0.20' => array('version' => '0.0.20', 'tables' => 8, 'date' => '8/12/2016'), ); diff --git a/setup/databaseScripts/update_database_V0.0.20.sql b/setup/databaseScripts/update_database_V0.0.20.sql new file mode 100644 index 0000000..bd70fd3 --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.20.sql @@ -0,0 +1,14 @@ +-- Gaslight Media Members Database - Events Add-On +-- File Created: 12/09/14 15:27:15 +-- Database Version: 0.0.18 +-- Database Update From Previous Version Script +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashes + + +ALTER TABLE {prefix}feed_import ADD duration INT NULL; -- The time it took to fetch the feed + +---- + +ALTER TABLE {prefix}feed_import ADD events INT NULL; -- The number of events last fetched diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 972d85d..333bebe 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -135,8 +135,17 @@ if (isset($this->config['addOns']['glm-member-db'])) { add_action( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, function( $id ) { - echo '
$id: ' . print_r( $id, true ) . '
'; - //$this->controller( 'cron', 'icalFeed' ); + if ( $id ) { + $admin_url = admin_url(); + $ical_feed_import_url = $admin_url + . 'admin-ajax.php?action=glm_members_admin_ajax&glm_action=icalFeedImport&id=' . $id; + echo '
$ical_feed_import_url: ' . print_r( $ical_feed_import_url, true ) . '
'; + $response = wp_remote_get( $ical_feed_import_url ); + $response_headers = wp_remote_retrieve_headers( $response ); + $response_body = wp_remote_retrieve_body( $response ); + echo '
$response_headers: ' . print_r( $response_headers, true ) . '
'; + echo '
$response_body: ' . print_r( $response_body, true ) . '
'; + } }, 10, 1 diff --git a/setup/validActions.php b/setup/validActions.php index 9912088..aba3399 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -33,11 +33,12 @@ $glmMembersEventsAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( - 'pdfOutput' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, - 'nameSearch' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, - 'icalFeed' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, - 'rssFeed' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, + 'pdfOutput' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, + 'nameSearch' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, + 'icalFeed' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, + 'rssFeed' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, 'eventsCalMonthAJAX' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, + 'icalFeedImport' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, ), 'member' => array( 'events' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG, diff --git a/views/admin/management/events.html b/views/admin/management/events.html index 9b43bde..29be050 100644 --- a/views/admin/management/events.html +++ b/views/admin/management/events.html @@ -120,7 +120,7 @@ {$feed.feed_url} {$feed.created} {$feed.updated} - {$feed.next_runtime} + {$feed.next_runtime|date_format:"%F %T"} Delete -- 2.17.1