| 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 |
* Item Repository |
| 12 |
* Handles database operations for items (item subtypes) using unified ClassificationsTable |
| 13 |
*/ |
| 14 |
class ItemRepository extends BaseRepository |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Get table name |
| 18 |
*/ |
| 19 |
protected function getTableName(): string |
| 20 |
{ |
| 21 |
return ClassificationsTable::getTableName(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get status counts for admin list views with automatic type filtering |
| 26 |
*/ |
| 27 |
public function getStatusCounts(array $args = []): array |
| 28 |
{ |
| 29 |
$table = esc_sql($this->table); |
| 30 |
|
| 31 |
// Always filter by type = 'item' for items |
| 32 |
$type_condition = "WHERE type = %s"; |
| 33 |
|
| 34 |
// Add additional where conditions if provided |
| 35 |
if (isset($args['where']) && !empty($args['where'])) { |
| 36 |
$additional_where = $this->buildWhereClause($args); |
| 37 |
if ($additional_where && $additional_where !== ' WHERE') { |
| 38 |
// Remove the WHERE keyword and add with AND |
| 39 |
$additional_where = str_replace('WHERE ', 'AND ', $additional_where); |
| 40 |
$type_condition .= ' ' . $additional_where; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
$sql = "SELECT status, COUNT(*) as count |
| 45 |
FROM `{$table}` |
| 46 |
{$type_condition} |
| 47 |
GROUP BY status"; |
| 48 |
|
| 49 |
$results = $this->wpdb->get_results($this->wpdb->prepare($sql, ClassificationTypes::ITEM)) ?: []; |
| 50 |
|
| 51 |
$counts = [ |
| 52 |
'publish' => 0, |
| 53 |
'draft' => 0, |
| 54 |
'trash' => 0, |
| 55 |
'total' => 0 |
| 56 |
]; |
| 57 |
|
| 58 |
foreach ($results as $row) { |
| 59 |
$status = $row->status; |
| 60 |
$count = (int) $row->count; |
| 61 |
|
| 62 |
// Map old status values to new ones if needed |
| 63 |
if ($status === 'active') { |
| 64 |
$status = 'publish'; |
| 65 |
} elseif ($status === 'inactive') { |
| 66 |
$status = 'trash'; |
| 67 |
} |
| 68 |
|
| 69 |
if (isset($counts[$status])) { |
| 70 |
$counts[$status] = $count; |
| 71 |
} |
| 72 |
$counts['total'] += $count; |
| 73 |
} |
| 74 |
|
| 75 |
return $counts; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Count items with automatic type filtering |
| 80 |
*/ |
| 81 |
public function count(array $args = []): int |
| 82 |
{ |
| 83 |
// Always filter by type = 'item' for items |
| 84 |
if (!isset($args['where']['type'])) { |
| 85 |
$args['where']['type'] = ClassificationTypes::ITEM; |
| 86 |
} |
| 87 |
|
| 88 |
return parent::count($args); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get all items with automatic type filtering |
| 93 |
*/ |
| 94 |
public function all(array $args = []): array |
| 95 |
{ |
| 96 |
// Always filter by type = 'item' for items |
| 97 |
if (!isset($args['where']['type'])) { |
| 98 |
$args['where']['type'] = ClassificationTypes::ITEM; |
| 99 |
} |
| 100 |
|
| 101 |
return parent::all($args); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Find by slug |
| 106 |
*/ |
| 107 |
public function findBySlug(string $slug): ?\stdClass |
| 108 |
{ |
| 109 |
$table = esc_sql($this->table); |
| 110 |
$result = $this->wpdb->get_row( |
| 111 |
$this->wpdb->prepare( |
| 112 |
"SELECT * FROM `{$table}` WHERE type = %s AND slug = %s", |
| 113 |
ClassificationTypes::ITEM, |
| 114 |
$slug |
| 115 |
) |
| 116 |
); |
| 117 |
|
| 118 |
return $result ?: null; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get published items |
| 123 |
*/ |
| 124 |
public function getPublished(array $args = []): array |
| 125 |
{ |
| 126 |
$args['where']['type'] = ClassificationTypes::ITEM; |
| 127 |
$args['where']['status'] = 'publish'; |
| 128 |
return $this->all($args); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get items by status |
| 133 |
*/ |
| 134 |
public function getByStatus(string $status, array $args = []): array |
| 135 |
{ |
| 136 |
$args['where']['type'] = ClassificationTypes::ITEM; |
| 137 |
$args['where']['status'] = $status; |
| 138 |
return $this->all($args); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get items by type ID |
| 143 |
*/ |
| 144 |
public function getByTypeId(int $type_id, array $args = []): array |
| 145 |
{ |
| 146 |
$args['where']['type'] = ClassificationTypes::ITEM; |
| 147 |
$args['where']['parent_id'] = $type_id; |
| 148 |
return $this->all($args); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Search items |
| 153 |
*/ |
| 154 |
public function search(string $search, array $args = []): array |
| 155 |
{ |
| 156 |
$table = esc_sql($this->table); |
| 157 |
$where = $this->buildWhereClause($args); |
| 158 |
$order = $this->buildOrderClause($args); |
| 159 |
$limit = $this->buildLimitClause($args); |
| 160 |
|
| 161 |
$search_where = $this->wpdb->prepare( |
| 162 |
"WHERE type = %s AND (name LIKE %s OR slug LIKE %s OR description LIKE %s)", |
| 163 |
ClassificationTypes::ITEM, |
| 164 |
'%' . $this->wpdb->esc_like($search) . '%', |
| 165 |
'%' . $this->wpdb->esc_like($search) . '%', |
| 166 |
'%' . $this->wpdb->esc_like($search) . '%' |
| 167 |
); |
| 168 |
|
| 169 |
if ($where) { |
| 170 |
$search_where .= ' AND ' . str_replace('WHERE ', '', $where); |
| 171 |
} |
| 172 |
|
| 173 |
$query = "SELECT * FROM `{$table}` {$search_where} {$order} {$limit}"; |
| 174 |
|
| 175 |
return $this->wpdb->get_results($query) ?: []; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Count how many times an item is used in trip itineraries |
| 180 |
*/ |
| 181 |
public function countUsage(int $itemId): int |
| 182 |
{ |
| 183 |
global $wpdb; |
| 184 |
$itineraryEntryTable = \Yatra\Database\Tables\TripItineraryDayEntryTable::getTableName(); |
| 185 |
|
| 186 |
$count = $wpdb->get_var($wpdb->prepare( |
| 187 |
"SELECT COUNT(*) FROM `{$itineraryEntryTable}` WHERE item_id = %d", |
| 188 |
$itemId |
| 189 |
)); |
| 190 |
|
| 191 |
return (int) $count; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|