| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Repositories; |
| 6 |
|
| 7 |
use Yatra\Constants\ClassificationTypes; |
| 8 |
use Yatra\Database\Tables\ClassificationsTable; |
| 9 |
|
| 10 |
/** |
| 11 |
* Traveler Category Repository |
| 12 |
* Handles database operations for traveler categories using unified ClassificationsTable |
| 13 |
*/ |
| 14 |
class TravelerCategoryRepository extends BaseRepository |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Get table name |
| 18 |
*/ |
| 19 |
protected function getTableName(): string |
| 20 |
{ |
| 21 |
return ClassificationsTable::getTableName(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get all traveler categories with automatic type filtering |
| 26 |
*/ |
| 27 |
public function all(array $args = []): array |
| 28 |
{ |
| 29 |
// Always filter by type = 'traveler_type' for traveler categories |
| 30 |
if (!isset($args['where']['type'])) { |
| 31 |
$args['where']['type'] = ClassificationTypes::TRAVELER_TYPE; |
| 32 |
} |
| 33 |
|
| 34 |
return parent::all($args); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Count traveler categories with automatic type filtering |
| 39 |
*/ |
| 40 |
public function count(array $args = []): int |
| 41 |
{ |
| 42 |
// Always filter by type = 'traveler_type' for traveler categories |
| 43 |
if (!isset($args['where']['type'])) { |
| 44 |
$args['where']['type'] = ClassificationTypes::TRAVELER_TYPE; |
| 45 |
} |
| 46 |
|
| 47 |
return parent::count($args); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Find by slug |
| 52 |
*/ |
| 53 |
public function findBySlug(string $slug): ?\stdClass |
| 54 |
{ |
| 55 |
$table = esc_sql($this->table); |
| 56 |
$result = $this->wpdb->get_row( |
| 57 |
$this->wpdb->prepare( |
| 58 |
"SELECT * FROM `{$table}` WHERE type = %s AND slug = %s", |
| 59 |
ClassificationTypes::TRAVELER_TYPE, |
| 60 |
$slug |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
return $result ?: null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get published categories |
| 69 |
*/ |
| 70 |
public function getPublished(array $args = []): array |
| 71 |
{ |
| 72 |
$args['where']['type'] = ClassificationTypes::TRAVELER_TYPE; |
| 73 |
$args['where']['status'] = 'publish'; |
| 74 |
return $this->all($args); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get categories by status |
| 79 |
*/ |
| 80 |
public function getByStatus(string $status, array $args = []): array |
| 81 |
{ |
| 82 |
$args['where']['type'] = ClassificationTypes::TRAVELER_TYPE; |
| 83 |
$args['where']['status'] = $status; |
| 84 |
return $this->all($args); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Search categories |
| 89 |
*/ |
| 90 |
public function search(string $search, array $args = []): array |
| 91 |
{ |
| 92 |
$table = esc_sql($this->table); |
| 93 |
$where = $this->buildWhereClause($args); |
| 94 |
$order = $this->buildOrderClause($args); |
| 95 |
$limit = $this->buildLimitClause($args); |
| 96 |
|
| 97 |
$search_where = $this->wpdb->prepare( |
| 98 |
"WHERE (name LIKE %s OR slug LIKE %s OR description LIKE %s) AND type = %s", |
| 99 |
'%' . $this->wpdb->esc_like($search) . '%', |
| 100 |
'%' . $this->wpdb->esc_like($search) . '%', |
| 101 |
'%' . $this->wpdb->esc_like($search) . '%', |
| 102 |
ClassificationTypes::TRAVELER_TYPE |
| 103 |
); |
| 104 |
|
| 105 |
if ($where) { |
| 106 |
$search_where .= ' AND ' . str_replace('WHERE ', '', $where); |
| 107 |
} |
| 108 |
|
| 109 |
$query = "SELECT * FROM `{$table}` {$search_where} {$order} {$limit}"; |
| 110 |
|
| 111 |
return $this->wpdb->get_results($query) ?: []; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get traveler categories by IDs |
| 116 |
* |
| 117 |
* @param array $categoryIds Array of category IDs |
| 118 |
* @return array Array of category objects |
| 119 |
*/ |
| 120 |
public function getByIds(array $categoryIds): array |
| 121 |
{ |
| 122 |
$table = $this->getTableName(); |
| 123 |
|
| 124 |
if (empty($categoryIds)) { |
| 125 |
return []; |
| 126 |
} |
| 127 |
|
| 128 |
$placeholders = implode(',', array_fill(0, count($categoryIds), '%d')); |
| 129 |
|
| 130 |
return $this->wpdb->get_results($this->wpdb->prepare( |
| 131 |
"SELECT id, name, slug, JSON_EXTRACT(metadata, '$.age_min') as age_min, JSON_EXTRACT(metadata, '$.age_max') as age_max |
| 132 |
FROM {$table} |
| 133 |
WHERE id IN ({$placeholders}) AND type = %s", |
| 134 |
ClassificationTypes::TRAVELER_TYPE, ...$categoryIds |
| 135 |
)); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get metadata by category IDs |
| 140 |
* |
| 141 |
* @param array $categoryIds Array of category IDs |
| 142 |
* @return array Array of metadata indexed by category ID |
| 143 |
*/ |
| 144 |
public function getMetadataByIds(array $categoryIds): array |
| 145 |
{ |
| 146 |
$table = $this->getTableName(); |
| 147 |
|
| 148 |
$categoryIds = array_unique(array_filter($categoryIds)); |
| 149 |
if (empty($categoryIds)) { |
| 150 |
return []; |
| 151 |
} |
| 152 |
|
| 153 |
$placeholders = implode(',', array_fill(0, count($categoryIds), '%d')); |
| 154 |
$rows = $this->wpdb->get_results($this->wpdb->prepare( |
| 155 |
"SELECT id, metadata FROM `{$table}` WHERE type = %s AND id IN ({$placeholders})", |
| 156 |
ClassificationTypes::TRAVELER_TYPE, |
| 157 |
...$categoryIds |
| 158 |
)); |
| 159 |
|
| 160 |
$result = []; |
| 161 |
foreach ($rows as $row) { |
| 162 |
$meta = !empty($row->metadata) ? json_decode($row->metadata, true) : []; |
| 163 |
$result[(int) $row->id] = [ |
| 164 |
'pricing_mode' => $meta['pricing_mode'] ?? 'per_person', |
| 165 |
'min_pax' => $meta['min_pax'] ?? null, |
| 166 |
'max_pax' => $meta['max_pax'] ?? null, |
| 167 |
'group_overflow' => $meta['group_overflow'] ?? 'block', |
| 168 |
]; |
| 169 |
} |
| 170 |
|
| 171 |
return $result; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|