PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.9
Yatra – Travel Booking & Tour Operator Software v3.0.2.9
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 / Controllers / DestinationController.php

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

290 lines 9.1 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\DestinationService;
11 use Yatra\Database\Tables\TripsTable;
12 use Yatra\Database\Tables\TripClassificationsTable;
13
14 /**
15 * Destination REST API Controller
16 *
17 * Endpoints:
18 * - GET /destinations - List destinations
19 * - POST /destinations - Create destination
20 * - GET /destinations/{id} - Get single destination
21 * - PUT /destinations/{id} - Update destination
22 * - DELETE /destinations/{id} - Delete destination
23 */
24 class DestinationController extends BaseController
25 {
26 protected string $rest_base = 'destinations';
27
28 private DestinationService $service;
29
30 public function __construct()
31 {
32 $this->service = new DestinationService();
33 }
34
35 public function register_routes(): void
36 {
37 $this->registerCrudRoutes($this->getStatusArg());
38
39 // Bulk operations
40 register_rest_route($this->namespace, '/' . $this->rest_base . '/bulk', [
41 'methods' => \WP_REST_Server::CREATABLE,
42 'callback' => [$this, 'bulkAction'],
43 'permission_callback' => [$this, 'check_permission'],
44 ]);
45
46 // Stats endpoint
47 register_rest_route($this->namespace, '/' . $this->rest_base . '/stats', [
48 'methods' => \WP_REST_Server::READABLE,
49 'callback' => [$this, 'getStats'],
50 'permission_callback' => [$this, 'check_permission'],
51 ]);
52 }
53
54 public function check_permission(?WP_REST_Request $request = null): bool
55 {
56 if ($request === null) {
57 return true;
58 }
59
60 if (!is_user_logged_in()) {
61 return false;
62 }
63
64 if (current_user_can('manage_options')) {
65 return true;
66 }
67
68 switch ($request->get_method()) {
69 case 'GET':
70 return current_user_can('yatra_view_trips');
71 case 'POST':
72 case 'PUT':
73 case 'PATCH':
74 case 'DELETE':
75 return current_user_can('yatra_edit_trips');
76 default:
77 return current_user_can('manage_options');
78 }
79 }
80
81 public function get_items(WP_REST_Request $request)
82 {
83 try {
84 $params = $this->getPaginationParams($request);
85
86 $args = [
87 'limit' => $params['per_page'],
88 'offset' => ($params['page'] - 1) * $params['per_page'],
89 'order_by' => $params['orderby'],
90 'order' => $params['order'],
91 ];
92
93 if (!empty($params['search'])) {
94 $args['search'] = $params['search'];
95 }
96
97 $status = $request->get_param('status');
98 if ($status && $status !== 'all') {
99 $args['status'] = sanitize_text_field($status);
100 }
101
102 $items = $this->service->getAll($args);
103 $total = $this->service->count($args);
104
105 // Attach trip counts to items
106 if (!empty($items)) {
107 foreach ($items as $item) {
108 $destinationId = isset($item->id) ? (int) $item->id : 0;
109 if ($destinationId <= 0) {
110 continue;
111 }
112
113 $tripCount = $this->service->getTripCount($destinationId);
114 $item->trip_count = $tripCount;
115 }
116 }
117
118 $prepared = array_map([$this, 'prepareItem'], $items);
119
120 return $this->paginated_response($prepared, $total, $params['page'], $params['per_page']);
121 } catch (\Exception $e) {
122 return $this->error_response($e->getMessage(), 500);
123 }
124 }
125
126 public function get_item(WP_REST_Request $request)
127 {
128 try {
129 $item = $this->service->getById($this->getId($request));
130
131 if (!$item) {
132 return $this->not_found(__('Destination not found', 'yatra'));
133 }
134
135 // Attach trip count for single destination
136 $destinationId = isset($item->id) ? (int) $item->id : 0;
137 if ($destinationId > 0) {
138 $tripCount = $this->service->getTripCount($destinationId);
139 $item->trip_count = $tripCount;
140 }
141
142 return $this->success_response($this->prepareItem($item));
143 } catch (\Exception $e) {
144 return $this->error_response($e->getMessage(), 500);
145 }
146 }
147
148 public function create_item(WP_REST_Request $request)
149 {
150 try {
151 $id = $this->service->create($this->getBody($request));
152
153 return $this->success_response([
154 'id' => $id,
155 'message' => __('Destination created successfully', 'yatra'),
156 ], 201);
157 } catch (\InvalidArgumentException $e) {
158 return $this->validation_error($e->getMessage());
159 } catch (\Exception $e) {
160 return $this->error_response($e->getMessage(), 500);
161 }
162 }
163
164 public function update_item(WP_REST_Request $request)
165 {
166 try {
167 $body = $this->getBody($request);
168
169 $result = $this->service->update($this->getId($request), $body);
170
171 if (!$result) {
172 return $this->error_response(__('Failed to update destination', 'yatra'), 500);
173 }
174
175 return $this->success_response([
176 'message' => __('Destination updated successfully', 'yatra'),
177 ]);
178 } catch (\InvalidArgumentException $e) {
179 return $this->validation_error($e->getMessage());
180 } catch (\Exception $e) {
181 return $this->error_response($e->getMessage(), 500);
182 }
183 }
184
185 public function delete_item(WP_REST_Request $request)
186 {
187 try {
188 $result = $this->service->delete($this->getId($request));
189
190 if (!$result) {
191 return $this->error_response(__('Failed to delete destination', 'yatra'), 500);
192 }
193
194 return $this->success_response([
195 'message' => __('Destination deleted successfully', 'yatra'),
196 ]);
197 } catch (\Exception $e) {
198 return $this->error_response($e->getMessage(), 500);
199 }
200 }
201
202 private function prepareItem($item): array
203 {
204 $prepared = (array) $item;
205
206
207 if (isset($prepared['icon']) && is_string($prepared['icon'])) {
208 $prepared['icon'] = maybe_unserialize($prepared['icon']);
209 }
210 if (isset($prepared['icon'])) {
211 $prepared['icon'] = $this->convert_icon_attachment_id_to_url($prepared['icon']);
212 }
213
214 // Handle metadata
215 if (isset($prepared['metadata']) && is_string($prepared['metadata'])) {
216 $prepared['metadata'] = maybe_unserialize($prepared['metadata']);
217 }
218
219 if (!empty($prepared['created_by'])) {
220 $user = get_userdata((int) $prepared['created_by']);
221 $prepared['created_by_name'] = $user ? esc_html($user->display_name) : null;
222 }
223
224 if (!empty($prepared['updated_by'])) {
225 $user = get_userdata((int) $prepared['updated_by']);
226 $prepared['updated_by_name'] = $user ? esc_html($user->display_name) : null;
227 }
228
229 return $prepared;
230 }
231
232 /**
233 * Handle bulk operations
234 */
235 public function bulkAction(WP_REST_Request $request)
236 {
237 try {
238 $action = sanitize_text_field($request->get_param('action'));
239 $ids = $request->get_param('ids');
240
241 if (empty($action)) {
242 return $this->validation_error(__('Action is required', 'yatra'));
243 }
244
245 if (empty($ids)) {
246 return $this->validation_error(__('No destinations selected', 'yatra'));
247 }
248
249 switch ($action) {
250 case 'trash':
251 $result = $this->service->bulkUpdateStatus($ids, 'trash');
252 break;
253 case 'publish':
254 $result = $this->service->bulkUpdateStatus($ids, 'publish');
255 break;
256 case 'draft':
257 $result = $this->service->bulkUpdateStatus($ids, 'draft');
258 break;
259 case 'restore':
260 $result = $this->service->bulkUpdateStatus($ids, 'publish');
261 break;
262 case 'delete':
263 $result = $this->service->bulkDelete($ids);
264 break;
265 default:
266 throw new \InvalidArgumentException(__('Invalid action', 'yatra'));
267 }
268
269 return $this->success_response($result);
270 } catch (\InvalidArgumentException $e) {
271 return $this->validation_error($e->getMessage());
272 } catch (\Exception $e) {
273 return $this->error_response($e->getMessage(), 500);
274 }
275 }
276
277 /**
278 * Get statistics for admin views
279 */
280 public function getStats(WP_REST_Request $request)
281 {
282 try {
283 $stats = $this->service->getStatusCounts();
284 return $this->success_response($stats);
285 } catch (\Exception $e) {
286 return $this->error_response($e->getMessage(), 500);
287 }
288 }
289 }
290