prepare("SELECT id FROM affiliates WHERE affiliate_code = ?"); $checkStmt->execute([$affiliate_code]); } while ($checkStmt->fetch()); // Insert affiliate $stmt = $pdo->prepare(" INSERT INTO affiliates (affiliate_code, type, company_name, incharge_name, state, postal_code, place_name, mobile, email, url, signup_reward, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $affiliate_code, $type, $company_name, $incharge_name, $state, $postal_code, $place_name, $mobile, $email, $url, $signup_reward, $_SESSION['admin_id'] ]); $affiliate_id = $pdo->lastInsertId(); // Handle file uploads if (!empty($_FILES['attachments']['name'][0])) { $upload_dir = 'uploads/affiliates/'; if (!file_exists($upload_dir)) { mkdir($upload_dir, 0755, true); } $allowed_types = ['application/pdf', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif']; $max_files = 10; for ($i = 0; $i < min(count($_FILES['attachments']['name']), $max_files); $i++) { if ($_FILES['attachments']['error'][$i] === UPLOAD_ERR_OK) { $file_type = $_FILES['attachments']['type'][$i]; if (in_array($file_type, $allowed_types)) { $file_name = $_FILES['attachments']['name'][$i]; $file_size = $_FILES['attachments']['size'][$i]; $file_tmp = $_FILES['attachments']['tmp_name'][$i]; // Generate unique filename $extension = pathinfo($file_name, PATHINFO_EXTENSION); $unique_name = $affiliate_code . '_' . time() . '_' . $i . '.' . $extension; $file_path = $upload_dir . $unique_name; if (move_uploaded_file($file_tmp, $file_path)) { $attachStmt = $pdo->prepare(" INSERT INTO affiliate_attachments (affiliate_id, file_name, file_path, file_type, file_size) VALUES (?, ?, ?, ?, ?) "); $attachStmt->execute([ $affiliate_id, $file_name, $file_path, $file_type, $file_size ]); } } } } } logActivity($_SESSION['admin_id'], 'create_affiliate', "Created affiliate: $affiliate_code", 'affiliate', $affiliate_id); $success = "Affiliate created successfully! Affiliate Code: $affiliate_code"; } catch (Exception $e) { $error = 'An error occurred. Please try again.'; error_log("Create affiliate error: " . $e->getMessage()); } } } // DELETE AFFILIATE if ($_POST['action'] === 'delete_affiliate') { $delete_id = intval($_POST['affiliate_id'] ?? 0); if ($delete_id) { try { $pdo = getDBConnection(); // Get affiliate code for logging $stmt = $pdo->prepare("SELECT affiliate_code FROM affiliates WHERE id = ?"); $stmt->execute([$delete_id]); $affiliate_code = $stmt->fetchColumn(); // Delete affiliate (cascade will delete attachments and signups) $stmt = $pdo->prepare("DELETE FROM affiliates WHERE id = ?"); $stmt->execute([$delete_id]); logActivity($_SESSION['admin_id'], 'delete_affiliate', "Deleted affiliate: $affiliate_code", 'affiliate', $delete_id); $success = 'Affiliate deleted successfully!'; } catch (Exception $e) { $error = 'An error occurred while deleting. Please try again.'; error_log("Delete affiliate error: " . $e->getMessage()); } } } } // Fetch all affiliates try { $pdo = getDBConnection(); $stmt = $pdo->query("SELECT * FROM affiliates ORDER BY created_at DESC"); $affiliates = $stmt->fetchAll(); } catch (Exception $e) { $affiliates = []; error_log("Fetch affiliates error: " . $e->getMessage()); } // Indian states list $indian_states = [ 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chhattisgarh', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu', 'Telangana', 'Tripura', 'Uttar Pradesh', 'Uttarakhand', 'West Bengal', 'Andaman and Nicobar Islands', 'Chandigarh', 'Dadra and Nagar Haveli and Daman and Diu', 'Delhi', 'Jammu and Kashmir', 'Ladakh', 'Lakshadweep', 'Puducherry' ]; include 'includes/header.php'; ?>
Manage affiliate partners and track panel member signups