query("SELECT 1 FROM marketing_files LIMIT 1"); } catch (PDOException $e) { echo "Marketing tables not found. Please run marketing_install.php first.
"; echo "Install Marketing System"; 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
"; exit; } echo "

Marketing File Migration

"; echo "

Migrating existing marketing files to the new system...

"; $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: $product_group
"; // 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 "  - Processing folder: $folder_name
"; // 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 "    ✓ $file
"; // 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 "

Migration Summary

"; echo "✓ Folders migrated: $migrated_folders
"; echo "✓ Files migrated: $migrated_files
"; if (!empty($errors)) { echo "

Errors/Warnings:

"; foreach ($errors as $error) { echo "⚠ $error
"; } } echo "
Migration completed successfully!
"; echo "Go to Marketing Page"; } catch (Exception $e) { echo "Migration error: " . $e->getMessage(); } ?>