From: Steve Sutton Date: Fri, 12 Aug 2016 13:02:07 +0000 (-0400) Subject: Setting up the cron tasks X-Git-Tag: v1.3.1^2~4^2~3 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=86107a82b65a937ab050e2cbf082096f6d90324c;p=WP-Plugins%2Fglm-member-db-events.git Setting up the cron tasks Working on getting the Cron task setup to run the feed import. Had it working when it was in the index file. But when I move it into the admin and front hook pages. Admin hook might work but the process kicked off from the cron will use only the front one. This might need to be setup as an ajax thing. --- diff --git a/defines.php b/defines.php index fa8dc36..9c296dd 100644 --- a/defines.php +++ b/defines.php @@ -24,6 +24,10 @@ if (trim($host) == '') { } define('GLM_MEMBER_EVENTS_PLUGIN_HOST', $host); +// Cron defines +define( 'GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT', 'glm_membersEvents_event' ); +define( 'GLM_MEMBERS_EVENTS_PLUGIN_CRON_RECURRANCE', 'twicedaily' ); + // Determine current http/https protocol $pageProtocol = 'http'; if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') { diff --git a/index.php b/index.php index fb39bd1..aa1579b 100644 --- a/index.php +++ b/index.php @@ -79,13 +79,13 @@ if (!defined('ABSPATH')) { $startupNotices = ''; // Get standard defined parameters -require_once('defines.php'); +require_once 'defines.php'; // Required to be able to get user capabilities when being called as a filter from the main plugin -require_once(ABSPATH . 'wp-includes/pluggable.php'); +require_once ABSPATH . 'wp-includes/pluggable.php' ; // Include defines to tell if a plugin is active -include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); +include_once ABSPATH . 'wp-admin/includes/plugin.php'; /* * Do some preliminary sanity checks @@ -196,7 +196,7 @@ add_filter('glm-member-db-register-addon','glmMembersRegisterEvents', 10, 1); function glmMembersEventsPluginActivate () { global $wpdb, $config; - require_once (GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/activate.php'); + require_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/activate.php'; new glmMembersEventsPluginActivate($wpdb, $config); } register_activation_hook(__FILE__, 'glmMembersEventsPluginActivate'); @@ -205,7 +205,7 @@ add_filter('glm-member-db-register-addon','glmMembersRegisterEvents', 10, 1); function glmMembersEventsPluginDeactivate () { global $wpdb, $config; - require_once (GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/deactivate.php'); + require_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/deactivate.php'; $x = new glmMembersEventsPluginDeactivate($wpdb, $config); return false; } @@ -214,5 +214,4 @@ add_filter('glm-member-db-register-addon','glmMembersRegisterEvents', 10, 1); /* * Hooks for testing capabilities provided by this add-on */ -require_once(GLM_MEMBERS_EVENTS_PLUGIN_SETUP_PATH.'/permissions.php'); - +require_once GLM_MEMBERS_EVENTS_PLUGIN_SETUP_PATH.'/permissions.php'; diff --git a/models/admin/cron/icalFeed.php b/models/admin/cron/icalFeed.php new file mode 100644 index 0000000..2abd99c --- /dev/null +++ b/models/admin/cron/icalFeed.php @@ -0,0 +1,481 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release admin.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +/* + * This model is called when the "Shortcodes" menu is selected + * + */ +class GlmMembersAdmin_cron_icalFeed +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + + /* + * Constructor + * + * This contructor performs the work for this model. This model returns + * an array containing the following. + * + * 'status' + * + * True if successfull and false if there was a fatal failure. + * + * 'view' + * + * A suggested view name that the contoller should use instead of the + * default view for this model or false to indicate that the default view + * should be used. + * + * 'data' + * + * Data that the model is returning for use in merging with the view to + * produce output. + * + * @wpdb object WordPress database object + * + * @return array Array containing status, suggested view, and any data + */ + 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 ) ) { + $this->importIcalFeed( $id ); + } + + // Return status, any suggested view, and any data to controller + return array( + 'status' => true, + 'modelRedirect' => false, + 'view' => 'admin/shortcodes/index.html', + 'data' => array( + 'addOns' => $this->config['addOns'] + ) + ); + + } + 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' => '', + ); + } + if ( isset( $event['ATTACH'] ) ) { + $img_url = preg_replace( '%FMTTYPE=image/(jpeg|gif|png)[:]%', '', $event['ATTACH'] ); + if ( $img_url ) { + $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', + ); + // 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'] + ) + ); + 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/models/admin/management/events.php b/models/admin/management/events.php index 599b6bb..4585756 100644 --- a/models/admin/management/events.php +++ b/models/admin/management/events.php @@ -219,10 +219,17 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement array( 'id' => $feed_id ), '%d' ); + if ( wp_next_scheduled( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( $feed_id ) ) ) { + wp_clear_scheduled_hook( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( $feed_id ) ); + } } if ( $option2 === 'refresh' && $feed_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ) ) { - //$icalFeedResult .= '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - $icalFeedResult .= $this->importIcalFeed( $feed_id ); + //$icalFeedResult .= $this->importIcalFeed( $feed_id ); + // Set up the event schedule + if ( wp_next_scheduled( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( $feed_id ) ) ) { + wp_clear_scheduled_hook( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( $feed_id ) ); + } + wp_schedule_event( time(), GLM_MEMBERS_EVENTS_PLUGIN_CRON_RECURRANCE, GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( $feed_id ) ); } if ( isset( $_REQUEST['feed_url'] ) && $feed_url = filter_var( $_REQUEST['feed_url'], FILTER_VALIDATE_URL ) ) { // Add this feed url to the table @@ -234,6 +241,12 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement ), '%s' ); + $feed_id = $this->wpdb->insert_id; + + // Setup the event feed to pull on schedule + 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 ) ); + } } break; @@ -300,59 +313,6 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement if (isset($_REQUEST['action'])) { $action = $_REQUEST['action']; } - switch ( $action ) { - case 'redo': - $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config); - $db_host = 'ds4'; - $db_name = 'discoverkalamazoo'; - $db_user = 'postgres'; - $db_password = ''; - $this->connectPostgresDb($db_host, $db_name, $db_user, $db_password); - $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config); - $sql = " - SELECT * - FROM events.event - WHERE edate >= current_date - AND (btime != '' OR etime != '')"; - $events = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); - foreach ( $events as $event ) { - echo '
$event[btime]: ' . print_r($event['btime'], true) . '
'; - $begining = $this->getTime( $event['btime'] ); - echo '
$begining: ' . print_r($begining, true) . '
'; - echo '
$event[etime]: ' . print_r($event['etime'], true) . '
'; - $ending = $this->getTime( $event['etime'] ); - echo '
$ending: ' . print_r($ending, true) . '
'; - // find the event with this old_event_id - $result = $this->wpdb->get_results( - "SELECT * FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "recurrences - WHERE event IN ( - SELECT id - FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX. "events - WHERE old_event_id = " . $event['id'] . ")", - ARRAY_A - ); - echo '
$result: ' . print_r($result, true) . '
'; - if ( $result[0] ) { - // update the recurrence - $return = $this->wpdb->update( - GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'recurrences', - array( - 'start_time' => $begining, - 'end_time' => $ending - ), - array( 'id' => $result[0]['id'] ), - array( - '%s', - '%s' - ), - array( '%d' ) - ); - echo '
$return: ' . print_r($return, true) . '
'; - $Recurrences->createRecurrenceTimesEntries( $result[0]['id'], true, true ); - } - } - $import_result = 'hello redo'; - } break; case 'settings': @@ -420,6 +380,13 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement $import_feeds = $importFeeds->getList(); + if ( $import_feeds ) { + foreach ( $import_feeds as $key => &$feed ) { + // check the next run time + $feed['next_runtime'] = wp_next_scheduled( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( (int)$feed['id'] ) ); + } + } + // Compile template data $template_data = array( 'import_feeds' => $import_feeds, @@ -1718,357 +1685,6 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement . "&start=$start\">Next

