| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Control multiple textbox |
| 5 |
* |
| 6 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 7 |
* @copyright (c) 2018, Webcraftic Ltd |
| 8 |
* |
| 9 |
* @package factory-forms |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly |
| 14 |
if( !defined('ABSPATH') ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if( !class_exists('Wbcr_FactoryForms420_MultipleTextboxControl') ) { |
| 19 |
|
| 20 |
class Wbcr_FactoryForms420_MultipleTextboxControl extends Wbcr_FactoryForms420_Control { |
| 21 |
|
| 22 |
public $type = 'multiple-textbox'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Preparing html attributes before rendering html of the control. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
protected function beforeHtml() |
| 31 |
{ |
| 32 |
|
| 33 |
$name_on_form = $this->getNameOnForm(); |
| 34 |
|
| 35 |
if( $this->getOption('maxLength', false) ) { |
| 36 |
$this->addHtmlAttr('maxlength', intval($this->getOption('maxLength'))); |
| 37 |
} |
| 38 |
|
| 39 |
if( $this->getOption('placeholder', false) ) { |
| 40 |
$this->addHtmlAttr('placeholder', $this->getOption('placeholder')); |
| 41 |
} |
| 42 |
|
| 43 |
$this->addCssClass('form-control'); |
| 44 |
$this->addHtmlAttr('type', 'text'); |
| 45 |
//$this->addHtmlAttr('id', $name_on_form); |
| 46 |
$this->addCssClass(str_replace('_', '-', $name_on_form)); |
| 47 |
$this->addHtmlAttr('name', $name_on_form . '[]'); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Shows the html markup of the control. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function html() |
| 57 |
{ |
| 58 |
|
| 59 |
$values = $this->getValue(); |
| 60 |
|
| 61 |
if( !empty($values) ) { |
| 62 |
$values = explode('{%spr%}', $values); |
| 63 |
} else { |
| 64 |
$values = array(); |
| 65 |
} |
| 66 |
|
| 67 |
?> |
| 68 |
<div class="factory-multiple-textbox-group"> |
| 69 |
<div class="factory-mtextbox-items"> |
| 70 |
<?php if( empty($values) ): ?> |
| 71 |
<div class="factory-mtextbox-item"> |
| 72 |
<input <?php $this->attrs() ?>/> |
| 73 |
</div> |
| 74 |
<?php else: ?> |
| 75 |
<?php $counter = 0; ?> |
| 76 |
<?php foreach($values as $value): ?> |
| 77 |
<div class="factory-mtextbox-item"> |
| 78 |
<input value="<?= esc_attr($value) ?>"<?php $this->attrs() ?>/> |
| 79 |
<?php if( $counter >= 1 ): ?> |
| 80 |
<button class="btn btn-default btn-small factory-mtextbox-remove-item"> |
| 81 |
<i class="fa fa-times" aria-hidden="true"></i></button> |
| 82 |
<?php endif; ?> |
| 83 |
</div> |
| 84 |
<?php $counter++; ?> |
| 85 |
<?php endforeach; ?> |
| 86 |
<?php endif; ?> |
| 87 |
</div> |
| 88 |
<button class="btn btn-default btn-small factory-mtextbox-add-item"> |
| 89 |
<i class="fa fa-plus" aria-hidden="true"></i> <?php _e('Add new', 'wbcr_factory_forms_420') ?> |
| 90 |
</button> |
| 91 |
</div> |
| 92 |
|
| 93 |
<?php |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Returns a submit value of the control by a given name. |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* @return mixed |
| 101 |
*/ |
| 102 |
public function getSubmitValue($name, $subName) |
| 103 |
{ |
| 104 |
$name_on_form = $this->getNameOnForm($name); |
| 105 |
|
| 106 |
$value = isset($_POST[$name_on_form]) |
| 107 |
? $_POST[$name_on_form] |
| 108 |
: null; |
| 109 |
|
| 110 |
if( is_array($value) ) { |
| 111 |
$value = array_map('sanitize_text_field', $value); |
| 112 |
$value = implode('{%spr%}', $value); |
| 113 |
} |
| 114 |
|
| 115 |
$value = sanitize_text_field($value); |
| 116 |
|
| 117 |
return $value; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|