PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.7
Yatra – Travel Booking & Tour Operator Software v3.0.2.7
3.0.15 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 All 83 releases
yatra / app / Services / DifficultyLevelService.php

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

328 lines 10.3 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'] = maybe_serialize($data['icon']);
138 } elseif (is_string($data['icon'])) {
139 // If it's already a string, sanitize it
140 $data['icon'] = sanitize_text_field($data['icon']);
141 }
142 }
143
144 // Sanitize metadata if it's an array
145 if (isset($data['metadata'])) {
146 if (is_array($data['metadata'])) {
147 $data['metadata'] = maybe_serialize($data['metadata']);
148 }
149 }
150
151 return $data;
152 }
153
154 /**
155 * Process before update
156 */
157 protected function processBeforeUpdate(int $id, array $data): array
158 {
159 // Ensure the type remains 'difficulty' for the ClassificationsTable
160 $data['type'] = 'difficulty';
161
162 // Sanitize name
163 if (isset($data['name'])) {
164 $data['name'] = sanitize_text_field($data['name']);
165 }
166
167 // Handle slug in EDIT mode: Only update if explicitly provided
168 // Do NOT auto-generate from name in edit mode
169 if (isset($data['slug']) && !empty($data['slug'])) {
170 // Slug was explicitly provided - ensure uniqueness
171 $data['slug'] = SlugHelper::generateUniqueFromDatabase(
172 $data['slug'],
173 ClassificationsTable::getTableName(),
174 'slug',
175 $id // Exclude current record when checking uniqueness
176 );
177 }
178 // If slug is not provided, don't modify it (keep existing slug)
179
180 // Sanitize Quill HTML description
181 if (isset($data['description'])) {
182 $data['description'] = FormatHelper::sanitizeQuillHtml($data['description']);
183 }
184
185 // Sanitize sorting
186 if (isset($data['sorting'])) {
187 $data['sorting'] = absint($data['sorting']);
188 }
189
190 // Sanitize is_featured
191 if (isset($data['is_featured'])) {
192 $data['is_featured'] = $data['is_featured'] ? 1 : 0;
193 }
194
195 // Sanitize status
196 if (isset($data['status'])) {
197 // Validate status
198 $allowed_statuses = ['draft', 'publish', 'trash'];
199 $data['status'] = in_array($data['status'], $allowed_statuses, true)
200 ? $data['status']
201 : 'draft';
202 }
203
204 // Set updated_by to current user
205 $data['updated_by'] = absint(get_current_user_id());
206
207 // Sanitize and serialize icon if it's an array
208 if (isset($data['icon'])) {
209 if (is_array($data['icon'])) {
210 // Convert URL back to attachment ID if possible
211 if ($data['icon']['type'] === 'image' && !empty($data['icon']['value'])) {
212 $value = $data['icon']['value'];
213
214 // If it's a URL, try to find the attachment ID
215 if (filter_var($value, FILTER_VALIDATE_URL)) {
216 $attachment_id = attachment_url_to_postid($value);
217 if ($attachment_id) {
218 $data['icon']['value'] = $attachment_id;
219 }
220 }
221 // If it's already numeric, keep it as is
222 elseif (is_numeric($value)) {
223 $data['icon']['value'] = (int) $value;
224 }
225 }
226
227 $data['icon'] = maybe_serialize($data['icon']);
228 } elseif (is_string($data['icon'])) {
229 // If it's already a string, sanitize it
230 $data['icon'] = sanitize_text_field($data['icon']);
231 }
232 }
233
234 // Sanitize metadata if it's an array
235 if (isset($data['metadata'])) {
236 if (is_array($data['metadata'])) {
237 $data['metadata'] = maybe_serialize($data['metadata']);
238 }
239 }
240
241 return $data;
242 }
243
244 /**
245 * Get published difficulty levels
246 */
247 public function getPublished(): array
248 {
249 return $this->repository->getPublished();
250 }
251
252 /**
253 * Get status counts for difficulty levels
254 */
255 public function getStatusCounts(array $args = []): array
256 {
257 return $this->repository->getStatusCounts($args);
258 }
259
260 /**
261 * Get all items with search and filters
262 */
263 public function getAll(array $args = []): array
264 {
265 // Sanitize and handle search
266 if (!empty($args['search'])) {
267 $search = sanitize_text_field($args['search']);
268 return $this->repository->search($search, $args);
269 }
270
271 // Sanitize and handle status filter
272 if (!empty($args['status']) && $args['status'] !== 'all') {
273 $allowed_statuses = ['draft', 'publish', 'trash'];
274 $status = in_array($args['status'], $allowed_statuses, true)
275 ? $args['status']
276 : null;
277 if ($status) {
278 $args['where']['status'] = $status;
279 }
280 }
281
282 return $this->repository->all($args);
283 }
284
285 /**
286 * Get ordered difficulty levels (by sorting)
287 */
288 public function getOrdered(array $args = []): array
289 {
290 $args['order_by'] = 'sorting';
291 $args['order'] = 'ASC';
292 return $this->getAll($args);
293 }
294
295 /**
296 * Count items
297 */
298 public function count(array $args = []): int
299 {
300 // Sanitize and handle search
301 if (!empty($args['search'])) {
302 $search = sanitize_text_field($args['search']);
303 return $this->repository->search($search, $args);
304 }
305
306 return $this->repository->count($args);
307 }
308
309 /**
310 * Get trip count for a difficulty level
311 */
312 public function getTripCount(int $levelId): int
313 {
314 $difficultyLevelRepository = new \Yatra\Repositories\DifficultyLevelRepository();
315 return $difficultyLevelRepository->getTripCount($levelId);
316 }
317
318 /**
319 * Get trip count for difficulty level (direct field method)
320 */
321 public function getTripCountDirect(int $levelId): int
322 {
323 $difficultyLevelRepository = new \Yatra\Repositories\DifficultyLevelRepository();
324 return $difficultyLevelRepository->getTripCountDirect($levelId);
325 }
326 }
327
328