Added Import for Large CSV
This commit is contained in:
+157
-82
@@ -45,7 +45,11 @@ class Import extends Base {
|
||||
|
||||
case "prompt":
|
||||
|
||||
$content = $this->applyXSL("", $this->getXSL("uploadCSVFile"));
|
||||
$import_type = filter_input(INPUT_POST, "import_type") ?: "incremental";
|
||||
|
||||
$XSLParms['IMPORT-TYPE'] = $import_type;
|
||||
|
||||
$content = $this->applyXSL("", $this->getXSL("uploadCSVFile"), $XSLParms);
|
||||
$this->displayContent($content);
|
||||
|
||||
break;
|
||||
@@ -54,6 +58,7 @@ class Import extends Base {
|
||||
|
||||
$XSLParms['UPLOAD_STATUS'] = "UPLOAD SUCCESSFUL";
|
||||
$environment = (string) $this->getSettings()->Environment ?: "Production";
|
||||
$upload_type = filter_input(INPUT_POST, 'upload_type') ?: "incremental";
|
||||
|
||||
if (empty($environment)) {
|
||||
$this->logError("Invalid Environment ({$environment}). " . __METHOD__);
|
||||
@@ -63,30 +68,57 @@ class Import extends Base {
|
||||
|
||||
// Process the Attachment
|
||||
|
||||
if (!empty($_FILES)) {
|
||||
if (empty($_FILES["csvfile"])) {
|
||||
$this->displayNotice("No CSV file was uploaded.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_dir($environment_directory)) {
|
||||
mkdir($environment_directory);
|
||||
}
|
||||
if (!is_dir($environment_directory) && !mkdir($environment_directory, 0775, true)) {
|
||||
$this->displayNotice("Unable to create the upload directory. Please contact Support.");
|
||||
return;
|
||||
}
|
||||
|
||||
$tempname = $_FILES["csvfile"]["tmp_name"];
|
||||
$filename = $_FILES["csvfile"]["name"];
|
||||
if ($_FILES["csvfile"]["error"] !== UPLOAD_ERR_OK) {
|
||||
$this->displayNotice("The CSV file upload failed. Please try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($tempname) or empty($filename)) {
|
||||
return;
|
||||
}
|
||||
$tempname = $_FILES["csvfile"]["tmp_name"];
|
||||
$filename = basename($_FILES["csvfile"]["name"]);
|
||||
|
||||
if (!move_uploaded_file($tempname, "{$environment_directory}/{$filename}")) {
|
||||
$XSLParms['UPLOAD_STATUS'] = "UPLOAD FAILED";
|
||||
}
|
||||
if (empty($tempname) or empty($filename)) {
|
||||
$this->displayNotice("The uploaded CSV file could not be read. Please try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
$file_directory = "{$environment_directory}/{$filename}";
|
||||
|
||||
$import = $this->parseCSVFile($file_directory);
|
||||
if (!move_uploaded_file($tempname, $file_directory)) {
|
||||
$this->displayNotice("Unable to save the uploaded CSV file. Please contact Support.");
|
||||
return;
|
||||
}
|
||||
|
||||
$content = $this->applyXSL("", $this->getXSL("uploadMessage"), $XSLParms);
|
||||
$this->displayContent($content);
|
||||
if ($upload_type === 'incremental') {
|
||||
$this->parseCSVFile($file_directory);
|
||||
$content = $this->applyXSL("", $this->getXSL("uploadMessage"), $XSLParms);
|
||||
$this->displayContent($content);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($upload_type === 'full') {
|
||||
|
||||
$successful = $this->createImportRequest($file_directory);
|
||||
|
||||
if (!$successful) {
|
||||
$this->displayNotice("An error occurred while creating the import request. Please contact Support.");
|
||||
return;
|
||||
}
|
||||
|
||||
$this->displayNotice("Your import has been queued. You will receive a notification when it is complete.");
|
||||
return;
|
||||
}
|
||||
|
||||
$this->displayNotice("Invalid upload type. Please contact Support.");
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -143,73 +175,74 @@ class Import extends Base {
|
||||
$County = (new Counties())->getCountiesByName($County)->record->county_serial;
|
||||
|
||||
$SQL = "INSERT INTO permits (
|
||||
permit_permit_facts_id,
|
||||
permit_entry_date,
|
||||
permit_permit_number,
|
||||
permit_permit_date,
|
||||
permit_project_addr,
|
||||
permit_sub_div_name,
|
||||
permit_project_type,
|
||||
permit_company,
|
||||
permit_address,
|
||||
permit_city,
|
||||
permit_state,
|
||||
permit_zip,
|
||||
permit_phone,
|
||||
permit_project_city,
|
||||
permit_project_state,
|
||||
permit_size,
|
||||
permit_units,
|
||||
permit_building_type,
|
||||
permit_value,
|
||||
permit_work_type,
|
||||
permit_county,
|
||||
permit_lot,
|
||||
permit_dist_ll,
|
||||
permit_ct1,
|
||||
permit_owner_name,
|
||||
permit_owner_address,
|
||||
permit_owner_city,
|
||||
permit_owner_state,
|
||||
permit_owner_zip,
|
||||
permit_owner_phone,
|
||||
permit_ytd_permits,
|
||||
permit_ytd_value,
|
||||
permit_12mth_prev_permits )
|
||||
permit_permit_facts_id,
|
||||
permit_entry_date,
|
||||
permit_permit_number,
|
||||
permit_permit_date,
|
||||
permit_project_addr,
|
||||
permit_sub_div_name,
|
||||
permit_project_type,
|
||||
permit_company,
|
||||
permit_address,
|
||||
permit_city,
|
||||
permit_state,
|
||||
permit_zip,
|
||||
permit_phone,
|
||||
permit_project_city,
|
||||
permit_project_state,
|
||||
permit_size,
|
||||
permit_units,
|
||||
permit_building_type,
|
||||
permit_value,
|
||||
permit_work_type,
|
||||
permit_county,
|
||||
permit_lot,
|
||||
permit_dist_ll,
|
||||
permit_ct1,
|
||||
permit_owner_name,
|
||||
permit_owner_address,
|
||||
permit_owner_city,
|
||||
permit_owner_state,
|
||||
permit_owner_zip,
|
||||
permit_owner_phone,
|
||||
permit_ytd_permits,
|
||||
permit_ytd_value,
|
||||
permit_12mth_prev_permits )
|
||||
|
||||
VALUES (
|
||||
:permit_permit_facts_id,
|
||||
:permit_entry_date,
|
||||
:permit_permit_number,
|
||||
:permit_permit_date,
|
||||
:permit_project_addr,
|
||||
:permit_sub_div_name,
|
||||
:permit_project_type,
|
||||
:permit_company,
|
||||
:permit_address,
|
||||
:permit_city,
|
||||
:permit_state,
|
||||
:permit_zip,
|
||||
:permit_phone,
|
||||
:permit_project_city,
|
||||
:permit_project_state,
|
||||
:permit_size,
|
||||
:permit_units,
|
||||
:permit_building_type,
|
||||
:permit_value,
|
||||
:permit_work_type,
|
||||
:permit_county,
|
||||
:permit_lot,
|
||||
:permit_dist_ll,
|
||||
:permit_ct1,
|
||||
:permit_owner_name,
|
||||
:permit_owner_address,
|
||||
:permit_owner_city,
|
||||
:permit_owner_state,
|
||||
:permit_owner_zip,
|
||||
:permit_owner_phone,
|
||||
:permit_ytd_permits,
|
||||
:permit_ytd_value,
|
||||
:permit_12mth_prev_permits )";
|
||||
:permit_permit_facts_id,
|
||||
:permit_entry_date,
|
||||
:permit_permit_number,
|
||||
:permit_permit_date,
|
||||
:permit_project_addr,
|
||||
:permit_sub_div_name,
|
||||
:permit_project_type,
|
||||
:permit_company,
|
||||
:permit_address,
|
||||
:permit_city,
|
||||
:permit_state,
|
||||
:permit_zip,
|
||||
:permit_phone,
|
||||
:permit_project_city,
|
||||
:permit_project_state,
|
||||
:permit_size,
|
||||
:permit_units,
|
||||
:permit_building_type,
|
||||
:permit_value,
|
||||
:permit_work_type,
|
||||
:permit_county,
|
||||
:permit_lot,
|
||||
:permit_dist_ll,
|
||||
:permit_ct1,
|
||||
:permit_owner_name,
|
||||
:permit_owner_address,
|
||||
:permit_owner_city,
|
||||
:permit_owner_state,
|
||||
:permit_owner_zip,
|
||||
:permit_owner_phone,
|
||||
:permit_ytd_permits,
|
||||
:permit_ytd_value,
|
||||
:permit_12mth_prev_permits )";
|
||||
|
||||
$statement = $connection->prepare($SQL);
|
||||
|
||||
@@ -255,6 +288,48 @@ class Import extends Base {
|
||||
return true;
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Create Import Request
|
||||
// =====================
|
||||
|
||||
public function createImportRequest($filepath = "") {
|
||||
|
||||
if (!$filepath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$SQL = "insert into importjobs
|
||||
|
||||
( importjob_file_path,
|
||||
importjob_notified,
|
||||
importjob_creator,
|
||||
importjob_created )
|
||||
|
||||
VALUES ( :importjob_file_path,
|
||||
:importjob_notified,
|
||||
:importjob_creator,
|
||||
:importjob_created )";
|
||||
|
||||
$statement = $this->connect()->prepare($SQL);
|
||||
|
||||
$statement->bindValue(":importjob_file_path", "../{$filepath}", PDO::PARAM_STR);
|
||||
$statement->bindValue(":importjob_notified", false, PDO::PARAM_BOOL);
|
||||
$statement->bindValue(":importjob_creator", $this->current_user_name, PDO::PARAM_STR);
|
||||
$statement->bindValue(":importjob_created", $this->getTimestamp());
|
||||
|
||||
if (!$statement->execute()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$to = $this->getSettings()->DeveloperEmail;
|
||||
$subject = "CSV Import Jobs Queued";
|
||||
$message = "Import initialized successfully. Processing will start momentarily.";
|
||||
|
||||
(new Email())->sendEmail($to, $subject, $message);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ========================
|
||||
// Return an Formatted Date
|
||||
// ========================
|
||||
|
||||
Reference in New Issue
Block a user