| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Parser; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Str; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper as Arr; |
| 7 |
|
| 8 |
class Form |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var \stdClass $form |
| 12 |
*/ |
| 13 |
protected $form; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var array $inputTypes |
| 17 |
*/ |
| 18 |
protected $inputTypes; |
| 19 |
|
| 20 |
/** |
| 21 |
* The parsed form fields. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $parsed; |
| 26 |
|
| 27 |
/** |
| 28 |
* The parsed validations |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $validations; |
| 33 |
|
| 34 |
/** |
| 35 |
* Form Parser constructor. |
| 36 |
* |
| 37 |
* @param \stdClass $form |
| 38 |
*/ |
| 39 |
public function __construct($form) |
| 40 |
{ |
| 41 |
$this->form = $form; |
| 42 |
|
| 43 |
$this->setInputTypes(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Set input types of the form. |
| 48 |
* |
| 49 |
* @param array $types |
| 50 |
* @return \FluentForm\App\Services\Parser\Form $this |
| 51 |
*/ |
| 52 |
public function setInputTypes($types = []) |
| 53 |
{ |
| 54 |
// If the $types is empty we'll use the default input types. |
| 55 |
$types = $types ?: [ |
| 56 |
'input_text', |
| 57 |
'input_name', |
| 58 |
'textarea', |
| 59 |
'select', |
| 60 |
'input_radio', |
| 61 |
'input_checkbox', |
| 62 |
'input_email', |
| 63 |
'input_url', |
| 64 |
'input_password', |
| 65 |
'input_file', |
| 66 |
'input_image', |
| 67 |
'input_date', |
| 68 |
'select_country', |
| 69 |
'input_number', |
| 70 |
'input_repeat', |
| 71 |
'address', |
| 72 |
'terms_and_condition', |
| 73 |
'input_hidden', |
| 74 |
'ratings', |
| 75 |
'net_promoter', |
| 76 |
'tabular_grid', |
| 77 |
'gdpr_agreement', |
| 78 |
'taxonomy' |
| 79 |
]; |
| 80 |
|
| 81 |
// Firing an event so that others can hook into it and add other input types. |
| 82 |
$this->inputTypes = apply_filters('fluentform_form_input_types', $types); |
| 83 |
|
| 84 |
return $this; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get form fields. |
| 89 |
* |
| 90 |
* @param boolean $asArray |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function getFields($asArray = false) |
| 94 |
{ |
| 95 |
$fields = json_decode($this->form->form_fields, $asArray); |
| 96 |
|
| 97 |
$default = $asArray ? [] : null; |
| 98 |
|
| 99 |
return Arr::get((array)$fields, 'fields', $default); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get flatten form inputs. Flatten implies that all |
| 104 |
* of the form fields will be in a simple array. |
| 105 |
* |
| 106 |
* @param array $with |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function getInputs($with = []) |
| 110 |
{ |
| 111 |
// If the form is already parsed we'll return it. Otherwise, |
| 112 |
// we'll parse the form and return the data after saving it. |
| 113 |
if (!$this->parsed) { |
| 114 |
$fields = $this->getFields(true); |
| 115 |
|
| 116 |
$with = $with ?: ['admin_label', 'element', 'options', 'attributes', 'raw']; |
| 117 |
$this->parsed = (new Extractor($fields, $with, $this->inputTypes))->extract(); |
| 118 |
} |
| 119 |
|
| 120 |
return $this->parsed; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get the inputs just as they setup in the form editor. |
| 125 |
* e.g. `names` as `names` not with the child fields. |
| 126 |
* |
| 127 |
* @param array $with |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
public function getEntryInputs($with = ['admin_label']) |
| 131 |
{ |
| 132 |
$inputs = $this->getInputs($with); |
| 133 |
|
| 134 |
// The inputs that has `[]` in their keys are custom fields |
| 135 |
// & for the purpose of this scenario we'll remove those. |
| 136 |
foreach ($inputs as $key => $value) { |
| 137 |
if (Str::contains($key, '[')) { |
| 138 |
unset($inputs[$key]); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $inputs; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get the flatten inputs as the result of the `getInputs` |
| 147 |
* method but replace the keys those have `[]` with `.` |
| 148 |
* And also remove the repeat fields' child fields. |
| 149 |
* |
| 150 |
* @param array $with |
| 151 |
* @param array |
| 152 |
*/ |
| 153 |
public function getShortCodeInputs($with = ['admin_label']) |
| 154 |
{ |
| 155 |
$inputs = $this->getInputs($with); |
| 156 |
|
| 157 |
$result = []; |
| 158 |
|
| 159 |
// For the purpose of this scenario we'll rename |
| 160 |
// the keys that have `[]` in 'em to `.` and |
| 161 |
// remove the keys that have `*` in 'em. |
| 162 |
foreach ($inputs as $key => $value) { |
| 163 |
if (Str::contains($key, '*')) { |
| 164 |
unset($inputs[$key]); |
| 165 |
} else { |
| 166 |
$key = str_replace(['[', ']'], ['.'], $key); |
| 167 |
|
| 168 |
$result[$key] = $value; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $result; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get admin labels of the form fields. |
| 177 |
* |
| 178 |
* @param array $fields |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public function getAdminLabels($fields = []) |
| 182 |
{ |
| 183 |
$fields = $fields ?: $this->getInputs(['admin_label']); |
| 184 |
|
| 185 |
$labels = []; |
| 186 |
|
| 187 |
foreach ($fields as $key => $field) { |
| 188 |
$labels[$key] = Arr::get($field, 'admin_label'); |
| 189 |
} |
| 190 |
|
| 191 |
return $labels; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get admin labels of the form fields. |
| 196 |
* |
| 197 |
* @param array $inputs |
| 198 |
* @param array $fields |
| 199 |
* @return array |
| 200 |
*/ |
| 201 |
public function getValidations($inputs, $fields = []) |
| 202 |
{ |
| 203 |
// If the form validations are already parsed we'll return it. |
| 204 |
// Otherwise, we'll parse the form validation and return |
| 205 |
// the data after saving it to the validations array. |
| 206 |
if (!$this->validations) { |
| 207 |
$fields = $fields ?: $this->getInputs(['rules']); |
| 208 |
$this->validations = (new Validations($fields, $inputs))->get(); |
| 209 |
} |
| 210 |
|
| 211 |
return $this->validations; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Get an element by it's name. |
| 216 |
* |
| 217 |
* @param string|array $name |
| 218 |
* @param array $with |
| 219 |
* @return array |
| 220 |
*/ |
| 221 |
public function getElement($name, $with = []) |
| 222 |
{ |
| 223 |
$this->inputTypes = (array) $name; |
| 224 |
|
| 225 |
return $this->getInputs($with); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Determine whether the form has an element. |
| 230 |
* |
| 231 |
* @param string $name |
| 232 |
* @return bool |
| 233 |
*/ |
| 234 |
public function hasElement($name) |
| 235 |
{ |
| 236 |
$elements = $this->getElement($name, ['element']); |
| 237 |
|
| 238 |
foreach ($elements as $item) { |
| 239 |
if ($item['element'] === $name) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Determine whether the form has any required fields. |
| 249 |
* |
| 250 |
* @param array $fields |
| 251 |
* @return bool |
| 252 |
*/ |
| 253 |
public function hasRequiredFields($fields = []) |
| 254 |
{ |
| 255 |
// $fields can be user provided when called this method or, |
| 256 |
// the current object could have already parsed fields or, |
| 257 |
// we should parse the form and use the processed result. |
| 258 |
$fields = $fields ?: $this->parsed ?: $this->getInputs(['rules']); |
| 259 |
|
| 260 |
$exist = false; |
| 261 |
|
| 262 |
foreach ($fields as $field) { |
| 263 |
$exist = Arr::get($field, 'rules.required.value'); |
| 264 |
|
| 265 |
if ($exist) { |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return (boolean)$exist; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* Get Payment Related Fields |
| 276 |
* |
| 277 |
* @param array $with array |
| 278 |
* @return array |
| 279 |
*/ |
| 280 |
public function getPaymentFields($with = ['element']) |
| 281 |
{ |
| 282 |
$fields = $this->getInputs($with); |
| 283 |
$paymentElements = apply_filters('fluentform_form_payment_fields', |
| 284 |
[ |
| 285 |
'custom_payment_component', |
| 286 |
'multi_payment_component', |
| 287 |
'payment_method', |
| 288 |
'item_quantity_component', |
| 289 |
'payment_coupon', |
| 290 |
'subscription_payment_component' |
| 291 |
]); |
| 292 |
|
| 293 |
return array_filter($fields, function ($field) use ($paymentElements) { |
| 294 |
return in_array($field['element'], $paymentElements); |
| 295 |
}); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Get Payment Input Fields |
| 300 |
* |
| 301 |
* @return array |
| 302 |
*/ |
| 303 |
public function getPaymentInputFields($with = ['element']) |
| 304 |
{ |
| 305 |
$fields = $this->getInputs($with); |
| 306 |
|
| 307 |
$paymentElements = apply_filters('fluentform_form_payment_inputs', [ |
| 308 |
'custom_payment_component', |
| 309 |
'multi_payment_component' |
| 310 |
]); |
| 311 |
|
| 312 |
return array_filter($fields, function ($field) use ($paymentElements) { |
| 313 |
return in_array($field['element'], $paymentElements); |
| 314 |
}); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Determine whether the form has payment elements |
| 319 |
* |
| 320 |
* @return bool |
| 321 |
*/ |
| 322 |
public function hasPaymentFields() |
| 323 |
{ |
| 324 |
$fields = $this->getInputs(['element']); |
| 325 |
|
| 326 |
$paymentElements = apply_filters('fluentform_form_payment_fields', |
| 327 |
[ |
| 328 |
'custom_payment_component', |
| 329 |
'multi_payment_component', |
| 330 |
'payment_method', |
| 331 |
'item_quantity_component', |
| 332 |
'payment_coupon', |
| 333 |
'subscription_payment_component' |
| 334 |
]); |
| 335 |
|
| 336 |
foreach ($fields as $field) { |
| 337 |
if (in_array($field['element'], $paymentElements)) { |
| 338 |
return true; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
return false; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
/** |
| 347 |
* Get an specific field for an element type. |
| 348 |
* |
| 349 |
* @param $element |
| 350 |
* @param $attribute |
| 351 |
* @param array $with |
| 352 |
* @return array|null |
| 353 |
*/ |
| 354 |
public function getField($element, $attribute, $with = []) |
| 355 |
{ |
| 356 |
$element = $this->getElement($element, $with); |
| 357 |
|
| 358 |
return array_intersect_key($element, array_flip((array)$attribute)); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
/** |
| 363 |
* Get Payment Input Fields |
| 364 |
* |
| 365 |
* @return array |
| 366 |
*/ |
| 367 |
public function getAttachmentInputFields($with = ['element']) |
| 368 |
{ |
| 369 |
$fields = $this->getInputs($with); |
| 370 |
$paymentElements = [ |
| 371 |
'input_file', |
| 372 |
'input_image', |
| 373 |
'featured_image', |
| 374 |
'signature' |
| 375 |
]; |
| 376 |
|
| 377 |
return array_filter($fields, function ($field) use ($paymentElements) { |
| 378 |
return in_array($field['element'], $paymentElements); |
| 379 |
}); |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Get Any Field Type |
| 384 |
* @return array |
| 385 |
*/ |
| 386 |
|
| 387 |
public function getInputsByElementTypes($types, $with = ['element']) |
| 388 |
{ |
| 389 |
$fields = $this->getInputs($with); |
| 390 |
|
| 391 |
return array_filter($fields, function ($field) use ($types) { |
| 392 |
return in_array($field['element'], $types); |
| 393 |
}); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Get Address Fields |
| 398 |
* |
| 399 |
* @return array |
| 400 |
*/ |
| 401 |
public function getAddressFields($with = ['admin_label', 'attributes']) |
| 402 |
{ |
| 403 |
$fields = $this->getInputs($with); |
| 404 |
$addressElements = [ |
| 405 |
'address' |
| 406 |
]; |
| 407 |
|
| 408 |
return array_filter($fields, function ($field) use ($addressElements) { |
| 409 |
return in_array($field['element'], $addressElements); |
| 410 |
}); |
| 411 |
} |
| 412 |
} |
| 413 |
|