Overview

Namespaces

  • None
  • Quform
    • Traduttore_Registry

Classes

  • Quform
  • Quform_Admin_InsertForm
  • Quform_Admin_Page
  • Quform_Admin_Page_Controller
  • Quform_Admin_Page_Dashboard
  • Quform_Admin_Page_Entries
  • Quform_Admin_Page_Entries_Edit
  • Quform_Admin_Page_Entries_List
  • Quform_Admin_Page_Entries_View
  • Quform_Admin_Page_Factory
  • Quform_Admin_Page_Forms_Add
  • Quform_Admin_Page_Forms_Edit
  • Quform_Admin_Page_Forms_List
  • Quform_Admin_Page_Help
  • Quform_Admin_Page_Preview
  • Quform_Admin_Page_Settings
  • Quform_Admin_Page_Tools
  • Quform_Admin_Page_Tools_ExportEntries
  • Quform_Admin_Page_Tools_ExportForm
  • Quform_Admin_Page_Tools_Home
  • Quform_Admin_Page_Tools_ImportForm
  • Quform_Admin_Page_Tools_Migrate
  • Quform_Admin_Page_Tools_Uninstall
  • Quform_Api
  • Quform_Block
  • Quform_Builder
  • Quform_Captcha
  • Quform_ClassLoader
  • Quform_Confirmation
  • Quform_Container
  • Quform_Dashboard_Widget
  • Quform_Dispatcher
  • Quform_Element
  • Quform_Element_Captcha
  • Quform_Element_Checkbox
  • Quform_Element_Column
  • Quform_Element_Container
  • Quform_Element_Container_Iterator
  • Quform_Element_Date
  • Quform_Element_Email
  • Quform_Element_Factory
  • Quform_Element_Field
  • Quform_Element_File
  • Quform_Element_Group
  • Quform_Element_Hidden
  • Quform_Element_Honeypot
  • Quform_Element_Html
  • Quform_Element_Multi
  • Quform_Element_Multiselect
  • Quform_Element_Name
  • Quform_Element_Page
  • Quform_Element_Password
  • Quform_Element_Radio
  • Quform_Element_Recaptcha
  • Quform_Element_Row
  • Quform_Element_Select
  • Quform_Element_Submit
  • Quform_Element_Text
  • Quform_Element_Textarea
  • Quform_Element_Time
  • Quform_Entry_Controller
  • Quform_Entry_Exporter
  • Quform_Entry_List_Settings
  • Quform_Entry_List_Table
  • Quform_Entry_Processor
  • Quform_Entry_UserSearcher
  • Quform_Filter_Abstract
  • Quform_Filter_Alpha
  • Quform_Filter_AlphaNumeric
  • Quform_Filter_Digits
  • Quform_Filter_Regex
  • Quform_Filter_Static
  • Quform_Filter_StripTags
  • Quform_Filter_Trim
  • Quform_Form
  • Quform_Form_Controller
  • Quform_Form_Exporter
  • Quform_Form_Factory
  • Quform_Form_Importer
  • Quform_Form_Iterator
  • Quform_Form_List_Settings
  • Quform_Form_List_Table
  • Quform_Form_Processor
  • Quform_License
  • Quform_Migrator
  • Quform_NonceRefresher
  • Quform_Notification
  • Quform_Notification_Resender
  • Quform_Options
  • Quform_Permissions
  • Quform_Repository
  • Quform_ScriptLoader
  • Quform_Session
  • Quform_Settings
  • Quform_Shortcode
  • Quform_Themes
  • Quform_TokenReplacer
  • Quform_Toolbar
  • Quform_Translations
  • Quform_Updater
  • Quform_Upgrader
  • Quform_Uploader
  • Quform_Validator_Abstract
  • Quform_Validator_Alpha
  • Quform_Validator_AlphaNumeric
  • Quform_Validator_Array
  • Quform_Validator_Captcha
  • Quform_Validator_Date
  • Quform_Validator_Digits
  • Quform_Validator_Duplicate
  • Quform_Validator_Email
  • Quform_Validator_FileUpload
  • Quform_Validator_GreaterThan
  • Quform_Validator_Honeypot
  • Quform_Validator_Identical
  • Quform_Validator_InArray
  • Quform_Validator_Length
  • Quform_Validator_LessThan
  • Quform_Validator_Recaptcha
  • Quform_Validator_Regex
  • Quform_Validator_Required
  • Quform_Validator_Static
  • Quform_Validator_Time
  • Quform_View
  • Quform_ViewFactory
  • Quform_Widget_Form
  • Quform_Widget_Popup