"; return $return_string; } - 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; - } - 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' => '', - ); - } - if ( isset( $event['ATTACH'] ) ) { - $img_url = preg_replace( '%FMTTYPE=image/(jpeg|gif|png)[:]%', '', $event['ATTACH'] ); - $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', - ); - // 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'] - ) - ); - 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; - } } ?> diff --git a/setup/adminHooks.php b/setup/adminHooks.php index c75852d..d4c1286 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -26,41 +26,19 @@ * Also note that parameters will be in the context of the main admin controller constructor. */ - -/* - Settingc up action hook for cron task. For cron not working so far. add_action( - 'glm_events_cron', - function ( $id, $host ) { + GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, + function( $id ) { echo '
$id: ' . print_r( $id, true ) . '
'; - echo '
$host: ' . print_r( $host, true ) . '
'; - $file = GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/test_run.txt'; - if( !touch( $file ) ) { - $fp = fopen( $file, 'w+' ); - fclose( $fp ); - } + $this->controller( 'cron', 'icalFeed' ); }, 10, - 2 + 1 ); - */ -//do_action( 'glm_events_cron', 1, 'http://gaslightmedia.com' ); + /* -wp_schedule_event( - time(), - 'hourly', - function( $id, $host ){ - echo '
$id: ' . print_r( $id, true ) . '
'; - echo '
$host: ' . print_r( $host, true ) . '
'; - $file = GLM_MEMBERS_EVENTS_PLUGIN_PATH . 'test_run.txt'; - if( !touch( $file ) ) { - $fp = fopen( $file, 'w+' ); - fclose( $fp ); - } - } - array( - 'id' => 1, - 'host' => 'Forteen.com' - ) -); -*/ +include_once GLM_MEMBERS_EVENTS_MAIN_PLUGIN_PATH . '/lib/GlmDataAbstract/DataAbstract.php'; +include_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/models/admin/management/events.php'; +$eventManagement = new GlmMembersAdmin_management_events( $this->wpdb, $this->config ); +add_action( GLM_MEMBERS_EVENTS_PLUGIN_CRON_EVENT, array( $eventManagement, 'importIcalFeed' ), 10, 1 ); + */ diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 205dd5a..972d85d 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -132,3 +132,12 @@ 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' ); + }, + 10, + 1 +); diff --git a/setup/validActions.php b/setup/validActions.php index c22203d..9912088 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -51,6 +51,9 @@ $glmMembersEventsAddOnValidActions = array( ), 'management' => array( 'events' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG + ), + 'cron' => array( + 'icalFeed' => GLM_MEMBERS_EVENTS_PLUGIN_SLUG ) ), 'frontActions' => array( diff --git a/views/admin/events/editSchedule.html b/views/admin/events/editSchedule.html index 732310c..1974422 100644 --- a/views/admin/events/editSchedule.html +++ b/views/admin/events/editSchedule.html @@ -26,7 +26,7 @@ - + All Day Event: @@ -150,12 +150,12 @@ Select with Calendar -
Show Calendar
+
Custom Dates

