From: Steve Sutton Date: Wed, 10 Aug 2016 16:34:31 +0000 (-0400) Subject: DB updates and the ical refresh for feed now works. X-Git-Tag: v1.3.0^2~2 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=3df1221b0192df7f74b9b2ee6ecffd54b82963ea;p=WP-Plugins%2Fglm-member-db-events.git DB updates and the ical refresh for feed now works. Can grab images from the ical feed. Adding field for the ical uid from the ical feed. If it finds a match then that event record will update. --- diff --git a/classes/icalReader.php b/classes/icalReader.php new file mode 100644 index 0000000..6bc73c3 --- /dev/null +++ b/classes/icalReader.php @@ -0,0 +1,322 @@ + + * @license http://www.opensource.org/licenses/mit-license.php MIT License + * @version SVN: + * @link http://code.google.com/p/ics-parser/ + * @example $ical = new ical('MyCal.ics'); + * print_r( $ical->events() ); + */ + +error_reporting(E_ALL); + +/** + * This is the iCal-class + * + * @category Parser + * @package Ics-parser + * @author Martin Thoma + * @license http://www.opensource.org/licenses/mit-license.php MIT License + * @link http://code.google.com/p/ics-parser/ + * + * @param {string} filename The name of the file which should be parsed + * @constructor + */ +class ICal +{ + /* How many ToDos are in this ical? */ + public /** @type {int} */ $todo_count = 0; + + /* How many events are in this ical? */ + public /** @type {int} */ $event_count = 0; + + /* The parsed calendar */ + public /** @type {Array} */ $cal; + + /* Which keyword has been added to cal at last? */ + private /** @type {string} */ $_lastKeyWord; + + /** + * Creates the iCal-Object + * + * @param {string} $filename The path to the iCal-file + * + * @return Object The iCal-Object + */ + public function __construct($filename) + { + if (!$filename) { + return false; + } + + $lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + if (stristr($lines[0], 'BEGIN:VCALENDAR') === false) { + return false; + } else { + // TODO: Fix multiline-description problem (see http://tools.ietf.org/html/rfc2445#section-4.8.1.5) + foreach ($lines as $line) { + $line = trim($line); + $add = $this->keyValueFromString($line); + if ($add === false) { + $this->addCalendarComponentWithKeyAndValue($type, false, $line); + continue; + } + + list($keyword, $value) = $add; + + switch ($line) { + // http://www.kanzaki.com/docs/ical/vtodo.html + case "BEGIN:VTODO": + $this->todo_count++; + $type = "VTODO"; + break; + + // http://www.kanzaki.com/docs/ical/vevent.html + case "BEGIN:VEVENT": + //echo "vevent gematcht"; + $this->event_count++; + $type = "VEVENT"; + break; + + //all other special strings + case "BEGIN:VCALENDAR": + case "BEGIN:DAYLIGHT": + // http://www.kanzaki.com/docs/ical/vtimezone.html + case "BEGIN:VTIMEZONE": + case "BEGIN:STANDARD": + $type = $value; + break; + case "END:VTODO": // end special text - goto VCALENDAR key + case "END:VEVENT": + case "END:VCALENDAR": + case "END:DAYLIGHT": + case "END:VTIMEZONE": + case "END:STANDARD": + $type = "VCALENDAR"; + break; + default: + $this->addCalendarComponentWithKeyAndValue($type, + $keyword, + $value); + break; + } + } + return $this->cal; + } + } + + /** + * Add to $this->ical array one value and key. + * + * @param {string} $component This could be VTODO, VEVENT, VCALENDAR, ... + * @param {string} $keyword The keyword, for example DTSTART + * @param {string} $value The value, for example 20110105T090000Z + * + * @return {None} + */ + public function addCalendarComponentWithKeyAndValue($component, + $keyword, + $value) + { + if ($keyword == false) { + $keyword = $this->last_keyword; + switch ($component) { + case 'VEVENT': + $value = $this->cal[$component][$this->event_count - 1] + [$keyword].$value; + break; + case 'VTODO' : + $value = $this->cal[$component][$this->todo_count - 1] + [$keyword].$value; + break; + } + } + + if (stristr($keyword, "DTSTART") or stristr($keyword, "DTEND")) { + $keyword = explode(";", $keyword); + $keyword = $keyword[0]; + } + + switch ($component) { + case "VTODO": + $this->cal[$component][$this->todo_count - 1][$keyword] = $value; + //$this->cal[$component][$this->todo_count]['Unix'] = $unixtime; + break; + case "VEVENT": + $this->cal[$component][$this->event_count - 1][$keyword] = $value; + break; + default: + $this->cal[$component][$keyword] = $value; + break; + } + $this->last_keyword = $keyword; + } + + /** + * Get a key-value pair of a string. + * + * @param {string} $text which is like "VCALENDAR:Begin" or "LOCATION:" + * + * @return {array} array("VCALENDAR", "Begin") + */ + public function keyValueFromString($text) + { + preg_match("/([^:;]+)[:;]([\w\W]*)/", $text, $matches); + if (count($matches) == 0) { + return false; + } + $matches = array_splice($matches, 1, 2); + return $matches; + } + + /** + * Return Unix timestamp from ical date time format + * + * @param {string} $icalDate A Date in the format YYYYMMDD[T]HHMMSS[Z] or + * YYYYMMDD[T]HHMMSS + * + * @return {int} + */ + public function iCalDateToUnixTimestamp($icalDate) + { + $icalDate = str_replace('T', '', $icalDate); + $icalDate = str_replace('Z', '', $icalDate); + + $pattern = '/([0-9]{4})'; // 1: YYYY + $pattern .= '([0-9]{2})'; // 2: MM + $pattern .= '([0-9]{2})'; // 3: DD + $pattern .= '([0-9]{0,2})'; // 4: HH + $pattern .= '([0-9]{0,2})'; // 5: MM + $pattern .= '([0-9]{0,2})/'; // 6: SS + preg_match($pattern, $icalDate, $date); + + // Unix timestamp can't represent dates before 1970 + if ($date[1] <= 1970) { + return false; + } + // Unix timestamps after 03:14:07 UTC 2038-01-19 might cause an overflow + // if 32 bit integers are used. + $timestamp = mktime((int)$date[4], + (int)$date[5], + (int)$date[6], + (int)$date[2], + (int)$date[3], + (int)$date[1]); + return $timestamp; + } + + /** + * Returns an array of arrays with all events. Every event is an associative + * array and each property is an element it. + * + * @return {array} + */ + public function events() + { + $array = $this->cal; + return $array['VEVENT']; + } + + /** + * Returns a boolean value whether thr current calendar has events or not + * + * @return {boolean} + */ + public function hasEvents() + { + return ( count($this->events()) > 0 ? true : false ); + } + + /** + * Returns false when the current calendar has no events in range, else the + * events. + * + * Note that this function makes use of a UNIX timestamp. This might be a + * problem on January the 29th, 2038. + * See http://en.wikipedia.org/wiki/Unix_time#Representing_the_number + * + * @param {boolean} $rangeStart Either true or false + * @param {boolean} $rangeEnd Either true or false + * + * @return {mixed} + */ + public function eventsFromRange($rangeStart = false, $rangeEnd = false) + { + $events = $this->sortEventsWithOrder($this->events(), SORT_ASC); + + if (!$events) { + return false; + } + + $extendedEvents = array(); + + if ($rangeStart !== false) { + $rangeStart = new DateTime(); + } + + if ($rangeEnd !== false or $rangeEnd <= 0) { + $rangeEnd = new DateTime('2038/01/18'); + } else { + $rangeEnd = new DateTime($rangeEnd); + } + + $rangeStart = $rangeStart->format('U'); + $rangeEnd = $rangeEnd->format('U'); + + + + // loop through all events by adding two new elements + foreach ($events as $anEvent) { + $timestamp = $this->iCalDateToUnixTimestamp($anEvent['DTSTART']); + if ($timestamp >= $rangeStart && $timestamp <= $rangeEnd) { + $extendedEvents[] = $anEvent; + } + } + + return $extendedEvents; + } + + /** + * Returns a boolean value whether thr current calendar has events or not + * + * @param {array} $events An array with events. + * @param {array} $sortOrder Either SORT_ASC, SORT_DESC, SORT_REGULAR, + * SORT_NUMERIC, SORT_STRING + * + * @return {boolean} + */ + public function sortEventsWithOrder($events, $sortOrder = SORT_ASC) + { + $extendedEvents = array(); + + // loop through all events by adding two new elements + foreach ($events as $anEvent) { + if (!array_key_exists('UNIX_TIMESTAMP', $anEvent)) { + $anEvent['UNIX_TIMESTAMP'] = + $this->iCalDateToUnixTimestamp($anEvent['DTSTART']); + } + + if (!array_key_exists('REAL_DATETIME', $anEvent)) { + $anEvent['REAL_DATETIME'] = + date("d.m.Y", $anEvent['UNIX_TIMESTAMP']); + } + + $extendedEvents[] = $anEvent; + } + + foreach ($extendedEvents as $key => $value) { + $timestamp[$key] = $value['UNIX_TIMESTAMP']; + } + array_multisort($timestamp, $sortOrder, $extendedEvents); + + return $extendedEvents; + } +} +?> diff --git a/models/admin/management/events.php b/models/admin/management/events.php index 9913a2f..6b5175c 100644 --- a/models/admin/management/events.php +++ b/models/admin/management/events.php @@ -15,9 +15,9 @@ // Load Management Events data abstract require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataManagement.php'; -require_once(GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataEvents.php'); -require_once(GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataRecurrences.php'); -require_once(GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataFeedImport.php'); +require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataEvents.php'; +require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataRecurrences.php'; +require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataFeedImport.php'; /** * GlmMembersAdmin_management_events @@ -113,7 +113,7 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement public function importEventImages() { - require_once(GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php'); + require_once GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php'; $ImageUpload = new GlmMembersAdmin_ajax_imageUpload($this->wpdb, $this->config); // get all events with images $sql = " @@ -220,7 +220,11 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement '%d' ); } - if ( $feed_url = filter_var( $_REQUEST['feed_url'], FILTER_VALIDATE_URL ) ) { + if ( $option2 === 'refresh' && $feed_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ) ) { + //$icalFeedResult .= '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $icalFeedResult .= $this->importIcalFeed( $feed_id ); + } + if ( isset( $_REQUEST['feed_url'] ) && $feed_url = filter_var( $_REQUEST['feed_url'], FILTER_VALIDATE_URL ) ) { // Add this feed url to the table $this->wpdb->insert( GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "feed_import", @@ -246,12 +250,9 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement $import_result = '
Failure: Not given owner id.
'; } - break; case 'eventFilesImport': - - break; case 'eventimport': @@ -489,6 +490,34 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement } } + public function getCategoryId( $category ) + { + $category_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . "categories + WHERE name = %d", + $category + ) + ); + if ( $category_id ) { + return $category_id; + } else { + $this->wpdb->insert( + GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'categories', + array( + 'name' => $category, + 'parent' => 0 + ), + arary( + '%s', + '%d' + ) + ); + $category_id = $this->wpdb->insert_id; + return $category_id; + } + } /** * addCategories * @@ -643,6 +672,9 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement */ public function getCityId($city_name) { + if ( !$city_name ) { + return false; + } $sql = " SELECT id FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities @@ -674,11 +706,7 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement public function addEvents() { // clear the events tables first - $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" ); + $this->clearData(); $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config); $return = ''; $old_event_format = ($this->settings['tablename'] == 'event'); @@ -721,7 +749,7 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement } } if ( $old_event_format ) { - $intro = substr(strip_tags($event['descr']), 0, 250); + $intro = substr(strip_tags($event['descr']), 0, 150); $event_data = array( 'status' => (($event['visable'] == '1') ? $this->config['status_numb']['Active'] @@ -907,10 +935,10 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement $recurring_event = 0; } } - $day_of_week = - $month_of_year = - $week_of_month = - $by_day_of_month = + $day_of_week = + $month_of_year = + $week_of_month = + $by_day_of_month = $last_day_of_month = null; if ( $recurring_event ) { if ( $event['daysow'] ) { @@ -1060,6 +1088,14 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement } return $return; } + 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 importFromTimely() { $limit = 25; $debug = false; @@ -1070,11 +1106,7 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement } if ( $start === 0 ) { // clear the events tables first. - $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" ); + $this->clearData(); // Add the Categories from Time.ly. $return_string = $this->addTimelyCategories(); } else { @@ -1089,7 +1121,7 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement $custom_date_events = 0; $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config); - require_once(GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php'); + require_once GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php'; $image_upload = new GlmMembersAdmin_ajax_imageUpload($this->wpdb, $this->config); $sql = " @@ -1097,13 +1129,9 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement FROM " . $this->wpdb->prefix . "posts WHERE post_type = 'ai1ec_event' AND post_status = 'publish'"; - //$sql .= " AND ID IN ( SELECT post_id FROM " . $this->wpdb->prefix . "ai1ec_events - //WHERE end >= " . time() . " )"; - //$sql .= " AND ID IN ( 3785 )"; $sql .= " LIMIT $limit OFFSET $start"; $results = $this->wpdb->get_results( $sql, ARRAY_A ); - //echo '

