a name of the control * value => a value to show in the control * default => a default value of the control if the "value" option is not specified * items => a callback to return items or an array of items to select * * @author Alex Kovalev * @copyright (c) 2018, Webcraftic Ltd * * @package factory-forms * @since 1.0.0 */ // Exit if accessed directly if( !defined('ABSPATH') ) { exit; } if( !class_exists('Wbcr_FactoryForms480_ListControl') ) { class Wbcr_FactoryForms480_ListControl extends Wbcr_FactoryForms480_Control { public $type = 'list'; /** * Returns a set of available items for the list. * * @since 1.0.0 * @return mixed[] */ private function getItems() { $data = $this->getOption('data', array()); // if the data options is a valid callback for an object method if( (is_array($data) && count($data) == 2 && is_object($data[0])) || is_string($data) ) { return call_user_func($data); } // if the data options is an array of values return $data; } /** * Returns true, if the data should be loaded via ajax. * * @since 1.0.0 * @return bool */ protected function isAjax() { $data = $this->getOption('data', array()); return is_array($data) && isset($data['ajax']); } /** * Shows the html markup of the control. * * @since 1.0.0 * @return void */ public function html() { $way = $this->getOption('way', 'default'); $this->addHtmlData('way', $way); if( $this->isAjax() ) { $data = $this->getOption('data', array()); $ajax_id = 'factory-list-' . rand(1000000, 9999999); $value = $this->getValue(null, true); if( empty($value) || empty($value[0]) ) { $value = array(); } ?>
addHtmlData('ajax', true); $this->addHtmlData('ajax-data-id', $ajax_id); $this->addCssClass('factory-hidden'); } if( 'checklist' == $way ) { $this->checklistHtml(); } else { $this->defaultHtml(); } } /** * Shows the Buttons Dropdown. * * @since 1.0.0 * @return void */ protected function checklistHtml() { $items = $this->getItems(); $value = explode(',', $this->getValue()); if( empty($value) || empty($value[0]) ) { $value = array(); } $name_on_form = $this->getNameOnForm(); $this->addCssClass('factory-checklist-way'); $this->addHtmlData('name', $name_on_form); $errors_callback = $this->getOption('errors'); $errors = !empty($errors_callback) ? call_user_func($errors_callback) : array(); $is_empty = $this->isAjax() || empty($items); $emptyList = $this->getOption('empty', __('The list is empty.', 'wbcr_factory_forms_480')); if( $is_empty ) { $this->addCssClass('factory-empty'); } ?> getItems(); $value = $this->getValue(); $name_on_form = $this->getNameOnForm(); $this->addHtmlAttr('id', $name_on_form); $this->addHtmlAttr('name', $name_on_form); $this->addCssClass('form-control'); ?>