PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Repositories / AdditionalServicesRepository.php

AdditionalServicesRepository.php in Yatra – Travel Booking & Tour Operator Software trunk, at app/Repositories/AdditionalServicesRepository.php

177 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Repositories;
6
7 /**
8 * Additional Services Repository
9 *
10 * Provides read access to the additional services table, if present.
11 * Table schema differs across versions / Pro, so we resolve table + columns at runtime.
12 */
13 class AdditionalServicesRepository extends BaseRepository
14 {
15 private ?string $resolvedTable = null;
16 private ?array $resolvedColumns = null;
17
18 protected function getTableName(): string
19 {
20 return $this->getResolvedTable();
21 }
22
23 private function getResolvedTable(): string
24 {
25 if ($this->resolvedTable !== null) {
26 return $this->resolvedTable;
27 }
28
29 // Candidate table names (avoid hardcoding wp_ prefix).
30 $candidates = [
31 $this->wpdb->prefix . 'yatra_additional_services',
32 ];
33
34 foreach ($candidates as $candidate) {
35 $pattern = $this->wpdb->esc_like($candidate);
36 $exists = $this->wpdb->get_var($this->wpdb->prepare('SHOW TABLES LIKE %s', $pattern));
37 if ($exists === $candidate) {
38 $this->resolvedTable = $candidate;
39 return $candidate;
40 }
41 }
42
43 // Not installed (e.g. Pro not active) — keep as prefix candidate for consistency,
44 // but callers should treat "no columns" as "no data".
45 $this->resolvedTable = $this->wpdb->prefix . 'yatra_additional_services';
46 return $this->resolvedTable;
47 }
48
49 /**
50 * Resolve columns for current schema.
51 *
52 * @return array{nameCol: string|null, priceCol: string|null, descCol: string|null, priceTypeCol: string|null, pricePerCol: string|null}
53 */
54 private function getResolvedColumns(): array
55 {
56 if ($this->resolvedColumns !== null) {
57 return $this->resolvedColumns;
58 }
59
60 $table = $this->getResolvedTable();
61 $columns = $this->wpdb->get_col("DESCRIBE {$table}", 0);
62 $columns = is_array($columns) ? $columns : [];
63
64 $has = static function (string $col) use ($columns): bool {
65 return in_array($col, $columns, true);
66 };
67
68 $nameCol = $has('name') ? 'name' : ($has('title') ? 'title' : null);
69 $priceCol = $has('price') ? 'price' : ($has('service_price') ? 'service_price' : null);
70 $descCol = $has('description') ? 'description' : null;
71 $priceTypeCol = $has('price_type') ? 'price_type' : null;
72 $pricePerCol = $has('price_per') ? 'price_per' : null;
73
74 $this->resolvedColumns = [
75 'nameCol' => $nameCol,
76 'priceCol' => $priceCol,
77 'descCol' => $descCol,
78 'priceTypeCol' => $priceTypeCol,
79 'pricePerCol' => $pricePerCol,
80 ];
81
82 return $this->resolvedColumns;
83 }
84
85 /**
86 * Fetch services by IDs (preserving input order).
87 *
88 * @param int[] $ids
89 * @return array<int, array{id:int,name:string,description:string,price:float,price_type?:string,price_per?:string}>
90 */
91 public function getByIds(array $ids): array
92 {
93 $ids = array_values(array_unique(array_map('intval', array_filter($ids))));
94 if ($ids === []) {
95 return [];
96 }
97
98 $table = $this->getResolvedTable();
99 $cols = $this->getResolvedColumns();
100 $nameCol = $cols['nameCol'];
101 $priceCol = $cols['priceCol'];
102 $descCol = $cols['descCol'];
103 $priceTypeCol = $cols['priceTypeCol'];
104 $pricePerCol = $cols['pricePerCol'];
105
106 if ($nameCol === null && $priceCol === null) {
107 return [];
108 }
109
110 $selectCols = ['id'];
111 if ($nameCol !== null) {
112 $selectCols[] = $nameCol;
113 }
114 if ($descCol !== null) {
115 $selectCols[] = $descCol;
116 }
117 if ($priceCol !== null) {
118 $selectCols[] = $priceCol;
119 }
120 if ($priceTypeCol !== null) {
121 $selectCols[] = $priceTypeCol;
122 }
123 if ($pricePerCol !== null) {
124 $selectCols[] = $pricePerCol;
125 }
126
127 $placeholders = implode(',', array_fill(0, count($ids), '%d'));
128 $rows = $this->wpdb->get_results($this->wpdb->prepare(
129 'SELECT ' . implode(', ', $selectCols) . " FROM {$table} WHERE id IN ({$placeholders})",
130 ...$ids
131 )) ?: [];
132
133 $index = [];
134 foreach ($rows as $row) {
135 if (!is_object($row) || empty($row->id)) {
136 continue;
137 }
138 $index[(int) $row->id] = $row;
139 }
140
141 $out = [];
142 foreach ($ids as $id) {
143 if (!isset($index[$id])) {
144 continue;
145 }
146 $row = $index[$id];
147
148 $label = '';
149 if ($nameCol !== null && !empty($row->{$nameCol})) {
150 $label = (string) $row->{$nameCol};
151 }
152
153 $price = 0.0;
154 if ($priceCol !== null && isset($row->{$priceCol})) {
155 $price = (float) $row->{$priceCol};
156 }
157
158 $desc = '';
159 if ($descCol !== null && isset($row->{$descCol})) {
160 $desc = (string) $row->{$descCol};
161 }
162
163 $out[] = [
164 'id' => (int) $row->id,
165 'name' => $label,
166 'description' => $desc,
167 'price' => $price,
168 'price_type' => $priceTypeCol !== null && isset($row->{$priceTypeCol}) ? (string) $row->{$priceTypeCol} : null,
169 'price_per' => $pricePerCol !== null && isset($row->{$pricePerCol}) ? (string) $row->{$pricePerCol} : null,
170 ];
171 }
172
173 return $out;
174 }
175 }
176
177