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 / ItemTypeController.php

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

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