PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.4
Yatra – Travel Booking & Tour Operator Software v3.0.4
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.4, at app/Controllers/TravelerCategoryController.php

225 lines 7.0 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 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 $args = [
94 'limit' => $params['per_page'],
95 'offset' => ($params['page'] - 1) * $params['per_page'],
96 'order_by' => $params['orderby'],
97 'order' => $params['order'],
98 ];
99
100 if (!empty($params['search'])) {
101 $args['search'] = $params['search'];
102 }
103
104 $status = $request->get_param('status');
105 if ($status && $status !== 'all') {
106 $args['status'] = sanitize_text_field($status);
107 }
108
109 $items = $this->service->getAll($args);
110 $total = $this->service->count($args);
111
112 $prepared = array_map([$this, 'prepareItem'], $items);
113
114 return $this->paginated_response($prepared, $total, $params['page'], $params['per_page']);
115 } catch (\Exception $e) {
116 return $this->error_response($e->getMessage(), 500);
117 }
118 }
119
120 public function get_item(WP_REST_Request $request)
121 {
122 try {
123 $item = $this->service->getById($this->getId($request));
124
125 if (!$item) {
126 return $this->not_found(__('Traveler category not found', 'yatra'));
127 }
128
129 return $this->success_response($this->prepareItem($item));
130 } catch (\Exception $e) {
131 return $this->error_response($e->getMessage(), 500);
132 }
133 }
134
135 public function create_item(WP_REST_Request $request)
136 {
137 try {
138 $id = $this->service->create($this->getBody($request));
139
140 return $this->success_response([
141 'id' => $id,
142 'message' => __('Traveler category created successfully', 'yatra'),
143 ], 201);
144 } catch (\InvalidArgumentException $e) {
145 return $this->validation_error($e->getMessage());
146 } catch (\Exception $e) {
147 return $this->error_response($e->getMessage(), 500);
148 }
149 }
150
151 public function update_item(WP_REST_Request $request)
152 {
153 try {
154 $result = $this->service->update($this->getId($request), $this->getBody($request));
155
156 if (!$result) {
157 return $this->error_response(__('Failed to update traveler category', 'yatra'), 500);
158 }
159
160 return $this->success_response([
161 'message' => __('Traveler category updated successfully', 'yatra'),
162 ]);
163 } catch (\InvalidArgumentException $e) {
164 return $this->validation_error($e->getMessage());
165 } catch (\Exception $e) {
166 return $this->error_response($e->getMessage(), 500);
167 }
168 }
169
170 public function delete_item(WP_REST_Request $request)
171 {
172 try {
173 $result = $this->service->delete($this->getId($request));
174
175 if (!$result) {
176 return $this->error_response(__('Failed to delete traveler category', 'yatra'), 500);
177 }
178
179 return $this->success_response([
180 'message' => __('Traveler category deleted successfully', 'yatra'),
181 ]);
182 } catch (\Exception $e) {
183 return $this->error_response($e->getMessage(), 500);
184 }
185 }
186
187 private function prepareItem($item): array
188 {
189 $prepared = (array) $item;
190
191 // Handle field mapping for backward compatibility - convert name back to label
192 if (isset($prepared['name'])) {
193 $prepared['label'] = $prepared['name'];
194 }
195
196 // Parse metadata JSON and merge into main data for edit form
197 if (!empty($prepared['metadata'])) {
198 $metadata = json_decode($prepared['metadata'], true);
199 if (is_array($metadata)) {
200 // Merge metadata fields into main response for edit form
201 $prepared = array_merge($prepared, $metadata);
202 }
203 }
204
205 if (isset($prepared['icon']) && is_string($prepared['icon'])) {
206 $prepared['icon'] = maybe_unserialize($prepared['icon']);
207 }
208 if (isset($prepared['icon'])) {
209 $prepared['icon'] = $this->convert_icon_attachment_id_to_url($prepared['icon']);
210 }
211
212 if (!empty($prepared['created_by'])) {
213 $user = get_userdata((int) $prepared['created_by']);
214 $prepared['created_by_name'] = $user ? esc_html($user->display_name) : null;
215 }
216
217 if (!empty($prepared['updated_by'])) {
218 $user = get_userdata((int) $prepared['updated_by']);
219 $prepared['updated_by_name'] = $user ? esc_html($user->display_name) : null;
220 }
221
222 return $prepared;
223 }
224 }
225