PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.5
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.5
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / api / Resource / FrontendResource / CustomerResource.php

CustomerResource.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.5, at api/Resource/FrontendResource/CustomerResource.php

291 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api\Resource\FrontendResource;
4
5 use FluentCart\Api\Resource\BaseResourceApi;
6 use FluentCart\Api\Resource\OrderResource;
7 use FluentCart\App\Helpers\Helper;
8 use FluentCart\App\Models\Customer;
9 use FluentCart\Framework\Database\Orm\Builder;
10 use FluentCart\Framework\Support\Arr;
11
12 class CustomerResource extends BaseResourceApi
13 {
14
15
16 public static function getQuery(): Builder
17 {
18 return Customer::query();
19 }
20
21 /**
22 * Get customers based on specified parameters.
23 *
24 * @param array $params Array containing the necessary parameters.
25 * [
26 * "params" => (array) Required.
27 * [
28 * 'search' => (string) Optional. Search Customer.
29 * [
30 * "column name(e.g., first_name|last_name|email|id)" => [
31 * column => "column name(e.g., first_name|last_name|email|id)",
32 * operator => "operator (e.g., like_all|rlike|or_rlike|or_like_all)",
33 * value => "value" ]
34 * ],
35 * 'filters' => (string) Optional. Filters customer.
36 * [
37 * "column name(e.g., first_name|last_name|email)" => [
38 * column => "column name(e.g., first_name|last_name|email)",
39 * operator => "operator (e.g., between|or_between|like_all|in)",
40 * value => "value" ]
41 * ],
42 * 'order_by' => (string) Optional. Column to order by,
43 * 'order_type' => (string) Optional. Order type for sorting (ASC or DESC),
44 * 'per_page' => (int) Optional. Number of items for per page,
45 * 'page' => (int) Optional. Page number for pagination
46 * ]
47 * ]
48 *
49 */
50 public static function get(array $params = [])
51 {
52 return static::getQuery()->when(Arr::get($params["params"], 'search'), function ($query) use ($params) {
53 return $query->search(Arr::get($params["params"], 'search', ''));
54 })
55 ->applyCustomFilters(Arr::get($params["params"], 'filters', []))
56 ->orderBy(
57 sanitize_sql_orderby(Arr::get($params["params"], 'order_by', 'id')),
58 sanitize_sql_orderby(Arr::get($params["params"], 'order_type', 'DESC')))
59 ->paginate(Arr::get($params["params"], 'per_page', 15), ['*'], 'page', Arr::get($params["params"], 'page'));
60 }
61
62 /**
63 * Find customer by ID.
64 *
65 * @param int $id Required. The ID of the customer.
66 * @param array $params Optional. Additional parameters for finding a customer.
67 * [
68 * 'with' => (array) Optional.Relationship name to be eagerly loaded,
69 * ]
70 *
71 */
72 public static function find($id, $params = [])
73 {
74 $with = Arr::get($params, 'with', []);
75 $customer = static::getQuery()->with($with)->findOrFail($id);
76 return [
77 'customer' => $customer
78 ];
79 }
80
81 /**
82 * Create a new customer with the given data
83 *
84 * @param array $data Required. Array containing the necessary parameters
85 * [
86 * 'first_name' => (string) Required. The first name of the customer,
87 * 'last_name' => (string) Optional. The last name of the customer,
88 * 'email' => (string) Required. The email of the customer,
89 * 'city' => (string) Optional. The city of the customer,
90 * 'state' => (string) Optional. The state of the customer,
91 * 'postcode' => (string) Optional. The postal code of the customer,
92 * 'country' => (string) Optional. The country of the customer,
93 * 'wp_user' => (string) Optional. Create customer as WP user,
94 * ]
95 * @param array $params Optional. Additional parameters for creating a customer.
96 *
97 */
98 public static function create($data, $params = [])
99 {
100 $email = Arr::get($data, 'email');
101
102 $ownerId = (int) Arr::get($data, 'user_id');
103 if (!$ownerId || $ownerId !== get_current_user_id()) {
104 unset($data['user_id']);
105 $ownerId = 0;
106 }
107 $customer = $ownerId ? static::getQuery()->where('user_id', $ownerId)->orderBy('id')->first() : null;
108 if (!$customer) {
109 // Matching an existing email never changes its owner or profile.
110 $customer = static::getQuery()->firstOrCreate(['email' => $email], $data);
111 }
112
113 if ($customer) {
114 return static::makeSuccessResponse(
115 $customer,
116 __('Customer created successfully!', 'fluent-cart')
117 );
118 }
119
120 return static::makeErrorResponse([
121 ['code' => 400, 'message' => __('Customer creation failed.', 'fluent-cart')]
122 ]);
123 }
124
125 /**
126 * Update customer with the given data
127 *
128 * @param array $data Required. Array containing the necessary parameters
129 * [
130 * 'first_name' => (string) Required. The first name of the customer,
131 * 'last_name' => (string) Optional. The last name of the customer,
132 * 'email' => (string) Required. The email of the customer,
133 * 'city' => (string) Optional. The city of the customer,
134 * 'state' => (string) Optional. The state of the customer,
135 * 'postcode' => (string) Optional. The postal code of the customer,
136 * 'country' => (string) Optional. The country of the customer,
137 * ]
138 * @param int $id Required. The ID of the customer.
139 * @param array $params Optional. Additional parameters for creating a customer.
140 *
141 */
142 public static function update($data, $id, $params = [])
143 {
144 $customer = static::getQuery()->findOrFail($id)->update($data);
145
146 if ($customer) {
147 return static::makeSuccessResponse(
148 $customer,
149 __('Customer updated successfully!', 'fluent-cart')
150 );
151 }
152
153 return static::makeErrorResponse([
154 ['code' => 400, 'message' => __('Customer update failed.', 'fluent-cart')]
155 ]);
156 }
157
158 /**
159 * Delete a customer based on the given ID and parameters.
160 *
161 * @param int $id Optional. The ID of the customer.
162 * @param array $params Optional. Additional parameters for deleting multiple customers.
163 * [
164 * 'ids' => (array) Required. The array of customer IDs to be deleted.
165 * ]
166 *
167 */
168 public static function delete($id, $params = [])
169 {
170 $ids = Arr::get($params, 'ids');
171
172 $customers = static::getQuery()->with(['orders'])->whereIn('id', $ids)->get();
173
174 foreach ($customers as $customer) {
175 $customer->orders()->delete();
176 $customer->delete();
177 }
178
179 if ($customer) {
180 return static::makeSuccessResponse(
181 '',
182 __('Selected Customers has been deleted permanently', 'fluent-cart')
183 );
184 }
185
186 return static::makeErrorResponse([
187 ['code' => 400, 'message' => __('Customer update failed.', 'fluent-cart')]
188 ]);
189 }
190
191 /**
192 * Get orders for a specific customer.
193 *
194 * @param array $data Required. Array containing the necessary parameters for order retrieval.
195 * [
196 * 'per_page' => (int) Optional. Number of items for per page,
197 * ]
198 * @param int $customerId Required. The ID of the customer for whom orders are being retrieved.
199 *
200 */
201 public static function getOrders(array $data, $customerId): array
202 {
203 return OrderResource::search(['customer_id' => ['column' => 'customer_id', 'value' => $customerId]], function (\FluentCart\Framework\Database\Orm\Builder $query) use ($data) {
204 return $query->orderBy('id', 'DESC')
205 ->paginate(Arr::get($data, 'per_page', 15));
206 });
207 }
208
209 /**
210 * Update the status of multiple customers with the given parameters.
211 *
212 * @param array $params Optional. Array containing the necessary parameters
213 * [
214 * 'new_status' => (string) Required. The new status to be set for the customers.
215 * 'customer_ids' => (array) Required. Customer IDs whose status will be updated.
216 * ]
217 *
218 */
219 public static function updateStatus($params = [])
220 {
221 $newStatus = Arr::get($params, 'new_status', '');
222
223 if (!$newStatus) {
224 return static::makeErrorResponse([
225 ['code' => 403, 'message' => __('Please select status', 'fluent-cart')]
226 ]);
227 }
228
229 $validStatuses = Status::getEditableCustomerStatuses();
230 if (!isset($validStatuses[$newStatus])) {
231 return static::makeErrorResponse([
232 ['code' => 403, 'message' => __('Provided customer status is not valid', 'fluent-cart')]
233 ]);
234 }
235
236 $customers = static::getQuery()->with(['orders'])->whereIn('id', Arr::get($params, 'customer_ids'))->get();
237
238 foreach ($customers as $customer) {
239 $customer->updateCustomerStatus($newStatus);
240 }
241
242 return static::makeSuccessResponse(
243 '',
244 __('Customer Status has been changed', 'fluent-cart')
245 );
246 }
247
248 /**
249 * Manage customers based on the provided action and customer IDs.
250 *
251 * @param array $params Optional. Array containing the necessary parameters
252 * [
253 * 'action' => (string) Required. The action to be performed on the selected customers.
254 * (e.g., Possible values: 'delete_customers', 'change_customer_status')
255 * 'customer_ids' => (array) Required. Customer IDs whose action will be performed.
256 * ]
257 *
258 */
259 public static function manageCustomer($params = [])
260 {
261
262 $action = Arr::get($params, 'action', '');
263 $customerIds = Arr::get($params, 'customer_ids', []);
264
265 $customerIds = array_map(function ($id) {
266 return (int)$id;
267 }, $customerIds);
268
269
270 $customerIds = array_filter($customerIds);
271
272 if (!$customerIds) {
273 return static::makeErrorResponse([
274 ['code' => 403, 'message' => __('Customers selection is required', 'fluent-cart')]
275 ]);
276 }
277
278 if ($action == 'delete_customers') {
279 return static::deleteAuthorized(null, ['ids' => $customerIds]);
280 }
281
282 if ($action == 'change_customer_status') {
283 return static::updateStatusAuthorized($params);
284 }
285
286 return static::makeErrorResponse([
287 ['code' => 400, 'message' => __('Selected action is invalid', 'fluent-cart')]
288 ]);
289 }
290 }
291