'', 'description' => '', 'price' => '', 'rrp' => '', 'quantity' => '', 'date_added' => date('Y-m-d\TH:i'), 'media' => [], 'categories' => [], 'options' => [], 'downloads' => [], 'weight' => '', 'url_slug' => '', 'status' => 1, 'productcode' => '' ]; // Get all the categories from the database $stmt = $pdo->query('SELECT * FROM categories'); $stmt->execute(); $categories = $stmt->fetchAll(PDO::FETCH_ASSOC); // Add product images to the database function addProductImages($pdo, $product_id) { // Get the total number of media if (isset($_POST['media']) && is_array($_POST['media']) && count($_POST['media']) > 0) { // Iterate media $delete_list = []; for ($i = 0; $i < count($_POST['media']); $i++) { // If the media doesnt exist in the database if (!intval($_POST['media_product_id'][$i])) { // Insert new media $stmt = $pdo->prepare('INSERT INTO products_media (product_id,media_id,position) VALUES (?,?,?)'); $stmt->execute([ $product_id, $_POST['media'][$i], $_POST['media_position'][$i] ]); $delete_list[] = $pdo->lastInsertId(); } else { // Update existing media $stmt = $pdo->prepare('UPDATE products_media SET position = ? WHERE id = ?'); $stmt->execute([ $_POST['media_position'][$i], $_POST['media_product_id'][$i] ]); $delete_list[] = $_POST['media_product_id'][$i]; } } // Delete media $in = str_repeat('?,', count($delete_list) - 1) . '?'; $stmt = $pdo->prepare('DELETE FROM products_media WHERE product_id = ? AND id NOT IN (' . $in . ')'); $stmt->execute(array_merge([ $product_id ], $delete_list)); } else { // No media exists, delete all $stmt = $pdo->prepare('DELETE FROM products_media WHERE product_id = ?'); $stmt->execute([ $product_id ]); } } // Add product categories to the database function addProductCategories($pdo, $product_id) { if (isset($_POST['categories']) && is_array($_POST['categories']) && count($_POST['categories']) > 0) { $in = str_repeat('?,', count($_POST['categories']) - 1) . '?'; $stmt = $pdo->prepare('DELETE FROM products_categories WHERE product_id = ? AND category_id NOT IN (' . $in . ')'); $stmt->execute(array_merge([ $product_id ], $_POST['categories'])); foreach ($_POST['categories'] as $cat) { $stmt = $pdo->prepare('INSERT IGNORE INTO products_categories (product_id,category_id) VALUES (?,?)'); $stmt->execute([ $product_id, $cat ]); } } else { $stmt = $pdo->prepare('DELETE FROM products_categories WHERE product_id = ?'); $stmt->execute([ $product_id ]); } } // Add product options to the database function addProductOptions($pdo, $product_id) { if (isset($_POST['option_title']) && is_array($_POST['option_title']) && count($_POST['option_title']) > 0) { $delete_list = []; for ($i = 0; $i < count($_POST['option_title']); $i++) { $delete_list[] = $_POST['option_title'][$i] . '__' . $_POST['option_name'][$i]; $stmt = $pdo->prepare('INSERT INTO products_options (title,name,quantity,price,price_modifier,weight,weight_modifier,type,required,position,product_id) VALUES (?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE quantity = VALUES(quantity), price = VALUES(price), price_modifier = VALUES(price_modifier), weight = VALUES(weight), weight_modifier = VALUES(weight_modifier), type = VALUES(type), required = VALUES(required), position = VALUES(position)'); $stmt->execute([ $_POST['option_title'][$i], $_POST['option_name'][$i], empty($_POST['option_quantity'][$i]) ? -1 : $_POST['option_quantity'][$i], empty($_POST['option_price'][$i]) ? 0.00 : $_POST['option_price'][$i], $_POST['option_price_modifier'][$i], empty($_POST['option_weight'][$i]) ? 0.00 : $_POST['option_weight'][$i], $_POST['option_weight_modifier'][$i], $_POST['option_type'][$i], $_POST['option_required'][$i], $_POST['option_position'][$i], $product_id ]); } $in = str_repeat('?,', count($delete_list) - 1) . '?'; $stmt = $pdo->prepare('DELETE FROM products_options WHERE product_id = ? AND CONCAT(title, "__", name) NOT IN (' . $in . ')'); $stmt->execute(array_merge([ $product_id ], $delete_list)); } else { $stmt = $pdo->prepare('DELETE FROM products_options WHERE product_id = ?'); $stmt->execute([ $product_id ]); } } // Add product downloads to the database function addProductDownloads($pdo, $product_id) { if (isset($_POST['download_file_path']) && is_array($_POST['download_file_path']) && count($_POST['download_file_path']) > 0) { $delete_list = []; for ($i = 0; $i < count($_POST['download_file_path']); $i++) { $delete_list[] = $_POST['download_file_path'][$i]; $stmt = $pdo->prepare('INSERT INTO products_downloads (product_id,file_path,position) VALUES (?,?,?) ON DUPLICATE KEY UPDATE position = VALUES(position)'); $stmt->execute([ $product_id, $_POST['download_file_path'][$i], $_POST['download_position'][$i] ]); } $in = str_repeat('?,', count($delete_list) - 1) . '?'; $stmt = $pdo->prepare('DELETE FROM products_downloads WHERE product_id = ? AND file_path NOT IN (' . $in . ')'); $stmt->execute(array_merge([ $product_id ], $delete_list)); } else { $stmt = $pdo->prepare('DELETE FROM products_downloads WHERE product_id = ?'); $stmt->execute([ $product_id ]); } } if (isset($_GET['id'])) { // ID param exists, edit an existing product $page = 'Edit'; if (isset($_POST['submit'])) { //decode product_config to JSON $product_config = $_POST['product_config'] ?? ''; $productcode = $_POST['productcode'] ?? ''; // Update the product $stmt = $pdo->prepare('UPDATE products SET name = ?, description = ?, price = ?, rrp = ?, quantity = ?, date_added = ?, weight = ?, url_slug = ?, status = ?, product_config = ?, productcode = ? WHERE id = ?'); $stmt->execute([ $_POST['name'], $_POST['description'], empty($_POST['price']) ? 0.00 : $_POST['price'], empty($_POST['rrp']) ? 0.00 : $_POST['rrp'], $_POST['quantity'], date('Y-m-d H:i:s', strtotime($_POST['date'])), empty($_POST['weight']) ? 0.00 : $_POST['weight'], $_POST['url_slug'], $_POST['status'], $product_config, $productcode, $_GET['id'] ]); addProductImages($pdo, $_GET['id']); addProductCategories($pdo, $_GET['id']); addProductOptions($pdo, $_GET['id']); addProductDownloads($pdo, $_GET['id']); // Clear session cart if (isset($_SESSION['cart'])) { unset($_SESSION['cart']); } header('Location: index.php?page=products&success_msg=2'); exit; } if (isset($_POST['delete'])) { // Redirect and delete product header('Location: index.php?page=products&delete=' . $_GET['id']); exit; } // Get the product and its images from the database $stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?'); $stmt->execute([ $_GET['id'] ]); $product = $stmt->fetch(PDO::FETCH_ASSOC); // get product media $stmt = $pdo->prepare('SELECT m.*, pm.position, pm.id AS product_id FROM media m JOIN products_media pm ON pm.media_id = m.id JOIN products p ON p.id = pm.product_id WHERE p.id = ? ORDER BY pm.position'); $stmt->execute([ $_GET['id'] ]); $product['media'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get the product categories $stmt = $pdo->prepare('SELECT c.name, c.id FROM products_categories pc JOIN categories c ON c.id = pc.category_id WHERE pc.product_id = ?'); $stmt->execute([ $_GET['id'] ]); $product['categories'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get the product options $stmt = $pdo->prepare('SELECT title, type, GROUP_CONCAT(name) AS list FROM products_options WHERE product_id = ? GROUP BY title, type, position ORDER BY position'); $stmt->execute([ $_GET['id'] ]); $product['options'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get the product full options $stmt = $pdo->prepare('SELECT * FROM products_options WHERE product_id = ? ORDER BY id'); $stmt->execute([ $_GET['id'] ]); $product['options_full'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get the product downloads $stmt = $pdo->prepare('SELECT * FROM products_downloads WHERE product_id = ? ORDER BY position'); $stmt->execute([ $_GET['id'] ]); $product['downloads'] = $stmt->fetchAll(PDO::FETCH_ASSOC); } else { // Create a new product $page = 'Create'; if (isset($_POST['submit'])) { $product_config = $_POST['product_config'] ?? ''; $productcode = $_POST['productcode'] ?? ''; $stmt = $pdo->prepare('INSERT INTO products (name,description,price,rrp,quantity,date_added,weight,url_slug,status, product_config, productcode) VALUES (?,?,?,?,?,?,?,?,?,?,?)'); $stmt->execute([ $_POST['name'], $_POST['description'], empty($_POST['price']) ? 0.00 : $_POST['price'], empty($_POST['rrp']) ? 0.00 : $_POST['rrp'], $_POST['quantity'], date('Y-m-d H:i:s', strtotime($_POST['date'])), empty($_POST['weight']) ? 0.00 : $_POST['weight'], $_POST['url_slug'], $_POST['status'], $product_config, $productcode ]); $id = $pdo->lastInsertId(); addProductImages($pdo, $id); addProductCategories($pdo, $id); addProductOptions($pdo, $id); addProductDownloads($pdo, $id); // Clear session cart if (isset($_SESSION['cart'])) { unset($_SESSION['cart']); } header('Location: index.php?page=products&success_msg=1'); exit; } } ?> =template_admin_header($page . ' Product', 'products', 'manage')?>
=template_admin_footer('initProduct()')?>