PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
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 / CategoryController.php

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

225 lines 6.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\CategoryService;
11 use Yatra\Database\Tables\TripsTable;
12 use Yatra\Database\Tables\TripClassificationsTable;
13
14 /**
15 * Category REST API Controller
16 *
17 * Endpoints:
18 * - GET /categories - List categories
19 * - POST /categories - Create category
20 * - GET /categories/{id} - Get single category
21 * - PUT /categories/{id} - Update category
22 * - DELETE /categories/{id} - Delete category
23 */
24 class CategoryController extends BaseController
25 {
26 /**
27 * @var CategoryService
28 */
29 private CategoryService $service;
30
31 /**
32 * Constructor
33 */
34 public function __construct()
35 {
36 $this->service = new CategoryService();
37 }
38
39 /**
40 * List categories
41 */
42 public function list_items(WP_REST_Request $request)
43 {
44
45 try {
46 $params = $this->getPaginationParams($request);
47
48 // Handle status filter
49 $status = $request->get_param('status');
50 if ($status && $status !== 'all') {
51 $args['status'] = sanitize_text_field($status);
52 }
53
54 $items = $this->service->getAll($args);
55 $total = $this->service->count($args);
56
57 // Attach trip counts to items
58 if (!empty($items)) {
59 foreach ($items as $item) {
60 $categoryId = isset($item->id) ? (int) $item->id : 0;
61 if ($categoryId <= 0) {
62 continue;
63 }
64
65 $tripCount = $this->service->getTripCount($categoryId);
66 $item->trip_count = $tripCount;
67 }
68 }
69
70
71 $prepared = array_map([$this, 'prepareItem'], $items);
72
73
74 return $this->paginated_response($prepared, $total, $params['page'], $params['per_page']);
75 } catch (\Exception $e) {
76 return $this->error_response($e->getMessage(), 500);
77 }
78 }
79
80 /**
81 * Get single category
82 */
83 public function get_item(WP_REST_Request $request)
84 {
85 try {
86 $item = $this->service->getById($this->getId($request));
87
88 if (!$item) {
89 return $this->not_found(__('Category not found', 'yatra'));
90 }
91
92 // Attach trip count for single category
93 $categoryId = isset($item->id) ? (int) $item->id : 0;
94 if ($categoryId > 0) {
95 $tripCount = $this->service->getTripCount($categoryId);
96 $item->trip_count = $tripCount;
97 }
98
99 return $this->success_response($this->prepareItem($item));
100 } catch (\Exception $e) {
101 return $this->error_response($e->getMessage(), 500);
102 }
103 }
104
105 /**
106 * Create category
107 */
108 public function create_item(WP_REST_Request $request)
109 {
110 try {
111 $id = $this->service->create($this->getBody($request));
112
113 return $this->success_response([
114 'id' => $id,
115 'message' => __('Category created successfully', 'yatra'),
116 ]);
117 } catch (\Exception $e) {
118 return $this->error_response($e->getMessage(), 500);
119 }
120 }
121
122 /**
123 * Update category
124 */
125 public function update_item(WP_REST_Request $request)
126 {
127 try {
128 $id = $this->getId($request);
129 $data = $this->getBody($request);
130
131
132 $result = $this->service->update($id, $data);
133
134 if (!$result) {
135 return $this->not_found(__('Category not found', 'yatra'));
136 }
137
138 return $this->success_response([
139 'id' => $id,
140 'message' => __('Category updated successfully', 'yatra'),
141 ]);
142 } catch (\Exception $e) {
143 return $this->error_response($e->getMessage(), 500);
144 }
145 }
146
147 /**
148 * Delete category
149 */
150 public function delete_item(WP_REST_Request $request)
151 {
152 try {
153 $id = $this->getId($request);
154 $result = $this->service->delete($id);
155
156 if (!$result) {
157 return $this->not_found(__('Category not found', 'yatra'));
158 }
159
160 return $this->success_response([
161 'message' => __('Category deleted successfully', 'yatra'),
162 ]);
163 } catch (\Exception $e) {
164 return $this->error_response($e->getMessage(), 500);
165 }
166 }
167
168 /**
169 * Get status counts
170 */
171 public function get_stats(WP_REST_Request $request)
172 {
173 try {
174 $counts = $this->service->getStatusCounts();
175 return $this->success_response($counts);
176 } catch (\Exception $e) {
177 return $this->error_response($e->getMessage(), 500);
178 }
179 }
180
181 /**
182 * Prepare item for response
183 */
184 protected function prepareItem($item): array
185 {
186
187 $data = [
188 'id' => (int) $item->id,
189 'type' => $item->type,
190 'name' => $item->name,
191 'slug' => $item->slug,
192 'description' => $item->description,
193 'parent_id' => $item->parent_id,
194 'level' => $item->level,
195 'icon' => $item->icon,
196 'color' => $item->color,
197 'image_id' => $item->image_id,
198 'metadata' => isset($item->metadata) && is_string($item->metadata) ? maybe_unserialize($item->metadata) : $item->metadata,
199 'sorting' => $item->sorting,
200 'is_featured' => !empty($item->is_featured) ? 1 : 0,
201 'status' => $item->status,
202 'created_at' => $item->created_at,
203 'updated_at' => $item->updated_at,
204 'created_by' => $item->created_by,
205 'updated_by' => $item->updated_by,
206 ];
207
208
209 // Add trip count if available
210 if (isset($item->trip_count)) {
211 $data['trip_count'] = (int) $item->trip_count;
212 }
213
214 // Add user names if available
215 if (isset($item->created_by_name)) {
216 $data['created_by_name'] = $item->created_by_name;
217 }
218 if (isset($item->updated_by_name)) {
219 $data['updated_by_name'] = $item->updated_by_name;
220 }
221
222 return $data;
223 }
224 }
225