Support

Home Forums Event Espresso Premium Attendee Batch Import Modification

Attendee Batch Import Modification

Posted: September 30, 2014 at 10:04 pm


Pixel Workshop

September 30, 2014 at 10:04 pm

Good evening!

So, I thought I was being über clever, and I modified the Attendee Batch Import add-on to only import a few pieces of data to the corresponding Question fields of existing registrants. And it works! Mostly. I can import the data, and see it in individual registrants’ records when I view them in the WP admin area, but if I export the data again, they don’t appear in the excel file.

For some background, this is for a endurance race company. Just before the event, we export registrant data and add information like bib number, swim cap color, etc. I’ve created these as Questions that are only visible to Admin. After we assign these data bits in the excel file, I’m using the batch import tool to bring that data back into the WP database. It all seems to work great except for the fact that it doesn’t export.

Any guidance would be appreciated.

Thanks!


Pixel Workshop

September 30, 2014 at 10:05 pm

I should include the modified code…

<?php
/*
  Plugin Name: Event Espresso - Attendee Import
  Plugin URI: https://eventespresso.com/
  Description: Allows the import of attndees into Event Espresso

  Version: 0.1

  Author: Event Espresso
  Author URI: http://www.eventespresso.com

  Copyright (c) 2008-2012 Event Espresso  All Rights Reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

function espresso_attendee_import() {
	$wp_plugin_url = WP_PLUGIN_URL;
	$wp_content_url = WP_CONTENT_URL;
	define("ESPRESSO_ATTENDEE_IMPORT_PLUGINPATH", "/" . plugin_basename(dirname(__FILE__)) . "/");
	define("ESPRESSO_ATTENDEE_IMPORT_PLUGINPATH", WP_PLUGIN_DIR . ESPRESSO_ATTENDEE_IMPORT_PLUGINPATH);
	define("ESPRESSO_ATTENDEE_PLUGINFULLURL", $wp_plugin_url . ESPRESSO_ATTENDEE_IMPORT_PLUGINPATH);
	
	
?>
    <h3>Attendee Import</h3>
    <ul>
        <li>
            <p>This page is for importing your attendees from a comma separated file (CSV) directly into the the database. This is the first pass at the up-loader, but for those of you who have a lot of attendees, particularly attendees that are similar in setup, this will be a time saver.</p>
            <p style=" font-weight:bold">Usage:</p>
			<ol>
                <li>Dates should be formatted YYYY-MM-DD (2009-07-04).</li>
                <li>I have included a template file <a href="<?php echo ESPRESSO_ATTENDEE_PLUGINFULLURL; ?>attendees.csv">here</a> that I recommend you download and use.  It is very easy to work with it in excel, just remember to save it as a csv and not excel sheet.</li>
                <li>The file name should be events.csv in order for it to work. I will fix this issue later, I just wanted to get this working first.</li>
				<li>One final note, you will see that the header row, fist column has a 0 while other rows have a 1.  This tells the upload to ignore rows that have the 0 identifier and only use rows with the 1.</li>
            </ol>
           
            <?php
			$success_messages = '';
			$error_messages = '';
			uploader( 1, array("csv"), 1048576, '../wp-content/uploads/espresso/', $success_messages, $error_messages );
            ?>
        </li>
    </ul>
<?php
}

/*
  uploader([int num_uploads [, arr file_types [, int file_size [, str upload_dir ]]]]);

  num_uploads = Number of uploads to handle at once.

  file_types = An array of all the file types you wish to use. The default is txt only.

  file_size = The maximum file size of EACH file. A non-number will results in using the default 1mb filesize.

  upload_dir = The directory to upload to, make sure this ends with a /
 */

