| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Component; |
| 4 |
|
| 5 |
trait ComponentInitTrait |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Initialize the component and register the |
| 9 |
* component by registering actions/filters. |
| 10 |
*/ |
| 11 |
public function _init() |
| 12 |
{ |
| 13 |
// Validate certain important required properties |
| 14 |
$this->_fluentFormValidateComponent(); |
| 15 |
|
| 16 |
// Hook to add component in the editor's components array |
| 17 |
add_filter('fluent_editor_components', [$this, '_fluentEditorComponenstCallback']); |
| 18 |
|
| 19 |
// Hook to add search keywords for the component |
| 20 |
add_filter('fluent_editor_element_search_tags', [$this, '_fluentEditorElementSearchTagsCallback']); |
| 21 |
|
| 22 |
// Hook for component's editor items/options placement settings |
| 23 |
add_filter('fluent_editor_element_settings_placement', [$this, '_fluentEditorElementSettingsPlacementCallback']); |
| 24 |
|
| 25 |
// Hook for component's customization settings |
| 26 |
add_filter('fluent_editor_element_customization_settings', [$this, '_fluentEditorElementCustomizationSettingsCallback']); |
| 27 |
|
| 28 |
// Hook for response data preperation on form submission |
| 29 |
add_filter('fluentform_insert_response_data', [$this, '_fluentformInsertResponseDataCallback'], 10, 3); |
| 30 |
|
| 31 |
// Hook to add component type in fluentform field types to be available in FormFieldParser. |
| 32 |
add_filter('fluentform_form_input_types', [$this, '_fluentformFormInputTypesCallback']); |
| 33 |
|
| 34 |
// Component render/compile hook (for form) |
| 35 |
add_action('fluentform_render_item_' . $this->element(), [$this, '_elementRenderHookCallback'], 10, 2); |
| 36 |
|
| 37 |
// Component's used element response transformation hook (for entries) |
| 38 |
add_filter('fluentform_response_render_' . $this->element(), [$this, '_elementEntryFormatCallback'], 10, 3); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Validate certain required properties |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
private function _fluentFormValidateComponent() |
| 47 |
{ |
| 48 |
$name = $this->name(); |
| 49 |
if (!$name || !is_string($name)) { |
| 50 |
wp_die('The name must be a valid string.'); |
| 51 |
} |
| 52 |
|
| 53 |
$label = $this->label(); |
| 54 |
if (!$label || !is_string($label)) { |
| 55 |
wp_die('The label must be a valid string.'); |
| 56 |
} |
| 57 |
|
| 58 |
$element = $this->element(); |
| 59 |
if (!$element || !is_string($element)) { |
| 60 |
$elements = 'text_input, text_email, textarea'; |
| 61 |
wp_die("The element must be one of the available elements, i.e: $elements e.t.c."); |
| 62 |
} |
| 63 |
|
| 64 |
$template = $this->template(); |
| 65 |
if (!$template || !is_string($template)) { |
| 66 |
$templates = 'inputText, selectCountry. addressFields'; |
| 67 |
wp_die("The template must be one of the available templates, i.e: $templates e.t.c."); |
| 68 |
} |
| 69 |
|
| 70 |
$group = $this->group(); |
| 71 |
$groups = ['general', 'advanced', 'container']; |
| 72 |
if (!$group || !in_array($group, $groups)) { |
| 73 |
wp_die('Invalid group, available groups: ' . implode(', ', $groups) . '.'); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Add the component in fluentform editor's components array. |
| 79 |
* |
| 80 |
* @param array $components |
| 81 |
* @return array $components |
| 82 |
*/ |
| 83 |
public function _fluentEditorComponenstCallback($components) |
| 84 |
{ |
| 85 |
$name = $this->name(); |
| 86 |
$label = $this->label(); |
| 87 |
$group = $this->group(); |
| 88 |
$index = $this->index(); |
| 89 |
$element = $this->element(); |
| 90 |
$template = $this->template(); |
| 91 |
$iconClass = $this->elementIconClass(); |
| 92 |
$validationRules = $this->validationRules(); |
| 93 |
|
| 94 |
$settings = $this->settings([ |
| 95 |
'label' => $label, |
| 96 |
'container_class' => '', |
| 97 |
'label_placement' => '', |
| 98 |
'help_message' => '', |
| 99 |
'admin_field_label' => $label, |
| 100 |
'conditional_logics' => [], |
| 101 |
'validation_rules' => $validationRules |
| 102 |
]); |
| 103 |
|
| 104 |
$attributes = $this->attributes([ |
| 105 |
'id' => "", |
| 106 |
'name' => $name, |
| 107 |
'class' => '', |
| 108 |
'value' => '', |
| 109 |
'placeholder' => '', |
| 110 |
]); |
| 111 |
|
| 112 |
$editorOptions = $this->options([ |
| 113 |
'title' => $label, |
| 114 |
'template' => $template, |
| 115 |
'icon_class' => $iconClass, |
| 116 |
]); |
| 117 |
|
| 118 |
if (is_null($index)) { |
| 119 |
$index = count($components[$group]) + 1; |
| 120 |
} |
| 121 |
|
| 122 |
$components[$group][] = [ |
| 123 |
'index' => $index, |
| 124 |
'element' => $element, |
| 125 |
'settings' => $settings, |
| 126 |
'attributes' => $attributes, |
| 127 |
'editor_options' => $editorOptions, |
| 128 |
]; |
| 129 |
|
| 130 |
return $components; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Add the keywords for the component to search in the editor. |
| 135 |
* |
| 136 |
* @param array $keywords |
| 137 |
* @return array $keywords |
| 138 |
*/ |
| 139 |
public function _fluentEditorElementSearchTagsCallback($keywords) |
| 140 |
{ |
| 141 |
$newKeywords = $this->searchBy(); |
| 142 |
|
| 143 |
if (!$newKeywords) { |
| 144 |
return $keywords; |
| 145 |
} |
| 146 |
|
| 147 |
$existingKeywords = []; |
| 148 |
$element = $this->element(); |
| 149 |
if (isset($keywords[$element])) { |
| 150 |
$existingKeywords = $keywords[$element]; |
| 151 |
} |
| 152 |
|
| 153 |
$keywords[$element] = array_merge( |
| 154 |
$existingKeywords, $newKeywords |
| 155 |
); |
| 156 |
|
| 157 |
return $keywords; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Configure placements of input customization options in editor. |
| 162 |
* |
| 163 |
* @param array $placemenSettings |
| 164 |
* @return array $placemenSettings |
| 165 |
*/ |
| 166 |
public function _fluentEditorElementSettingsPlacementCallback($placementSettings) |
| 167 |
{ |
| 168 |
$default = [ |
| 169 |
'general' => [ |
| 170 |
'label', |
| 171 |
'label_placement', |
| 172 |
'admin_field_label', |
| 173 |
'placeholder', |
| 174 |
'value', |
| 175 |
'validation_rules', |
| 176 |
], |
| 177 |
'advanced' => [ |
| 178 |
'name', |
| 179 |
'class', |
| 180 |
'help_message', |
| 181 |
'container_class', |
| 182 |
'conditional_logics', |
| 183 |
], |
| 184 |
]; |
| 185 |
|
| 186 |
$placementSettings[$this->element()] = array_merge_recursive( |
| 187 |
$this->placementSettings($default), $placementSettings |
| 188 |
); |
| 189 |
|
| 190 |
return $placementSettings; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Configure input customization options/items in the editor. |
| 195 |
* |
| 196 |
* @param array $customizationSettings |
| 197 |
* @return array $customizationSettings |
| 198 |
*/ |
| 199 |
public function _fluentEditorElementCustomizationSettingsCallback($customizationSettings) |
| 200 |
{ |
| 201 |
return $this->customizationSettings($customizationSettings); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Prepare the submission data for the element on Form Submission. |
| 206 |
* |
| 207 |
* @param array $formData |
| 208 |
* @param int $formId |
| 209 |
* @param array $inputConfigs |
| 210 |
* @return array $formData |
| 211 |
*/ |
| 212 |
public function _fluentformInsertResponseDataCallback($formData, $formId, $inputConfigs) |
| 213 |
{ |
| 214 |
return $this->onSubmit($formData, $formId, $inputConfigs); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Add the component type in fluentform field |
| 219 |
* types to be available in FormFieldParser. |
| 220 |
* |
| 221 |
* @param array $types |
| 222 |
* @return array $types |
| 223 |
*/ |
| 224 |
public function _fluentformFormInputTypesCallback($types) |
| 225 |
{ |
| 226 |
return $this->addType($types); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Render the component. |
| 231 |
* |
| 232 |
* @param array $data |
| 233 |
* @param stdClass $form |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
public function _elementRenderHookCallback($item, $form) |
| 237 |
{ |
| 238 |
$this->render($item, $form); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Element's entry value transformation. |
| 243 |
* |
| 244 |
* @param mixed $value |
| 245 |
* @param string $field |
| 246 |
* @param int $formId |
| 247 |
* @return mixed $value |
| 248 |
*/ |
| 249 |
public function _elementEntryFormatCallback($value, $field, $formId) |
| 250 |
{ |
| 251 |
return $this->formatEntryValue($value, $field, $formId); |
| 252 |
} |
| 253 |
} |
| 254 |
|