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 / Services / DifficultyLevelService.php

DifficultyLevelService.php in Yatra – Travel Booking & Tour Operator Software 3.0.3, at app/Services/DifficultyLevelService.php

330 lines 10.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\Services;
6
7 use Yatra\Repositories\DifficultyLevelRepository;
8 use Yatra\Helpers\SlugHelper;
9 use Yatra\Helpers\FormatHelper;
10 use Yatra\Database\Tables\ClassificationsTable;
11
12 /**
13 * Difficulty Level Service
14 * Contains business logic for difficulty levels using ClassificationsTable
15 */
16 class DifficultyLevelService extends BaseService
17 {
18 /**
19 * @var DifficultyLevelRepository
20 */
21 private DifficultyLevelRepository $repository;
22
23 /**
24 * Constructor
25 */
26 public function __construct()
27 {
28 $this->repository = new DifficultyLevelRepository();
29 }
30
31 /**
32 * Get repository
33 */
34 protected function getRepository(): DifficultyLevelRepository
35 {
36 return $this->repository;
37 }
38
39 /**
40 * Validate difficulty level data
41 */
42 protected function validate(array $data, ?int $id = null): void
43 {
44 if (empty($data['name'])) {
45 throw new \InvalidArgumentException('Difficulty level name is required');
46 }
47
48 // Validate sorting if provided
49 if (isset($data['sorting'])) {
50 $sorting = (int) $data['sorting'];
51 if ($sorting < 0) {
52 throw new \InvalidArgumentException('Sorting must be a non-negative integer');
53 }
54 }
55 }
56
57 /**
58 * Process before create
59 */
60 protected function processBeforeCreate(array $data): array
61 {
62 // Set the type to 'difficulty' for the ClassificationsTable
63 $data['type'] = 'difficulty';
64
65 // Sanitize name
66 if (isset($data['name'])) {
67 $data['name'] = sanitize_text_field($data['name']);
68 }
69
70 // Always auto-generate slug from name (backend ensures uniqueness)
71 if (!empty($data['name'])) {
72 $data['slug'] = SlugHelper::generateUniqueFromDatabase(
73 $data['name'],
74 ClassificationsTable::getTableName(),
75 'slug'
76 );
77 } elseif (isset($data['slug'])) {
78 // If name is empty but slug is provided, sanitize it
79 $data['slug'] = SlugHelper::generate($data['slug']);
80 }
81
82 // Sanitize description (rich text)
83 if (isset($data['description'])) {
84 $data['description'] = FormatHelper::sanitizeQuillHtml($data['description']);
85 }
86
87 // Sanitize sorting
88 if (isset($data['sorting'])) {
89 $data['sorting'] = absint($data['sorting']);
90 } else {
91 $data['sorting'] = 0; // Default sorting
92 }
93
94 // Sanitize is_featured
95 if (isset($data['is_featured'])) {
96 $data['is_featured'] = $data['is_featured'] ? 1 : 0;
97 } else {
98 $data['is_featured'] = 0; // Default not featured
99 }
100
101 // Sanitize status
102 if (isset($data['status'])) {
103 // Validate status
104 $allowed_statuses = ['draft', 'publish', 'trash'];
105 $data['status'] = in_array($data['status'], $allowed_statuses, true)
106 ? $data['status']
107 : 'draft';
108 } else {
109 $data['status'] = 'draft';
110 }
111
112 // Set created_by and updated_by to current user
113 $current_user_id = get_current_user_id();
114 $data['created_by'] = absint($current_user_id);
115 $data['updated_by'] = absint($current_user_id);
116
117 // Sanitize and serialize icon if it's an array
118 if (isset($data['icon'])) {
119 if (is_array($data['icon'])) {
120 // Convert URL back to attachment ID if possible
121 if ($data['icon']['type'] === 'image' && !empty($data['icon']['value'])) {
122 $value = $data['icon']['value'];
123
124 // If it's a URL, try to find the attachment ID
125 if (filter_var($value, FILTER_VALIDATE_URL)) {
126 $attachment_id = attachment_url_to_postid($value);
127 if ($attachment_id) {
128 $data['icon']['value'] = $attachment_id;
129 }
130 }
131 // If it's already numeric, keep it as is
132 elseif (is_numeric($value)) {
133 $data['icon']['value'] = (int) $value;
134 }
135 }
136
137 $data['icon'] = yatra_normalize_icon_picker_for_storage($data['icon']);
138 $data['icon'] = maybe_serialize($data['icon']);
139 } elseif (is_string($data['icon'])) {
140 // If it's already a string, sanitize it
141 $data['icon'] = sanitize_text_field($data['icon']);
142 }
143 }
144
145 // Sanitize metadata if it's an array
146 if (isset($data['metadata'])) {
147 if (is_array($data['metadata'])) {
148 $data['metadata'] = maybe_serialize($data['metadata']);
149 }
150 }
151
152 return $data;
153 }
154
155 /**
156 * Process before update
157 */
158 protected function processBeforeUpdate(int $id, array $data): array
159 {
160 // Ensure the type remains 'difficulty' for the ClassificationsTable
161 $data['type'] = 'difficulty';
162
163 // Sanitize name
164 if (isset($data['name'])) {
165 $data['name'] = sanitize_text_field($data['name']);
166 }
167
168 // Handle slug in EDIT mode: Only update if explicitly provided
169 // Do NOT auto-generate from name in edit mode
170 if (isset($data['slug']) && !empty($data['slug'])) {
171 // Slug was explicitly provided - ensure uniqueness
172 $data['slug'] = SlugHelper::generateUniqueFromDatabase(
173 $data['slug'],
174 ClassificationsTable::getTableName(),
175 'slug',
176 $id // Exclude current record when checking uniqueness
177 );
178 }
179 // If slug is not provided, don't modify it (keep existing slug)
180
181 // Sanitize Quill HTML description
182 if (isset($data['description'])) {
183 $data['description'] = FormatHelper::sanitizeQuillHtml($data['description']);
184 }
185
186 // Sanitize sorting
187 if (isset($data['sorting'])) {
188 $data['sorting'] = absint($data['sorting']);
189 }
190
191 // Sanitize is_featured
192 if (isset($data['is_featured'])) {
193 $data['is_featured'] = $data['is_featured'] ? 1 : 0;
194 }
195
196 // Sanitize status
197 if (isset($data['status'])) {
198 // Validate status
199 $allowed_statuses = ['draft', 'publish', 'trash'];
200 $data['status'] = in_array($data['status'], $allowed_statuses, true)
201 ? $data['status']
202 : 'draft';
203 }
204
205 // Set updated_by to current user
206 $data['updated_by'] = absint(get_current_user_id());
207
208 // Sanitize and serialize icon if it's an array
209 if (isset($data['icon'])) {
210 if (is_array($data['icon'])) {
211 // Convert URL back to attachment ID if possible
212 if ($data['icon']['type'] === 'image' && !empty($data['icon']['value'])) {
213 $value = $data['icon']['value'];
214
215 // If it's a URL, try to find the attachment ID
216 if (filter_var($value, FILTER_VALIDATE_URL)) {
217 $attachment_id = attachment_url_to_postid($value);
218 if ($attachment_id) {
219 $data['icon']['value'] = $attachment_id;
220 }
221 }
222 // If it's already numeric, keep it as is
223 elseif (is_numeric($value)) {
224 $data['icon']['value'] = (int) $value;
225 }
226 }
227
228 $data['icon'] = yatra_normalize_icon_picker_for_storage($data['icon']);
229 $data['icon'] = maybe_serialize($data['icon']);
230 } elseif (is_string($data['icon'])) {
231 // If it's already a string, sanitize it
232 $data['icon'] = sanitize_text_field($data['icon']);
233 }
234 }
235
236 // Sanitize metadata if it's an array
237 if (isset($data['metadata'])) {
238 if (is_array($data['metadata'])) {
239 $data['metadata'] = maybe_serialize($data['metadata']);
240 }
241 }
242
243 return $data;
244 }
245
246 /**
247 * Get published difficulty levels
248 */
249 public function getPublished(): array
250 {
251 return $this->repository->getPublished();
252 }
253
254 /**
255 * Get status counts for difficulty levels
256 */
257 public function getStatusCounts(array $args = []): array
258 {
259 return $this->repository->getStatusCounts($args);
260 }
261
262 /**
263 * Get all items with search and filters
264 */
265 public function getAll(array $args = []): array
266 {
267 // Sanitize and handle search
268 if (!empty($args['search'])) {
269 $search = sanitize_text_field($args['search']);
270 return $this->repository->search($search, $args);
271 }
272
273 // Sanitize and handle status filter
274 if (!empty($args['status']) && $args['status'] !== 'all') {
275 $allowed_statuses = ['draft', 'publish', 'trash'];
276 $status = in_array($args['status'], $allowed_statuses, true)
277 ? $args['status']
278 : null;
279 if ($status) {
280 $args['where']['status'] = $status;
281 }
282 }
283
284 return $this->repository->all($args);
285 }
286
287 /**
288 * Get ordered difficulty levels (by sorting)
289 */
290 public function getOrdered(array $args = []): array
291 {
292 $args['order_by'] = 'sorting';
293 $args['order'] = 'ASC';
294 return $this->getAll($args);
295 }
296
297 /**
298 * Count items
299 */
300 public function count(array $args = []): int
301 {
302 // Sanitize and handle search
303 if (!empty($args['search'])) {
304 $search = sanitize_text_field($args['search']);
305 return $this->repository->search($search, $args);
306 }
307
308 return $this->repository->count($args);
309 }
310
311 /**
312 * Get trip count for a difficulty level
313 */
314 public function getTripCount(int $levelId): int
315 {
316 $difficultyLevelRepository = new \Yatra\Repositories\DifficultyLevelRepository();
317 return $difficultyLevelRepository->getTripCount($levelId);
318 }
319
320 /**
321 * Get trip count for difficulty level (direct field method)
322 */
323 public function getTripCountDirect(int $levelId): int
324 {
325 $difficultyLevelRepository = new \Yatra\Repositories\DifficultyLevelRepository();
326 return $difficultyLevelRepository->getTripCountDirect($levelId);
327 }
328 }
329
330