PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.3
Yatra – Travel Booking & Tour Operator Software v3.0.3
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 3.0.3, at app/Repositories/DifficultyLevelRepository.php

190 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 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(ClassificationsTable::getTableName());
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 * Get published difficulty levels
60 */
61 public function getPublished(): array
62 {
63 $table = esc_sql($this->table);
64 return $this->wpdb->get_results(
65 $this->wpdb->prepare(
66 "SELECT * FROM `{$table}` WHERE type = %s AND status = 'publish' ORDER BY sorting ASC, id ASC",
67 ClassificationTypes::DIFFICULTY
68 )
69 );
70 }
71
72 /**
73 * Get all difficulty levels with type filtering
74 */
75 public function all(array $args = []): array
76 {
77 // Always filter by type = 'difficulty' for difficulty levels
78 $args['where']['type'] = ClassificationTypes::DIFFICULTY;
79
80 return parent::all($args);
81 }
82
83 /**
84 * Count difficulty levels with type filtering
85 */
86 public function count(array $args = []): int
87 {
88 // Always filter by type = 'difficulty' for difficulty levels
89 $args['where']['type'] = ClassificationTypes::DIFFICULTY;
90
91 return parent::count($args);
92 }
93
94 /**
95 * Get status counts for difficulty levels
96 */
97 public function getStatusCounts(array $args = []): array
98 {
99 $table = esc_sql($this->table);
100 $where = $this->buildWhereClause($args);
101
102 // Ensure we only count difficulty type records
103 $typeCondition = "type = %s";
104
105 $whereClause = "WHERE {$typeCondition}";
106 if (!empty($args['where'])) {
107 $additionalWhere = $this->buildWhereClause($args);
108 if ($additionalWhere && $additionalWhere !== ' WHERE') {
109 $additionalWhere = str_replace('WHERE ', 'AND ', $additionalWhere);
110 $whereClause .= ' ' . $additionalWhere;
111 }
112 }
113
114 $sql = "SELECT status, COUNT(*) as count
115 FROM `{$table}`
116 {$whereClause}
117 GROUP BY status";
118
119 $results = $this->wpdb->get_results($this->wpdb->prepare($sql, ClassificationTypes::DIFFICULTY)) ?: [];
120
121 $counts = [
122 'publish' => 0,
123 'draft' => 0,
124 'trash' => 0,
125 'total' => 0
126 ];
127
128 foreach ($results as $row) {
129 $status = $row->status;
130 $count = (int) $row->count;
131
132 // Only count new status values, no legacy mapping
133 if (isset($counts[$status])) {
134 $counts[$status] = $count;
135 }
136 $counts['total'] += $count;
137 }
138
139 return $counts;
140 }
141
142 /**
143 * Get trip count for a difficulty level
144 *
145 * @param int $levelId Difficulty level ID
146 * @return int Number of trips with this difficulty level
147 */
148 public function getTripCount(int $levelId): int
149 {
150 global $wpdb;
151 $tripRepository = new \Yatra\Repositories\TripRepository();
152 $tripsTable = $tripRepository->getTableName();
153
154 // Use TripClassificationsTable for trip-difficulty relationships
155 $tripClassificationsTable = \Yatra\Database\Tables\TripClassificationsTable::getTableName();
156
157 return (int) $wpdb->get_var($wpdb->prepare(
158 "SELECT COUNT(DISTINCT t.id)
159 FROM `{$tripsTable}` t
160 INNER JOIN `{$tripClassificationsTable}` tc ON tc.trip_id = t.id
161 WHERE tc.classification_id = %d
162 AND tc.classification_type = %s
163 AND t.status IN ('publish', 'published')",
164 $levelId,
165 ClassificationTypes::DIFFICULTY
166 ));
167 }
168
169 /**
170 * Get trip count for difficulty level (direct field method)
171 *
172 * @param int $levelId Difficulty level ID
173 * @return int Number of trips with this difficulty level
174 */
175 public function getTripCountDirect(int $levelId): int
176 {
177 global $wpdb;
178 $tripRepository = new \Yatra\Repositories\TripRepository();
179 $tripTable = $tripRepository->getTableName();
180
181 return (int) $wpdb->get_var($wpdb->prepare(
182 "SELECT COUNT(*)
183 FROM `{$tripTable}` t
184 WHERE t.difficulty_level = %d
185 AND t.status != 'trash'",
186 $levelId
187 ));
188 }
189 }
190