/* Native professional filter helpers. Kept separate from legacy functions. */ function onehd_filter_build_where($types, $years, $countries, $genres, $rating, $keyword) { $clauses = array('1=1'); $bindTypes = ''; $params = array(); $types = array_values(array_unique(array_filter(array_map('intval', (array) $types), function ($value) { return in_array($value, array(1, 2), true); }))); if (!$types) $types = array(1, 2); $clauses[] = 'type IN (' . implode(',', array_fill(0, count($types), '?')) . ')'; foreach ($types as $value) { $bindTypes .= 'i'; $params[] = $value; } $years = array_values(array_unique(array_filter(array_map('intval', (array) $years), function ($value) { return $value >= 1900 && $value <= ((int) date('Y') + 2); }))); if ($years) { $clauses[] = 'year IN (' . implode(',', array_fill(0, count($years), '?')) . ')'; foreach ($years as $value) { $bindTypes .= 'i'; $params[] = $value; } } foreach (array('country' => (array) $countries, 'genre' => (array) $genres) as $column => $values) { $values = array_values(array_unique(array_filter(array_map('trim', $values), function ($value) { return $value !== '' && mb_strlen($value, 'UTF-8') <= 80; }))); if ($values) { $or = array(); foreach ($values as $value) { $or[] = "FIND_IN_SET(?, REPLACE(`$column`, ', ', ',')) > 0"; $bindTypes .= 's'; $params[] = $value; } $clauses[] = '(' . implode(' OR ', $or) . ')'; } } $rating = max(0, min(10, (float) $rating)); if ($rating > 0) { $clauses[] = 'COALESCE(NULLIF(imdbratingcount, \'\'), 0) >= ?'; $bindTypes .= 'd'; $params[] = $rating; } $keyword = trim((string) $keyword); if ($keyword !== '') { $keyword = mb_substr($keyword, 0, 100, 'UTF-8'); $clauses[] = 'name LIKE ?'; $bindTypes .= 's'; $params[] = '%' . $keyword . '%'; } return array(implode(' AND ', $clauses), $bindTypes, $params); } function onehd_filter_bind($stmt, $types, &$params) { if ($types === '' || !$params) return; $args = array($types); foreach ($params as $index => $value) { $args[] = &$params[$index]; } call_user_func_array(array($stmt, 'bind_param'), $args); } function onehd_count_filter_data($types, $years, $countries, $genres, $rating, $keyword, $conn) { list($where, $bindTypes, $params) = onehd_filter_build_where($types, $years, $countries, $genres, $rating, $keyword); $stmt = $conn->prepare('SELECT COUNT(*) FROM movies WHERE ' . $where); if (!$stmt) return 0; onehd_filter_bind($stmt, $bindTypes, $params); $stmt->execute(); $stmt->bind_result($count); $stmt->fetch(); $stmt->close(); return (int) $count; } function onehd_get_filter_data($types, $years, $countries, $genres, $rating, $keyword, $sort, $order, $conn, $offset, $limit) { list($where, $bindTypes, $params) = onehd_filter_build_where($types, $years, $countries, $genres, $rating, $keyword); $sortMap = array( 'latest' => 'date_posted', 'rating' => 'imdbratingcount', 'year' => 'year', 'title' => 'name' ); $sortColumn = isset($sortMap[$sort]) ? $sortMap[$sort] : 'date_posted'; $direction = strtolower((string) $order) === 'asc' ? 'ASC' : 'DESC'; $offset = max(0, (int) $offset); $limit = max(1, min(100, (int) $limit)); $sql = "SELECT * FROM movies WHERE $where ORDER BY `$sortColumn` $direction, id DESC LIMIT ?, ?"; $stmt = $conn->prepare($sql); if (!$stmt) return array(); $bindTypes .= 'ii'; $params[] = $offset; $params[] = $limit; onehd_filter_bind($stmt, $bindTypes, $params); $stmt->execute(); $result = $stmt->get_result(); $rows = array(); while ($row = $result->fetch_assoc()) $rows[] = $row; $stmt->close(); return $rows; }