Click on Dates to select/remove those dates for this schedule.

- NOTE: This calendar only shows these specific dates and does not include other dates from the schedule. + NOTE: This calendar only shows these specific dates and does not include other dates from the schedule. See the Summary Calendar to view all dates for this event.

@@ -164,7 +164,7 @@ {if $r.specific_dates} {$ncei = 1000} - {foreach $r.specific_dates as $sd} + {foreach $r.specific_dates as $sd} {$sd} {$ncei = $ncei+1} {/foreach} @@ -341,7 +341,7 @@

Click on Dates to select/remove those dates for this schedule.

- NOTE: This calendar only shows these specific dates and does not include other dates from the schedule. + NOTE: This calendar only shows these specific dates and does not include other dates from the schedule. See the Summary Calendar to view all dates for this event.

diff --git a/views/admin/management/events.html b/views/admin/management/events.html index 0b113f7..9b43bde 100644 --- a/views/admin/management/events.html +++ b/views/admin/management/events.html @@ -110,18 +110,22 @@ Feed Url Created Date Updated Date + Next Run   - {foreach $import_feeds as $feed} - - Refresh - {$feed.feed_url} - {$feed.created} - {$feed.updated} - Delete - - {/foreach} + {if $import_feeds} + {foreach $import_feeds as $key => $feed} + + Refresh + {$feed.feed_url} + {$feed.created} + {$feed.updated} + {$feed.next_runtime} + Delete + + {/foreach} + {/if}