| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Acl\Acl; |
| 6 |
use FluentForm\Framework\Foundation\Application; |
| 7 |
|
| 8 |
class Form |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var \FluentForm\Framework\Request\Request $request |
| 12 |
*/ |
| 13 |
protected $request; |
| 14 |
|
| 15 |
/** |
| 16 |
* Set this value when we need predefined default settings. |
| 17 |
* |
| 18 |
* @var array $defaultSettings |
| 19 |
*/ |
| 20 |
protected $defaultSettings; |
| 21 |
|
| 22 |
/** |
| 23 |
* Set this value when we need predefined form fields. |
| 24 |
* |
| 25 |
* @var array $formFields |
| 26 |
*/ |
| 27 |
protected $formFields; |
| 28 |
|
| 29 |
/** |
| 30 |
* Form constructor. |
| 31 |
* |
| 32 |
* @param \FluentForm\Framework\Foundation\Application $application |
| 33 |
* |
| 34 |
* @throws \Exception |
| 35 |
*/ |
| 36 |
public function __construct(Application $application) |
| 37 |
{ |
| 38 |
Acl::verify('fluentform_forms_manager'); |
| 39 |
|
| 40 |
$this->request = $application->request; |
| 41 |
|
| 42 |
$this->model = wpFluent()->table('fluentform_forms'); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get all forms from database |
| 47 |
* |
| 48 |
* @return void |
| 49 |
* @throws \Exception |
| 50 |
*/ |
| 51 |
public function index() |
| 52 |
{ |
| 53 |
$search = $this->request->get('search'); |
| 54 |
$status = $this->request->get('status'); |
| 55 |
|
| 56 |
$query = $this->model->orderBy('created_at', 'DESC'); |
| 57 |
|
| 58 |
if($status && $status != 'all') { |
| 59 |
$query->where('status', $status); |
| 60 |
} |
| 61 |
|
| 62 |
if($search) { |
| 63 |
$query->where(function($q) use ($search) |
| 64 |
{ |
| 65 |
$q->where('id', 'LIKE', '%'.$search.'%'); |
| 66 |
$q->where('title', 'LIKE', '%'.$search.'%'); |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
$forms = $query->paginate(); |
| 71 |
|
| 72 |
foreach ($forms['data'] as $form) { |
| 73 |
$form->preview_url = site_url('?fluentform_pages=1&preview_id='.$form->id).'#ff_preview';; |
| 74 |
$form->edit_url = $this->getAdminPermalink('editor', $form->id); |
| 75 |
$form->entries_url = $this->getAdminPermalink('entries', $form->id); |
| 76 |
$form->analytics_url = $this->getAdminPermalink('analytics', $form->id); |
| 77 |
$form->total_views = $this->getFormViewCount($form->id); |
| 78 |
$form->total_views = $this->getFormViewCount($form->id); |
| 79 |
$form->total_Submissions = $this->getSubmissionCount($form->id); |
| 80 |
$form->conversion = $this->getConversionRate($form); |
| 81 |
} |
| 82 |
|
| 83 |
wp_send_json($forms, 200); |
| 84 |
} |
| 85 |
|
| 86 |
private function getFormViewCount($formId) |
| 87 |
{ |
| 88 |
$hasCount = wpFluent() |
| 89 |
->table('fluentform_form_meta') |
| 90 |
->where('meta_key', '_total_views') |
| 91 |
->where('form_id', $formId) |
| 92 |
-> first(); |
| 93 |
if($hasCount) |
| 94 |
return intval($hasCount->value); |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
private function getSubmissionCount($formID) |
| 99 |
{ |
| 100 |
return wpFluent()->table('fluentform_submissions')->where('form_id', $formID)->count(); |
| 101 |
} |
| 102 |
|
| 103 |
private function getConversionRate($form) |
| 104 |
{ |
| 105 |
if(!$form->total_Submissions) |
| 106 |
return 0; |
| 107 |
|
| 108 |
if(!$form->total_views) |
| 109 |
return 0; |
| 110 |
|
| 111 |
return ceil(($form->total_Submissions / $form->total_views) * 100); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Create a form from backend/editor |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function store() |
| 119 |
{ |
| 120 |
$type = $this->request->get('type', 'form'); |
| 121 |
$title = $this->request->get('title', 'My New Form'); |
| 122 |
$status = $this->request->get('status', 'published'); |
| 123 |
$createdBy = get_current_user_id(); |
| 124 |
|
| 125 |
//$this->validate(); |
| 126 |
|
| 127 |
$now = date("Y-m-d H:i:s"); |
| 128 |
|
| 129 |
$insertData = [ |
| 130 |
'title' => $title, |
| 131 |
'type' => $type, |
| 132 |
'status' => $status, |
| 133 |
'created_by' => $createdBy, |
| 134 |
'created_at' => $now, |
| 135 |
'updated_at' => $now |
| 136 |
]; |
| 137 |
|
| 138 |
if ($this->formFields) { |
| 139 |
$insertData['form_fields'] = $this->formFields; |
| 140 |
} |
| 141 |
|
| 142 |
$formId = $this->model->insert($insertData); |
| 143 |
|
| 144 |
// add default form settings now |
| 145 |
$defaultSettings = $this->defaultSettings ?: $this->getFormsDefaultSettings($formId); |
| 146 |
|
| 147 |
wpFluent()->table('fluentform_form_meta') |
| 148 |
->insert(array( |
| 149 |
'form_id' => $formId, |
| 150 |
'meta_key' => 'formSettings', |
| 151 |
'value' => json_encode($defaultSettings) |
| 152 |
)); |
| 153 |
|
| 154 |
do_action('fluentform_inserted_new_form', $formId, $insertData); |
| 155 |
|
| 156 |
wp_send_json_success(array( |
| 157 |
'formId' => $formId, |
| 158 |
'redirect_url' => admin_url('admin.php?page=fluent_forms&form_id='.$formId.'&route=editor'), |
| 159 |
'message' => __('Successfully created a form.', 'fluentform') |
| 160 |
), 200); |
| 161 |
} |
| 162 |
|
| 163 |
private function getFormsDefaultSettings($formId = false) |
| 164 |
{ |
| 165 |
$defaultSettings = array( |
| 166 |
'confirmation' => array( |
| 167 |
'redirectTo' => 'samePage', |
| 168 |
'messageToShow' => 'Thank you for your message. We will get in touch with you shortly', |
| 169 |
'customPage' => null, |
| 170 |
'samePageFormBehavior' => 'hide_form', |
| 171 |
'customUrl' => null |
| 172 |
), |
| 173 |
'restrictions' => array( |
| 174 |
'limitNumberOfEntries' => array( |
| 175 |
'enabled' => false, |
| 176 |
'numberOfEntries' => null, |
| 177 |
'period' => 'total', |
| 178 |
'limitReachedMsg' => null |
| 179 |
), |
| 180 |
'scheduleForm' => array( |
| 181 |
'enabled' => false, |
| 182 |
'start' => null, |
| 183 |
'end' => null, |
| 184 |
'pendingMsg' => "Form submission is not started yet.", |
| 185 |
'expiredMsg' => "Form submission is now closed." |
| 186 |
), |
| 187 |
'requireLogin' => array( |
| 188 |
'enabled' => false, |
| 189 |
'requireLoginMsg' => null, |
| 190 |
), |
| 191 |
'denyEmptySubmission' => [ |
| 192 |
'enabled' => false, |
| 193 |
'message' => 'Sorry, you cannot submit an empty form. Let\'s hear what you wanna say.' |
| 194 |
] |
| 195 |
), |
| 196 |
'layout' => array( |
| 197 |
'labelPlacement' => 'top', |
| 198 |
'helpMessagePlacement' => 'with_label', |
| 199 |
'errorMessagePlacement' => 'inline', |
| 200 |
'cssClassName' => '' |
| 201 |
) |
| 202 |
); |
| 203 |
|
| 204 |
$globalSettings = get_option('_fluentform_global_form_settings'); |
| 205 |
|
| 206 |
if(isset($globalSettings['layout'])) { |
| 207 |
$defaultSettings['layout'] = $globalSettings['layout']; |
| 208 |
} |
| 209 |
|
| 210 |
return $defaultSettings; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Find/Read a from from the database |
| 215 |
* @return void |
| 216 |
*/ |
| 217 |
public function find() |
| 218 |
{ |
| 219 |
$formId = $this->request->get('formId'); |
| 220 |
|
| 221 |
$form = $this->model->find($formId); |
| 222 |
|
| 223 |
wp_send_json(['form' => $form, 'metas' => []], 200); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Save/update a form from backend/editor |
| 228 |
* @return void |
| 229 |
*/ |
| 230 |
public function update() |
| 231 |
{ |
| 232 |
$formId = $this->request->get('formId'); |
| 233 |
$title = $this->request->get('title'); |
| 234 |
$formFields = $this->request->get('formFields'); |
| 235 |
$status = $this->request->get('status', 'Draft'); |
| 236 |
|
| 237 |
$this->validate(); |
| 238 |
|
| 239 |
$this->model->where('id', $formId)->update([ |
| 240 |
'title' => $title, |
| 241 |
'status' => $status, |
| 242 |
'form_fields' => $formFields, |
| 243 |
'updated_at' => date('Y-m-d h:i:s') |
| 244 |
]); |
| 245 |
|
| 246 |
wp_send_json([ |
| 247 |
'message' => __('The form is successfully updated.', 'fluentform') |
| 248 |
], 200); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Delete a from from database |
| 253 |
* @return void |
| 254 |
*/ |
| 255 |
public function delete() |
| 256 |
{ |
| 257 |
$formId = $this->request->get('formId'); |
| 258 |
|
| 259 |
$this->model->where('id', $formId)->delete(); |
| 260 |
|
| 261 |
wp_send_json([ |
| 262 |
'message' => __('Successfully deleted the form.', 'fluentform') |
| 263 |
], 200); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Validate a form only by form title |
| 268 |
* @return void |
| 269 |
*/ |
| 270 |
private function validate() |
| 271 |
{ |
| 272 |
if (! $this->request->get('title')) { |
| 273 |
wp_send_json([ |
| 274 |
'title' => 'The title field is required.' |
| 275 |
], 423); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
private function getAdminPermalink($route, $formId) |
| 280 |
{ |
| 281 |
$baseUrl = admin_url('admin.php?page=fluent_forms'); |
| 282 |
return $baseUrl.'&route='.$route."&form_id=".$formId; |
| 283 |
} |
| 284 |
|
| 285 |
public function getAllForms() |
| 286 |
{ |
| 287 |
$forms = $this->model->orderBy('created_at', 'DESC')->get(); |
| 288 |
|
| 289 |
wp_send_json($forms, 200); |
| 290 |
} |
| 291 |
} |
| 292 |
|