| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form\Settings; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Acl\Acl; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
use FluentForm\Framework\Foundation\Application; |
| 8 |
use FluentForm\App\Modules\Form\Settings\Validator\Validator; |
| 9 |
|
| 10 |
class FormSettings |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var \FluentForm\Framework\Request\Request |
| 14 |
*/ |
| 15 |
private $request; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var int form ID. |
| 19 |
*/ |
| 20 |
private $formId; |
| 21 |
|
| 22 |
/** |
| 23 |
* The settings (fluentform_form_meta) query builder. |
| 24 |
* |
| 25 |
* @var \WpFluent\QueryBuilder\QueryBuilderHandler |
| 26 |
*/ |
| 27 |
private $settingsQuery; |
| 28 |
|
| 29 |
/** |
| 30 |
* Construct the object |
| 31 |
* @throws \Exception |
| 32 |
*/ |
| 33 |
public function __construct(Application $application) |
| 34 |
{ |
| 35 |
Acl::verify('fluentform_settings_manager'); |
| 36 |
|
| 37 |
$this->request = $application->request; |
| 38 |
|
| 39 |
$this->formId = intval($this->request->get('form_id')); |
| 40 |
|
| 41 |
$this->settingsQuery = wpFluent()->table('fluentform_form_meta')->where('form_id', $this->formId); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get settings for a particular form by id |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function index() |
| 49 |
{ |
| 50 |
$metaKey = sanitize_text_field($this->request->get('meta_key')); |
| 51 |
|
| 52 |
// We'll always try to get a collection for a given meta key. |
| 53 |
// Acknowledging that a certain meta key can have multiple |
| 54 |
// results. The developer using the api knows beforehand |
| 55 |
// that whether the expected result contains multiple |
| 56 |
// or one value. The developer will access that way. |
| 57 |
$result = $this->settingsQuery->where('meta_key', $metaKey)->get(); |
| 58 |
|
| 59 |
foreach ($result as $item) { |
| 60 |
$item->value = json_decode($item->value, true); |
| 61 |
} |
| 62 |
|
| 63 |
wp_send_json_success([ |
| 64 |
'result' => $result |
| 65 |
], 200); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Save settings/meta for a form in database |
| 70 |
*/ |
| 71 |
public function store() |
| 72 |
{ |
| 73 |
$key = sanitize_text_field($this->request->get('meta_key')); |
| 74 |
|
| 75 |
$value = $this->request->get('value', ''); |
| 76 |
|
| 77 |
$valueArray = $value ? json_decode($value, true) : []; |
| 78 |
|
| 79 |
if ($key == 'formSettings') { |
| 80 |
$confirmation = ArrayHelper::get($valueArray, 'confirmation', []); |
| 81 |
|
| 82 |
Validator::validate('confirmations', $confirmation); |
| 83 |
} else { |
| 84 |
Validator::validate($key, $valueArray); |
| 85 |
} |
| 86 |
|
| 87 |
$data = [ |
| 88 |
'form_id' => $this->formId, |
| 89 |
'meta_key' => $key, |
| 90 |
'value' => $value |
| 91 |
]; |
| 92 |
|
| 93 |
// If the request has an valid id field it's safe to assume |
| 94 |
// that the user wants to update an existing settings. |
| 95 |
// So, we'll proceed to do so by finding it first. |
| 96 |
$id = intval($this->request->get('id')); |
| 97 |
|
| 98 |
if ($id) { |
| 99 |
$settings = $this->settingsQuery->find($id); |
| 100 |
} |
| 101 |
|
| 102 |
if (isset($settings)) { |
| 103 |
$this->settingsQuery->where('id', $settings->id)->update($data); |
| 104 |
|
| 105 |
$insertId = $settings->id; |
| 106 |
} else { |
| 107 |
$insertId = $this->settingsQuery->insert($data); |
| 108 |
} |
| 109 |
|
| 110 |
wp_send_json_success([ |
| 111 |
'message' => __('Settings has been saved.', 'fluentform'), |
| 112 |
'settings' => json_decode($value, true), |
| 113 |
'id' => $insertId |
| 114 |
], 200); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Delete settings/meta from database for a given form |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function remove() |
| 122 |
{ |
| 123 |
$id = intval($this->request->get('id')); |
| 124 |
|
| 125 |
$this->settingsQuery->where('id', $id)->delete(); |
| 126 |
|
| 127 |
wp_send_json([], 200); |
| 128 |
} |
| 129 |
} |