| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Http\Controllers; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentForm\App\Services\Form\FormService; |
| 7 |
use FluentForm\App\Services\Form\HistoryService; |
| 8 |
use FluentForm\Framework\Support\Arr; |
| 9 |
|
| 10 |
class FormController extends Controller |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Get the paginated forms matching search criteria. |
| 14 |
* |
| 15 |
* @param \FluentForm\App\Services\Form\FormService $formService |
| 16 |
* |
| 17 |
* @return \WP_REST_Response |
| 18 |
*/ |
| 19 |
public function index(FormService $formService) |
| 20 |
{ |
| 21 |
$attributes = $this->request->all(); |
| 22 |
|
| 23 |
return $this->sendSuccess( |
| 24 |
$formService->get($attributes) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Create a form from backend/editor |
| 30 |
* |
| 31 |
* @param \FluentForm\App\Services\Form\FormService $formService |
| 32 |
* |
| 33 |
* @return \WP_REST_Response |
| 34 |
*/ |
| 35 |
public function store(FormService $formService) |
| 36 |
{ |
| 37 |
try { |
| 38 |
$attributes = $this->request->all(); |
| 39 |
|
| 40 |
$sanitizeMap = [ |
| 41 |
'title' => 'sanitize_text_field', |
| 42 |
'template_id' => 'intval', |
| 43 |
]; |
| 44 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 45 |
|
| 46 |
$form = $formService->store($attributes); |
| 47 |
|
| 48 |
return $this->sendSuccess([ |
| 49 |
'formId' => $form->id, |
| 50 |
'redirect_url' => admin_url( |
| 51 |
'admin.php?page=fluent_forms&form_id=' . $form->id . '&route=editor' |
| 52 |
), |
| 53 |
'message' => __('Successfully created a form.', 'fluentform'), |
| 54 |
]); |
| 55 |
} catch (Exception $e) { |
| 56 |
return $this->sendError([ |
| 57 |
'message' => $e->getMessage(), |
| 58 |
], 422); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public function duplicate(FormService $formService, $formId) |
| 63 |
{ |
| 64 |
try { |
| 65 |
$attributes = $this->request->all(); |
| 66 |
$attributes['form_id'] = (int) $formId; |
| 67 |
|
| 68 |
$form = $formService->duplicate($attributes); |
| 69 |
|
| 70 |
return $this->sendSuccess([ |
| 71 |
'message' => __('Form has been successfully duplicated.', 'fluentform'), |
| 72 |
'form_id' => $form->id, |
| 73 |
'redirect' => admin_url('admin.php?page=fluent_forms&route=editor&form_id=' . $form->id), |
| 74 |
], 200); |
| 75 |
} catch (Exception $e) { |
| 76 |
return $this->sendError([ |
| 77 |
'message' => $e->getMessage(), |
| 78 |
], 422); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function find(FormService $formService, $formId) |
| 83 |
{ |
| 84 |
try { |
| 85 |
$id = (int) $formId; |
| 86 |
|
| 87 |
$form = $formService->find($id); |
| 88 |
|
| 89 |
return $this->sendSuccess($form, 200); |
| 90 |
} catch (Exception $e) { |
| 91 |
return $this->sendError([ |
| 92 |
'message' => $e->getMessage(), |
| 93 |
], 422); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function delete(FormService $formService, $formId) |
| 98 |
{ |
| 99 |
try { |
| 100 |
$id = (int) $formId; |
| 101 |
|
| 102 |
$formService->delete($id); |
| 103 |
|
| 104 |
return $this->sendSuccess([ |
| 105 |
'message' => __('Successfully deleted the form.', 'fluentform'), |
| 106 |
], 200); |
| 107 |
} catch (Exception $e) { |
| 108 |
return $this->sendError([ |
| 109 |
'message' => $e->getMessage(), |
| 110 |
], 422); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public function update(FormService $formService, $formId) |
| 115 |
{ |
| 116 |
try { |
| 117 |
// Sanitization handled in Updater::update() — only title, status, form_id, formFields are extracted |
| 118 |
$attributes = $this->request->all(); |
| 119 |
$attributes['form_id'] = (int) $formId; |
| 120 |
|
| 121 |
$formService->update($attributes); |
| 122 |
|
| 123 |
return $this->sendSuccess([ |
| 124 |
'message' => __('The form is successfully updated.', 'fluentform'), |
| 125 |
], 200); |
| 126 |
} catch (Exception $e) { |
| 127 |
return $this->sendError([ |
| 128 |
'message' => $e->getMessage(), |
| 129 |
], 422); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
public function convert(FormService $formService, $formId) |
| 134 |
{ |
| 135 |
try { |
| 136 |
$formId = (int) $formId; |
| 137 |
$formService->convert($formId); |
| 138 |
|
| 139 |
return $this->sendSuccess([ |
| 140 |
'message' => __('The form is successfully converted.', 'fluentform'), |
| 141 |
], 200); |
| 142 |
} catch (Exception $e) { |
| 143 |
return $this->sendError([ |
| 144 |
'message' => $e->getMessage(), |
| 145 |
], 422); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public function templates(FormService $formService) |
| 150 |
{ |
| 151 |
try { |
| 152 |
return $this->sendSuccess($formService->templates(), 200); |
| 153 |
} catch (Exception $e) { |
| 154 |
return $this->sendError([ |
| 155 |
'message' => $e->getMessage(), |
| 156 |
], 422); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public function resources(FormService $formService, $formId) |
| 161 |
{ |
| 162 |
$components = $formService->components($formId); |
| 163 |
|
| 164 |
$disabledComponents = $formService->getDisabledComponents(); |
| 165 |
|
| 166 |
return $this->sendSuccess([ |
| 167 |
'components' => $components, |
| 168 |
'disabled_components' => $disabledComponents, |
| 169 |
'shortcodes' => fluentFormEditorShortCodes(), |
| 170 |
'edit_history' => HistoryService::get($formId) |
| 171 |
]); |
| 172 |
} |
| 173 |
|
| 174 |
public function fields(FormService $formService, $formId) |
| 175 |
{ |
| 176 |
return $this->sendSuccess($formService->fields($formId)); |
| 177 |
} |
| 178 |
|
| 179 |
public function shortcodes(FormService $formService, $formId) |
| 180 |
{ |
| 181 |
return $this->sendSuccess($formService->shortcodes($formId)); |
| 182 |
} |
| 183 |
|
| 184 |
public function pages(FormService $formService) |
| 185 |
{ |
| 186 |
return $this->sendSuccess($formService->pages()); |
| 187 |
} |
| 188 |
|
| 189 |
public function findShortCodePage(FormService $formService, $formId) |
| 190 |
{ |
| 191 |
return $this->sendSuccess($formService->findShortCodePage($formId)); |
| 192 |
} |
| 193 |
|
| 194 |
public function formEditHistory(HistoryService $historyService, $formId) |
| 195 |
{ |
| 196 |
return $this->sendSuccess($historyService::get($formId)); |
| 197 |
} |
| 198 |
|
| 199 |
public function clearEditHistory(HistoryService $historyService, $formId) |
| 200 |
{ |
| 201 |
try { |
| 202 |
$id = (int) $formId; |
| 203 |
|
| 204 |
$historyService->delete($id); |
| 205 |
return $this->sendSuccess([ |
| 206 |
'message' => __('Successfully deleted edit history.', 'fluentform'), |
| 207 |
]); |
| 208 |
} catch (Exception $e) { |
| 209 |
return $this->sendError([ |
| 210 |
'message' => $e->getMessage(), |
| 211 |
], 422); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
public function ping() |
| 216 |
{ |
| 217 |
return ['message' => 'pong']; |
| 218 |
} |
| 219 |
} |
| 220 |
|