| 1 |
<?php |
| 2 |
/** |
| 3 |
* The file contains the base class for all control holder |
| 4 |
* |
| 5 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 6 |
* @copyright (c) 2018, Webcraftic Ltd |
| 7 |
* |
| 8 |
* @package factory-forms |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if( !defined('ABSPATH') ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if( !class_exists('Wbcr_FactoryForms420_Holder') ) { |
| 18 |
|
| 19 |
/** |
| 20 |
* The base class for control holders. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
abstract class Wbcr_FactoryForms420_Holder extends Wbcr_FactoryForms420_FormElement { |
| 25 |
|
| 26 |
/** |
| 27 |
* Holder Elements. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @var Wbcr_FactoryForms420_Control[] |
| 31 |
*/ |
| 32 |
protected $elements = array(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Is this element a control holder? |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @var bool |
| 39 |
*/ |
| 40 |
public $is_holder = true; |
| 41 |
|
| 42 |
/** |
| 43 |
* Creates a new instance of control holder. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* @param mixed[] $options A holder options. |
| 47 |
* @param Wbcr_FactoryForms420_Form $form A parent form. |
| 48 |
*/ |
| 49 |
public function __construct($options, $form) |
| 50 |
{ |
| 51 |
parent::__construct($options, $form); |
| 52 |
$this->elements = $form->createElements($options['items']); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns holder elements. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
* @return Wbcr_FactoryForms420_Control[]. |
| 60 |
*/ |
| 61 |
public function getElements() |
| 62 |
{ |
| 63 |
return $this->elements; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Renders the form or a given control holder. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
function render() |
| 73 |
{ |
| 74 |
|
| 75 |
$this->beforeRendering(); |
| 76 |
|
| 77 |
$is_first_item = true; |
| 78 |
|
| 79 |
foreach($this->elements as $element) { |
| 80 |
|
| 81 |
$element->setOption('isFirst', $is_first_item); |
| 82 |
|
| 83 |
if( $is_first_item ) { |
| 84 |
$is_first_item = false; |
| 85 |
} |
| 86 |
|
| 87 |
do_action('wbcr_factory_422_form_before_element_' . $element->getOption('name')); |
| 88 |
|
| 89 |
// if a current item is a control holder |
| 90 |
if( $element->is_holder ) { |
| 91 |
|
| 92 |
$this->form->layout->beforeHolder($element); |
| 93 |
$element->render(); |
| 94 |
$this->form->layout->afterHolder($element); |
| 95 |
// if a current item is an input control |
| 96 |
} elseif( $element->is_control ) { |
| 97 |
$this->form->layout->beforeControl($element); |
| 98 |
$element->render(); |
| 99 |
$this->form->layout->afterControl($element); |
| 100 |
// if a current item is a custom form element |
| 101 |
} elseif( $element->is_custom ) { |
| 102 |
|
| 103 |
$element->render(); |
| 104 |
// otherwise, show the error |
| 105 |
} else { |
| 106 |
echo('[ERROR] Invalid item.'); |
| 107 |
} |
| 108 |
|
| 109 |
do_action('wbcr_factory_form_after_element_' . $element->getOption('name')); |
| 110 |
} |
| 111 |
|
| 112 |
$this->afterRendering(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Rendering a beginning of a holder. |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
protected function beforeRendering() |
| 122 |
{ |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Rendering an end of a holder. |
| 127 |
* |
| 128 |
* @since 1.0.0 |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
protected function afterRendering() |
| 132 |
{ |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Rendering some html before an inner holder. |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
protected function beforeInnerHolder() |
| 142 |
{ |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Rendering some html after an inner holder. |
| 147 |
* |
| 148 |
* @since 1.0.0 |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
protected function afterInnerHolder() |
| 152 |
{ |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
protected function beforeInnerElement() |
| 157 |
{ |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Rendering some html after an inner element. |
| 162 |
* |
| 163 |
* @since 1.0.0 |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
protected function afterInnerElement() |
| 167 |
{ |
| 168 |
} |
| 169 |
} |
| 170 |
} |