| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Entry; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 6 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 7 |
use IvyForms\Controllers\Controller; |
| 8 |
use IvyForms\Services\Entry\EntryService; |
| 9 |
use IvyForms\Services\Translations\BackendStrings; |
| 10 |
use WP_REST_Request; |
| 11 |
use WP_REST_Response; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class BulkUpdateEntryController |
| 15 |
* |
| 16 |
* @package IvyForms\Controllers\Entry |
| 17 |
*/ |
| 18 |
class BulkUpdateEntryController extends Controller |
| 19 |
{ |
| 20 |
private EntryService $entryService; |
| 21 |
|
| 22 |
public function __construct(EntryService $entryService) |
| 23 |
{ |
| 24 |
$this->entryService = $entryService; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Handle bulk update request for entries |
| 29 |
* |
| 30 |
* @param WP_REST_Request $data |
| 31 |
* |
| 32 |
* @return WP_REST_Response |
| 33 |
* |
| 34 |
* @throws InvalidArgumentException |
| 35 |
*/ |
| 36 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 37 |
{ |
| 38 |
// Verify the nonce |
| 39 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 40 |
|
| 41 |
// Check if the entry data is provided |
| 42 |
if (empty($data->get_params())) { |
| 43 |
throw new InvalidArgumentException( |
| 44 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
// Sanitize request data |
| 49 |
$sanitized = Sanitizer::sanitizeBulkUpdateData([ |
| 50 |
'column' => $data->get_param('column'), |
| 51 |
'value' => $data->get_param('value'), |
| 52 |
'ids' => $data->get_param('ids'), |
| 53 |
]); |
| 54 |
|
| 55 |
// Validate IDs |
| 56 |
if (empty($sanitized['ids'])) { |
| 57 |
return new WP_REST_Response([ |
| 58 |
'success' => false, |
| 59 |
'message' => BackendStrings::getExceptionStrings()['invalid_or_missing_ids'], |
| 60 |
], 400); |
| 61 |
} |
| 62 |
|
| 63 |
// Validate column |
| 64 |
if (empty($sanitized['column'])) { |
| 65 |
return new WP_REST_Response([ |
| 66 |
'success' => false, |
| 67 |
'message' => BackendStrings::getExceptionStrings()['table_or_attribute_not_allowed'], |
| 68 |
], 400); |
| 69 |
} |
| 70 |
|
| 71 |
$this->entryService->getEntryManager()->bulkUpdateColumn( |
| 72 |
$sanitized['column'], |
| 73 |
$sanitized['value'], |
| 74 |
$sanitized['ids'] |
| 75 |
); |
| 76 |
|
| 77 |
return new WP_REST_Response([ |
| 78 |
'success' => true, |
| 79 |
'message' => BackendStrings::getExceptionStrings()['batch_update_successful'], |
| 80 |
], 200); |
| 81 |
} |
| 82 |
} |
| 83 |
|