| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Http\Controllers; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentForm\App\Models\Submission; |
| 7 |
use FluentForm\App\Services\Submission\SubmissionService; |
| 8 |
use FluentForm\Framework\Support\Arr; |
| 9 |
|
| 10 |
class SubmissionController extends Controller |
| 11 |
{ |
| 12 |
public function index(SubmissionService $submissionService) |
| 13 |
{ |
| 14 |
try { |
| 15 |
$attributes = $this->sanitizeSubmissionAttributes($this->request->all()); |
| 16 |
|
| 17 |
return $this->sendSuccess( |
| 18 |
$submissionService->get($attributes) |
| 19 |
); |
| 20 |
} catch (Exception $e) { |
| 21 |
return $this->sendError([ |
| 22 |
'message' => $e->getMessage(), |
| 23 |
]); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public function find(SubmissionService $submissionService, $submissionId) |
| 28 |
{ |
| 29 |
try { |
| 30 |
return $this->sendSuccess( |
| 31 |
$submissionService->find($submissionId) |
| 32 |
); |
| 33 |
} catch (Exception $e) { |
| 34 |
return $this->sendError([ |
| 35 |
'message' => $e->getMessage(), |
| 36 |
]); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public function resources(SubmissionService $submissionService) |
| 41 |
{ |
| 42 |
try { |
| 43 |
$attributes = $this->request->all(); |
| 44 |
|
| 45 |
$sanitizeMap = [ |
| 46 |
'form_id' => 'intval', |
| 47 |
]; |
| 48 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 49 |
|
| 50 |
return $this->sendSuccess( |
| 51 |
$submissionService->resources($attributes) |
| 52 |
); |
| 53 |
} catch (Exception $e) { |
| 54 |
return $this->sendError([ |
| 55 |
'message' => $e->getMessage(), |
| 56 |
]); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function updateStatus(SubmissionService $submissionService) |
| 61 |
{ |
| 62 |
try { |
| 63 |
$status = $submissionService->updateStatus($this->request->all()); |
| 64 |
|
| 65 |
/* translators: %s is the submission status */ |
| 66 |
$message = sprintf(__('The submission has been marked as %s', 'fluentform'), $status); |
| 67 |
|
| 68 |
return $this->sendSuccess([ |
| 69 |
'message' => $message, |
| 70 |
'status' => $status, |
| 71 |
]); |
| 72 |
} catch (Exception $e) { |
| 73 |
return $this->sendError([ |
| 74 |
'message' => $e->getMessage(), |
| 75 |
]); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public function toggleIsFavorite(SubmissionService $submissionService) |
| 80 |
{ |
| 81 |
try { |
| 82 |
[$message, $isFavourite] = $submissionService->toggleIsFavorite( |
| 83 |
$this->request->get('entry_id') |
| 84 |
); |
| 85 |
|
| 86 |
return $this->sendSuccess([ |
| 87 |
'message' => $message, |
| 88 |
'is_favourite' => $isFavourite, |
| 89 |
]); |
| 90 |
} catch (Exception $e) { |
| 91 |
return $this->sendError([ |
| 92 |
'message' => $e->getMessage(), |
| 93 |
]); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function handleBulkActions(SubmissionService $submissionService) |
| 98 |
{ |
| 99 |
try { |
| 100 |
$message = $submissionService->handleBulkActions($this->request->all()); |
| 101 |
|
| 102 |
return $this->sendSuccess(['message' => $message]); |
| 103 |
} catch (Exception $e) { |
| 104 |
return $this->sendError([ |
| 105 |
'message' => $e->getMessage(), |
| 106 |
]); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public function remove(SubmissionService $submissionService, $submissionId) |
| 111 |
{ |
| 112 |
try { |
| 113 |
$submission = Submission::findOrFail($submissionId); |
| 114 |
$submissionService->deleteEntries([$submissionId], $submission->form_id); |
| 115 |
do_action('fluentform/submission_deleted', $submissionId); |
| 116 |
|
| 117 |
return $this->sendSuccess([ |
| 118 |
'message' => __('Selected submission successfully deleted Permanently', 'fluentform'), |
| 119 |
]); |
| 120 |
|
| 121 |
} catch (Exception $e) { |
| 122 |
return $this->sendError([ |
| 123 |
'message' => $e->getMessage(), |
| 124 |
]); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get user list for submission page |
| 130 |
* @return \WP_REST_Response |
| 131 |
*/ |
| 132 |
public function submissionUsers() |
| 133 |
{ |
| 134 |
$search = sanitize_text_field($this->request->get('search')); |
| 135 |
$users = get_users([ |
| 136 |
'search' => "*{$search}*", |
| 137 |
'number' => 50, |
| 138 |
]); |
| 139 |
|
| 140 |
$formattedUsers = []; |
| 141 |
foreach ($users as $user) { |
| 142 |
$formattedUsers[] = [ |
| 143 |
'ID' => $user->ID, |
| 144 |
'label' => $user->display_name . ' - ' . $user->user_email, |
| 145 |
]; |
| 146 |
} |
| 147 |
|
| 148 |
return $this->sendSuccess([ |
| 149 |
'users' => $formattedUsers, |
| 150 |
]); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Update User of a submission |
| 155 |
* @param SubmissionService $submissionService |
| 156 |
* @return \WP_REST_Response |
| 157 |
*/ |
| 158 |
public function updateSubmissionUser(SubmissionService $submissionService) |
| 159 |
{ |
| 160 |
try { |
| 161 |
$userId = intval($this->request->get('user_id')); |
| 162 |
// Use entry_id from route parameter — not submission_id from body — |
| 163 |
// to ensure authorization target matches the mutation target. |
| 164 |
$submissionId = intval($this->request->get('entry_id')); |
| 165 |
$response = $submissionService->updateSubmissionUser($userId, $submissionId); |
| 166 |
return $this->sendSuccess($response); |
| 167 |
} catch (Exception $e) { |
| 168 |
return $this->sendError([ |
| 169 |
'message' => $e->getMessage(), |
| 170 |
]); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get All Submissions |
| 176 |
* @param Submission $submission |
| 177 |
* @return \WP_REST_Response |
| 178 |
*/ |
| 179 |
public function all(Submission $submission) |
| 180 |
{ |
| 181 |
try { |
| 182 |
$attributes = $this->sanitizeSubmissionAttributes($this->request->all()); |
| 183 |
|
| 184 |
return $this->sendSuccess( |
| 185 |
$submission->allSubmissions($attributes) |
| 186 |
); |
| 187 |
} catch (Exception $e) { |
| 188 |
return $this->sendError([ |
| 189 |
'message' => $e->getMessage(), |
| 190 |
]); |
| 191 |
} |
| 192 |
} |
| 193 |
/** |
| 194 |
* Get printable content |
| 195 |
* @param SubmissionService $submissionService |
| 196 |
* @return \WP_REST_Response |
| 197 |
*/ |
| 198 |
public function print(SubmissionService $submissionService) |
| 199 |
{ |
| 200 |
try { |
| 201 |
$attributes = $this->request->all(); |
| 202 |
|
| 203 |
$sanitizeMap = [ |
| 204 |
'submission_ids' => function($value) { |
| 205 |
if (is_array($value)) { |
| 206 |
return array_map('intval', $value); |
| 207 |
} |
| 208 |
return []; |
| 209 |
}, |
| 210 |
'form_id' => 'intval', |
| 211 |
]; |
| 212 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 213 |
|
| 214 |
// Preserve backward compatibility with any legacy callers that still |
| 215 |
// send entry_ids, while normalizing to the current submission_ids key. |
| 216 |
if (empty($attributes['submission_ids']) && isset($attributes['entry_ids'])) { |
| 217 |
$entryIds = $attributes['entry_ids']; |
| 218 |
$attributes['submission_ids'] = is_array($entryIds) |
| 219 |
? array_map('intval', $entryIds) |
| 220 |
: []; |
| 221 |
} |
| 222 |
|
| 223 |
return $this->sendSuccess( |
| 224 |
$submissionService->getPrintContent($attributes) |
| 225 |
); |
| 226 |
} catch (Exception $e) { |
| 227 |
return $this->sendError([ |
| 228 |
'message' => $e->getMessage() |
| 229 |
]); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
private function sanitizeSubmissionAttributes($attributes) |
| 234 |
{ |
| 235 |
$sanitizeMap = [ |
| 236 |
'search' => 'sanitize_text_field', |
| 237 |
'status' => 'sanitize_text_field', |
| 238 |
'entry_type' => 'sanitize_text_field', |
| 239 |
'form_id' => 'intval', |
| 240 |
'per_page' => 'intval', |
| 241 |
'page' => 'intval', |
| 242 |
'is_favourite' => 'rest_sanitize_boolean', |
| 243 |
]; |
| 244 |
|
| 245 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 246 |
|
| 247 |
if (isset($attributes['entry_type']) && !isset($attributes['status'])) { |
| 248 |
$attributes['status'] = $attributes['entry_type']; |
| 249 |
} |
| 250 |
|
| 251 |
if (isset($attributes['date_range']) && is_array($attributes['date_range'])) { |
| 252 |
$attributes['date_range'] = array_map('sanitize_text_field', $attributes['date_range']); |
| 253 |
} |
| 254 |
|
| 255 |
if (isset($attributes['payment_statuses']) && is_array($attributes['payment_statuses'])) { |
| 256 |
$attributes['payment_statuses'] = array_map('sanitize_text_field', $attributes['payment_statuses']); |
| 257 |
} |
| 258 |
|
| 259 |
return $attributes; |
| 260 |
} |
| 261 |
} |
| 262 |
|