customs
1 year ago
holders
1 year ago
checkbox.php
1 year ago
color-and-opacity.php
1 year ago
color.php
1 year ago
datepicker-range.php
1 year ago
dropdown-and-colors.php
1 year ago
dropdown.php
1 year ago
font.php
1 year ago
google-font.php
1 year ago
gradient.php
1 year ago
hidden.php
1 year ago
index.php
3 years ago
integer.php
1 year ago
list.php
1 year ago
multiple-textbox.php
1 year ago
paddings-editor.php
1 year ago
pattern.php
1 year ago
radio-colors.php
1 year ago
radio.php
1 year ago
textarea.php
1 year ago
textbox.php
1 year ago
url.php
1 year ago
wp-editor.php
1 year ago
list.php
230 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Multiselect List Control |
| 5 | * |
| 6 | * Main options: |
| 7 | * name => a name of the control |
| 8 | * value => a value to show in the control |
| 9 | * default => a default value of the control if the "value" option is not specified |
| 10 | * items => a callback to return items or an array of items to select |
| 11 | * |
| 12 | * @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 13 | * @copyright (c) 2018, Webcraftic Ltd |
| 14 | * |
| 15 | * @package factory-forms |
| 16 | * @since 1.0.0 |
| 17 | */ |
| 18 | |
| 19 | // Exit if accessed directly |
| 20 | if( !defined('ABSPATH') ) { |
| 21 | exit; |
| 22 | } |
| 23 | |
| 24 | if( !class_exists('Wbcr_FactoryForms480_ListControl') ) { |
| 25 | |
| 26 | class Wbcr_FactoryForms480_ListControl extends Wbcr_FactoryForms480_Control { |
| 27 | |
| 28 | public $type = 'list'; |
| 29 | |
| 30 | /** |
| 31 | * Returns a set of available items for the list. |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | * @return mixed[] |
| 35 | */ |
| 36 | private function getItems() |
| 37 | { |
| 38 | |
| 39 | $data = $this->getOption('data', array()); |
| 40 | |
| 41 | // if the data options is a valid callback for an object method |
| 42 | if( (is_array($data) && count($data) == 2 && is_object($data[0])) || is_string($data) ) { |
| 43 | |
| 44 | return call_user_func($data); |
| 45 | } |
| 46 | |
| 47 | // if the data options is an array of values |
| 48 | return $data; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns true, if the data should be loaded via ajax. |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | * @return bool |
| 56 | */ |
| 57 | protected function isAjax() |
| 58 | { |
| 59 | |
| 60 | $data = $this->getOption('data', array()); |
| 61 | |
| 62 | return is_array($data) && isset($data['ajax']); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Shows the html markup of the control. |
| 67 | * |
| 68 | * @since 1.0.0 |
| 69 | * @return void |
| 70 | */ |
| 71 | public function html() |
| 72 | { |
| 73 | |
| 74 | $way = $this->getOption('way', 'default'); |
| 75 | $this->addHtmlData('way', $way); |
| 76 | |
| 77 | if( $this->isAjax() ) { |
| 78 | |
| 79 | $data = $this->getOption('data', array()); |
| 80 | $ajax_id = 'factory-list-' . rand(1000000, 9999999); |
| 81 | |
| 82 | $value = $this->getValue(null, true); |
| 83 | |
| 84 | if( empty($value) || empty($value[0]) ) { |
| 85 | $value = array(); |
| 86 | } |
| 87 | |
| 88 | ?> |
| 89 | <div class="factory-ajax-loader <?php echo $ajax_id . '-loader'; ?>"></div> |
| 90 | <script> |
| 91 | window['<?php echo esc_attr($ajax_id); ?>'] = { |
| 92 | 'loader': '.<?php echo esc_attr($ajax_id) . '-loader' ?>', |
| 93 | 'url': '<?php echo esc_url($data['url']); ?>', |
| 94 | 'data': <?php echo json_encode( $data['data'] ) ?>, |
| 95 | 'selected': <?php echo json_encode( $value ) ?>, |
| 96 | 'emptyList': '<?php echo esc_html($this->getOption('empty', __('The list is empty.', 'wbcr_factory_forms_480') )); ?>' |
| 97 | }; |
| 98 | </script> |
| 99 | <?php |
| 100 | |
| 101 | $this->addHtmlData('ajax', true); |
| 102 | $this->addHtmlData('ajax-data-id', $ajax_id); |
| 103 | $this->addCssClass('factory-hidden'); |
| 104 | } |
| 105 | |
| 106 | if( 'checklist' == $way ) { |
| 107 | $this->checklistHtml(); |
| 108 | } else { |
| 109 | $this->defaultHtml(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Shows the Buttons Dropdown. |
| 115 | * |
| 116 | * @since 1.0.0 |
| 117 | * @return void |
| 118 | */ |
| 119 | protected function checklistHtml() |
| 120 | { |
| 121 | $items = $this->getItems(); |
| 122 | |
| 123 | $value = explode(',', $this->getValue()); |
| 124 | if( empty($value) || empty($value[0]) ) { |
| 125 | $value = array(); |
| 126 | } |
| 127 | |
| 128 | $name_on_form = $this->getNameOnForm(); |
| 129 | |
| 130 | $this->addCssClass('factory-checklist-way'); |
| 131 | $this->addHtmlData('name', $name_on_form); |
| 132 | |
| 133 | $errors_callback = $this->getOption('errors'); |
| 134 | $errors = !empty($errors_callback) |
| 135 | ? call_user_func($errors_callback) |
| 136 | : array(); |
| 137 | |
| 138 | $is_empty = $this->isAjax() || empty($items); |
| 139 | $emptyList = $this->getOption('empty', __('The list is empty.', 'wbcr_factory_forms_480')); |
| 140 | |
| 141 | if( $is_empty ) { |
| 142 | $this->addCssClass('factory-empty'); |
| 143 | } |
| 144 | |
| 145 | ?> |
| 146 | <ul <?php $this->attrs() ?>> |
| 147 | <?php if( $is_empty ) { ?> |
| 148 | <li><?php echo esc_html($emptyList); ?></li> |
| 149 | <?php } else { ?> |
| 150 | <?php foreach($items as $item) { ?> |
| 151 | <li> |
| 152 | <label for="factory-checklist-<?php echo esc_attr($name_on_form); ?>-<?php echo esc_attr($item[0]); ?>" class="<?php if( !empty($errors[$item[0]]) ) { |
| 153 | echo 'factory-has-error'; |
| 154 | } ?>"> |
| 155 | <?php if( !empty($errors[$item[0]]) ) { ?> |
| 156 | <span class="factory-error"> |
| 157 | <i class="fa fa-exclamation-triangle"></i> |
| 158 | <div class='factory-error-text'><?php echo esc_html($errors[$item[0]]); ?></div> |
| 159 | </span> |
| 160 | <?php } else { ?> |
| 161 | <span> |
| 162 | <input |
| 163 | type="checkbox" |
| 164 | name="<?php echo esc_attr($name_on_form); ?>[]" |
| 165 | value="<?php echo esc_attr($item[0]); ?>" |
| 166 | id="factory-checklist-<?php echo esc_attr($name_on_form); ?>-<?php echo esc_attr($item[0]); ?>" |
| 167 | <?php if( in_array($item[0], $value) ) { |
| 168 | echo 'checked="checked"'; |
| 169 | } ?> /> |
| 170 | </span> |
| 171 | <?php } ?> |
| 172 | |
| 173 | <span><?php echo esc_html($item[1]); ?></span> |
| 174 | </label> |
| 175 | </li> |
| 176 | <?php } ?> |
| 177 | <?php } ?> |
| 178 | </ul> |
| 179 | <?php |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Shows the standart dropdown. |
| 184 | * |
| 185 | * @since 1.3.1 |
| 186 | * @return void |
| 187 | */ |
| 188 | protected function defaultHtml() |
| 189 | { |
| 190 | |
| 191 | $items = $this->getItems(); |
| 192 | $value = $this->getValue(); |
| 193 | |
| 194 | $name_on_form = $this->getNameOnForm(); |
| 195 | |
| 196 | $this->addHtmlAttr('id', $name_on_form); |
| 197 | $this->addHtmlAttr('name', $name_on_form); |
| 198 | $this->addCssClass('form-control'); |
| 199 | |
| 200 | ?> |
| 201 | <select multiple="multiple" <?php $this->attrs() ?>/> |
| 202 | <?php foreach($items as $item) { |
| 203 | if( count($item) == 3 ) { |
| 204 | ?> |
| 205 | <optgroup label="<?php echo esc_attr($item[1]); ?>"> |
| 206 | <?php foreach($item[2] as $subitem) { ?> |
| 207 | <?php $selected = ($subitem[0] == $value) |
| 208 | ? 'selected="selected"' |
| 209 | : ''; ?> |
| 210 | <option value='<?php echo esc_attr($subitem[0]); ?>' <?php echo $selected ?>> |
| 211 | <?php echo esc_html($subitem[1]); ?> |
| 212 | </option> |
| 213 | <?php } ?> |
| 214 | </optgroup> |
| 215 | <?php |
| 216 | } else { |
| 217 | $selected = ($item[0] == $value) |
| 218 | ? 'selected="selected"' |
| 219 | : ''; |
| 220 | ?> |
| 221 | <option value='<?php echo esc_attr($item[0]); ?>' <?php echo $selected ?>> |
| 222 | <?php echo esc_html($item[1]); ?> |
| 223 | </option> |
| 224 | <?php } ?> |
| 225 | <?php } ?> |
| 226 | </select> |
| 227 | <?php |
| 228 | } |
| 229 | } |
| 230 | } |