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