File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Models/Report.php
Back
<?php /** * Report Model * Maps to `reports` table * Handles user-submitted reports on products, sellers, orders, and reviews */ require_once __DIR__ . '/../Core/Model.php'; class Report extends Model { protected string $table = 'reports'; public function getReports(array $filters = [], int $limit = 20, int $offset = 0): array { [$whereSql, $params] = $this->buildConditions($filters); $sql = "SELECT r.*, u.username as reporter_username, u.first_name as reporter_first_name, u.last_name as reporter_last_name, u.email as reporter_email, a.username as assigned_admin_username, a.first_name as assigned_admin_first_name FROM `reports` r LEFT JOIN `users` u ON r.reporter_id = u.id LEFT JOIN `users` a ON r.resolved_by = a.id WHERE {$whereSql} ORDER BY r.created_at DESC LIMIT :limit OFFSET :offset"; $stmt = $this->db->prepare($sql); foreach ($params as $k => $v) { $stmt->bindValue($k, $v); } $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetchAll(); } public function countReports(array $filters = []): int { [$whereSql, $params] = $this->buildConditions($filters); $sql = "SELECT COUNT(*) FROM `reports` r LEFT JOIN `users` u ON r.reporter_id = u.id WHERE {$whereSql}"; $stmt = $this->db->prepare($sql); foreach ($params as $k => $v) { $stmt->bindValue($k, $v); } $stmt->execute(); return (int)$stmt->fetchColumn(); } public function findReport(int $id): ?array { $sql = "SELECT r.*, u.username as reporter_username, u.first_name as reporter_first_name, u.last_name as reporter_last_name, u.email as reporter_email, a.username as assigned_admin_username, a.first_name as assigned_admin_first_name FROM `reports` r LEFT JOIN `users` u ON r.reporter_id = u.id LEFT JOIN `users` a ON r.resolved_by = a.id WHERE r.id = :id LIMIT 1"; return $this->fetchOne($sql, [':id' => $id]); } public function updateStatus(int $id, string $status, ?string $note, ?int $adminId): bool { $data = [ 'status' => $status, 'resolution_note' => $note, 'updated_at' => date('Y-m-d H:i:s'), ]; if ($adminId) { $data['resolved_by'] = $adminId; } $sets = implode(', ', array_map(fn($k) => "`{$k}` = :{$k}", array_keys($data))); $sql = "UPDATE `reports` SET {$sets} WHERE `id` = :id"; $params = array_merge($data, ['id' => $id]); $stmt = $this->query($sql, $params); return $stmt->rowCount() > 0; } public function submitReport(int $reporterUserId, string $reportType, int $targetId, string $reason, ?string $description = null, ?string $evidenceImages = null): int { $desc = trim($reason . (!empty($description) ? " - " . $description : "")); return $this->insert([ 'reporter_id' => $reporterUserId, 'report_type' => $reportType, 'target_id' => $targetId, 'description' => $desc, 'evidence_images' => $evidenceImages, 'status' => 'pending', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), ]); } public function getStatusCounts(): array { $sql = "SELECT status, COUNT(*) as cnt FROM `reports` GROUP BY status"; $rows = $this->fetchAll($sql); $result = ['pending' => 0, 'reviewing' => 0, 'resolved' => 0, 'rejected' => 0]; foreach ($rows as $row) { $result[$row['status']] = (int)$row['cnt']; } return $result; } private function buildConditions(array $filters): array { $conditions = ['1=1']; $params = []; if (!empty($filters['status']) && $filters['status'] !== 'all') { $conditions[] = 'r.status = :status'; $params[':status'] = $filters['status']; } if (!empty($filters['report_type']) && $filters['report_type'] !== 'all') { $conditions[] = 'r.report_type = :report_type'; $params[':report_type'] = $filters['report_type']; } if (!empty($filters['q'])) { $conditions[] = '(u.username LIKE :q_user OR r.description LIKE :q_desc)'; $params[':q_user'] = '%' . trim($filters['q']) . '%'; $params[':q_desc'] = '%' . trim($filters['q']) . '%'; } return [implode(' AND ', $conditions), $params]; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.22 |
proxy
|
phpinfo
|
Settings