function uploader($num_of_uploads = 1, $file_types_array = array("csv"), $max_file_size = 1048576, $upload_dir = "../wp-content/uploads/espresso/", $success_messages, $error_messages) {

	
    if (!is_numeric($max_file_size)) {
        $max_file_size = 1048576;
    }
    if (!isset($_POST["submitted"])) {
        $form = "<form action='admin.php?page=espresso_attendee_import&action=attendee_import' method='post' enctype='multipart/form-data'><p>Upload files:</p><input type='hidden' name='submitted' value='TRUE' id='" . time() . "'><input name='action' type='hidden' value='attendee_import' /><input type='hidden' name='MAX_FILE_SIZE' value='" . $max_file_size . "'>";
        for ($x = 0; $x < $num_of_uploads; $x++) {
            $form .= "<p><font color='red'>*</font><input type='file' name='file[]'>";
        }
        $form .= "<input class='button-primary' type='submit' value='Upload Attendee(s)'></p></form>";
        echo($form);
    } else {
        foreach ($_FILES["file"]["error"] as $key => $value) {
            if ($_FILES["file"]["name"][$key] != "") {
                if ($value == UPLOAD_ERR_OK) {
                    $origfilename = $_FILES["file"]["name"][$key];
                    $filename = explode(".", $_FILES["file"]["name"][$key]);
                    $filenameext = $filename[count($filename) - 1];
                    unset($filename[count($filename) - 1]);
                    $filename = implode(".", $filename);
                    $filename = substr($filename, 0, 15) . "." . $filenameext;
                    $file_ext_allow = FALSE;
                    for ($x = 0; $x < count($file_types_array); $x++) {
                        if ($filenameext == $file_types_array[$x]) {
                            $file_ext_allow = TRUE;
                        }
                    }
                    if ($file_ext_allow) {
                        if ($_FILES["file"]["size"][$key] < $max_file_size) {
                            if (move_uploaded_file($_FILES["file"]["tmp_name"][$key], $upload_dir . $filename)) {
                                $success_messages .= "<p>File uploaded successfully. - <a href='" . $upload_dir . $filename . "' target='_blank'>" . $filename . "</a></p>";
                            } else {
                                $error_messages .= '<p>'.$origfilename . " was not successfully uploaded</p>";
                            }
                        } else {
                            $error_messages .= '<p>'.$origfilename . " was too big, not uploaded</p>";
                        }
                    } else {
                        $error_messages .= '<p>'.$origfilename . " had an invalid file extension, not uploaded</p>";
                    }
                } else {
                    $error_messages .= '<p>'.$origfilename . " was not successfully uploaded</p>";
                }
            }
        }
    }
	if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'attendee_import') {
   		load_attendees_to_db( $success_messages, $error_messages );
	}
}

