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 / Controllers / DifficultyLevelController.php

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

235 lines 7.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\Controllers;
6
7 use WP_REST_Request;
8 use WP_REST_Response;
9 use WP_Error;
10 use Yatra\Services\DifficultyLevelService;
11
12 /**
13 * Difficulty Level REST API Controller
14 *
15 * Endpoints:
16 * - GET /difficulty-levels - List difficulty levels
17 * - POST /difficulty-levels - Create difficulty level
18 * - GET /difficulty-levels/{id} - Get single difficulty level
19 * - PUT /difficulty-levels/{id} - Update difficulty level
20 * - DELETE /difficulty-levels/{id} - Delete difficulty level
21 */
22 class DifficultyLevelController extends BaseController
23 {
24 protected string $rest_base = 'difficulty-levels';
25
26 private DifficultyLevelService $service;
27
28 public function __construct()
29 {
30 $this->service = new DifficultyLevelService();
31 }
32
33 public function register_routes(): void
34 {
35 $this->registerCrudRoutes($this->getStatusArg());
36
37 // Status statistics for admin views (All / Published / Draft / Trash)
38 register_rest_route($this->namespace, '/' . $this->rest_base . '/stats', [
39 [
40 'methods' => \WP_REST_Server::READABLE,
41 'callback' => [$this, 'getStats'],
42 'permission_callback' => [$this, 'check_permission'],
43 ],
44 ]);
45 }
46
47 /**
48 * GET /difficulty-levels/stats
49 * Return stable status counts for admin list views.
50 */
51 public function getStats(WP_REST_Request $request)
52 {
53 try {
54 $stats = $this->service->getStatusCounts();
55 return $this->success_response($stats);
56 } catch (\Exception $e) {
57 return $this->error_response($e->getMessage(), 500);
58 }
59 }
60
61 public function check_permission(?WP_REST_Request $request = null): bool
62 {
63 if ($request === null) {
64 return true;
65 }
66
67 if (!is_user_logged_in()) {
68 return false;
69 }
70
71 if (current_user_can('manage_options')) {
72 return true;
73 }
74
75 switch ($request->get_method()) {
76 case 'GET':
77 return current_user_can('yatra_view_trips');
78 case 'POST':
79 case 'PUT':
80 case 'PATCH':
81 case 'DELETE':
82 return current_user_can('yatra_edit_trips');
83 default:
84 return current_user_can('manage_options');
85 }
86 }
87
88 public function get_items(WP_REST_Request $request)
89 {
90 try {
91 $params = $this->getPaginationParams($request);
92
93 // Override default orderby for difficulty levels
94 $orderby = $request->get_param('orderby') ?: 'sorting';
95 $order = strtoupper($request->get_param('order') ?: 'ASC');
96
97 $args = [
98 'limit' => $params['per_page'],
99 'offset' => ($params['page'] - 1) * $params['per_page'],
100 'order_by' => $orderby,
101 'order' => $order,
102 ];
103
104 if (!empty($params['search'])) {
105 $args['search'] = $params['search'];
106 }
107
108 $status = $request->get_param('status');
109 if ($status && $status !== 'all') {
110 $args['status'] = sanitize_text_field($status);
111 }
112
113 $items = $this->service->getAll($args);
114 $total = $this->service->count($args);
115
116 // Attach trip counts to items
117 if (!empty($items)) {
118 foreach ($items as $item) {
119 $levelId = isset($item->id) ? (int) $item->id : 0;
120 if ($levelId <= 0) {
121 continue;
122 }
123
124 $tripCount = $this->service->getTripCount($levelId);
125 $item->trip_count = $tripCount;
126 }
127 }
128
129 $prepared = array_map([$this, 'prepareItem'], $items);
130
131 return $this->paginated_response($prepared, $total, $params['page'], $params['per_page']);
132 } catch (\Exception $e) {
133 return $this->error_response($e->getMessage(), 500);
134 }
135 }
136
137 public function get_item(WP_REST_Request $request)
138 {
139 try {
140 $item = $this->service->getById($this->getId($request));
141
142 if (!$item) {
143 return $this->not_found(__('Difficulty level not found', 'yatra'));
144 }
145
146 // Attach trip count for single level
147 $levelId = isset($item->id) ? (int) $item->id : 0;
148 if ($levelId > 0) {
149 $tripCount = $this->service->getTripCountDirect($levelId);
150 $item->trip_count = $tripCount;
151 }
152
153 return $this->success_response($this->prepareItem($item));
154 } catch (\Exception $e) {
155 return $this->error_response($e->getMessage(), 500);
156 }
157 }
158
159 public function create_item(WP_REST_Request $request)
160 {
161 try {
162 $id = $this->service->create($this->getBody($request));
163
164 return $this->success_response([
165 'id' => $id,
166 'message' => __('Difficulty level created successfully', 'yatra'),
167 ], 201);
168 } catch (\InvalidArgumentException $e) {
169 return $this->validation_error($e->getMessage());
170 } catch (\Exception $e) {
171 return $this->error_response($e->getMessage(), 500);
172 }
173 }
174
175 public function update_item(WP_REST_Request $request)
176 {
177 try {
178 $result = $this->service->update($this->getId($request), $this->getBody($request));
179
180 if (!$result) {
181 return $this->error_response(__('Failed to update difficulty level', 'yatra'), 500);
182 }
183
184 return $this->success_response([
185 'message' => __('Difficulty level updated successfully', 'yatra'),
186 ]);
187 } catch (\InvalidArgumentException $e) {
188 return $this->validation_error($e->getMessage());
189 } catch (\Exception $e) {
190 return $this->error_response($e->getMessage(), 500);
191 }
192 }
193
194 public function delete_item(WP_REST_Request $request)
195 {
196 try {
197 $result = $this->service->delete($this->getId($request));
198
199 if (!$result) {
200 return $this->error_response(__('Failed to delete difficulty level', 'yatra'), 500);
201 }
202
203 return $this->success_response([
204 'message' => __('Difficulty level deleted successfully', 'yatra'),
205 ]);
206 } catch (\Exception $e) {
207 return $this->error_response($e->getMessage(), 500);
208 }
209 }
210
211 private function prepareItem($item): array
212 {
213 $prepared = (array) $item;
214
215 if (isset($prepared['icon']) && is_string($prepared['icon'])) {
216 $prepared['icon'] = maybe_unserialize($prepared['icon']);
217 }
218 if (isset($prepared['icon'])) {
219 $prepared['icon'] = $this->convert_icon_attachment_id_to_url($prepared['icon']);
220 }
221
222 if (!empty($prepared['created_by'])) {
223 $user = get_userdata((int) $prepared['created_by']);
224 $prepared['created_by_name'] = $user ? esc_html($user->display_name) : null;
225 }
226
227 if (!empty($prepared['updated_by'])) {
228 $user = get_userdata((int) $prepared['updated_by']);
229 $prepared['updated_by_name'] = $user ? esc_html($user->display_name) : null;
230 }
231
232 return $prepared;
233 }
234 }
235