From 4ff01d3475cbf01fccc731656c00569c5d7c0789 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Sun, 19 Jun 2016 14:35:56 -0500 Subject: [PATCH] Several enhancements, including more robust date format support: - post_id is now required in the database, so add it - verify new location matches existing by comparing postcodes - added support for custom datetime formats in the import CSV using the dtformat column - added support for specifying start and end times (rather than hardcoding 00:00:00) - added example CSV to the README --- README.md | 27 +++++++++++++++++++ includes/class.EM_ImpExpImport.php | 43 ++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6c77e14..9942c53 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,33 @@ Basic import and export function for [Events Manager](https://wordpress.org/plugins/events-manager/). +## Example Import CSV + +A CSV imported into Wordpress should contain the following columns: + +- uid - a unique number representing this event +- summary - title of the event +- dtstart - start date and time of the event (in format matching the `dtformat` column) +- dtend - end date and time of the event (in format matching the `dtformat` column) +- dtformat - format of the `dtstart` and `dtend` columns in the [PHP date() format](http://php.net/manual/en/function.date.php) +- categories - a comma-separated list of categories that the event should be added to +- post_content - description of the event +- location_name - name of the location (optional) +- location_address - street address of the location +- location_town - city or town portion of the location address +- location_state - state portion of the address +- location_postcode - zipcode portion of the address +- location_country - country portion of the address +- location_latitude - latitude for the location (optional) +- location_longitude - longitude for the location (optional) + +Below is an example row: + +``` +"uid","summary","dtstart","dtend","dtformat","categories","post_content","location_name","location_address","location_town","location_state","location_postcode","location_country","location_latitude","location_longitude" +"233","My Example Event","2016-06-16 11:00:00","2016-06-16 21:00:00","Y-m-d H:i:s","My Category 1,My Category 2","This is a description of the event.","The White House","1600 Pennsylvania Avenue","Washington","DC","20500","US","38.89761","-77.03673" +``` + ## Note from author Although I never officially released this plugin, it seems to have leaked out and become a part of quite a few websites. I figure that means I ought to get it up and onto [GitHub](https://github.com/webaware/events-manager-import-export) where people can find it and report bugs. diff --git a/includes/class.EM_ImpExpImport.php b/includes/class.EM_ImpExpImport.php index cb75b6e..f17d114 100644 --- a/includes/class.EM_ImpExpImport.php +++ b/includes/class.EM_ImpExpImport.php @@ -193,6 +193,7 @@ protected function importEventsXCal($filepath) { // must create a new event $event = new EM_Event(); } + $event->post_id = $data['uid']; // post_id is now NOT NULL $event->location_id = $location ? $location->location_id : 0; $event->event_attributes['em_impexp_uid'] = $data['uid']; $event->event_attributes['em_impexp_url'] = $data['url']; @@ -374,6 +375,7 @@ protected function importEventsCSV($filepath) { 'summary' => isset($cols['summary']) ? $cols['summary'] : '', 'dtstart' => isset($cols['dtstart']) ? $cols['dtstart'] : '', 'dtend' => isset($cols['dtend']) ? $cols['dtend'] : '', + 'dtformat' => isset($cols['dtformat']) ? $cols['dtformat'] : '', 'categories' => isset($cols['categories']) ? $cols['categories'] : '', 'freq' => isset($cols['freq']) ? $cols['freq'] : '', 'byday' => isset($cols['byday']) ? $cols['byday'] : '', @@ -412,6 +414,12 @@ protected function importEventsCSV($filepath) { // try to find location by name $location = $this->getLocationByName($data['location_name']); } + // make sure the existing location is the same one by comparing postcodes + if ($data['location_postcode'] && $location->location_postcode != $data['location_postcode']) { + // this location has the same location_name as the one we want to create, but + // is actually a different location (e.g. City Hall in City A vs City Hall in City B) + $location = false; + } if (!$location) { // must create a new location object $location = new EM_Location(); @@ -445,29 +453,36 @@ protected function importEventsCSV($filepath) { // must create a new event $event = new EM_Event(); } + $event->post_id = $data['uid']; // post_id is now NOT NULL $event->location_id = $location ? $location->location_id : 0; $event->event_attributes['em_impexp_uid'] = $data['uid']; $event->event_attributes['em_impexp_url'] = $data['url']; $event->event_name = $data['summary']; $event->post_content = $data['post_content']; $event->post_excerpt = $data['post_excerpt']; - if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d$@', $data['dtstart'])) { - $data['dtstart'] .= ' 00:00:00'; - $event->start = date_create_from_format('d/m/Y H:i:s', $data['dtstart'])->getTimestamp(); - $event->event_start_date = date('Y-m-d', $event->start); - $event->event_start_time = date('H:i:s', $event->start); + $dtformat = 'd/m/Y H:i:s'; + if (isset($data['dtformat']) && !empty($data['dtformat'])) { + $dtformat = $data['dtformat']; } - if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d$@', $data['dtend'])) { - $data['dtend'] .= ' 00:00:00'; - $event->end = date_create_from_format('d/m/Y H:i:s', $data['dtend'])->getTimestamp(); - $event->event_end_date = date('Y-m-d', $event->end); - $event->event_end_time = date('H:i:s', $event->end); + + # parse start time + $sevent = date_create_from_format($dtformat, $data['dtstart']); + if ($sevent === FALSE) { + die("invalid start date for " . $data['summary'] . ": dtformat is $dtformat and start date is " . $data['dtstart']); } - else { - $event->end = $event->start; - $event->event_end_date = $event->event_start_date; - $event->event_end_time = $event->event_start_time; + $event->start = $sevent->getTimestamp(); + $event->event_start_date = date('Y-m-d', $event->start); + $event->event_start_time = date('H:i:s', $event->start); + + # parse end time + $eevent = date_create_from_format($dtformat, $data['dtend']); + if ($eevent === FALSE) { + die("invalid end date for " . $data['summary'] . ": dtformat is $dtformat and end date is " . $data['dtend']); } + $event->end = $eevent->getTimestamp(); + $event->event_end_date = date('Y-m-d', $event->end); + $event->event_end_time = date('H:i:s', $event->end); + $event->event_date_modified = current_time('mysql'); $event->event_all_day = ($event->event_start_time === '00:00:00' && $event->event_end_time === '00:00:00') ? 1 : 0;