function load_attendees_to_db( $success_messages, $error_messages ) {
    global $wpdb;

    $fieldseparator = ",";
    $lineseparator = "\n";
    $csvfile = "../wp-content/uploads/espresso/events.csv";

    function getCSVValues($string, $separator = ",") {
        global $wpdb;
        $wpdb->show_errors();
        $elements = explode($separator, $string);
        for ($i = 0; $i < count($elements); $i++) {
            $nquotes = substr_count($elements[$i], '"');

            if ($nquotes % 2 == 1) {
                for ($j = $i + 1; $j < count($elements); $j++) {
                    if (substr_count($elements[$j], '"') > 0) {
                        // Put the quoted string's pieces back together again
                        array_splice($elements, $i, $j - $i + 1, implode($separator, array_slice($elements, $i, $j - $i + 1)));
                        break;
                    }
                }
            }

            if ($nquotes > 0) {
                // Remove first and last quotes, then merge pairs of quotes
                $qstr = & $elements[$i];
                $qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
                $qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
                $qstr = str_replace('""', '"', $qstr);
            }
        }

        return $elements;
    }

    if (!file_exists($csvfile)) {
  		$error_messages .= '<p>File not found. Make sure you specified the correct path.</p>';
 	 	espresso_display_attendee_import_messages( $success_messages, $error_messages );
       exit;
    }

    $file = fopen($csvfile, "r");

    if (!$file) {
  		$error_messages .= '<p>Error opening data file.</p>';
  	 	espresso_display_attendee_import_messages( $success_messages, $error_messages );
       exit;
    }

    $size = filesize($csvfile);

    if (!$size) {
  		$error_messages .= '<p>File is empty.</p>';
  	 	espresso_display_attendee_import_messages( $success_messages, $error_messages );
      exit;
    }

    $file = file_get_contents($csvfile);
    $dataStrings = explode("\r", $file);

    $i = 0;
 	$tot_records = 0;
   
 
/*echo '<pre style="height:auto;border:2px solid lightblue;">' . print_r( $dataStrings, TRUE ) . '</pre><br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>';die();*/

    foreach ($dataStrings as $data) {
        ++$i;

        for ($j = 0; $j < $i; ++$j) {
            $strings = getCSVValues($dataStrings[$j]);
        }
		
//		echo "<pre>".print_r($strings,true)."</pre>";
//		echo '<h4>$valid : ' . $valid . '  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span></h4>';
       
		if (array_key_exists('2', $strings)) {
            //echo "The  element is in the array<br />";
            $skip = $strings[0];
			
			$registration_id = $strings[1];

			$attendee_id = $strings[2];

			$answer_data = $strings[3];
/*			
			if ($skip >= "1" && !empty($event_id)) {
               				
				$registration_id = !empty($strings[1]) ? $strings[1] : apply_filters('filter_hook_espresso_registration_id', $event_id);
                
				//Add event data 
				if ( $wpdb->insert(               
					EVENTS_ATTENDEE_TABLE, 
					array( 
						'registration_id' => $registration_id, 
						'fname' => $fname,
						'lname' => $lname,
						'email' => $email,
						'address' => $strings[13],
						'address2' => $strings[14],
						'city' => $strings[15],
						'state' => $strings[16],
						'zip' => $strings[17],
						'phone' => $strings[19],
						'date' => $strings[8],
						'quantity' => $strings[5],
						'event_id' => $event_id,
						'amount_pd' => $strings[7],
						'payment_status' => $strings[9],
						'txn_id' => $strings[41],
						'txn_type' => $strings[43],
						'payment_date' => event_date_display($strings['42'], 'Y-m-d'),
						'price_option' => $strings[10],
						'start_date' => event_date_display($strings['11'], 'Y-m-d'),
						'event_time' => $strings[12]
					), 
					array( 
						'%s', // registration_id
						'%s',  // fname
						'%s',  // lname
						'%s',  // email
						'%s',  // address
						'%s',  // address2
						'%s',  // city
						'%s',  // state
						'%s',  // zip
						'%s',  // phone
						'%s',  // reg_date
						'%d',  // quantity
						'%d',  // event_id
						'%f',  // amount_pd
						'%s',  // payment_status
						'%s',  // transaction_id
						'%s',  // payment_date
						'%s',  // transaction_type
						'%s',  // price_option
						'%s',  // start_date
						'%s'  // event_time
					) 
				) === false ) {
					print $wpdb->print_error();
				} else {
                    $last_attendee_id = $wpdb->insert_id;
                }
			 
*/				for ( $q = 1; $q <= 99; $q++ ) {
					$answer = null;
					
					switch ( $q ) {
						case 13 :
							$answer = $strings[3];
							break;	
						case 14 :
							$answer = $strings[4];
							break;
						case 63 :
							$answer = $strings[5];	
							break;		
						case 64 :
							$answer = $strings[6];
							break;	
					
					}

					if ($answer != null && $wpdb->insert( 
						EVENTS_ANSWER_TABLE, 
						array( 
							'registration_id' => $registration_id,
							'attendee_id' => $attendee_id,
							'question_id' => $q,
							'answer' => $answer,
						), 
						array( 
							'%s', 
							'%s', 
							'%d', 
							'%s' 
						) 
					) === false ) {
						print $wpdb->print_error();
					}
				}				
				 $tot_records++;
            }
        }

    unlink($csvfile);
    if (!file_exists($csvfile)) {
        $success_messages .= '<p>Temporary upload file has been successfully deleted.</p>';
    }
 	$success_messages .='<p>Added a total of '.$tot_records.' attendees to the database.</p>';
 	espresso_display_attendee_import_messages( $success_messages, $error_messages );

}

function espresso_display_attendee_import_messages( $success_messages = '', $error_messages = '' ) {
	if ($success_messages != '') {
		//showMessage( $success_messages );
		echo '<div id="message1" class="updated fade"><p>' . $success_messages . '</p></div>';
	}

	if ($error_messages != '') {
		//showMessage( $error_messages, TRUE );
		echo '<div id="message2" class="error fade fade-away"><p>' . $error_messages . '</p></div>';
	}	
}


Josh

  • Support Staff

October 1, 2014 at 9:39 am

Hi there,

When you go to export this data out again, is there anything getting logged to your php error_log file? It may help to turn WP_DEBUG on and set it to log errors to a file (not display). You can set this up by adding some debug logging code to your wp-config.php file:

http://codex.wordpress.org/Debugging_in_WordPress#Example_wp-config.php_for_Debugging


Pixel Workshop

October 1, 2014 at 12:12 pm

Hi Josh!

There’s no errors logged in my Apache/PHP Error log, but something interesting I noticed is that I modified two athletes’ registration records (one changed her shirt size and the other changed from one distance race to another) and their bib numbers are now showing up in the exported file, but still none of the others are.


Josh

  • Support Staff

October 1, 2014 at 1:27 pm

Hi there,

Here’s the guidance I can give you:

1) Do a diff comparing your code to our code. I recommend using DiffMerge, or your IDE may have something built in or available as a bundle/plugin. You might find something you added/removed/modified that you didn’t intend. This will help you narrow down the issue.

2) At the end of the two arrays under the //Add event data comment, I can advise setting the is_primary value to 1. So in the first array you’ll add this:

'is_primary' => '1',

and in the second array, this:

'%d', // is_primary

The support post ‘Attendee Batch Import Modification’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso