| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class Select extends BaseComponent |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Compile and echo the html element |
| 12 |
* @param array $data [element data] |
| 13 |
* @param stdClass $form [Form Object] |
| 14 |
* @return viod |
| 15 |
*/ |
| 16 |
public function compile($data, $form) |
| 17 |
{ |
| 18 |
$elementName = $data['element']; |
| 19 |
$data = apply_filters('fluenform_rendering_field_data_'.$elementName, $data, $form); |
| 20 |
|
| 21 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 22 |
|
| 23 |
if (ArrayHelper::get($data['attributes'], 'multiple')) { |
| 24 |
$data['attributes']['name'] = $data['attributes']['name'].'[]'; |
| 25 |
wp_enqueue_script('selectWoo'); |
| 26 |
wp_enqueue_style('select2'); |
| 27 |
$data['attributes']['class'] .= ' ff_has_multi_select'; |
| 28 |
} |
| 29 |
|
| 30 |
$data['attributes']['data-calc_value'] = 0; |
| 31 |
|
| 32 |
|
| 33 |
if(!isset($data['attributes']['class'])) { |
| 34 |
$data['attributes']['class'] = ''; |
| 35 |
} |
| 36 |
|
| 37 |
$data['attributes']['class'] = trim('ff-el-form-control ' . $data['attributes']['class']); |
| 38 |
|
| 39 |
if($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { |
| 40 |
$data['attributes']['tabindex'] = $tabIndex; |
| 41 |
} |
| 42 |
|
| 43 |
$defaultValues = (array) $this->extractValueFromAttributes($data); |
| 44 |
|
| 45 |
$elMarkup = "<select ".$this->buildAttributes($data['attributes']).">".$this->buildOptions($data, $defaultValues)."</select>"; |
| 46 |
|
| 47 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 48 |
echo apply_filters('fluenform_rendering_field_html_'.$elementName, $html, $data, $form); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Build options for select |
| 53 |
* @param array $options |
| 54 |
* @return string/html [compiled options] |
| 55 |
*/ |
| 56 |
protected function buildOptions($data, $defaultValues) |
| 57 |
{ |
| 58 |
if(!$formattedOptions = ArrayHelper::get($data, 'settings.advanced_options')) { |
| 59 |
$options = ArrayHelper::get($data, 'options', []); |
| 60 |
$formattedOptions = []; |
| 61 |
foreach($options as $value => $label) { |
| 62 |
$formattedOptions[] = [ |
| 63 |
'label' => $label, |
| 64 |
'value' => $value, |
| 65 |
'calc_value' => '' |
| 66 |
]; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$opts = ''; |
| 71 |
if (!empty($data['settings']['placeholder'])) { |
| 72 |
$opts .= '<option value="">'.$data['settings']['placeholder'].'</option>'; |
| 73 |
} else if(!empty($data['attributes']['placeholder'])) { |
| 74 |
$opts .= '<option value="">'.$data['attributes']['placeholder'].'</option>'; |
| 75 |
} |
| 76 |
|
| 77 |
foreach ($formattedOptions as $option) { |
| 78 |
if(in_array($option['value'], $defaultValues)) { |
| 79 |
$selected = 'selected'; |
| 80 |
} else { |
| 81 |
$selected = ''; |
| 82 |
} |
| 83 |
|
| 84 |
$atts = [ |
| 85 |
'data-calc_value' => ArrayHelper::get($option, 'calc_value'), |
| 86 |
'value' => ArrayHelper::get($option, 'value'), |
| 87 |
]; |
| 88 |
|
| 89 |
$opts .="<option ".$this->buildAttributes($atts)." {$selected}>{$option['label']}</option>"; |
| 90 |
} |
| 91 |
return $opts; |
| 92 |
} |
| 93 |
} |
| 94 |
|