2020-05-29 13:41:58 +02:00
|
|
|
<?php
|
2021-10-25 16:20:36 +02:00
|
|
|
/*
|
2020-05-29 13:41:58 +02:00
|
|
|
* Generic form builder
|
|
|
|
*
|
|
|
|
* Simply pass a JSON with the following keys set:
|
|
|
|
* - model: The model used to create the form (such as Attribute, Event)
|
|
|
|
* - fields: an array with each element generating an input field
|
|
|
|
* - field is the actual field name (such as org_id, name, etc) which is required
|
|
|
|
* - optional fields: default, type, options, placeholder, label - these are passed directly to $this->Form->input(),
|
|
|
|
* - requirements: boolean, if false is passed the field is skipped
|
|
|
|
* - metafields: fields that are outside of the scope of the form itself
|
|
|
|
- use these to define dynamic form fields, or anything that will feed into the regular fields via JS population
|
|
|
|
* - submit: The submit button itself. By default it will simply submit to the form as defined via the 'model' field
|
|
|
|
*/
|
2020-12-15 10:40:49 +01:00
|
|
|
$this->Form->setConfig('errorClass', 'is-invalid');
|
2020-05-29 13:41:58 +02:00
|
|
|
$modelForForm = empty($data['model']) ?
|
|
|
|
h(\Cake\Utility\Inflector::singularize(\Cake\Utility\Inflector::classify($this->request->getParam('controller')))) :
|
|
|
|
h($data['model']);
|
2020-11-25 16:45:55 +01:00
|
|
|
$entity = isset($entity) ? $entity : null;
|
2020-05-29 13:41:58 +02:00
|
|
|
$fieldsString = '';
|
2020-09-28 01:25:07 +02:00
|
|
|
$simpleFieldWhitelist = [
|
2023-02-24 10:03:45 +01:00
|
|
|
'default', 'type', 'placeholder', 'label', 'empty', 'rows', 'div', 'required', 'templates', 'options', 'value', 'checked',
|
2020-09-28 01:25:07 +02:00
|
|
|
];
|
2020-05-29 13:41:58 +02:00
|
|
|
if (empty($data['url'])) {
|
|
|
|
$data['url'] = ["controller" => $this->request->getParam('controller'), "action" => $this->request->getParam('url')];
|
|
|
|
}
|
|
|
|
$formRandomValue = Cake\Utility\Security::randomString(8);
|
2021-10-25 16:20:36 +02:00
|
|
|
$initSelect2 = false;
|
2023-05-26 16:13:52 +02:00
|
|
|
|
|
|
|
if (!empty($enumerations)) {
|
|
|
|
foreach ($data['fields'] as $k => $field) {
|
|
|
|
if (isset($enumerations[$field['field']])) {
|
|
|
|
$data['fields'][$k]['options'] = $enumerations[$field['field']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 00:43:11 +02:00
|
|
|
$formCreate = $this->Form->create($entity, ['id' => 'form-' . $formRandomValue]);
|
2020-05-29 13:41:58 +02:00
|
|
|
$default_template = [
|
2021-09-17 13:04:37 +02:00
|
|
|
'inputContainer' => '<div class="row mb-3">{{content}}</div>',
|
|
|
|
'inputContainerError' => '<div class="row mb-3 has-error">{{content}}</div>',
|
2020-05-29 13:41:58 +02:00
|
|
|
'label' => '{{text}}',
|
|
|
|
'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} />',
|
|
|
|
'textarea' => '<textarea name="{{name}}" {{attrs}}>{{value}}</textarea>',
|
|
|
|
'select' => '<select name="{{name}}" {{attrs}}>{{content}}</select>',
|
|
|
|
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
|
|
|
|
'checkboxFormGroup' => '{{label}}',
|
2021-12-08 11:08:40 +01:00
|
|
|
'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
|
|
|
|
'radioWrapper' => '{{label}}',
|
2021-11-04 10:47:03 +01:00
|
|
|
'formGroup' => '<label class="col-sm-2 col-form-label form-label" {{attrs}}>{{label}}</label><div class="col-sm-10">{{input}}{{error}}</div>',
|
2021-09-17 13:04:37 +02:00
|
|
|
'nestingLabel' => '{{hidden}}<div class="col-sm-2 form-label">{{text}}</div><div class="col-sm-10">{{input}}</div>',
|
2020-06-19 00:43:11 +02:00
|
|
|
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
|
|
|
|
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
|
2020-12-15 10:40:49 +01:00
|
|
|
'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
|
|
|
|
'error' => '<div class="error-message invalid-feedback d-block">{{content}}</div>',
|
|
|
|
'errorList' => '<ul>{{content}}</ul>',
|
|
|
|
'errorItem' => '<li>{{text}}</li>',
|
2020-05-29 13:41:58 +02:00
|
|
|
];
|
|
|
|
if (!empty($data['fields'])) {
|
|
|
|
foreach ($data['fields'] as $fieldData) {
|
2020-11-05 10:17:42 +01:00
|
|
|
if (!empty($fields)) {
|
|
|
|
if (!in_array($fieldData['field'], $fields)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 16:20:36 +02:00
|
|
|
$initSelect2 = $initSelect2 || (!empty($fieldData['type']) && $fieldData['type'] == 'dropdown' && !empty($fieldData['select2']));
|
2021-10-04 16:16:13 +02:00
|
|
|
$formTemplate = $default_template;
|
|
|
|
if (!empty($fieldData['floating-label'])) {
|
|
|
|
$formTemplate['inputContainer'] = '<div class="form-floating input {{type}}{{required}}">{{content}}</div>';
|
|
|
|
$formTemplate['label'] = '<label{{attrs}}>{{text}}</label>';
|
|
|
|
$formTemplate['formGroup'] = '{{input}}{{label}}';
|
|
|
|
$fieldData['placeholder'] = !empty($fieldData['label']) ? $fieldData['label'] : h($fieldData['field']);
|
|
|
|
}
|
2021-12-08 11:08:40 +01:00
|
|
|
if (!empty($data['templates'])) {
|
|
|
|
$formTemplate = array_merge($formTemplate, $data['templates']);
|
|
|
|
}
|
2020-05-29 13:41:58 +02:00
|
|
|
// we reset the template each iteration as individual fields might override the defaults.
|
2021-10-04 16:16:13 +02:00
|
|
|
$this->Form->setConfig($formTemplate);
|
|
|
|
$this->Form->setTemplates($formTemplate);
|
2020-05-29 13:41:58 +02:00
|
|
|
if (isset($fieldData['requirements']) && !$fieldData['requirements']) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
$fieldsString .= $this->element(
|
2021-10-25 16:20:36 +02:00
|
|
|
'genericElements/Form/fieldScaffold',
|
|
|
|
[
|
2020-05-29 13:41:58 +02:00
|
|
|
'fieldData' => $fieldData,
|
2020-09-28 01:25:07 +02:00
|
|
|
'form' => $this->Form,
|
|
|
|
'simpleFieldWhitelist' => $simpleFieldWhitelist
|
|
|
|
]
|
|
|
|
);
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 08:10:32 +01:00
|
|
|
$metaTemplateString = '';
|
2021-11-08 14:08:47 +01:00
|
|
|
if (!empty($entity['MetaTemplates']) && count($entity['MetaTemplates']) > 0) {
|
2020-12-07 16:20:20 +01:00
|
|
|
$metaTemplateString = $this->element(
|
2021-10-25 16:20:36 +02:00
|
|
|
'genericElements/Form/metaTemplateScaffold',
|
|
|
|
[
|
2020-12-07 16:20:20 +01:00
|
|
|
'form' => $this->Form,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2020-09-28 01:25:07 +02:00
|
|
|
$submitButtonData = ['model' => $modelForForm, 'formRandomValue' => $formRandomValue];
|
2020-05-29 13:41:58 +02:00
|
|
|
if (!empty($data['submit'])) {
|
|
|
|
$submitButtonData = array_merge($submitButtonData, $data['submit']);
|
|
|
|
}
|
|
|
|
if (!empty($data['ajaxSubmit'])) {
|
|
|
|
$submitButtonData['ajaxSubmit'] = $ajaxSubmit;
|
|
|
|
}
|
|
|
|
$ajaxFlashMessage = '';
|
2021-06-12 12:04:57 +02:00
|
|
|
if (!empty($ajax)) {
|
2020-05-29 13:41:58 +02:00
|
|
|
$ajaxFlashMessage = sprintf(
|
|
|
|
'<div id="flashContainer"><div id="main-view-container">%s</div></div>',
|
|
|
|
$this->Flash->render()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$formEnd = $this->Form->end();
|
2020-06-19 00:43:11 +02:00
|
|
|
$actionName = h(\Cake\Utility\Inflector::humanize($this->request->getParam('action')));
|
|
|
|
$modelName = h(\Cake\Utility\Inflector::humanize(\Cake\Utility\Inflector::singularize($this->request->getParam('controller'))));
|
2020-05-29 13:41:58 +02:00
|
|
|
if (!empty($ajax)) {
|
2021-10-25 16:20:36 +02:00
|
|
|
$seedModal = 'mseed-' . mt_rand();
|
2021-11-05 17:44:05 +01:00
|
|
|
echo $this->Bootstrap->modal([
|
2020-06-19 00:43:11 +02:00
|
|
|
'title' => empty($data['title']) ? sprintf('%s %s', $actionName, $modelName) : h($data['title']),
|
2021-11-05 17:44:05 +01:00
|
|
|
'bodyHtml' => $this->element('genericElements/Form/formLayouts/formRaw', [
|
2022-11-09 14:00:18 +01:00
|
|
|
'data' => $data,
|
2021-11-03 11:47:10 +01:00
|
|
|
'formCreate' => $formCreate,
|
|
|
|
'ajaxFlashMessage' => $ajaxFlashMessage,
|
|
|
|
'fieldsString' => $fieldsString,
|
|
|
|
'formEnd' => $formEnd,
|
|
|
|
'metaTemplateString' => $metaTemplateString,
|
|
|
|
]),
|
2021-11-05 17:44:05 +01:00
|
|
|
'size' => !empty($fieldsString) ? 'xl' : 'lg',
|
|
|
|
'type' => 'confirm',
|
|
|
|
'modalClass' => $seedModal,
|
2020-09-28 01:25:07 +02:00
|
|
|
]);
|
2021-03-15 22:47:13 +01:00
|
|
|
} else if (!empty($raw)) {
|
2021-11-03 11:47:10 +01:00
|
|
|
echo $this->element('genericElements/Form/formLayouts/formDefault', [
|
2022-11-09 14:00:18 +01:00
|
|
|
'data' => $data,
|
2021-11-04 08:10:32 +01:00
|
|
|
'actionName' => $actionName,
|
|
|
|
'modelName' => $modelName,
|
|
|
|
'submitButtonData' => $submitButtonData,
|
2021-11-03 11:47:10 +01:00
|
|
|
'formCreate' => $formCreate,
|
|
|
|
'ajaxFlashMessage' => $ajaxFlashMessage,
|
|
|
|
'fieldsString' => $fieldsString,
|
|
|
|
'formEnd' => $formEnd,
|
|
|
|
'metaTemplateString' => $metaTemplateString,
|
|
|
|
]);
|
2020-05-29 13:41:58 +02:00
|
|
|
} else {
|
2021-11-03 11:47:10 +01:00
|
|
|
echo $this->element('genericElements/Form/formLayouts/formDefault', [
|
2022-11-09 14:00:18 +01:00
|
|
|
'data' => $data,
|
2021-11-03 11:47:10 +01:00
|
|
|
'actionName' => $actionName,
|
|
|
|
'modelName' => $modelName,
|
|
|
|
'submitButtonData' => $submitButtonData,
|
|
|
|
'formCreate' => $formCreate,
|
|
|
|
'ajaxFlashMessage' => $ajaxFlashMessage,
|
|
|
|
'fieldsString' => $fieldsString,
|
|
|
|
'formEnd' => $formEnd,
|
|
|
|
'metaTemplateString' => $metaTemplateString,
|
|
|
|
]);
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|
|
|
|
?>
|
2020-06-04 10:05:45 +02:00
|
|
|
<script type="text/javascript">
|
|
|
|
$(document).ready(function() {
|
|
|
|
executeStateDependencyChecks();
|
|
|
|
$('.formDropdown').on('change', function() {
|
|
|
|
executeStateDependencyChecks('#' + this.id);
|
|
|
|
})
|
|
|
|
});
|
2021-10-25 16:20:36 +02:00
|
|
|
</script>
|