| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Checkbox 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 |
* |
| 11 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 12 |
* @copyright (c) 2018, Webcraftic Ltd |
| 13 |
* |
| 14 |
* @package factory-forms |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
// Exit if accessed directly |
| 19 |
if( !defined('ABSPATH') ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
if( !class_exists('Wbcr_FactoryForms420_CheckboxControl') ) { |
| 24 |
|
| 25 |
class Wbcr_FactoryForms420_CheckboxControl extends Wbcr_FactoryForms420_Control { |
| 26 |
|
| 27 |
public $type = 'checkbox'; |
| 28 |
|
| 29 |
public function getSubmitValue($name, $sub_name) |
| 30 |
{ |
| 31 |
$name_on_form = $this->getNameOnForm($name); |
| 32 |
|
| 33 |
return isset($_POST[$name_on_form]) && $_POST[$name_on_form] != 0 |
| 34 |
? 1 |
| 35 |
: 0; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Shows the html markup of the control. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function html() |
| 45 |
{ |
| 46 |
|
| 47 |
$events_on_data = $this->getOption('eventsOn', array()); |
| 48 |
$events_off_data = $this->getOption('eventsOff', array()); |
| 49 |
|
| 50 |
if( !empty($events_on_data) || !empty($events_off_data) ) { |
| 51 |
|
| 52 |
$events_on_string_data = json_encode($events_on_data); |
| 53 |
$events_off_string_data = json_encode($events_off_data); |
| 54 |
|
| 55 |
$name_on_form = $this->getNameOnForm(); |
| 56 |
$value = $this->getValue(); |
| 57 |
|
| 58 |
$print_styles = ''; |
| 59 |
|
| 60 |
if( $value ) { |
| 61 |
$current_events_data = $events_on_data; |
| 62 |
} else { |
| 63 |
$current_events_data = $events_off_data; |
| 64 |
} |
| 65 |
|
| 66 |
foreach($current_events_data as $event_name => $selectors) { |
| 67 |
if( $event_name == 'hide' ) { |
| 68 |
$print_styles .= $selectors . '{display:none;}'; |
| 69 |
} else if( $event_name == 'show' ) { |
| 70 |
$print_styles .= $selectors . '{display:block;}'; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
echo '<style>' . $print_styles . '</style>'; |
| 75 |
?> |
| 76 |
|
| 77 |
<script> |
| 78 |
// Onepress factory checkbox control events |
| 79 |
if( void 0 === window.__factory_checkbox_control_events_on_data ) { |
| 80 |
window.__factory_checkbox_control_events_on_data = {}; |
| 81 |
} |
| 82 |
if( void 0 === window.__factory_checkbox_control_events_off_data ) { |
| 83 |
window.__factory_checkbox_control_events_off_data = {}; |
| 84 |
} |
| 85 |
window.__factory_checkbox_control_events_on_data['<?php echo $name_on_form ?>'] = <?= $events_on_string_data ?>; |
| 86 |
window.__factory_checkbox_control_events_off_data['<?php echo $name_on_form ?>'] = <?= $events_off_string_data ?>; |
| 87 |
</script> |
| 88 |
<?php |
| 89 |
} |
| 90 |
|
| 91 |
if( 'buttons' == $this->getOption('way') ) { |
| 92 |
$this->buttonsHtml(); |
| 93 |
} else { |
| 94 |
$this->defaultHtml(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Shows the Buttons Checkbox. |
| 100 |
* |
| 101 |
* @since 1.0.0 |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
protected function buttonsHtml() |
| 105 |
{ |
| 106 |
$value = esc_attr($this->getValue()); |
| 107 |
$name_on_form = $this->getNameOnForm(); |
| 108 |
|
| 109 |
$this->addCssClass('factory-buttons-way'); |
| 110 |
$this->addCssClass('btn-group'); |
| 111 |
|
| 112 |
if( $this->getOption('tumbler', false) ) { |
| 113 |
$this->addCssClass('factory-tumbler'); |
| 114 |
} |
| 115 |
|
| 116 |
$tumbler_function = $this->getOption('tumblerFunction', false); |
| 117 |
if( $tumbler_function ) { |
| 118 |
$this->addHtmlData('tumbler-function', $tumbler_function); |
| 119 |
} |
| 120 |
|
| 121 |
if( $this->getOption('tumblerHint', false) ) { |
| 122 |
$this->addCssClass('factory-has-tumbler-hint'); |
| 123 |
|
| 124 |
$delay = $this->getOption('tumblerDelay', 3000); |
| 125 |
$this->addHtmlData('tumbler-delay', $delay); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
?> |
| 130 |
<div <?php $this->attrs() ?>> |
| 131 |
<button type="button" class="btn btn-default btn-small btn-sm factory-on <?php if( $value ) { |
| 132 |
echo 'active'; |
| 133 |
} ?>"><?php _e('On', 'wbcr_factory_forms_420') ?></button> |
| 134 |
<button type="button" class="btn btn-default btn-small btn-sm factory-off <?php if( !$value ) { |
| 135 |
echo 'active'; |
| 136 |
} ?>" data-value="0"><?php _e('Off', 'wbcr_factory_forms_420') ?></button> |
| 137 |
<input type="checkbox" style="display: none" id="<?php echo $name_on_form ?>" class="factory-result" name="<?php echo $name_on_form ?>" value="<?= $value ?>" <?php if( $value ) { |
| 138 |
echo 'checked="checked"'; |
| 139 |
} ?>" /> |
| 140 |
</div> |
| 141 |
<?php if( $this->getOption('tumblerHint', false) ) { ?> |
| 142 |
<div class="factory-checkbox-tumbler-hint factory-tumbler-hint" style="display: none;"> |
| 143 |
<div class="factory-tumbler-content"> |
| 144 |
<?php echo $this->getOption('tumblerHint') ?> |
| 145 |
</div> |
| 146 |
</div> |
| 147 |
<?php } ?> |
| 148 |
<?php |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Shows the standart checkbox. |
| 153 |
* |
| 154 |
* @since 1.0.0 |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
protected function defaultHtml() |
| 158 |
{ |
| 159 |
$value = esc_attr($this->getValue()); |
| 160 |
$name_on_form = $this->getNameOnForm(); |
| 161 |
|
| 162 |
$this->addHtmlAttr('type', 'checkbox'); |
| 163 |
$this->addHtmlAttr('id', $name_on_form); |
| 164 |
$this->addHtmlAttr('name', $name_on_form); |
| 165 |
$this->addHtmlAttr('value', $value); |
| 166 |
|
| 167 |
if( $value ) { |
| 168 |
$this->addHtmlAttr('checked', 'checked'); |
| 169 |
} |
| 170 |
$this->addCssClass('factory-default-way'); |
| 171 |
|
| 172 |
?> |
| 173 |
<input <?php $this->attrs() ?>/> |
| 174 |
<?php |
| 175 |
} |
| 176 |
} |
| 177 |
} |