Event Import

Upload files:

"; for ($x = 0; $x < $num_of_uploads; $x++) { $form .= "

*"; } $form .= "

*Maximum file name length (minus extension) is 15 characters. Anything over that will be cut to only 15 characters. Valid file type(s): "; for ($x = 0; $x < count($file_types_array); $x++) { if ($x < count($file_types_array) - 1) { $form .= $file_types_array[$x] . ", "; } else { $form .= $file_types_array[$x] . ".

"; } } $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)) { echo("

File uploaded successfully. - " . $filename . "

"); } else { echo($origfilename . " was not successfully uploaded
"); } } else { echo($origfilename . " was too big, not uploaded
"); } } else { echo($origfilename . " had an invalid file extension, not uploaded
"); } } else { echo($origfilename . " was not successfully uploaded
"); } } } } } function load_events_to_db() { global $wpdb, $current_user; $events_detail_tbl = get_option('events_detail_tbl'); $curdate = date("Y-m-j"); $month = date('M'); $day = date('j'); $year = date('Y'); $fieldseparator = ","; $lineseparator = "\n"; $csvfile = "../wp-content/uploads/events.csv"; if (!function_exists('espresso_member_data')) { $current_user->ID = 1; } 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)) { echo "File not found. Make sure you specified the correct path.\n"; exit; } $file = fopen($csvfile, "r"); if (!$file) { echo "Error opening data file.\n"; exit; } $size = filesize($csvfile); if (!$size) { echo "File is empty.\n"; exit; } $file = file_get_contents($csvfile); $dataStrings = explode("\r", $file); $i = 0; $question_groups_rs = $wpdb->get_results("select * from ".EVENTS_QST_GROUP_TABLE." where wp_user = ".$current_user->ID." and system_group = 1"); $question_groups_ar = array(); foreach($question_groups_rs as $question_group) { $question_groups_ar[] = $question_group->id; } $question_groups = serialize($question_groups_ar); foreach ($dataStrings as $data) { ++$i; for ($j = 0; $j < $i; ++$j) { $strings = getCSVValues($dataStrings[$j]); } //echo "valid is :'".$valid."'"; if (array_key_exists('2', $strings)) { //echo "The element is in the array"; $skip = $strings[0]; if ($skip >= "1") { // Event meta info - $event_meta = array(); $event_meta['default_payment_status'] = ""; $event_meta['venue_id'] = ''; $event_meta['additional_attendee_reg_info'] = 1; $event_meta['add_attendee_question_groups'] = unserialize($question_groups); $event_meta['date_submitted'] = date("Y-m-d H:i:s"); $event_meta = serialize($event_meta); //Add event data $events_sql = "INSERT INTO " . EVENTS_DETAIL_TABLE . " (event_name, event_desc, address, city, state, country, zip, phone, display_desc, event_identifier, "; $events_sql .= " start_date, end_date, reg_limit, allow_multiple, additional_limit, send_mail, is_active, conf_mail, registration_start, registration_end, question_groups,event_meta)"; $events_sql .= " VALUES ('$strings[1]', '$strings[2]', '$strings[3]', '$strings[4]', '$strings[5]', '$strings[6]', '$strings[7]', '$strings[8]', "; $events_sql .= " '$strings[9]','$strings[10]', '" . event_date_display($strings['11'], 'Y-m-d') . "', '" . event_date_display($strings['12'], 'Y-m-d') . "','$strings[15]', "; $events_sql .= " '$strings[17]', '$strings[18]', '$strings[19]', '$strings[20]', '$strings[21]','" . event_date_display($strings['22'], 'Y-m-d') . "', '" . event_date_display($strings['23'], 'Y-m-d') . "','". $question_groups."','". $event_meta."')"; // echo $events_sql; if ($wpdb->query($wpdb->prepare($events_sql)) === false) { print $wpdb->print_error(); } else { $last_event_id = $wpdb->insert_id; } //Add times data $times_sql = "INSERT INTO " . EVENTS_START_END_TABLE . " (event_id, start_time, end_time) VALUES ('" . $last_event_id . "', '" . date("h:i A", strtotime($strings[13])) . "', '" . date("h:i A", strtotime($strings[14])) . "')"; //echo $times_sql; //$wpdb->query ( $wpdb->prepare( $times_sql ) ); if ($wpdb->query($wpdb->prepare($times_sql)) === false) { print $wpdb->print_error(); } //Add price data $prices_sql = "INSERT INTO " . EVENTS_PRICES_TABLE . " (event_id, event_cost) VALUES ('" . $last_event_id . "', '$strings[16]')"; //echo $prices_sql; // $wpdb->query ( $wpdb->prepare( $prices_sql ) ); if ($wpdb->query($wpdb->prepare($prices_sql)) === false) { print $wpdb->print_error(); } } } } unlink($csvfile); if (!file_exists($csvfile)) { echo "
Upload file has been deleted.
"; } $tot_records = $i - "2"; echo "Added a total of $tot_records events to the database.
"; }