« Back to History
ps_shayari_assign.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php // File: /public_html/shayari/post_shayari_assign.php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/config.php'; $pdo = db_connect(); $errors = []; $success = null; /* ------------------- LOAD POSTS, CATEGORIES, DESIGNS ------------------- */ $posts = []; $cats = []; $designs = []; try { $st = $pdo->query("SELECT id, title FROM posts ORDER BY created_at DESC"); $posts = $st->fetchAll(); $st = $pdo->query("SELECT id, name FROM shayari_categories ORDER BY sort_order ASC, name ASC"); $cats = $st->fetchAll(); $st = $pdo->query("SELECT id, name, css_class FROM shayari_box_designs WHERE is_active = 1 ORDER BY sort_order ASC, name ASC"); $designs = $st->fetchAll(); } catch (Exception $e) { $errors[] = "Init load error: " . htmlspecialchars($e->getMessage()); } /* ------------------- READ FILTERS / POST SELECTION ------------------- */ $selected_post_id = isset($_REQUEST['post_id']) ? (int)$_REQUEST['post_id'] : 0; $filter_cat_id = isset($_REQUEST['category_id']) ? (int)$_REQUEST['category_id'] : 0; $filter_q = trim($_REQUEST['q'] ?? ''); /* ------------------- HANDLE SAVE MAPPINGS ------------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'save') { if ($selected_post_id <= 0) { $errors[] = "Pehle koi post select karo."; } // arrays from form $include = $_POST['include'] ?? []; // [shayari_id => 1] $positions = $_POST['position'] ?? []; // [shayari_id => pos] $design_ids = $_POST['design_id'] ?? []; // [shayari_id => design_id] if (empty($include)) { // allowed: empty mapping -> sab hata do } if (empty($errors)) { try { $pdo->beginTransaction(); // purane mapping delete $del = $pdo->prepare("DELETE FROM post_shayari_map WHERE post_id = :pid"); $del->execute([':pid' => $selected_post_id]); // naya insert $ins = $pdo->prepare(" INSERT INTO post_shayari_map (post_id, shayari_id, position, design_id, created_at, updated_at) VALUES (:post_id, :shayari_id, :position, :design_id, NOW(), NOW()) "); $order = 1; foreach ($include as $sid => $val) { $sid = (int)$sid; if ($sid <= 0) continue; $pos = isset($positions[$sid]) && (int)$positions[$sid] > 0 ? (int)$positions[$sid] : $order; $did = isset($design_ids[$sid]) ? (int)$design_ids[$sid] : null; if ($did === 0) $did = null; $ins->execute([ ':post_id' => $selected_post_id, ':shayari_id'=> $sid, ':position' => $pos, ':design_id' => $did, ]); $order++; } $pdo->commit(); $success = "Mappings saved for selected post."; } catch (Exception $e) { $pdo->rollBack(); $errors[] = "Save error: " . htmlspecialchars($e->getMessage()); } } } /* ------------------- CURRENT MAPPING (for selected post) ------------------- */ $current_map = []; // [shayari_id => ['position'=>..,'design_id'=>..]] if ($selected_post_id > 0) { $sql = "SELECT shayari_id, position, design_id FROM post_shayari_map WHERE post_id = :pid ORDER BY position ASC, shayari_id ASC"; $st = $pdo->prepare($sql); $st->execute([':pid' => $selected_post_id]); foreach ($st as $row) { $sid = (int)$row['shayari_id']; $current_map[$sid] = [ 'position' => (int)$row['position'], 'design_id' => $row['design_id'] !== null ? (int)$row['design_id'] : 0, ]; } } /* ------------------- LOAD SHAYARI LIST WITH FILTERS ------------------- */ $shayaris = []; if ($selected_post_id > 0) { $where = ["s.status = 'active'"]; $params = []; if ($filter_cat_id > 0) { $where[] = "s.category_id = :cid"; $params[':cid'] = $filter_cat_id; } if ($filter_q !== '') { $where[] = "s.text LIKE :q"; $params[':q'] = '%' . $filter_q . '%'; } $whereSql = $where ? ('WHERE ' . implode(' AND ', $where)) : ''; $sql = " SELECT s.id, s.text, sc.name AS category_name FROM shayari s LEFT JOIN shayari_categories sc ON sc.id = s.category_id $whereSql ORDER BY s.id DESC LIMIT 300 "; $st = $pdo->prepare($sql); $st->execute($params); $shayaris = $st->fetchAll(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Post–Shayari Assign</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body{margin:0;font-family:system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;background:#f5f5f5;} .wrap{max-width:1100px;margin:20px auto;background:#fff;border-radius:8px;padding:20px;box-shadow:0 4px 12px rgba(0,0,0,0.08);} h1{margin-top:0;font-size:22px;} .row{display:flex;flex-wrap:wrap;gap:12px;margin-bottom:16px;} .field{margin-bottom:10px;} label{font-weight:600;font-size:13px;} select,input[type="text"]{padding:6px 8px;font-size:14px;} .msg-error{background:#ffe2e2;border:1px solid #f5aaaa;padding:10px;margin-bottom:12px;border-radius:6px;} .msg-success{background:#e2ffe7;border:1px solid #aaf5bc;padding:10px;margin-bottom:12px;border-radius:6px;} table{width:100%;border-collapse:collapse;font-size:13px;margin-top:10px;} th,td{border:1px solid #ddd;padding:6px 8px;vertical-align:top;} th{background:#f0f0f0;font-weight:600;} textarea{width:100%;min-height:60px;font-size:13px;} .pos-input{width:50px;text-align:center;} .design-select{width:140px;} .btn{padding:8px 14px;border:none;border-radius:4px;cursor:pointer;font-weight:600;font-size:13px;} .btn-primary{background:#34A853;color:#fff;} .btn-small{padding:5px 9px;font-size:12px;} .topbar{display:flex;flex-wrap:wrap;gap:12px;margin-bottom:10px;align-items:flex-end;} .topbar .field{margin:0;} .help{font-size:12px;color:#666;margin-top:4px;} @media (max-width:700px){ table{display:block;overflow-x:auto;white-space:nowrap;} } </style> </head> <body> <div class="wrap"> <h1>Post ke liye Shayari Assign karo</h1> <?php if ($success): ?> <div class="msg-success"><?php echo htmlspecialchars($success); ?></div> <?php endif; ?> <?php if ($errors): ?> <div class="msg-error"> <ul style="margin:0;padding-left:18px;"> <?php foreach ($errors as $e): ?> <li><?php echo htmlspecialchars($e); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <!-- TOP: Post select + Filters --> <form method="get"> <div class="topbar"> <div class="field"> <label for="post_id">Post select karo</label><br> <select name="post_id" id="post_id" onchange="this.form.submit()"> <option value="0">-- Select Post --</option> <?php foreach ($posts as $p): ?> <option value="<?php echo (int)$p['id']; ?>" <?php echo $selected_post_id === (int)$p['id'] ? 'selected' : ''; ?>> <?php echo htmlspecialchars($p['title']); ?> </option> <?php endforeach; ?> </select> </div> <div class="field"> <label for="category_id">Category filter</label><br> <select name="category_id" id="category_id"> <option value="0">-- All --</option> <?php foreach ($cats as $c): ?> <option value="<?php echo (int)$c['id']; ?>" <?php echo $filter_cat_id === (int)$c['id'] ? 'selected' : ''; ?>> <?php echo htmlspecialchars($c['name']); ?> </option> <?php endforeach; ?> </select> </div> <div class="field"> <label for="q">Text search</label><br> <input type="text" name="q" id="q" value="<?php echo htmlspecialchars($filter_q); ?>" placeholder="shayari ke words se search"> </div> <div class="field"> <button type="submit" class="btn btn-small">Filter Apply</button> </div> </div> <div class="help"> Pehle post select karo, phir category/text filter se shayari list narrow karo. </div> </form> <?php if ($selected_post_id > 0): ?> <form method="post"> <input type="hidden" name="action" value="save"> <input type="hidden" name="post_id" value="<?php echo (int)$selected_post_id; ?>"> <input type="hidden" name="category_id" value="<?php echo (int)$filter_cat_id; ?>"> <input type="hidden" name="q" value="<?php echo htmlspecialchars($filter_q); ?>"> <table> <thead> <tr> <th>Use?</th> <th>ID</th> <th>Shayari</th> <th>Category</th> <th>Position</th> <th>Design</th> </tr> </thead> <tbody> <?php if ($shayaris): ?> <?php $autoPos = 1; foreach ($shayaris as $s): $sid = (int)$s['id']; $text = $s['text']; $catNm = $s['category_name'] ?? ''; $map = $current_map[$sid] ?? null; $checked = $map !== null; $posValue = $map['position'] ?? $autoPos; $designVal = $map['design_id'] ?? 0; if (!$checked) $autoPos++; ?> <tr> <td style="text-align:center;"> <input type="checkbox" name="include[<?php echo $sid; ?>]" value="1" <?php echo $checked ? 'checked' : ''; ?>> </td> <td><?php echo $sid; ?></td> <td> <textarea readonly><?php echo trim($text); ?></textarea> </td> <td><?php echo htmlspecialchars($catNm); ?></td> <td style="text-align:center;"> <input type="number" class="pos-input" name="position[<?php echo $sid; ?>]" value="<?php echo (int)$posValue; ?>"> </td> <td> <select class="design-select" name="design_id[<?php echo $sid; ?>]"> <option value="0">Default (plain)</option> <?php foreach ($designs as $d): ?> <option value="<?php echo (int)$d['id']; ?>" <?php echo $designVal === (int)$d['id'] ? 'selected' : ''; ?>> <?php echo htmlspecialchars($d['name']); ?> </option> <?php endforeach; ?> </select> </td> </tr> <?php endforeach; ?> <?php else: ?> <tr><td colspan="6">Is filter ke hisaab se koi shayari nahi mili.</td></tr> <?php endif; ?> </tbody> </table> <div style="margin-top:12px;"> <button type="submit" class="btn btn-primary">Save Mapping for this Post</button> </div> <div class="help"> ✔ Checkbox tick ka matlab: ye shayari is post me dikhani hai. ✔ Position = order (1,2,3...). ✔ Design se box ka look change hoga. </div> </form> <?php else: ?> <p style="margin-top:20px;">Start karne ke liye pehle koi post select karo.</p> <?php endif; ?> </div> </body> </html>