providers
1 year ago
complex-control.class.php
1 year ago
control-holder.class.php
1 year ago
control.class.php
1 year ago
custom-element.class.php
1 year ago
form-element.class.php
1 year ago
form-layout.class.php
1 year ago
form.class.php
1 year ago
holder.class.php
1 year ago
html-builder.class.php
1 year ago
index.php
3 years ago
control-holder.class.php
173 lines
| 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_FactoryForms480_ControlHolder') ) { |
| 18 | /** |
| 19 | * The base class for control holders. |
| 20 | * |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | abstract class Wbcr_FactoryForms480_ControlHolder extends Wbcr_FactoryForms480_Control { |
| 24 | |
| 25 | /** |
| 26 | * Holder Elements. |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | * @var Wbcr_FactoryForms480_Control[] |
| 30 | */ |
| 31 | protected $elements = array(); |
| 32 | |
| 33 | /** |
| 34 | * Is this element a control holder? |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | * @var bool |
| 38 | */ |
| 39 | public $is_holder = true; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new instance of control holder. |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | * @param mixed[] $options A holder options. |
| 46 | * @param Wbcr_FactoryForms480_Form $form A parent form. |
| 47 | */ |
| 48 | public function __construct($options, $form) |
| 49 | { |
| 50 | parent::__construct($options, $form); |
| 51 | |
| 52 | $this->elements = $form->createElements($options['items']); |
| 53 | |
| 54 | foreach((array)$this->elements as $val) { |
| 55 | $val->parent = $this; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns holder elements. |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | * @return Wbcr_FactoryForms480_FormElement[]. |
| 64 | */ |
| 65 | public function getElements() |
| 66 | { |
| 67 | return $this->elements; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Renders the form or a given control holder. |
| 72 | * |
| 73 | * @since 1.0.0 |
| 74 | * @return void |
| 75 | */ |
| 76 | function render() |
| 77 | { |
| 78 | $this->beforeRendering(); |
| 79 | |
| 80 | $is_first_item = true; |
| 81 | |
| 82 | foreach($this->elements as $element) { |
| 83 | $element->setOption('isFirst', $is_first_item); |
| 84 | |
| 85 | if( $is_first_item ) { |
| 86 | $is_first_item = false; |
| 87 | } |
| 88 | |
| 89 | do_action('wbcr_factory_form_before_element_' . $element->getName()); |
| 90 | |
| 91 | // if a current item is a control holder |
| 92 | if( $element->is_holder ) { |
| 93 | |
| 94 | $this->form->layout->beforeHolder($element); |
| 95 | $element->render(); |
| 96 | $this->form->layout->afterHolder($element); |
| 97 | // if a current item is an input control |
| 98 | } elseif( $element->is_control ) { |
| 99 | |
| 100 | $this->form->layout->beforeControl($element); |
| 101 | $element->render(); |
| 102 | $this->form->layout->afterControl($element); |
| 103 | // if a current item is a custom form element |
| 104 | } elseif( $element->is_custom ) { |
| 105 | |
| 106 | $element->render(); |
| 107 | // otherwise, show the error |
| 108 | } else { |
| 109 | echo('[ERROR] Invalid item.'); |
| 110 | } |
| 111 | |
| 112 | do_action('wbcr_factory_form_after_element_' . $element->getName()); |
| 113 | } |
| 114 | |
| 115 | $this->afterRendering(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Rendering a beginning of a holder. |
| 120 | * |
| 121 | * @since 1.0.0 |
| 122 | * @return void |
| 123 | */ |
| 124 | public function beforeRendering() |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Rendering an end of a holder. |
| 130 | * |
| 131 | * @since 1.0.0 |
| 132 | * @return void |
| 133 | */ |
| 134 | public function afterRendering() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Rendering some html before an inner holder. |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | * @return void |
| 143 | */ |
| 144 | public function beforeInnerHolder() |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Rendering some html after an inner holder. |
| 150 | * |
| 151 | * @since 1.0.0 |
| 152 | * @return void |
| 153 | */ |
| 154 | public function afterInnerHolder() |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | |
| 159 | public function beforeInnerElement() |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Rendering some html after an inner element. |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | * @return void |
| 168 | */ |
| 169 | public function afterInnerElement() |
| 170 | { |
| 171 | } |
| 172 | } |
| 173 | } |