PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.9
Yatra – Travel Booking & Tour Operator Software v3.0.9
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 / TravelerCategoryController.php

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

229 lines 7.2 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\TravelerCategoryService;
11
12 /**
13 * Traveler Category REST API Controller
14 *
15 * Endpoints:
16 * - GET /traveler-categories - List traveler categories
17 * - POST /traveler-categories - Create traveler category
18 * - GET /traveler-categories/{id} - Get single traveler category
19 * - PUT /traveler-categories/{id} - Update traveler category
20 * - DELETE /traveler-categories/{id} - Delete traveler category
21 */
22 class TravelerCategoryController extends BaseController
23 {
24 protected string $rest_base = 'traveler-categories';
25
26 private TravelerCategoryService $service;
27
28 public function __construct()
29 {
30 $this->service = new TravelerCategoryService();
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 /traveler-categories/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 // Unknown HTTP method — deny explicitly. The earlier
85 // fallback to `manage_options` was a regression that
86 // silently broadened admin-only access for any verb not
87 // in the explicit switch.
88 return false;
89 }
90 }
91
92 public function get_items(WP_REST_Request $request)
93 {
94 try {
95 $params = $this->getPaginationParams($request);
96
97 $args = [
98 'limit' => $params['per_page'],
99 'offset' => ($params['page'] - 1) * $params['per_page'],
100 'order_by' => $params['orderby'],
101 'order' => $params['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 $prepared = array_map([$this, 'prepareItem'], $items);
117
118 return $this->paginated_response($prepared, $total, $params['page'], $params['per_page']);
119 } catch (\Exception $e) {
120 return $this->error_response($e->getMessage(), 500);
121 }
122 }
123
124 public function get_item(WP_REST_Request $request)
125 {
126 try {
127 $item = $this->service->getById($this->getId($request));
128
129 if (!$item) {
130 return $this->not_found(__('Traveler category not found', 'yatra'));
131 }
132
133 return $this->success_response($this->prepareItem($item));
134 } catch (\Exception $e) {
135 return $this->error_response($e->getMessage(), 500);
136 }
137 }
138
139 public function create_item(WP_REST_Request $request)
140 {
141 try {
142 $id = $this->service->create($this->getBody($request));
143
144 return $this->success_response([
145 'id' => $id,
146 'message' => __('Traveler category created successfully', 'yatra'),
147 ], 201);
148 } catch (\InvalidArgumentException $e) {
149 return $this->validation_error($e->getMessage());
150 } catch (\Exception $e) {
151 return $this->error_response($e->getMessage(), 500);
152 }
153 }
154
155 public function update_item(WP_REST_Request $request)
156 {
157 try {
158 $result = $this->service->update($this->getId($request), $this->getBody($request));
159
160 if (!$result) {
161 return $this->error_response(__('Failed to update traveler category', 'yatra'), 500);
162 }
163
164 return $this->success_response([
165 'message' => __('Traveler category updated successfully', 'yatra'),
166 ]);
167 } catch (\InvalidArgumentException $e) {
168 return $this->validation_error($e->getMessage());
169 } catch (\Exception $e) {
170 return $this->error_response($e->getMessage(), 500);
171 }
172 }
173
174 public function delete_item(WP_REST_Request $request)
175 {
176 try {
177 $result = $this->service->delete($this->getId($request));
178
179 if (!$result) {
180 return $this->error_response(__('Failed to delete traveler category', 'yatra'), 500);
181 }
182
183 return $this->success_response([
184 'message' => __('Traveler category deleted successfully', 'yatra'),
185 ]);
186 } catch (\Exception $e) {
187 return $this->error_response($e->getMessage(), 500);
188 }
189 }
190
191 private function prepareItem($item): array
192 {
193 $prepared = (array) $item;
194
195 // Handle field mapping for backward compatibility - convert name back to label
196 if (isset($prepared['name'])) {
197 $prepared['label'] = $prepared['name'];
198 }
199
200 // Parse metadata JSON and merge into main data for edit form
201 if (!empty($prepared['metadata'])) {
202 $metadata = json_decode($prepared['metadata'], true);
203 if (is_array($metadata)) {
204 // Merge metadata fields into main response for edit form
205 $prepared = array_merge($prepared, $metadata);
206 }
207 }
208
209 if (isset($prepared['icon']) && is_string($prepared['icon'])) {
210 $prepared['icon'] = maybe_unserialize($prepared['icon']);
211 }
212 if (isset($prepared['icon'])) {
213 $prepared['icon'] = $this->convert_icon_attachment_id_to_url($prepared['icon']);
214 }
215
216 if (!empty($prepared['created_by'])) {
217 $user = get_userdata((int) $prepared['created_by']);
218 $prepared['created_by_name'] = $user ? esc_html($user->display_name) : null;
219 }
220
221 if (!empty($prepared['updated_by'])) {
222 $user = get_userdata((int) $prepared['updated_by']);
223 $prepared['updated_by_name'] = $user ? esc_html($user->display_name) : null;
224 }
225
226 return $prepared;
227 }
228 }
229