| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\App; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class Predefined extends Form |
| 9 |
{ |
| 10 |
/** |
| 11 |
* all JSON data will be stored here |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
private function getPredefinedForms() { |
| 15 |
return array( |
| 16 |
'contact_form' => array( |
| 17 |
'screenshot' => App::publicUrl('img/forms/contact_form.jpg'), |
| 18 |
'createable' => true, |
| 19 |
'brief' => 'A simple contact form for your site.', |
| 20 |
'json' => '[{"id":"22","title":"Contact Form","form":{"fields":[{"index":2,"element":"input_name","attributes":{"name":"names","data-type":"name-element"},"settings":{"container_class":"","admin_field_label":"","conditional_logics":[]},"fields":{"first_name":{"element":"input_text","attributes":{"type":"text","name":"first_name","value":"","id":"","class":"","placeholder":""},"settings":{"container_class":"","label":"First Name","help_message":"","visible":true,"validation_rules":{"required":{"value":true,"message":"First Name field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"middle_name":{"element":"input_text","attributes":{"type":"text","name":"middle_name","value":"","id":"","class":"","placeholder":"","required":false},"settings":{"container_class":"","label":"Middle Name","help_message":"","error_message":"","visible":false,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"last_name":{"element":"input_text","attributes":{"type":"text","name":"last_name","value":"","id":"","class":"","placeholder":"","required":false},"settings":{"container_class":"","label":"Last Name","help_message":"","error_message":"","visible":true,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}}},"editor_options":{"title":"Name Fields","element":"name-fields","icon_class":"icon-user","template":"nameFields"},"uniqElKey":"el_1516797564818"},{"index":7,"element":"input_email","attributes":{"type":"email","name":"email","value":"","id":"","class":"","placeholder":"you@domain.com"},"settings":{"container_class":"","label":"Email","label_placement":"","help_message":"","validation_rules":{"required":{"value":true,"message":"Email field is required"},"email":{"value":true,"message":"This field must contain a valid email"}},"conditional_logics":[]},"editor_options":{"title":"Email","icon_class":"icon-envelope-o","template":"inputText"},"uniqElKey":"el_1516797583697"},{"index":2,"element":"textarea","attributes":{"name":"message","value":"","id":"","class":"","placeholder":"Type your message here...","rows":4,"cols":2},"settings":{"container_class":"","label":"Message","label_placement":"","help_message":"","validation_rules":{"required":{"value":true,"message":"Message field is required"}},"conditional_logics":[]},"editor_options":{"title":"Textarea","icon_class":"icon-paragraph","template":"inputTextarea"},"uniqElKey":"el_1516797589062"}],"submitButton":{"element":"button","attributes":{"type":"submit","id":"","class":""},"settings":{"align":"left","container_class":"","btn_text":"Submit Form","help_message":""},"editor_options":{"title":"Submit Button"}}},"formSettings":{"confirmation":{"redirectTo":"samePage","messageToShow":"Thank you for your message. We will get in touch with you shortly","customPage":null,"samePageFormBehavior":"hide_form","customUrl":null},"restrictions":{"limitNumberOfEntries":{"enabled":false,"numberOfEntries":null,"period":"total","limitReachedMsg":null},"scheduleForm":{"enabled":false,"start":null,"end":null,"pendingMsg":"Form submission is not started yet.","expiredMsg":"Form submission is now closed."},"requireLogin":{"enabled":false,"requireLoginMsg":null},"denyEmptySubmission":{"enabled":false,"message":"Sorry, you cannot submit an empty form. Let\'s hear what you wanna say."}},"layout":{"labelPlacement":"top","helpMessagePlacement":"with_label","errorMessagePlacement":"inline"}}}]' |
| 21 |
), |
| 22 |
'support_form' => array( |
| 23 |
'screenshot' => App::publicUrl('img/forms/support_form.jpg'), |
| 24 |
'createable' => true, |
| 25 |
'brief' => 'Using this support form users can ask questions.', |
| 26 |
'json' => '[{"id":"23","title":"Support Form","form":{"fields":[{"index":2,"element":"input_name","attributes":{"name":"names","data-type":"name-element"},"settings":{"container_class":"","admin_field_label":"","conditional_logics":[]},"fields":{"first_name":{"element":"input_text","attributes":{"type":"text","name":"first_name","value":"","id":"","class":"","placeholder":""},"settings":{"container_class":"","label":"First Name","help_message":"","visible":true,"validation_rules":{"required":{"value":true,"message":"First Name field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"middle_name":{"element":"input_text","attributes":{"type":"text","name":"middle_name","value":"","id":"","class":"","placeholder":"","required":false},"settings":{"container_class":"","label":"Middle Name","help_message":"","error_message":"","visible":false,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"last_name":{"element":"input_text","attributes":{"type":"text","name":"last_name","value":"","id":"","class":"","placeholder":"","required":false},"settings":{"container_class":"","label":"Last Name","help_message":"","error_message":"","visible":true,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}}},"editor_options":{"title":"Name Fields","element":"name-fields","icon_class":"icon-user","template":"nameFields"},"uniqElKey":"el_1516797727681"},{"index":7,"element":"input_email","attributes":{"type":"email","name":"email","value":"","id":"","class":"","placeholder":"you@domain.com"},"settings":{"container_class":"","label":"Email","label_placement":"","help_message":"","validation_rules":{"required":{"value":true,"message":"Email field is required"},"email":{"value":true,"message":"This field must contain a valid email"}},"conditional_logics":[]},"editor_options":{"title":"Email","icon_class":"icon-envelope-o","template":"inputText"},"uniqElKey":"el_1516797731343"},{"index":3,"element":"select","attributes":{"name":"department","value":"","id":"","class":""},"settings":{"label":"Department","help_message":"","container_class":"","label_placement":"","placeholder":"- Select Department -","validation_rules":{"required":{"value":true,"message":"Department field is required"}},"conditional_logics":[]},"options":{"Web Design":"Web Design","Web Development":"Web Development","WordPress Development":"WordPress Development","DevOps":"DevOps"},"editor_options":{"title":"Dropdown","icon_class":"icon-caret-square-o-down","element":"select","template":"select"},"uniqElKey":"el_1516797735245"},{"index":1,"element":"input_text","attributes":{"type":"text","name":"subject","value":"","class":"","placeholder":"Type your subject"},"settings":{"container_class":"","label":"Subject","label_placement":"","admin_field_label":"","help_message":"","validation_rules":{"required":{"value":true,"message":"Subject field is required"}},"conditional_logics":[]},"editor_options":{"title":"Text Input","icon_class":"icon-text-width","template":"inputText"},"uniqElKey":"el_1516797770161"},{"index":2,"element":"textarea","attributes":{"name":"description","value":"","id":"","class":"","placeholder":"","rows":4,"cols":2},"settings":{"container_class":"","label":"Description","label_placement":"","help_message":"","validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"title":"Textarea","icon_class":"icon-paragraph","template":"inputTextarea"},"uniqElKey":"el_1516797774136"}],"submitButton":{"element":"button","attributes":{"type":"submit","id":"","class":""},"settings":{"align":"left","container_class":"","btn_text":"Submit Form","help_message":""},"editor_options":{"title":"Submit Button"}}},"formSettings":{"confirmation":{"redirectTo":"samePage","messageToShow":"Thank you for your message. We will get in touch with you shortly","customPage":null,"samePageFormBehavior":"hide_form","customUrl":null},"restrictions":{"limitNumberOfEntries":{"enabled":false,"numberOfEntries":null,"period":"total","limitReachedMsg":null},"scheduleForm":{"enabled":false,"start":null,"end":null,"pendingMsg":"Form submission is not started yet.","expiredMsg":"Form submission is now closed."},"requireLogin":{"enabled":false,"requireLoginMsg":null},"denyEmptySubmission":{"enabled":false,"message":"Sorry, you cannot submit an empty form. Let\'s hear what you wanna say."}},"layout":{"labelPlacement":"top","helpMessagePlacement":"with_label","errorMessagePlacement":"inline"}}}]' |
| 27 |
), |
| 28 |
'event_registration_form' => array( |
| 29 |
'screenshot' => App::publicUrl('img/forms/event_form.jpg'), |
| 30 |
'createable' => true, |
| 31 |
'brief' => 'Using this registration form you can enable your event to achieve it\'s goal.', |
| 32 |
'json' => '[{"id":"24","title":"Event Registration Form","form":{"fields":[{"index":2,"element":"input_name","attributes":{"name":"names","data-type":"name-element"},"settings":{"container_class":"","admin_field_label":"","conditional_logics":[]},"fields":{"first_name":{"element":"input_text","attributes":{"type":"text","name":"first_name","value":"","id":"","class":"","placeholder":""},"settings":{"container_class":"","label":"First Name","help_message":"","visible":true,"validation_rules":{"required":{"value":true,"message":"First Name field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"middle_name":{"element":"input_text","attributes":{"type":"text","name":"middle_name","value":"","id":"","class":"","placeholder":"","required":false},"settings":{"container_class":"","label":"Middle Name","help_message":"","error_message":"","visible":false,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"last_name":{"element":"input_text","attributes":{"type":"text","name":"last_name","value":"","id":"","class":"","placeholder":"","required":false},"settings":{"container_class":"","label":"Last Name","help_message":"","error_message":"","visible":true,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}}},"editor_options":{"title":"Name Fields","element":"name-fields","icon_class":"icon-user","template":"nameFields"},"uniqElKey":"el_1516797727681"},{"index":7,"element":"input_email","attributes":{"type":"email","name":"email","value":"","id":"","class":"","placeholder":"you@domain.com"},"settings":{"container_class":"","label":"Email","label_placement":"","help_message":"","validation_rules":{"required":{"value":true,"message":"Email field is required"},"email":{"value":true,"message":"This field must contain a valid email"}},"conditional_logics":[]},"editor_options":{"title":"Email","icon_class":"icon-envelope-o","template":"inputText"},"uniqElKey":"el_1516797731343"},{"index":14,"element":"input_number","attributes":{"type":"number","name":"phone","value":"","id":"","class":"","placeholder":"Your phone number"},"settings":{"container_class":"","label":"Phone","label_placement":"","help_message":"","validation_rules":{"required":{"value":true,"message":"Phone field is required"},"numeric":{"value":true,"message":"This field must contain numeric value"},"min":{"value":"","message":"Minimum value is "},"max":{"value":"","message":"Maximum value is "}},"conditional_logics":[]},"editor_options":{"title":"Numeric Field","icon_class":"icon-slack","template":"inputText"},"uniqElKey":"el_1516798280530"},{"index":1,"element":"input_text","attributes":{"type":"text","name":"company","value":"","class":"","placeholder":"Type your company name"},"settings":{"container_class":"","label":"Company","label_placement":"","admin_field_label":"","help_message":"","validation_rules":{"required":{"value":false,"message":"Company field is required"}},"conditional_logics":[]},"editor_options":{"title":"Text Input","icon_class":"icon-text-width","template":"inputText"},"uniqElKey":"el_1516797770161"},{"index":8,"element":"input_url","attributes":{"type":"url","name":"url","value":"","id":"","class":"","placeholder":"http:\/\/www.google.com"},"settings":{"container_class":"","label":"Website","label_placement":"","help_message":"","validation_rules":{"required":{"value":false,"message":"Website field is required"},"url":{"value":true,"message":"This field must contain a valid url"}},"conditional_logics":[]},"editor_options":{"title":"URL","icon_class":"icon-link","template":"inputText"},"uniqElKey":"el_1516798321477"},{"index":2,"element":"textarea","attributes":{"name":"message","value":"","id":"","class":"","placeholder":"","rows":4,"cols":2},"settings":{"container_class":"","label":"Message (if any)","label_placement":"","help_message":"","validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"title":"Textarea","icon_class":"icon-paragraph","template":"inputTextarea"},"uniqElKey":"el_1516797774136"}],"submitButton":{"element":"button","attributes":{"type":"submit","id":"","class":""},"settings":{"align":"left","container_class":"","btn_text":"Submit Form","help_message":""},"editor_options":{"title":"Submit Button"}}},"formSettings":{"confirmation":{"redirectTo":"samePage","messageToShow":"Thank you for your message. We will get in touch with you shortly","customPage":null,"samePageFormBehavior":"hide_form","customUrl":null},"restrictions":{"limitNumberOfEntries":{"enabled":false,"numberOfEntries":null,"period":"total","limitReachedMsg":null},"scheduleForm":{"enabled":false,"start":null,"end":null,"pendingMsg":"Form submission is not started yet.","expiredMsg":"Form submission is now closed."},"requireLogin":{"enabled":false,"requireLoginMsg":null},"denyEmptySubmission":{"enabled":false,"message":"Sorry, you cannot submit an empty form. Let\'s hear what you wanna say."}},"layout":{"labelPlacement":"top","helpMessagePlacement":"with_label","errorMessagePlacement":"inline"},"id":"29"}}]' |
| 33 |
) |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Fetch simplified information for all predefined forms |
| 39 |
*/ |
| 40 |
public function all() |
| 41 |
{ |
| 42 |
$data = array (); |
| 43 |
foreach ($this->getPredefinedForms() as $key => $item) { |
| 44 |
$jsonArray = json_decode($item['json'], true); |
| 45 |
$jsonArray = $jsonArray[0]; |
| 46 |
|
| 47 |
$data[$key] = array ( |
| 48 |
'brief' => $item['brief'], |
| 49 |
'title' => $jsonArray['title'], |
| 50 |
'buy_url' => 'https://wpfluentform.com/pro', |
| 51 |
'screenshot' => $item['screenshot'], |
| 52 |
'createable' => $item['createable'], |
| 53 |
); |
| 54 |
} |
| 55 |
$data = apply_filters('fluentform_predefined_forms', $data); |
| 56 |
wp_send_json($data, 200); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Create a predefined form |
| 61 |
* @param $name |
| 62 |
*/ |
| 63 |
public function create() |
| 64 |
{ |
| 65 |
$predefined = $this->request->get('predefined'); |
| 66 |
|
| 67 |
$predefinedForm = ArrayHelper::get($this->getPredefinedForms(), $predefined); |
| 68 |
|
| 69 |
if ($predefinedForm) { |
| 70 |
$predefinedForm = json_decode($predefinedForm['json'], true)[0]; |
| 71 |
|
| 72 |
$this->request->merge(['title' => $predefinedForm['title']]); |
| 73 |
|
| 74 |
$this->formFields = json_encode($predefinedForm['form']); |
| 75 |
|
| 76 |
$this->defaultSettings = $predefinedForm['formSettings']; |
| 77 |
|
| 78 |
$this->store(); |
| 79 |
} |
| 80 |
|
| 81 |
wp_send_json_error([ |
| 82 |
'message' => __("The selected template couldn't be found.", 'fluentform') |
| 83 |
], 423); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|