Files
assetmgt/marketing_migrate.php

171 lines
8.0 KiB
PHP

<?php
defined(page_security_key) or exit;
/**
* Marketing System Migration
* Migrate existing marketing files from directory structure to database
*/
include_once './assets/functions.php';
$pdo = dbConnect($dbname);
// Check if marketing tables exist
try {
$pdo->query("SELECT 1 FROM marketing_files LIMIT 1");
} catch (PDOException $e) {
echo "Marketing tables not found. Please run marketing_install.php first.<br>";
echo "<a href='marketing_install.php'>Install Marketing System</a>";
exit;
}
// Get marketing structure from settings
$main_marketing_dir = './marketing/';
if (!file_exists($main_marketing_dir)) {
echo "Marketing directory not found at: $main_marketing_dir<br>";
exit;
}
echo "<h2>Marketing File Migration</h2>";
echo "<p>Migrating existing marketing files to the new system...</p>";
$migrated_folders = 0;
$migrated_files = 0;
$errors = [];
try {
// Build partner hierarchy for current user
$partner_hierarchy = json_encode(array("salesid" => $partner->salesid, "soldto" => $partner->soldto), JSON_UNESCAPED_UNICODE);
// Scan marketing directory structure
if (isset($marketing_structure) && is_array($marketing_structure)) {
foreach ($marketing_structure as $product_group => $folders) {
echo "Processing product group: <strong>$product_group</strong><br>";
// Create product group folder
$stmt = $pdo->prepare("INSERT INTO marketing_folders (folder_name, description, createdby, accounthierarchy) VALUES (?, ?, ?, ?)");
$stmt->execute([$product_group, "Migrated from legacy structure", $username, $partner_hierarchy]);
$group_folder_id = $pdo->lastInsertId();
$migrated_folders++;
foreach ($folders as $folder_name) {
$folder_path = $main_marketing_dir . $product_group . '/' . $folder_name;
if (is_dir($folder_path)) {
echo "&nbsp;&nbsp;- Processing folder: $folder_name<br>";
// Create content folder
$stmt = $pdo->prepare("INSERT INTO marketing_folders (folder_name, parent_id, description, createdby, accounthierarchy) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$folder_name, $group_folder_id, "Migrated from legacy structure", $username, $partner_hierarchy]);
$content_folder_id = $pdo->lastInsertId();
$migrated_folders++;
// Process files in folder
$files = array_diff(scandir($folder_path), array('.', '..', 'Thumb'));
foreach ($files as $file) {
$file_path = $folder_path . '/' . $file;
if (is_file($file_path) && !is_dir($file_path)) {
$file_info = pathinfo($file);
$file_ext = strtolower($file_info['extension'] ?? '');
// Skip system files
if (in_array($file_ext, ['ds_store', 'thumbs.db']) || empty($file_ext)) {
continue;
}
// Check if file type is supported
$allowed_types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'mp4', 'mov', 'avi'];
if (!in_array($file_ext, $allowed_types)) {
$errors[] = "Skipped unsupported file type: $file";
continue;
}
$file_size = filesize($file_path);
$relative_path = str_replace('./', '', $file_path);
// Check for existing thumbnail
$thumbnail_path = null;
$thumb_file = $folder_path . '/Thumb/' . $file;
if (file_exists($thumb_file)) {
$thumbnail_path = str_replace('./', '', $thumb_file);
}
// Generate tags based on product group and folder
$tags = [$product_group, $folder_name];
if (strpos(strtolower($file), 'brochure') !== false) $tags[] = 'brochure';
if (strpos(strtolower($file), 'manual') !== false) $tags[] = 'manual';
if (strpos(strtolower($file), 'spec') !== false) $tags[] = 'specifications';
try {
$stmt = $pdo->prepare("
INSERT INTO marketing_files
(title, original_filename, file_path, thumbnail_path, file_type, file_size, folder_id, tags, createdby, accounthierarchy)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$file_info['filename'],
$file,
$relative_path,
$thumbnail_path,
$file_ext,
$file_size,
$content_folder_id,
json_encode($tags),
$username,
$partner_hierarchy
]);
$migrated_files++;
echo "&nbsp;&nbsp;&nbsp;&nbsp;✓ $file<br>";
// Insert tags
foreach ($tags as $tag) {
$tag = trim($tag);
if (!empty($tag)) {
// Insert tag if not exists
$pdo->prepare("INSERT IGNORE INTO marketing_tags (tag_name) VALUES (?)")->execute([$tag]);
// Link file to tag
$tag_id_stmt = $pdo->prepare("SELECT id FROM marketing_tags WHERE tag_name = ?");
$tag_id_stmt->execute([$tag]);
$tag_id = $tag_id_stmt->fetchColumn();
if ($tag_id) {
$pdo->prepare("INSERT IGNORE INTO marketing_file_tags (file_id, tag_id) VALUES (?, ?)")
->execute([$pdo->lastInsertId(), $tag_id]);
}
}
}
} catch (PDOException $e) {
$errors[] = "Error migrating file $file: " . $e->getMessage();
}
}
}
}
}
}
}
echo "<br><h3>Migration Summary</h3>";
echo "✓ Folders migrated: <strong>$migrated_folders</strong><br>";
echo "✓ Files migrated: <strong>$migrated_files</strong><br>";
if (!empty($errors)) {
echo "<br><h4>Errors/Warnings:</h4>";
foreach ($errors as $error) {
echo "$error<br>";
}
}
echo "<br><strong>Migration completed successfully!</strong><br>";
echo "<a href='index.php?page=marketing'>Go to Marketing Page</a>";
} catch (Exception $e) {
echo "Migration error: " . $e->getMessage();
}
?>