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