Advertisement
Guest User

EM importer datefix

a guest
Jul 29th, 2013
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.70 KB | None | 0 0
  1. <?php
  2.  
  3. class EM_ImpExpImport {
  4.  
  5.     protected $plugin;
  6.     protected $menuPage;
  7.  
  8.     /**
  9.     * @param EM_ImpExpPlugin $plugin handle to the plugin object
  10.     * @param string $menuPage slug for menu page
  11.     */
  12.     public function __construct($plugin, $menuPage) {
  13.         $this->plugin = $plugin;
  14.         $this->menuPage = $menuPage;
  15.     }
  16.  
  17.     /**
  18.     * render the admin page
  19.     */
  20.     public function render() {
  21.         ?>
  22.  
  23.         <div class='wrap'>
  24.         <?php screen_icon(); ?>
  25.         <h2>Import Events</h2>
  26.  
  27.         <?php
  28.  
  29.         $action = self::getPostValue('action');
  30.         $format = self::getPostValue('imp_format');
  31.  
  32.         if (self::isFormPost() && $action == 'em_impexp_import') {
  33.             $this->importEvents($format);
  34.         }
  35.  
  36.         $url = admin_url('edit.php') . "?post_type=event&amp;page={$this->menuPage}";
  37.  
  38.         ?>
  39.  
  40.             <p>Import a file of events exported from Events Manager Import/Export plugin on another website.</p>
  41.             <form action="<?php echo $url; ?>" method="post" enctype="multipart/form-data">
  42.             <table class="form-table">
  43.  
  44.                 <tr>
  45.                     <th>File format:</th>
  46.                     <td>
  47.                         <label><input type="radio" name="imp_format" id="imp_format_xcal" value="xCal" checked="checked" /> xCal / Events Manager</label><br />
  48.                         <label><input type="radio" name="imp_format" id="imp_format_csv" value="csv" /> CSV in Events Manager Import/Export format</label>
  49.                     </td>
  50.                 </tr>
  51.  
  52.                 <tr valign='top'>
  53.                     <th>File:</th>
  54.                     <td>
  55.                         <input type="file" class="regular-text" name="import_file" />
  56.                     </td>
  57.                 </tr>
  58.  
  59.                 <tr>
  60.                     <th>&nbsp;</th>
  61.                     <td>
  62.                         <input type="submit" class="button-primary" value="upload" />
  63.                         <input type="hidden" name="action" value="em_impexp_import" />
  64.                     </td>
  65.                 </tr>
  66.  
  67.             </table>
  68.             </form>
  69.         </div>
  70.  
  71.         <?php
  72.     }
  73.  
  74.     /**
  75.     * import events based on specified format
  76.     * @param string $format which format used
  77.     */
  78.     protected function importEvents($format) {
  79.         // test the file upload
  80.         if (isset($_FILES['import_file'])) {
  81.             switch ($_FILES['import_file']['error']) {
  82.                 case UPLOAD_ERR_OK:
  83.                     $filepath = $_FILES['import_file']['tmp_name'];
  84.                     break;
  85.                 case UPLOAD_ERR_NO_FILE:
  86.                     $errmsg .= "# no upload file selected.<br/>\n";
  87.                     break;
  88.                 case UPLOAD_ERR_INI_SIZE:
  89.                 case UPLOAD_ERR_FORM_SIZE:
  90.                     $errmsg .= "# error uploading file - file too big.<br/>\n";
  91.                     break;
  92.                 default:
  93.                     $errmsg .= "# error uploading file.<br/>\n";
  94.                     break;
  95.             }
  96.         }
  97.         else {
  98.             $errmsg .= "# no upload file selected.<br/>\n";
  99.         }
  100.  
  101.         if (empty($errmsg)) {
  102.             try {
  103.                 set_time_limit(600);
  104.  
  105.                 switch ($format) {
  106.                     case 'xCal':
  107.                         $this->importEventsXCal($filepath);
  108.                         break;
  109.  
  110.                     case 'csv':
  111.                         $this->importEventsCSV($filepath);
  112.                         break;
  113.  
  114.                 }
  115.             }
  116.             catch (Exception $e) {
  117.                 $errmsg .= "# error importing events: " . htmlspecialchars($e->getMessage()) . "<br/>\n";
  118.             }
  119.         }
  120.  
  121.         if (!empty($errmsg)) {
  122.             $this->plugin->showError($errmsg);
  123.         }
  124.     }
  125.  
  126.     /**
  127.     * import events from xCal upload
  128.     * @param string $filepath
  129.     */
  130.     protected function importEventsXCal($filepath) {
  131.         global $wpdb;
  132.  
  133.         $xml = new XMLReader();
  134.         if (!$xml->open($filepath, 'UTF-8')) {
  135.             throw new EM_ImpExpImportException("error opening xCal file.");
  136.         }
  137.  
  138.         $text = '';
  139.         $haveRecord = FALSE;
  140.         $haveLocation = FALSE;
  141.         $records = 0;
  142.         $attrs = array();
  143.         $eventCategories = self::getEventCategories();
  144.  
  145.         while ($xml->read()) {
  146.             switch ($xml->nodeType) {
  147.                 case XMLReader::ELEMENT:
  148.                     if ($xml->name === 'vevent') {
  149.                         $data = array(
  150.                             'uid' => '',
  151.                             'url' => '',
  152.                             'summary' => '',
  153.                             'dtstart' => '',
  154.                             'dtend' => '',
  155.                             'categories' => '',
  156.                             'freq' => '',
  157.                             'byday' => '',
  158.                             'interval' => '',
  159.                             'until' => '',
  160.                             'x-post_content' => '',
  161.                             'x-event_spaces' => '',
  162.                             'x-location_name' => '',
  163.                             'x-location_address' => '',
  164.                             'x-location_town' => '',
  165.                             'x-location_state' => '',
  166.                             'x-location_postcode' => '',
  167.                             'x-location_country' => '',
  168.                             'x-location_region' => '',
  169.                             'x-location_latitude' => '',
  170.                             'x-location_longitude' => '',
  171.                         );
  172.                         $attrs = array();
  173.                         $haveRecord = TRUE;
  174.                         $haveLocation = FALSE;
  175.                     }
  176.                     $text = '';
  177.                     break;
  178.  
  179.                 case XMLReader::END_ELEMENT:
  180.                     if ($xml->name === 'vevent') {
  181.                         // end of vevent element, save record
  182.                         if ($haveRecord) {
  183.  
  184. //~ error_log(__METHOD__ . "\n" . print_r($data,1));
  185. //~ error_log(__METHOD__ . "\n" . print_r($attrs,1));
  186. //~ exit;
  187.  
  188.                             // if we have location, try to either retrieve it by name, or create a new location object
  189.                             $location = FALSE;
  190.                             if ($haveLocation) {
  191.                                 if ($data['x-location_name']) {
  192.                                     add_filter('em_locations_get_default_search', array(__CLASS__, 'filterLocationArgs'), 10, 2);
  193.                                     add_filter('em_locations_build_sql_conditions', array(__CLASS__, 'filterLocationSQL'), 10, 2);
  194.  
  195.                                     $location = EM_Locations::get(array('location_name' => $data['x-location_name']));
  196.                                     $location = count($location) > 0 ? $location[0] : FALSE;
  197.  
  198.                                     remove_filter('em_locations_get_default_search', array(__CLASS__, 'filterLocationArgs'), 10, 2);
  199.                                     remove_filter('em_locations_build_sql_conditions', array(__CLASS__, 'filterLocationSQL'), 10, 2);
  200.                                 }
  201.                                 if (!$location) {
  202.                                     // must create a new location object
  203.                                     $location = new EM_Location();
  204.                                     $location->location_name = $data['x-location_name'];
  205.                                     $location->location_address = $data['x-location_address'];
  206.                                     $location->location_town = $data['x-location_town'];
  207.                                     $location->location_state = $data['x-location_state'];
  208.                                     $location->location_postcode = $data['x-location_postcode'];
  209.                                     $location->location_country = $data['x-location_country'];
  210.                                     $location->location_region = $data['x-location_region'];
  211.                                     $location->location_latitude = $data['x-location_latitude'];
  212.                                     $location->location_longitude = $data['x-location_longitude'];
  213.                                     $location->save();
  214.                                 }
  215.  
  216. //~ error_log(__METHOD__ . "\n" . print_r($location,1));
  217. //~ exit;
  218.  
  219.                             }
  220.  
  221.                             // try to find existing event with matching unique ID first, so can update it
  222.                             $event = FALSE;
  223.                             if ($data['uid']) {
  224.                                 add_filter('em_events_get_default_search', array(__CLASS__, 'filterEventArgs'), 10, 2);
  225.                                 add_filter('em_events_build_sql_conditions', array(__CLASS__, 'filterEventSQL'), 10, 2);
  226.  
  227.                                 $event = EM_Events::get(array('em_impexp_uid' => $data['uid']));
  228.                                 $event = count($event) > 0 ? $event[0] : FALSE;
  229.  
  230.                                 remove_filter('em_events_get_default_search', array(__CLASS__, 'filterEventArgs'), 10, 2);
  231.                                 remove_filter('em_events_build_sql_conditions', array(__CLASS__, 'filterEventSQL'), 10, 2);
  232.                             }
  233.                             if (!$event) {
  234.                                 // must create a new event
  235.                                 $event = new EM_Event();
  236.                             }
  237.                             $event->location_id = $location ? $location->location_id : 0;
  238.                             $event->event_attributes['em_impexp_uid'] = $data['uid'];
  239.                             $event->event_attributes['em_impexp_url'] = $data['url'];
  240.                             $event->event_name = $data['summary'];
  241.                             $event->post_content = $data['x-post_content'];
  242.                             if ($data['dtstart']) {
  243.                                 $event->start = strtotime($data['dtstart']);
  244.                                 $event->event_start_date = date('Y-m-d', $event->start);
  245.                                 $event->event_start_time = date('H:i:s', $event->start);
  246.                             }
  247.                             if ($data['dtend']) {
  248.                                 $event->end = strtotime($data['dtend']);
  249.                                 $event->event_end_date = date('Y-m-d', $event->end);
  250.                                 $event->event_end_time = date('H:i:s', $event->end);
  251.                             }
  252.                             $event->event_date_modified = current_time('mysql');
  253.                             $event->event_all_day = ($event->event_start_time == '00:00:00' && $event->event_end_time == '00:00:00') ? 1 : 0;
  254.  
  255.                             foreach ($attrs as $attrName => $value) {
  256.                                 $event->event_attributes[$attrName] = $value;
  257.                             }
  258.  
  259.                             // TODO: recurring events
  260.                             switch ($data['freq']) {
  261.                                 case 'DAILY':
  262.                                     break;
  263.  
  264.                                 case 'WEEKLY':
  265.                                     //~ $event->freq = $data['freq'];
  266.                                     //~ $event->byday = $data['byday'];
  267.                                     //~ $event->interval = $data['interval'];
  268.                                     //~ $event->until = $data['until'];
  269.                                     break;
  270.  
  271.                                 case 'MONTHLY':
  272.                                     break;
  273.  
  274.                             }
  275.  
  276. //~ error_log(__METHOD__ . "\n" . print_r($event,1));
  277.  
  278.                             if ($event) {
  279.                                 $event->save();
  280.                                 $event->save_meta();
  281.  
  282.                                 if ($data['categories']) {
  283.                                     $categories = explode(',', $data['categories']);
  284.                                     foreach ($categories as $category) {
  285.                                         $category = trim($category);
  286.                                         if (isset($eventCategories[$category])) {
  287.                                             $cat = $eventCategories[$category];
  288.                                         }
  289.                                         else {
  290.                                             $cat = wp_insert_term($category, 'event-categories');
  291.                                             if (is_array($cat)) {
  292.                                                 $cat = new EM_Category($cat['term_id']);
  293.                                                 $eventCategories[$category] = $cat;
  294.                                             }
  295.                                         }
  296.  
  297.                                         if ($cat) {
  298.                                             $event->categories->categories[$cat->id] = $cat;
  299.                                         }
  300.                                     }
  301.                                     $event->categories->save();
  302.                                 }
  303.                             }
  304.  
  305.                             $records++;
  306.                         }
  307.                         $haveRecord = FALSE;
  308.                         $haveLocation = FALSE;
  309.                     }
  310.                     elseif ($haveRecord) {
  311.                         // still inside a vevent element, record field value and move on
  312.                         $name = $xml->name;
  313.  
  314.                         switch ($name) {
  315.                             case 'x-event_attribute':
  316.                                 // add to attributes array
  317.                                 $attrs[$xml->getAttribute('name')] = $text;
  318.                                 break;
  319.  
  320.                             default:
  321.                                 if (array_key_exists($name, $data) && $text !== '') {
  322.                                     // add to fields array
  323.                                     $data[$name] = $text;
  324.  
  325.                                     // flag location fields that have data
  326.                                     if (strpos($name, 'x-location') !== FALSE) {
  327.                                         $haveLocation = TRUE;
  328.                                     }
  329.                                 }
  330.                                 break;
  331.                         }
  332.                     }
  333.                     $text = '';
  334.                     break;
  335.  
  336.                 case XMLReader::TEXT:
  337.                 case XMLReader::CDATA:
  338.                     // record value (or part value) of text or cdata node
  339.                     $text .= (string) $xml->value;
  340.                     break;
  341.  
  342.                 default:
  343.                     break;
  344.             }
  345.         }
  346.  
  347.         $this->plugin->showMessage($records === 1 ? '1 events loaded' : "$records events loaded");
  348.     }
  349.  
  350.     /**
  351.     * import events from CSV upload
  352.     * @param string $filepath
  353.     */
  354.     protected function importEventsCSV($filepath) {
  355.         global $wpdb;
  356.  
  357.         $fp = fopen($filepath, 'r');
  358.         if ($fp === FALSE) {
  359.             throw new EM_ImpExpImportException('error opening CSV file');
  360.         }
  361.  
  362.         // read first line of CSV to make sure it's the correct format -- fgetscsv is fine for this simple task!
  363.         $header = fgetcsv($fp);
  364.         if ($header === FALSE)
  365.             throw new EM_ImpExpImportException('error reading import file or file is empty');
  366.         if (is_null($header))
  367.             throw new EM_ImpExpImportException('import file handle is null');
  368.         if (!is_array($header))
  369.             throw new EM_ImpExpImportException('import file did not scan as CSV');
  370.         if (!in_array('summary', $header))
  371.             throw new EM_ImpExpImportException('import file does not contain a field "summary"');
  372.  
  373.         $wpdb->query('start transaction');
  374.  
  375.         $records = 0;
  376.         $attrs = array();
  377.         $eventCategories = self::getEventCategories();
  378.         $eventCountries = self::getEventCountries();
  379.  
  380.         $csv = new parseCSV();
  381.         $csv->fields = $header;
  382.  
  383.         while ($line = fgets($fp)) {
  384.             $line = "\n$line\n";        // fix up line so that it can be parsed correctly
  385.  
  386.             $cols = $csv->parse_string($line);
  387.  
  388.             if ($cols) {
  389.                 $rows++;
  390.                 $cols = $cols[0];
  391.  
  392.                 // collect standard event properties
  393.                 $data = array(
  394.                     'uid' => isset($cols['uid']) ? trim($cols['uid']) : '',
  395.                     'url' => isset($cols['url']) ? self::safeURL($cols['url']) : '',
  396.                     'summary' => isset($cols['summary']) ? $cols['summary'] : '',
  397.                     'dtstart' => isset($cols['dtstart']) ? $cols['dtstart'] : '',
  398.                     'dtend' => isset($cols['dtend']) ? $cols['dtend'] : '',
  399.                     'categories' => isset($cols['categories']) ? $cols['categories'] : '',
  400.                     'freq' => isset($cols['freq']) ? $cols['freq'] : '',
  401.                     'byday' => isset($cols['byday']) ? $cols['byday'] : '',
  402.                     'interval' => isset($cols['interval']) ? $cols['interval'] : '',
  403.                     'until' => isset($cols['until']) ? $cols['until'] : '',
  404.                     'post_content' => isset($cols['post_content']) ? $cols['post_content'] : '',
  405.                     'event_spaces' => isset($cols['event_spaces']) ? $cols['event_spaces'] : '',
  406.                     'location_name' => isset($cols['location_name']) ? $cols['location_name'] : '',
  407.                     'location_address' => isset($cols['location_address']) ? $cols['location_address'] : '',
  408.                     'location_town' => isset($cols['location_town']) ? $cols['location_town'] : '',
  409.                     'location_state' => isset($cols['location_state']) ? $cols['location_state'] : '',
  410.                     'location_postcode' => isset($cols['location_postcode']) ? $cols['location_postcode'] : '',
  411.                     'location_country' => isset($cols['location_country']) ? $cols['location_country'] : '',
  412.                     'location_region' => isset($cols['location_region']) ? $cols['location_region'] : '',
  413.                     'location_latitude' => isset($cols['location_latitude']) ? $cols['location_latitude'] : '',
  414.                     'location_longitude' => isset($cols['location_longitude']) ? $cols['location_longitude'] : '',
  415.                 );
  416.  
  417.                 if (isset($eventCountries[strtolower($data['location_country'])])) {
  418.                     $data['location_country'] = $eventCountries[strtolower($data['location_country'])];
  419.                 }
  420.  
  421.                 // collect custom event attributes, being columns not found in standard event properties
  422.                 $attrs = array();
  423.                 foreach ($cols as $key => $value) {
  424.                     if (strlen($value) > 0 && !isset($data[$key])) {
  425.                         $attrs[$key] = $value;
  426.                     }
  427.                 }
  428.  
  429. //~ error_log(__METHOD__ . "\n" . print_r($data,1));
  430. //~ error_log(__METHOD__ . "\n" . print_r($attrs,1));
  431. //~ exit;
  432.  
  433.                 // if we have location, try to either retrieve it by name, or create a new location object
  434.                 $location = FALSE;
  435.                 if (self::hasLocation($data)) {
  436.                     if ($data['location_name']) {
  437.                         // try to find location by name
  438.                         $location = $this->getLocationByName($data['location_name']);
  439.                     }
  440.                     if (!$location) {
  441.                         // must create a new location object
  442.                         $location = new EM_Location();
  443.                         $location->location_name = empty($data['location_name']) ? self::fudgeLocationName($data) : $data['location_name'];
  444.                         $location->location_address = empty($data['location_address']) ? $data['location_name'] : $data['location_address'];
  445.                         $location->location_town = $data['location_town'];
  446.                         $location->location_state = $data['location_state'];
  447.                         $location->location_postcode = $data['location_postcode'];
  448.                         $location->location_country = $data['location_country'];
  449.                         $location->location_region = $data['location_region'];
  450.                         $location->location_latitude = $data['location_latitude'];
  451.                         $location->location_longitude = $data['location_longitude'];
  452.                         $location->save();
  453.                     }
  454.  
  455. //~ echo "<pre>", print_r($location,1), "</pre>\n";
  456. //~ exit;
  457.                 }
  458.  
  459.                 // try to find existing event with matching unique ID first, so can update it
  460.                 $event = FALSE;
  461.                 if ($data['uid']) {
  462.                     add_filter('em_events_get_default_search', array(__CLASS__, 'filterEventArgs'), 10, 2);
  463.                     add_filter('em_events_build_sql_conditions', array(__CLASS__, 'filterEventSQL'), 10, 2);
  464.  
  465.                     $event = EM_Events::get(array('em_impexp_uid' => $data['uid']));
  466.                     $event = count($event) > 0 ? $event[0] : FALSE;
  467.  
  468.                     remove_filter('em_events_get_default_search', array(__CLASS__, 'filterEventArgs'), 10, 2);
  469.                     remove_filter('em_events_build_sql_conditions', array(__CLASS__, 'filterEventSQL'), 10, 2);
  470.                 }
  471.                 if (!$event) {
  472.                     // must create a new event
  473.                     $event = new EM_Event();
  474.                 }
  475.                 $event->location_id = $location ? $location->location_id : 0;
  476.                 $event->event_attributes['em_impexp_uid'] = $data['uid'];
  477.                 $event->event_attributes['em_impexp_url'] = $data['url'];
  478.                 $event->event_name = $data['summary'];
  479.                 $event->post_content = $data['post_content'];
  480.                 if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d \\d\\d:\\d\\d:\\d\\d$@', $data['dtstart'])) {
  481.                                   //  if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d$@', $data['dtstart'])) {
  482.                     //$data['dtstart'] .= ' 00:00:00';
  483.                     $event->start = date_create_from_format('d/m/Y H:i:s', $data['dtstart'])->getTimestamp();
  484.                     $event->event_start_date = date('Y-m-d', $event->start);
  485.                     $event->event_start_time = date('H:i:s', $event->start);
  486.                 }
  487.                 if (preg_match('@^\\d\\d/\\d\\d/\\d\\d\\d\\d \\d\\d:\\d\\d:\\d\\d$@', $data['dtend'])) {
  488.                     //$data['dtend'] .= ' 00:00:00';
  489.                     $event->end = date_create_from_format('d/m/Y H:i:s', $data['dtend'])->getTimestamp();
  490.                     $event->event_end_date = date('Y-m-d', $event->end);
  491.                     $event->event_end_time = date('H:i:s', $event->end);
  492.                 }
  493.                 else {
  494.                     $event->end = $event->start;
  495.                     $event->event_end_date = $event->event_start_date;
  496.                     $event->event_end_time = $event->event_start_time;
  497.                 }
  498.                 $event->event_date_modified = current_time('mysql');
  499.                 $event->event_all_day = ($event->event_start_time == '00:00:00' && $event->event_end_time == '00:00:00') ? 1 : 0;
  500.  
  501.                 foreach ($attrs as $attrName => $value) {
  502.                     $event->event_attributes[$attrName] = $value;
  503.                 }
  504.  
  505.                 // TODO: recurring events
  506.                 switch ($data['freq']) {
  507.                     case 'DAILY':
  508.                         break;
  509.  
  510.                     case 'WEEKLY':
  511.                         //~ $event->freq = $data['freq'];
  512.                         //~ $event->byday = $data['byday'];
  513.                         //~ $event->interval = $data['interval'];
  514.                         //~ $event->until = $data['until'];
  515.                         break;
  516.  
  517.                     case 'MONTHLY':
  518.                         break;
  519.  
  520.                 }
  521.  
  522. //~ echo "<pre>", print_r($event,1), "</pre>\n";
  523.  
  524.                 if ($event) {
  525.                     $event->save();
  526.                     $event->save_meta();
  527.  
  528.                     if ($data['categories']) {
  529.                         $categories = explode(',', $data['categories']);
  530.                         foreach ($categories as $category) {
  531.                             $category = trim($category);
  532.                             if (isset($eventCategories[$category])) {
  533.                                 $cat = $eventCategories[$category];
  534.                             }
  535.                             else {
  536.                                 $cat = wp_insert_term($category, 'event-categories');
  537.                                 if (is_array($cat)) {
  538.                                     $cat = new EM_Category($cat['term_id']);
  539.                                     $eventCategories[$category] = $cat;
  540.                                 }
  541.                             }
  542.  
  543.                             if ($cat) {
  544.                                 $event->categories->categories[$cat->id] = $cat;
  545.                             }
  546.                         }
  547.                         $event->categories->save();
  548.                     }
  549.                 }
  550.  
  551.                 $records++;
  552.             }
  553.         }
  554.  
  555.         $wpdb->query('commit');
  556.  
  557.         $this->plugin->showMessage($records === 1 ? '1 events loaded' : "$records events loaded");
  558.     }
  559.  
  560.     /**
  561.     * Is this web request a form post?
  562.     * Checks to see whether the HTML input form was posted.
  563.     * @return boolean
  564.     */
  565.     protected static function isFormPost() {
  566.         return ($_SERVER['REQUEST_METHOD'] == 'POST');
  567.     }
  568.  
  569.     /**
  570.     * Read a field from form post input.
  571.     *
  572.     * Guaranteed to return a string, trimmed of leading and trailing spaces, and with sloshes stripped out.
  573.     *
  574.     * @param string $fieldname name of the field in the form post
  575.     * @return string
  576.     */
  577.     protected static function getPostValue($fieldname) {
  578.         return isset($_POST[$fieldname]) ? stripslashes(trim($_POST[$fieldname])) : '';
  579.     }
  580.  
  581.     /**
  582.     * ensure that URL has a protocol, give it http: if it doesn't
  583.     * @param string $url
  584.     * @return string
  585.     */
  586.     protected static function safeURL($url) {
  587.         if (!preg_match('@https?://@i', $url)) {
  588.             $url = 'http://' . $url;
  589.         }
  590.  
  591.         return $url;
  592.     }
  593.  
  594.     /**
  595.     * check CSV data to see if location is given
  596.     * @param array $data columns from a CSV row
  597.     * @return bool
  598.     */
  599.     protected static function hasLocation($data) {
  600.         // get location fields that have a value
  601.         $location = array_filter(array (
  602.             $data['location_name'],
  603.             $data['location_address'],
  604.             $data['location_town'],
  605.             $data['location_state'],
  606.             $data['location_postcode'],
  607.             $data['location_country'],
  608.             $data['location_region'],
  609.             $data['location_latitude'],
  610.             $data['location_longitude'],
  611.         ), 'strlen');
  612.  
  613.         // if any were found, return true
  614.         return count($location) > 0;
  615.     }
  616.  
  617.     /**
  618.     * find first non-empty location element for location name
  619.     * @param array $data columns from a CSV row
  620.     * @return string
  621.     */
  622.     protected static function fudgeLocationName($data) {
  623.         // get location fields that have a value
  624.         $location = array_filter(array (
  625.             $data['location_name'],
  626.             $data['location_address'],
  627.             $data['location_town'],
  628.             $data['location_state'],
  629.             $data['location_postcode'],
  630.             $data['location_country'],
  631.             $data['location_region'],
  632.             $data['location_latitude'],
  633.             $data['location_longitude'],
  634.         ), 'strlen');
  635.  
  636.         // return the first element
  637.         return array_shift($location[0]);
  638.     }
  639.  
  640.     /**
  641.     * get a location by name
  642.     * @param string $location_name
  643.     * @return EM_Location
  644.     */
  645.     protected static function getLocationByName($location_name) {
  646.         // add query filters to splice in our search term
  647.         add_filter('em_locations_get_default_search', array(__CLASS__, 'filterLocationArgs'), 10, 2);
  648.         add_filter('em_locations_build_sql_conditions', array(__CLASS__, 'filterLocationSQL'), 10, 2);
  649.  
  650.         $location = EM_Locations::get(array('location_name' => $location_name));
  651.         $location = count($location) > 0 ? $location[0] : FALSE;
  652.  
  653.         // remove our query filters
  654.         remove_filter('em_locations_get_default_search', array(__CLASS__, 'filterLocationArgs'), 10, 2);
  655.         remove_filter('em_locations_build_sql_conditions', array(__CLASS__, 'filterLocationSQL'), 10, 2);
  656.  
  657.         return $location;
  658.     }
  659.  
  660.     /**
  661.     * filter the search arguments for an events search, to restore the em_impexp_uid argument
  662.     * @param array $filtered assoc. array of filtered arguments used for search
  663.     * @param array $args assoc. array of original search arguments
  664.     * @return array
  665.     */
  666.     public static function filterEventArgs($filtered, $args) {
  667.         if (isset($args['em_impexp_uid'])) {
  668.             $filtered['em_impexp_uid'] = $args['em_impexp_uid'];
  669.         }
  670.  
  671.         return $filtered;
  672.     }
  673.  
  674.     /**
  675.     * filter the SQL where clause conditions for an events search, to include em_impexp_uid
  676.     * @param array $conditions where clause conditions
  677.     * @param array $args assoc. array of search arguments
  678.     * @return array
  679.     */
  680.     public static function filterEventSQL($conditions, $args) {
  681.         if (isset($args['em_impexp_uid'])) {
  682.             $em_events_table = EM_EVENTS_TABLE;
  683.             $uid = mysql_real_escape_string($args['em_impexp_uid']);
  684.             $conditions[] = "($em_events_table.event_attributes regexp '\"em_impexp_uid\";s:\[0-9\]+:\"$uid\"')";
  685.         }
  686.  
  687.         return $conditions;
  688.     }
  689.  
  690.     /**
  691.     * filter the search arguments for a location search, to restore the location_name argument
  692.     * @param array $filtered assoc. array of filtered arguments used for search
  693.     * @param array $args assoc. array of original search arguments
  694.     * @return array
  695.     */
  696.     public static function filterLocationArgs($filtered, $args) {
  697.         if (isset($args['location_name'])) {
  698.             $filtered['location_name'] = $args['location_name'];
  699.         }
  700.  
  701.         return $filtered;
  702.     }
  703.  
  704.     /**
  705.     * filter the SQL where clause conditions for a location search, to include location_name
  706.     * @param array $conditions where clause conditions
  707.     * @param array $args assoc. array of search arguments
  708.     * @return array
  709.     */
  710.     public static function filterLocationSQL($conditions, $args) {
  711.         if (isset($args['location_name'])) {
  712.             $conditions[] = "location_name='" . mysql_real_escape_string($args['location_name']) . "'";
  713.         }
  714.  
  715.         return $conditions;
  716.     }
  717.  
  718.     /**
  719.     * get a list of event categories, keyed by category name => category
  720.     * @return array
  721.     */
  722.     public static function getEventCategories() {
  723.         $cats = EM_Categories::get();
  724.         $eventCats = array();
  725.  
  726.         foreach ($cats as $cat) {
  727.             $eventCats[$cat->name] = $cat;
  728.         }
  729.  
  730.         return $eventCats;
  731.     }
  732.  
  733.     /**
  734.     * get a list of countries, keyed by lowercase name => code
  735.     * @return array
  736.     */
  737.     public static function getEventCountries() {
  738.         $countries = em_get_countries();
  739.         $map = array();
  740.         foreach ($countries as $code => $name) {
  741.             $map[strtolower($name)] = $code;
  742.         }
  743.  
  744.         return $map;
  745.     }
  746. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement