| Server IP : 104.21.21.239 / Your IP : 216.73.216.11 Web Server : Apache/2.4.68 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-69-123.ec2.internal 6.1.176-223.369.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jul 24 13:34:27 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/tgnewdev/admin/batch/ |
Upload File : |
<?php
use classes\lib\BatchJob;
include(__DIR__ . '/../mod/_mod_security.php');
$job_id = (int)$argv[2];
$batch_job = new BatchJob($job_id);
try {
$csv_file = (string)$argv[3];
$import_column_names = (bool)$argv[4];
//************* Get CSV File *************
if (empty($csv_file)) :
throw new RuntimeException('No CSV file path provided ');
endif;
if (!file_exists($csv_file)) :
throw new RuntimeException('CSV file does not exist - ' . $csv_file);
endif;
$handle = fopen($csv_file, 'r');
if ($handle === false) :
throw new RuntimeException('Unable to open CSV file - ' . $csv_file);
endif;
//************* Set up global parameters *************
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$sql = "
INSERT INTO media SET
media_cat = :media_cat,
media_type = :media_type,
media_path = :media_path,
media_filename = :media_filename,
media_desc = :media_desc,
media_credit = :media_credit,
hash = :hash,
create_date = NOW()
";
$stmt_img = $db->prepare($sql);
$sql = "
INSERT INTO events SET
name = :name,
slug = :slug,
featured_image = :featured_image,
html = :html,
status = :status,
start_time_local = :start_time_local,
end_time_local = :end_time_local,
event_timezone = 'America/New_York',
location_id = :location_id,
location_type = :location_type,
location_url = :location_url,
location_url_text = :location_url_text,
price = :price,
event_url = :event_url,
submitted_name = 'CSV Import',
submitted_location = :submitted_location,
create_date = NOW()
";
$stmt_event = $db->prepare($sql);
$sql = "
INSERT INTO events_categories SET
event_id = :event_id,
category_id = :category_id
";
$stmt_category_event = $db->prepare($sql);
$event_count = 0;
$count = 0;
$cancel = false;
$first_row = true;
//************* Process the CSV file *************
while (($row = fgetcsv($handle)) !== false) :
if ($import_column_names && $first_row) :
$first_row = false;
continue;
endif;
if (!$cancel) :
$slug = generate_unique_slug($row[0], 'events', 'slug');
//************ process image *************
$save_image_file = [];
if (!empty($row[10])) :
$image_url = clean_string($row[10]);
$save_image_file = image_url_to_file($image_url, 'img/content/');
if (!empty($save_image_file['filename']) && !$save_image_file['duplicate']) :
$data_img = [
':media_cat' => 'content',
':media_type' => 'img',
':media_path' => 'img/content/',
':media_filename' => $save_image_file['filename'],
':media_desc' => clean_string($row[0]),
':media_credit' => clean_string($row[11]),
':hash' => $save_image_file['hash'],
];
$stmt_img->execute($data_img);
endif;
endif;
//********** loop through all images in the post ************
$post_html = normalize_html($row[12]);
$post_html = $purifier->purify($post_html);
$post_html = html_image_parse_and_store($post_html, 'content', $row[0]);
//********* Get Location *****************
$location_type = strtolower($row[5]);
$location_url = null;
$location_id = null;
$submitted_location = null;
if ($location_type === 'physical') :
$location_id = lookup_db_field('locations', 'name', $row[6], 'id');
if (empty($location_id)) :
$location_id = null;
$submitted_location = clean_string($row[6]);
endif;
elseif ($location_type === 'url') :
$location_url = clean_string($row[6]);
endif;
//****************************************
$stmt_event->execute([
':name' => clean_string($row[0]),
':slug' => $slug,
':featured_image' => !empty($save_image_file['filename']) ? $save_image_file['filename'] : null,
':html' => $post_html,
':status' => 0,
':start_time_local' => nzdatetime($row[1] . ' ' . $row[2], 'NULL', false),
':end_time_local' => nzdatetime($row[3] . ' ' . $row[4], 'NULL', false),
':location_id' =>$location_id,
':location_type' => $location_type,
':location_url' => $location_url,
':location_url_text' => 'Register Here',
':event_url' => clean_string($row[8]),
':price' => clean_string($row[9]),
':submitted_location' => $submitted_location,
]);
$event_id = last_id();
$event_count++;
//************ categories ************
$ar_categories = array_map('trim', explode(',', $row[7]));
foreach ($ar_categories as $category_name) :
$category_id = lookup_db_field('categories', 'name', $category_name, 'id');
if (!empty($category_id)) :
$stmt_category_event->execute([
':event_id' => $event_id,
':category_id' => (int)$category_id,
]);
endif;
endforeach;
//************************************
endif;
$count++;
if ($count % 20 === 0) :
$batch_job->set_processed_recs($count);
endif;
if ($batch_job->cancel_requested()) :
$cancel = true;
$html = '<p>Job cancelled.</p>';
$batch_job->append_output($html);
break;
endif;
endwhile;
$batch_job->set_total_recs($count);
$batch_job->set_processed_recs($count);
$html = '<p>process_csv_data_import job statistics:</p>';
$html .= '<p>Records selected: ' . $count . '</p>';
$html .= '<p>Event Records inserted: ' . $event_count . '</p>';
$batch_job->append_output($html);
$batch_job->finish($cancel);
} catch (Throwable $e) {
$batch_job->fail(nl2br(
'Error: ' . $e->getMessage() . PHP_EOL .
'File: ' . $e->getFile() . PHP_EOL .
'Line: ' . $e->getLine() . PHP_EOL .
'Stack trace:' . PHP_EOL . $e->getTraceAsString()
));
}