Total events: ' . count( $results ) . '

'; $event_counter = 0; if ( !empty( $results ) ) { @@ -1114,13 +1142,11 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement echo '
'; } ++$event_counter; - //echo '
$post: ' . print_r($post, true) . '
'; $sql = " SELECT * FROM " . $this->wpdb->prefix . "ai1ec_events WHERE post_id = {$post['ID']}"; $event_data = $this->wpdb->get_row( $sql, ARRAY_A ); - //echo '
$event_data: ' . print_r($event_data, true) . '
'; $cost_data = unserialize( $event_data['cost'] ); $cost = $cost_data['cost']; $is_free = $cost_data['is_free']; @@ -1241,7 +1267,7 @@ class GlmMembersAdmin_management_events extends GlmDataEventsManagement $recurring_event = $start_time_only = - $recurring_event = 0; + $recurring_event = 0; $weekly = $daily = false; @@ -1692,6 +1718,341 @@ 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 + ) + ); + + $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($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 ) . '
'; + } + } + //echo '
$freq: ' . print_r( $freq, true ) . '
'; + //echo '
$byday: ' . print_r( $byday, 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 56f9b52..c75852d 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -24,5 +24,43 @@ * }); * * 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 ) { + 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 ); + } + }, + 10, + 2 +); + */ +//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' + ) +); +*/ diff --git a/setup/databaseScripts/create_database_V0.0.19.sql b/setup/databaseScripts/create_database_V0.0.19.sql index 150d368..56d1eba 100644 --- a/setup/databaseScripts/create_database_V0.0.19.sql +++ b/setup/databaseScripts/create_database_V0.0.19.sql @@ -149,6 +149,7 @@ CREATE TABLE {prefix}events ( contact_phone TINYTEXT NULL, -- Event Phone use_member_location BOOLEAN NULL, -- Use location of the member (if provided) rather than location table data old_event_id INT NULL, -- ID of event from old site for reference + ical_uid TINYTEXT NULL, -- The ical UID for this event. notes TEXT NULL, -- Internal notes for this event PRIMARY KEY (id), INDEX(custom_time), diff --git a/setup/databaseScripts/update_database_V0.0.19.sql b/setup/databaseScripts/update_database_V0.0.19.sql index 4340c80..c52ff1c 100644 --- a/setup/databaseScripts/update_database_V0.0.19.sql +++ b/setup/databaseScripts/update_database_V0.0.19.sql @@ -15,3 +15,7 @@ CREATE TABLE {prefix}feed_import ( updated DATETIME NULL, -- Last time this feed was updated PRIMARY KEY (id) ); + +---- + +ALTER TABLE {prefix}events ADD ical_uid TINYTEXT NULL; -- The ical UID for this event. diff --git a/views/admin/management/events.html b/views/admin/management/events.html index 2ce6480..0b113f7 100644 --- a/views/admin/management/events.html +++ b/views/admin/management/events.html @@ -2,7 +2,7 @@ @@ -114,7 +114,7 @@ {foreach $import_feeds as $feed} - Refresh + Refresh {$feed.feed_url} {$feed.created} {$feed.updated}