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 / DifficultyLevelRepository.php

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

214 lines 6.0 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 use Yatra\Constants\ClassificationTypes;
8 use Yatra\Database\Tables\ClassificationsTable;
9
10 /**
11 * Difficulty Level Repository
12 * Handles database operations for difficulty levels using ClassificationsTable
13 */
14 class DifficultyLevelRepository extends BaseRepository
15 {
16 /**
17 * Rich text fields specific to difficulty levels
18 */
19 protected array $richTextFields = ['description'];
20
21 /**
22 * Integer fields specific to difficulty levels
23 */
24 protected array $integerFields = ['parent_id', 'level', 'sorting', 'is_featured', 'created_by', 'updated_by'];
25
26 /**
27 * Constructor
28 */
29 public function __construct()
30 {
31 parent::__construct();
32 }
33
34 /**
35 * Get table name
36 */
37 protected function getTableName(): string
38 {
39 return ClassificationsTable::getTableName();
40 }
41
42 /**
43 * Find by slug
44 */
45 public function findBySlug(string $slug): ?\stdClass
46 {
47 $table = esc_sql($this->table);
48 $result = $this->wpdb->get_row(
49 $this->wpdb->prepare(
50 "SELECT * FROM `{$table}` WHERE type = %s AND slug = %s",
51 ClassificationTypes::DIFFICULTY,
52 $slug
53 )
54 );
55 return $result ?: null;
56 }
57
58 /**
59 * Find by name (case-insensitive match)
60 */
61 public function findByName(string $name): ?\stdClass
62 {
63 $name = trim($name);
64 if ($name === '') {
65 return null;
66 }
67
68 $table = esc_sql($this->table);
69 $result = $this->wpdb->get_row(
70 $this->wpdb->prepare(
71 "SELECT * FROM `{$table}`
72 WHERE type = %s AND LOWER(name) = LOWER(%s)
73 LIMIT 1",
74 ClassificationTypes::DIFFICULTY,
75 $name
76 )
77 );
78
79 return $result ?: null;
80 }
81
82 /**
83 * Get published difficulty levels
84 */
85 public function getPublished(): array
86 {
87 $table = esc_sql($this->table);
88 return $this->wpdb->get_results(
89 $this->wpdb->prepare(
90 "SELECT * FROM `{$table}` WHERE type = %s AND status = 'publish' ORDER BY sorting ASC, id ASC",
91 ClassificationTypes::DIFFICULTY
92 )
93 );
94 }
95
96 /**
97 * Get all difficulty levels with type filtering
98 */
99 public function all(array $args = []): array
100 {
101 // Always filter by type = 'difficulty' for difficulty levels
102 $args['where']['type'] = ClassificationTypes::DIFFICULTY;
103
104 return parent::all($args);
105 }
106
107 /**
108 * Count difficulty levels with type filtering
109 */
110 public function count(array $args = []): int
111 {
112 // Always filter by type = 'difficulty' for difficulty levels
113 $args['where']['type'] = ClassificationTypes::DIFFICULTY;
114
115 return parent::count($args);
116 }
117
118 /**
119 * Get status counts for difficulty levels
120 */
121 public function getStatusCounts(array $args = []): array
122 {
123 $table = esc_sql($this->table);
124 $where = $this->buildWhereClause($args);
125
126 // Ensure we only count difficulty type records
127 $typeCondition = "type = %s";
128
129 $whereClause = "WHERE {$typeCondition}";
130 if (!empty($args['where'])) {
131 $additionalWhere = $this->buildWhereClause($args);
132 if ($additionalWhere && $additionalWhere !== ' WHERE') {
133 $additionalWhere = str_replace('WHERE ', 'AND ', $additionalWhere);
134 $whereClause .= ' ' . $additionalWhere;
135 }
136 }
137
138 $sql = "SELECT status, COUNT(*) as count
139 FROM `{$table}`
140 {$whereClause}
141 GROUP BY status";
142
143 $results = $this->wpdb->get_results($this->wpdb->prepare($sql, ClassificationTypes::DIFFICULTY)) ?: [];
144
145 $counts = [
146 'publish' => 0,
147 'draft' => 0,
148 'trash' => 0,
149 'total' => 0
150 ];
151
152 foreach ($results as $row) {
153 $status = $row->status;
154 $count = (int) $row->count;
155
156 // Only count new status values, no legacy mapping
157 if (isset($counts[$status])) {
158 $counts[$status] = $count;
159 }
160 $counts['total'] += $count;
161 }
162
163 return $counts;
164 }
165
166 /**
167 * Get trip count for a difficulty level
168 *
169 * @param int $levelId Difficulty level ID
170 * @return int Number of trips with this difficulty level
171 */
172 public function getTripCount(int $levelId): int
173 {
174 global $wpdb;
175 $tripRepository = new \Yatra\Repositories\TripRepository();
176 $tripsTable = $tripRepository->getTableName();
177
178 // Use TripClassificationsTable for trip-difficulty relationships
179 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
180
181 return (int) $wpdb->get_var($wpdb->prepare(
182 "SELECT COUNT(DISTINCT t.id)
183 FROM `{$tripsTable}` t
184 INNER JOIN `{$tripClassificationsTable}` tc ON tc.trip_id = t.id
185 WHERE tc.classification_id = %d
186 AND tc.classification_type = %s
187 AND t.status IN ('publish', 'published')",
188 $levelId,
189 ClassificationTypes::DIFFICULTY
190 ));
191 }
192
193 /**
194 * Get trip count for difficulty level (direct field method)
195 *
196 * @param int $levelId Difficulty level ID
197 * @return int Number of trips with this difficulty level
198 */
199 public function getTripCountDirect(int $levelId): int
200 {
201 global $wpdb;
202 $tripRepository = new \Yatra\Repositories\TripRepository();
203 $tripTable = $tripRepository->getTableName();
204
205 return (int) $wpdb->get_var($wpdb->prepare(
206 "SELECT COUNT(*)
207 FROM `{$tripTable}` t
208 WHERE t.difficulty_level = %d
209 AND t.status != 'trash'",
210 $levelId
211 ));
212 }
213 }
214