Interfaces

  • Quform_Attachable
  • Quform_Element_Editable
  • Quform_Filter_Interface
  • Quform_Validator_Interface

Constants

  • Quform\Traduttore_Registry\TRANSIENT_KEY_PLUGIN
  • Quform\Traduttore_Registry\TRANSIENT_KEY_THEME

Functions

  • Quform\Traduttore_Registry\add_project
  • Quform\Traduttore_Registry\clean_translations_cache
  • Quform\Traduttore_Registry\get_available_locales
  • Quform\Traduttore_Registry\get_installed_translations
  • Quform\Traduttore_Registry\get_translations
  • Quform\Traduttore_Registry\register_clean_translations_cache
  • Quform\Traduttore_Registry\sanitize_date
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: /**
  4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
  5:  */
  6: class Quform_Element_Multiselect extends Quform_Element_Select
  7: {
  8:     /**
  9:      * @var array
 10:      */
 11:     protected $value = array();
 12: 
 13:     /**
 14:      * @var bool
 15:      */
 16:     protected $isMultiple = true;
 17: 
 18:     /**
 19:      * Prepare the dynamic default value
 20:      *
 21:      * @param   string  $value
 22:      * @return  array
 23:      */
 24:     public function prepareDynamicValue($value)
 25:     {
 26:         return Quform::isNonEmptyString($value) ? explode(',', $value) : $this->getEmptyValue();
 27:     }
 28: 
 29:     /**
 30:      * Set the value
 31:      *
 32:      * @param   array  $value
 33:      * @return  bool
 34:      */
 35:     protected function isValidValue($value)
 36:     {
 37:         if ( ! is_array($value)) {
 38:             return false;
 39:         }
 40: 
 41:         foreach ($value as $val) {
 42:             if ( ! parent::isValidValue($val)) {
 43:                 return false;
 44:             }
 45:         }
 46: 
 47:         return true;
 48:     }
 49: 
 50:     /**
 51:      * Does this element have the given value?
 52:      *
 53:      * @param   mixed    $value
 54:      * @return  boolean
 55:      */
 56:     public function hasValue($value)
 57:     {
 58:         return in_array($value, $this->getValue(), true);
 59:     }
 60: 
 61:     /**
 62:      * Get the empty value for this element
 63:      *
 64:      * @return array
 65:      */
 66:     public function getEmptyValue()
 67:     {
 68:         return array();
 69:     }
 70: 
 71:     /**
 72:      * Get the filtered value
 73:      *
 74:      * @return array
 75:      */
 76:     public function getValue()
 77:     {
 78:         $value = $this->value;
 79: 
 80:         $this->filterValueRecursive($value);
 81: 
 82:         $value = apply_filters('quform_get_value_' . $this->getIdentifier(), $value, $this, $this->getForm());
 83: 
 84:         return $value;
 85:     }
 86: 
 87:     /**
 88:      * Get the value formatted for HTML
 89:      *
 90:      * @return string
 91:      */
 92:     public function getValueHtml()
 93:     {
 94:         $value = '';
 95: 
 96:         if ( ! $this->isEmpty()) {
 97:             $ulStyle = apply_filters('quform_value_list_multiselect_ul_style', 'margin:0;padding:0;list-style:disc inside;', $this, $this->getForm());
 98:             $ulStyle = apply_filters('quform_value_list_multiselect_ul_style_' . $this->getIdentifier(), $ulStyle, $this, $this->getForm());
 99: 
100:             $liStyle = apply_filters('quform_value_list_multiselect_li_style', '', $this, $this->getForm());
101:             $liStyle = apply_filters('quform_value_list_multiselect_li_style_' . $this->getIdentifier(), $liStyle, $this, $this->getForm());
102: 
103:             $value = sprintf(
104:                 '<ul class="quform-value-list quform-value-list-multiselect"%s>',
105:                 Quform::isNonEmptyString($ulStyle) ? ' style="' . esc_attr($ulStyle) . '"' : ''
106:             );
107: 
108:             foreach ($this->getValue() as $option) {
109:                 $value .= sprintf(
110:                     '<li class="quform-value-list-item quform-value-list-item-multiselect"%s>',
111:                     Quform::isNonEmptyString($liStyle) ? ' style="' . esc_attr($liStyle) . '"' : ''
112:                 );
113: 
114:                 $value .= Quform::escape($option);
115: 
116:                 $value .= '</li>';
117:             }
118: 
119:             $value .= '</ul>';
120:         }
121: 
122:         $value = apply_filters('quform_get_value_html_' . $this->getIdentifier(), $value, $this, $this->getForm());
123: 
124:         return $value;
125:     }
126: 
127:     /**
128:      * Get the value formatted in plain text
129:      *
130:      * @param   string  $separator  The separator
131:      * @return  string
132:      */
133:     public function getValueText($separator = ', ')
134:     {
135:         $value = join($separator, $this->getValue());
136: 
137:         $value = apply_filters('quform_get_value_text_' . $this->getIdentifier(), $value, $this, $this->getForm());
138: 
139:         return $value;
140:     }
141: 
142:     /**
143:      * Get the value in storage format
144:      *
145:      * @return string
146:      */
147:     protected function getConvertedValueForStorage()
148:     {
149:         return serialize($this->getValue());
150:     }
151: 
152:     /**
153:      * Convert given the value from storage format
154:      *
155:      * @param   string  $value
156:      * @return  array
157:      */
158:     protected function convertValueFromStorage($value)
159:     {
160:         return is_serialized($value) ? unserialize($value) : $this->getEmptyValue();
161:     }
162: 
163:     /**
164:      * If the value is not an array or is an empty array it's empty
165:      *
166:      * @return bool
167:      */
168:     public function isEmpty()
169:     {
170:         return ! count($this->getValue());
171:     }
172: 
173:     /**
174:      * Get the HTML attributes for the field
175:      *
176:      * @param   array  $context
177:      * @return  array
178:      */
179:     protected function getFieldAttributes(array $context = array())
180:     {
181:         $attributes = array(
182:             'id' => $this->getUniqueId(),
183:             'name' => $this->getFullyQualifiedName(),
184:             'class' => Quform::sanitizeClass($this->getFieldClasses($context)),
185:             'multiple' => true
186:         );
187: 
188:         if ($this->config('enhancedSelectEnabled')) {
189:             $attributes['data-options'] = wp_json_encode(array(
190:                 'rtl' => $this->form->isRtl(),
191:                 'placeholder' => $this->getTranslation('enhancedSelectPlaceholder', __('Please select', 'quform')),
192:                 'noResultsFound' => $this->getTranslation('enhancedSelectNoResultsFound', __('No results found.', 'quform')),
193:             ));
194: 
195:             $attributes['style'] = 'width: 100%;';
196:         }
197: 
198:         if (Quform::isNonEmptyString($this->config('aria-labelledby')))  {
199:             $attributes['aria-labelledby'] = $this->config('aria-labelledby');
200:         }
201: 
202:         if (Quform::isNonEmptyString($this->config('sizeAttribute'))) {
203:             if ($this->config('sizeAttribute') == 'auto') {
204:                 $attributes['size'] = $this->getOptionsCount();
205:             } else {
206:                 $attributes['size'] = $this->config('sizeAttribute');
207:             }
208:         }
209: 
210:         $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context);
211:         $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context);
212: 
213:         return $attributes;
214:     }
215: 
216:     /**
217:      * Get the total number of options, including optgroup options
218:      *
219:      * @return int
220:      */
221:     protected function getOptionsCount()
222:     {
223:         $count = count($this->getOptions());
224: 
225:         foreach ($this->getOptions() as $option) {
226:             if (isset($option['options'])) {
227:                 $count += count($option['options']);
228:             }
229:         }
230: 
231:         return $count;
232:     }
233: 
234:     /**
235:      * Get the classes for the field
236:      *
237:      * @param   array  $context
238:      * @return  array
239:      */
240:     protected function getFieldClasses(array $context = array())
241:     {
242:         $classes = array(
243:             'quform-field',
244:             'quform-field-multiselect',
245:             sprintf('quform-field-%s', $this->getIdentifier())
246:         );
247: 
248:         if ($this->config('enhancedSelectEnabled')) {
249:             $classes[] = 'quform-field-multiselect-enhanced';
250:         }
251: 
252:         if (Quform::isNonEmptyString($this->config('customClass'))) {
253:             $classes[] = $this->config('customClass');
254:         }
255: 
256:         return $classes;
257:     }
258: 
259:     /**
260:      * Get the HTML for the field
261:      *
262:      * @param   array   $context
263:      * @return  string
264:      */
265:     protected function getFieldHtml(array $context = array())
266:     {
267:         return Quform::getHtmlTag('select', $this->getFieldAttributes($context), $this->getOptionsHtml());
268:     }
269: 
270:     /**
271:      * Get the default options for this element
272:      *
273:      * @return array
274:      */
275:     protected static function getDefaultOptions()
276:     {
277:         $options = array();
278:         $defaults = array(__('Option 1', 'quform'), __('Option 2', 'quform'), __('Option 3', 'quform'));
279: 
280:         foreach ($defaults as $key => $value) {
281:             $option = self::getDefaultOptionConfig();
282:             $option['id'] = $key + 1;
283:             $option['label'] = $option['value'] = $value;
284:             $options[] = $option;
285:         }
286: 
287:         return $options;
288:     }
289: 
290:     /**
291:      * Get the default element configuration
292:      *
293:      * @param   string|null  $key  Get the config by key, if omitted the full config is returned
294:      * @return  array
295:      */
296:     public static function getDefaultConfig($key = null)
297:     {
298:         $config = apply_filters('quform_default_config_multi_select', array(
299:             // Basic
300:             'label' => __('Untitled', 'quform'),
301:             'options' => self::getDefaultOptions(),
302:             'nextOptionId' => 4,
303:             'defaultValue' => array(),
304:             'customiseValues' => false,
305:             'description' => '',
306:             'descriptionAbove' => '',
307:             'required' => false,
308: 
309:             // Styles
310:             'labelIcon' => '',
311:             'fieldSize' => 'inherit',
312:             'fieldWidth' => 'inherit',
313:             'fieldWidthCustom' => '',
314:             'enhancedSelectEnabled' => false,
315:             'enhancedSelectPlaceholder' => '',
316:             'customClass' => '',
317:             'customElementClass' => '',
318:             'sizeAttribute' => '',
319:             'styles' => array(),
320: 
321:             // Labels
322:             'subLabel' => '',
323:             'subLabelAbove' => '',
324:             'adminLabel' => '',
325:             'tooltip' => '',
326:             'tooltipType' => 'icon',
327:             'tooltipEvent' => 'inherit',
328:             'labelPosition' => 'inherit',
329:             'labelWidth' => '',
330: 
331:             // Logic
332:             'logicEnabled' => false,
333:             'logicAction' => true,
334:             'logicMatch' => 'all',
335:             'logicRules' => array(),
336: 
337:             // Data
338:             'inArrayValidator' => true,
339:             'dynamicDefaultValue' => false,
340:             'dynamicKey' => '',
341:             'showInEmail' => true,
342:             'saveToDatabase' => true,
343: 
344:             // Advanced
345:             'visibility' => '',
346:             'validators' => array(),
347: 
348:             // Translations
349:             'messageRequired' => '',
350:             'enhancedSelectNoResultsFound' => ''
351:         ));
352: 
353:         $config['type'] = 'multiselect';
354: 
355:         if (Quform::isNonEmptyString($key)) {
356:             return Quform::get($config, $key);
357:         }
358: 
359:         return $config;
360:     }
361: }
362: 
API documentation generated by ApiGen