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