1 | <?php |
||
2 | /** |
||
3 | * Admin Page Framework |
||
4 | * |
||
5 | * http://admin-page-framework.michaeluno.jp/ |
||
6 | * Copyright (c) 2013-2022, Michael Uno; Licensed MIT |
||
7 | * |
||
8 | */ |
||
9 | |||
10 | |||
11 | /** |
||
12 | * Provides methods to build forms. |
||
13 | * |
||
14 | * @package AdminPageFramework/Common/Form/Model |
||
15 | * @since 3.7.0 |
||
16 | * @internal |
||
17 | * @todo This may be deprecated. Investigate what this was for and why this is not used at the moment. |
||
18 | * It seems the form works properly without this conditioning routine. |
||
19 | */ |
||
20 | class AdminPageFramework_Form_Model___FieldConditioner extends AdminPageFramework_Form_Model___SectionConditioner { |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
21 | |||
22 | public $aSectionsets = array(); |
||
23 | public $aFieldsets = array(); |
||
24 | |||
25 | /** |
||
26 | * Sets up properties. |
||
27 | * @since 3.7.0 |
||
28 | */ |
||
29 | public function __construct( /* $aSectionsets, $aFieldsets */ ) { |
||
30 | |||
31 | $_aParameters = func_get_args() + array( |
||
32 | $this->aSectionsets, |
||
33 | $this->aFieldsets, |
||
34 | ); |
||
35 | $this->aSectionsets = $_aParameters[ 0 ]; |
||
36 | $this->aFieldsets = $_aParameters[ 1 ]; |
||
37 | |||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @since 3.7.0 |
||
42 | * @return array The conditioned fieldsets array. |
||
43 | */ |
||
44 | public function get() { |
||
45 | return $this->_getFieldsConditioned( |
||
46 | $this->aFieldsets, |
||
47 | $this->aSectionsets |
||
48 | ); |
||
49 | } |
||
50 | /** |
||
51 | * Returns a fields-array by applying the conditions. |
||
52 | * |
||
53 | * @remark Assumes sections are conditioned already. |
||
54 | * @since 3.0.0 |
||
55 | * @since 3.5.3 Added type hints to the parameters and removed default values. |
||
56 | * @since 3.7.0 Moved from `AdminPageFramework_FormDefinition`. Changed the name from `getConditionedFields()`. |
||
57 | * @return array The conditioned fieldsets array. |
||
58 | */ |
||
59 | private function _getFieldsConditioned( array $aFields, array $aSections ) { |
||
60 | |||
61 | // Drop keys of fields-array which do not exist in the sections-array. |
||
62 | // For this reasons, the sections-array should be conditioned first before applying this method. |
||
63 | $aFields = $this->castArrayContents( $aSections, $aFields ); |
||
64 | |||
65 | $_aNewFields = array(); |
||
66 | foreach( $aFields as $_sSectionID => $_aSubSectionOrFields ) { |
||
67 | |||
68 | // This type check is important as the parsing field array is content-cast, which can set null value to elements. |
||
69 | if ( ! is_array( $_aSubSectionOrFields ) ) { |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | $this->_setConditionedFields( |
||
74 | $_aNewFields, // by reference - gets updated in the method. |
||
75 | $_aSubSectionOrFields, |
||
76 | $_sSectionID |
||
77 | ); |
||
78 | |||
79 | } |
||
80 | |||
81 | return $_aNewFields; |
||
82 | |||
83 | } |
||
84 | /** |
||
85 | * Updates the given array of conditioned fields. |
||
86 | * |
||
87 | * @since 3.5.3 |
||
88 | * @since 3.7.0 Moved from `AdminPageFramework_FormDefinition`. |
||
89 | * @internal |
||
90 | * @return void |
||
91 | */ |
||
92 | private function _setConditionedFields( array &$_aNewFields, $_aSubSectionOrFields, $_sSectionID ) { |
||
93 | |||
94 | foreach( $_aSubSectionOrFields as $_sIndexOrFieldID => $_aSubSectionOrField ) { |
||
95 | |||
96 | // If it is a sub-section array. |
||
97 | if ( $this->isNumericInteger( $_sIndexOrFieldID ) ) { |
||
98 | $_sSubSectionIndex = $_sIndexOrFieldID; |
||
99 | $_aFields = $_aSubSectionOrField; |
||
100 | foreach( $_aFields as $_aField ) { |
||
101 | if ( ! $this->_isAllowed( $_aField ) ) { |
||
102 | continue; |
||
103 | } |
||
104 | $_aNewFields[ $_sSectionID ][ $_sSubSectionIndex ][ $_aField[ 'field_id' ] ] = $_aField; |
||
105 | } |
||
106 | continue; |
||
107 | |||
108 | } |
||
109 | |||
110 | // Otherwise, insert the formatted field definition array. |
||
111 | $_aField = $_aSubSectionOrField; |
||
112 | if ( ! $this->_isAllowed( $_aField ) ) { |
||
113 | continue; |
||
114 | } |
||
115 | $_aNewFields[ $_sSectionID ][ $_aField[ 'field_id' ] ] = $_aField; |
||
116 | |||
117 | } |
||
118 | |||
119 | } |
||
120 | |||
121 | |||
122 | } |
||
123 |