/** * Process a directory with a bunch of .csv files */ function process_dir($dir) { if (!is_dir($importdir)) { die("ERROR {$importdir} is not a directory<br/>"); } $this->importdir = $dir; $fs = scandir($importdir); foreach ($fs as $f) { if ($f === '.' || $f === '..') { continue; } $files_processed++; $err = process_csv($f); if ($err) { $files_with_errors++; } } }
// Got a file to import require_once e_HANDLER . 'upload_handler.php'; if (($files = process_uploaded_files(e_UPLOAD, FALSE, array('overwrite' => TRUE, 'max_file_count' => 1, 'file_mask' => 'csv'))) === FALSE) { // Invalid file $error = true; $mes->addError(BANLAN_47); } if (empty($files) || vartrue($files[0]['error'])) { $error = true; if (varset($files[0]['message'])) { $mes->addError($files[0]['message']); } } if (!$error) { // Got a file of some sort $message = process_csv(e_UPLOAD . $files[0]['name'], intval(varset($_POST['ban_over_import'], 0)), intval(varset($_POST['ban_over_expiry'], 0)), $separator_char[intval(varset($_POST['ban_separator'], 1))], $quote_char[intval(varset($_POST['ban_quote'], 3))]); banlist_adminlog('07', 'File: ' . e_UPLOAD . $files[0]['name'] . '<br />' . $message); } } $text = "\n\t\t\t<form method='post' action='" . e_ADMIN_ABS . "banlist_export.php' id='core-banlist-transfer-form' >\n\t\t\t\t<fieldset id='core-banlist-transfer-export'>\n\t\t\t\t\t<legend>" . BANLAN_40 . "</legend>\n\t\t\t\t\t<table class='table adminlist'>\n\t\t\t\t\t\t<colgroup>\n\t\t\t\t\t\t\t<col style='width:30%' />\n\t\t\t\t\t\t\t<col style='width:30%' />\n\t\t\t\t\t\t\t<col style='width:40%' />\n\t\t\t\t\t\t</colgroup>\n\t\t\t\t\t\t<tbody>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th colspan='2'>" . BANLAN_36 . "</th>\n\t\t\t\t\t\t\t\t<th> </th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t"; foreach ($ipAdministrator->getValidReasonList() as $i) { $text .= "\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td colspan='3'>\n\t\t\t\t\t\t\t\t" . $frm->checkbox("ban_types[{$i}]", $i) . $frm->label($ipAdministrator->getBanTypeString($i, FALSE), "ban_types[{$i}]", $i) . "\n\t\t\t\t\t\t\t\t<span class='field-help'>(" . $ipAdministrator->getBanTypeString($i, TRUE) . ")</span>\n\t\t\t\t\t\t\t</td></tr>\n\t\t\t"; } $text .= "<tr>\n\t\t\t<td>" . BANLAN_79 . "</td>\n\t\t\t<td>" . select_box('ban_separator', $separator_char) . ' ' . BANLAN_37 . "</td>\n\t\t<td>" . select_box('ban_quote', $quote_char) . ' ' . BANLAN_38 . "</td></tr>"; $text .= "\n\n\t\t\t\t\t\t</tbody>\n\t\t\t\t\t</table>\n\t\t\t\t\t<div class='buttons-bar center'>" . $frm->admin_button('ban_export', BANLAN_39, 'export', BANLAN_39) . "</div>\n\t\t\t\t\t\t<input type='hidden' name='e-token' value='" . e_TOKEN . "' />\n\t\t\t\t</fieldset>\n\t\t\t</form>\n\t\t"; // Now do the import options $text .= "\n\t\t\t<form enctype='multipart/form-data' method='post' action='" . e_SELF . "?transfer' id='ban_import_form' >\n\t\t\t\t<fieldset id='core-banlist-transfer-import'>\n\t\t\t\t\t<legend>" . BANLAN_41 . "</legend>\n\t\t\t\t\t<table class='table adminlist'>\n\t\t\t\t\t\t<colgroup>\n\t\t\t\t\t\t\t<col style='width:30%' />\n\t\t\t\t\t\t\t<col style='width:30%' />\n\t\t\t\t\t\t\t<col style='width:40%' />\n\t\t\t\t\t\t</colgroup>\n\t\t\t\t\t\t<tbody>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<th colspan='2'>" . BANLAN_42 . "</th>\n\t\t\t\t\t\t\t\t<th> </th>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='3'>" . $frm->checkbox('ban_over_import', 1, '', array('label' => BANLAN_43)) . "</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan='3'>" . $frm->checkbox('ban_over_expiry', 1, '', array('label' => BANLAN_44)) . "</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td>" . BANLAN_46 . "</td>\n\t\t\t\t\t\t\t\t<td colspan='2'>\n\t\t\t\t\t\t\t\t\t" . $frm->file('file_userfile[]', array('size' => '40')) . "\n\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t<td>" . BANLAN_80 . "</td>\n\t\t\t<td>" . select_box('ban_separator', $separator_char) . ' ' . BANLAN_37 . "</td>\n\t\t<td>" . select_box('ban_quote', $quote_char) . ' ' . BANLAN_38 . "</td></tr>\n\t\t\t\t\t\t</tbody>\n\t\t\t\t\t</table>\n\t\t\t\t\t\t\t\t<div class='buttons-bar center'>\n\t\t\t\t\t\t\t\t" . $frm->admin_button('ban_import', BANLAN_45, 'import') . "\n\t\t\t\t\t\t\t\t<input type='hidden' name='e-token' value='" . e_TOKEN . "' />\n\t\t\t\t\t\t\t\t</div>\n\n\n\t\t\t\t</fieldset>\n\t\t\t</form>\n\t\t"; echo $mes->render() . $text; // e107::getRender()->tablerender(BANLAN_16.SEP.BANLAN_35, $mes->render().$text); break; // End case 'transfer'
$files_processed = 0; $records_processed = 0; $files_with_errors = 0; $total_errors = 0; $csv2sql = array('Company Name' => 'companyName', 'Email' => 'companyEmail', 'SIC Code' => 'sicCode', 'SIC Code Description' => 'sicCodeDescription', 'SIC Code6' => 'sicCode6', 'SIC Code 6' => 'sicCode6', 'SIC Code6 Description' => 'sicCode6Description', 'SIC Code 6 Description' => 'sicCode6Description', 'NAICS Code' => 'naicsCode', 'Contact Name' => 'contactName', 'First Name' => 'firstName', 'Last Name' => 'lastName', 'Title' => 'title', 'Address' => 'address', 'Address2' => 'address2', 'City' => 'city', 'State' => 'state', 'Zip' => 'zip', 'Phone' => 'phone', 'Fax' => 'fax', 'Company Website' => 'companyWebsite', 'Revenue' => 'revenue', 'Annual Revenue' => 'revenue', 'Employees' => 'employees', 'Industry' => 'industry', 'Desc' => 'description', 'County' => 'county'); if (!is_dir($importdir)) { die("ERROR {$importdir} is not a directory<br/>"); } $dbh = db_connect('rusty', '3st00ges'); $fs = scandir($importdir); foreach ($fs as $f) { if ($f === '.' || $f === '..') { continue; } $files_processed++; $err = process_csv($f); if ($err) { $files_with_errors++; } } $dbh = null; function process_csv($csvfile) { global $csv2sql; global $importdir; $err = 0; $path = $importdir . '/' . $csvfile; echo "Processing: {$csvfile}.. "; $handle = fopen($path, 'r'); if ($handle == FALSE) { die("Could not open file {$path} <br/>");
function import_csv_data($dbh, $csv_data) { $csv_data = process_csv($_FILES['csv']['tmp_name']); $success_list = array(); $error_list = array(); $report = "<ul>"; # Loop through each row, check if person is in `people` table foreach ($csv_data as $row) { $email = $row['E-mail']; $row['Name'] = stripcslashes($row['Name']); if ($row['Status'] === 'Approved') { insert_db_row($dbh, $row); $success_list[] = $row; $status = '<span class="label label-success">' . $row['Status'] . "</span>"; $report .= "<li>{$status} <span class=\"badge\">{$row['Spaces']}</span> {$row['Name']} <{$email}></li>"; } else { $error_list[] = $row; } } $report .= "</ul>"; $error_report = "<ul>"; foreach ($error_list as $row) { $email = $row['E-mail']; $status = '<span class="label label-danger">' . $row['Status'] . "</span>"; $error_report .= "<li>{$status} <span class=\"badge\">{$row['Spaces']}</span> {$row['Name']} <{$email}></li>"; } $error_report .= "</ul>"; # Status is 'Approved': # Person exists in `people` table: # Check that person has correct number of seats (both rows and `tickets.seats`) # Generate more if needed # Email ticket PDF # ELSE person does not exist: # Add to `people` table, generate ticket codes and row # Email ticket PDF # Status is 'Cancelled': # Person exists in `people` table: # Remove associated rows from `tickets` # Remove row from `people` # return array("success" => $report, "errors" => $error_report); }
$result = $query->fetch(); if ($result) { $ungenerated = $result['ungenerated']; } else { $ungenerated = 0; } if (isset($_FILES['csv'])) { $errors = array(); $filename = explode('.', $_FILES['csv']['name']); $file_ext = strtolower(end($filename)); if ($file_ext !== 'csv') { $errors[] = "File extension must be CSV."; } if (empty($errors) == true) { try { $csv_data = process_csv($_FILES['csv']['tmp_name']); # Status is 'Approved': # Person exists in `people` table: # Check that person has correct number of seats (both rows and `tickets.seats`) # Generate more if needed # Email ticket PDF # ELSE person does not exist: # Add to `people` table, generate ticket codes and row # Email ticket PDF # Status is 'Cancelled': # Person exists in `people` table: # Remove associated rows from `tickets` # Remove row from `people` } catch (CSVException $e) { show_message("danger", "Error while processing file: " . $e->getMessage()); $error = $e->getMessage();