256 lines
8.6 KiB
PHP
256 lines
8.6 KiB
PHP
<?php
|
|
|
|
// ======================
|
|
// Import Large CSV Class
|
|
// ======================
|
|
|
|
class ImportLargeCSV extends Base {
|
|
|
|
// Variables
|
|
|
|
private $current_user_serial = 0;
|
|
private $current_user_id = "";
|
|
private $current_user_name = "";
|
|
private $current_user_email = "";
|
|
private $pagination_size = 20;
|
|
private $pagination_page = 1;
|
|
|
|
// =================
|
|
// Class Constructor
|
|
// =================
|
|
|
|
public function __construct() {
|
|
|
|
set_error_handler(array($this, "displayError"));
|
|
|
|
date_default_timezone_set(self::TIMEZONE);
|
|
|
|
$this->current_user_serial = $_SESSION[self::USER_SERIAL] ?? 0;
|
|
$this->current_user_id = $_SESSION[self::USER_ID] ?? "";
|
|
$this->current_user_name = $_SESSION[self::USER_NAME] ?? "";
|
|
$this->current_user_email = $_SESSION[self::USER_EMAIL] ?? "";
|
|
|
|
$this->pagination_page = $_SESSION[self::PAGINATION_PAGE] ?? 1;
|
|
}
|
|
|
|
// =====================
|
|
// Create Import Request
|
|
// =====================
|
|
|
|
public function createImportRequest() {
|
|
|
|
$subscription_serial = filter_input(INPUT_POST, "subscription_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
|
|
|
$subscription = (new Subscriptions())->getSubscription($subscription_serial);
|
|
|
|
if ($subscription->count() === 0) {
|
|
$this->logError("Invalid Subscription ({$subscription_serial}): " . __METHOD__);
|
|
}
|
|
|
|
$import_type = filter_input(INPUT_POST, "import_type") ?: "";
|
|
$report_type = filter_input(INPUT_POST, "report_type") ?: "";
|
|
|
|
$importjob_import_type = str_replace(' ', '', $subscription->record->subscription_full_title) . "$import_type";
|
|
$importjob_file_name = str_replace(' ', '_', $subscription->record->subscription_full_title) . "_all_records.{$report_type}";
|
|
// $importjob_file_name = str_replace('&', 'And', $subscription->record->subscription_full_title) . "_all_records.{$report_type}";
|
|
|
|
$SQL = "insert into importjobs
|
|
|
|
( importjob_subscription,
|
|
importjob_import_type,
|
|
importjob_file_name,
|
|
importjob_file_type,
|
|
importjob_creator,
|
|
importjob_created )
|
|
|
|
VALUES ( :importjob_subscription,
|
|
:importjob_import_type,
|
|
:importjob_file_name,
|
|
:importjob_file_type,
|
|
:importjob_creator,
|
|
:importjob_created )";
|
|
|
|
$statement = $this->connect()->prepare($SQL);
|
|
|
|
$statement->bindValue(":importjob_subscription", $subscription_serial);
|
|
$statement->bindValue(":importjob_import_type", $importjob_import_type);
|
|
$statement->bindValue(":importjob_file_name", $importjob_file_name);
|
|
$statement->bindValue(":importjob_file_type", strtoupper($report_type));
|
|
$statement->bindValue(":importjob_creator", $this->current_user_serial);
|
|
$statement->bindValue(":importjob_created", $this->getTimestamp());
|
|
|
|
$statement->execute();
|
|
|
|
$this->displayNotice("Your import has been queued. You will receive an notification when it is ready.");
|
|
}
|
|
|
|
// ============
|
|
// User Imports
|
|
// ============
|
|
|
|
public function usersImports() {
|
|
|
|
$imports = $this->getUsersImports();
|
|
$popovers = (new Popovers())->getPopovers();
|
|
|
|
$this->markImportsAsSeen();
|
|
|
|
$XML = $this->mergeXML($imports, "<XML/>");
|
|
$XML = $this->mergeXML($popovers, $XML);
|
|
|
|
$html = $this->applyXSL($XML, $this->getXSL("viewImports"));
|
|
|
|
$this->displayContent($html);
|
|
}
|
|
|
|
// =========================
|
|
// Mark User Imports as Seen
|
|
// =========================
|
|
|
|
public function markImportsAsSeen() {
|
|
|
|
$SQL = "update importjobs
|
|
set importjob_notified = true
|
|
where importjobs.importjob_creator = {$this->current_user_serial}";
|
|
|
|
$statement = $this->connect()->prepare($SQL);
|
|
|
|
$statement->execute();
|
|
}
|
|
|
|
// =======================
|
|
// Return a Imports Object
|
|
// =======================
|
|
|
|
public function getUsersImports() {
|
|
|
|
// Find Criteria
|
|
|
|
$find_importjob = filter_input(INPUT_POST, "find_importjob") ?: "";
|
|
$find_words = $this->sanitizeString($find_importjob);
|
|
|
|
// Show Per Page
|
|
$this->pagination_size = filter_input(INPUT_POST, "importjobs_per_page", FILTER_SANITIZE_NUMBER_INT) ?: $this->pagination_size;
|
|
|
|
// Record Count
|
|
|
|
$connection = $this->connect();
|
|
|
|
$SQL = "select count(*) as count
|
|
from view_importjobs
|
|
where view_importjobs.importjob_creator = {$this->current_user_serial}
|
|
and view_importjobs.subscription_full_title regexp :subscription_full_title";
|
|
|
|
$statement = $connection->prepare($SQL);
|
|
|
|
$statement->bindValue(":subscription_full_title", $find_words);
|
|
|
|
$statement->execute();
|
|
|
|
$recordset = $statement->fetch(PDO::FETCH_ASSOC);
|
|
|
|
$count = (count($recordset) > 0) ? (integer) $recordset["count"] : (integer) 0;
|
|
|
|
// Pagination
|
|
|
|
$pagination = filter_input(INPUT_POST, "pagination") ?: "";
|
|
|
|
switch ($pagination) {
|
|
|
|
case "first" : $this->pagination_page = 1;
|
|
break;
|
|
case "previous" : $this->pagination_page = max(($this->pagination_page - 1), 1);
|
|
break;
|
|
case "next" : $this->pagination_page = ($this->pagination_page + 1);
|
|
break;
|
|
case "last" : $this->pagination_page = ($count / $this->pagination_size);
|
|
break;
|
|
}
|
|
|
|
$offset = (($this->pagination_page * $this->pagination_size) - $this->pagination_size);
|
|
$start = ($offset + 1);
|
|
$end = min($count, (($start + $this->pagination_size) - 1));
|
|
|
|
// Data
|
|
|
|
$SQL = "select view_importjobs.*
|
|
from view_importjobs
|
|
where view_importjobs.importjob_creator = {$this->current_user_serial}
|
|
and view_importjobs.subscription_full_title regexp :subscription_full_title
|
|
limit {$this->pagination_size}
|
|
offset {$offset}";
|
|
|
|
$statement = $connection->prepare($SQL);
|
|
|
|
$statement->bindValue(":subscription_full_title", $find_words);
|
|
|
|
$statement->execute();
|
|
|
|
$importjobs = $this->getTableXML("importjobs", $statement);
|
|
|
|
// Insert Pagination Attributes
|
|
|
|
$importjobs->addAttribute("count", $count);
|
|
$importjobs->addAttribute("start", $start);
|
|
$importjobs->addAttribute("end", $end);
|
|
$importjobs->addAttribute("page", $this->pagination_page);
|
|
|
|
$_SESSION[self::PAGINATION_PAGE] = $this->pagination_page;
|
|
|
|
return $importjobs;
|
|
}
|
|
|
|
// =====================================
|
|
// Return if users imports are completed
|
|
// =====================================
|
|
|
|
public function checkUsersCompletedImports_AJAX() {
|
|
|
|
$SQL = "select count(*) as notify
|
|
from view_importjobs
|
|
where view_importjobs.importjob_creator = :current_user_serial
|
|
and view_importjobs.importjob_status = 'complete'
|
|
and view_importjobs.importjob_notified = 0";
|
|
|
|
$importjobs = $this->getTable("importjobs", $SQL, array('current_user_serial' => $this->current_user_serial));
|
|
|
|
// Create the User Object.
|
|
|
|
$users_imports = array();
|
|
|
|
$users_imports["notify"] = ($importjobs->record->notify == 0) ? false : true;
|
|
|
|
if ($_SESSION[self::AJAX]) {
|
|
echo json_encode($users_imports);
|
|
} else {
|
|
return $users_imports;
|
|
}
|
|
}
|
|
|
|
// ===============
|
|
// Download Import
|
|
// ===============
|
|
|
|
public function downloadImport() {
|
|
|
|
$import_code = filter_input(INPUT_POST, "import_code") ?: "";
|
|
|
|
$SQL = "select view_importjobs.*
|
|
from view_importjobs
|
|
where view_importjobs.importjob_download_token = :import_code
|
|
and view_importjobs.importjob_status = 'complete' limit 1";
|
|
|
|
$importjob = $this->getTable("importjob", $SQL, array("import_code" => $import_code));
|
|
|
|
$file_path = str_replace('../imports/', './imports/', $importjob->record->importjob_file_path);
|
|
$file_name = $importjob->record->importjob_file_name;
|
|
|
|
header('Content-Type: application/octet-stream');
|
|
header("Content-Disposition: attachment; filename=\"$file_name\"");
|
|
|
|
readfile($file_path);
|
|
}
|
|
}
|
|
|
|
?>
|