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
5 months ago
complex-control.class.php
133 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The file contains the base class for all complex controls. |
| 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 | if ( ! class_exists( 'Wbcr_FactoryForms600_ComplexControl' ) ) { |
| 14 | /** |
| 15 | * The base class for all controls. |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | abstract class Wbcr_FactoryForms600_ComplexControl extends Wbcr_FactoryForms600_Control { |
| 20 | |
| 21 | /** |
| 22 | * Is this element a complex control? |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | * @var bool |
| 26 | */ |
| 27 | public $is_complex_control = true; |
| 28 | |
| 29 | /** |
| 30 | * Contains a set of internal controls. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | * @var Wbcr_FactoryForms600_Control[] |
| 34 | */ |
| 35 | public $inner_controls = []; |
| 36 | |
| 37 | /** |
| 38 | * Sets a provider for the control. |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | * @param Wbcr_IFactoryForms600_ValueProvider $provider |
| 42 | * @return void |
| 43 | */ |
| 44 | public function setProvider( $provider ) { |
| 45 | $this->provider = $provider; |
| 46 | |
| 47 | foreach ( $this->inner_controls as $control ) { |
| 48 | $control->setProvider( $provider ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns a control name used to save data with a provider. |
| 54 | * |
| 55 | * The method can return if the control have several elements. |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | * @return array|string|null A control name. |
| 59 | */ |
| 60 | public function getName() { |
| 61 | $names = []; |
| 62 | |
| 63 | foreach ( $this->inner_controls as $control ) { |
| 64 | $inner_names = $control->getName(); |
| 65 | if ( is_array( $inner_names ) ) { |
| 66 | $names = array_merge( $names, $inner_names ); |
| 67 | } else { |
| 68 | $names[] = $inner_names; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return $names; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns an array of value to save received after submission of a form. |
| 77 | * |
| 78 | * @see getSubmitValue |
| 79 | * |
| 80 | * The array has the following format: |
| 81 | * array( |
| 82 | * 'control-name1' => 'value1', |
| 83 | * 'control-name2__sub-name1' => 'value2' |
| 84 | * 'control-name2__sub-name2' => 'value3' |
| 85 | * ) |
| 86 | * |
| 87 | * @since 1.0.0 |
| 88 | * @return array |
| 89 | */ |
| 90 | public function getValuesToSave() { |
| 91 | $values = []; |
| 92 | |
| 93 | foreach ( $this->inner_controls as $control ) { |
| 94 | $inner_values = $control->getValuesToSave(); |
| 95 | if ( is_array( $inner_values ) ) { |
| 96 | $values = array_merge( $values, $inner_values ); |
| 97 | } else { |
| 98 | $values[] = $inner_values; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $values; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns an initial value of control that is used to render the control first time. |
| 107 | * |
| 108 | * @since 1.0.0 |
| 109 | * @param null $index |
| 110 | * @param bool $multiple |
| 111 | * @return array |
| 112 | */ |
| 113 | public function getValue( $index = null, $multiple = false ) { |
| 114 | |
| 115 | $values = []; |
| 116 | foreach ( $this->inner_controls as $control ) { |
| 117 | $inner_values = array_merge( $values, $control->getValue() ); |
| 118 | if ( is_array( $inner_values ) ) { |
| 119 | $values = array_merge( $values, $inner_values ); |
| 120 | } else { |
| 121 | $values[] = $inner_values; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if ( $index !== null ) { |
| 126 | return $values[ $index ]; |
| 127 | } else { |
| 128 | return $values; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 |