Completed
Branch dev (bec91c)
by
unknown
04:56
created

unsetResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides methods for the user to interact with the class object.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Common/Form/Controller
15
 * @since       3.7.0
16
 * @internal
17
 */
18
class AdminPageFramework_Form_Controller extends AdminPageFramework_Form_View {
19
   
20
    /**
21
     * Sets a given field errors.
22
     * @since       3.7.0
23
     * @return      void
24
     */
25
    public function setFieldErrors( $aErrors ) {
26
        $this->oFieldError->set( $aErrors );
0 ignored issues
show
Bug introduced by
The property oFieldError does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
   
29
    /**
30
     * Checks whether a field error exists.
31
     * @return      boolean
32
     * @since       3.7.0
33
     */
34
    public function hasFieldError() {
35
        return $this->oFieldError->hasError();
36
    }
37
    
38
    /**
39
     * Checks if an error settings notice has been set.
40
     * 
41
     * This is used in the internal validation callback method to decide whether the system error or update notice should be added or not.
42
     * If this method yields true, the framework discards the system message and displays the user set notification message.
43
     * 
44
     * @since       3.7.0
45
     * @param       string      $sType If empty, the method will check if a message exists in all types. Otherwise, it checks the existence of a message of the specified type.
46
     * @return      boolean     True if a setting notice is set; otherwise, false.
47
     */
48
    public function hasSubmitNotice( $sType='' ) {
49
        return $this->oSubmitNotice->hasNotice( $sType );
0 ignored issues
show
Bug introduced by
The property oSubmitNotice does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
    }
51
    
52
    /**
53
     * Sets the given message to be displayed in the next page load. 
54
     * 
55
     * This is used to inform users about the submitted input data, such as "Updated successfully." or "Problem occurred." etc. 
56
     * and normally used in validation callback methods.
57
     * 
58
     * <h4>Example</h4>
59
     * `
60
     * if ( ! $bVerified ) {
61
     *       $this->setFieldErrors( $aErrors );     
62
     *       $this->setSettingNotice( 'There was an error in your input.' );
63
     *       return $aOldPageOptions;
64
     * }
65
     * `
66
     * @since        3.7.0
67
     * @access       public
68
     * @param        string      $sMessage       the text message to be displayed.
69
     * @param        string      $sType          (optional) the type of the message, either "error" or "updated"  is used.
70
     * @param        array       $asAttributes   (optional) the tag attribute array applied to the message container HTML element. If a string is given, it is used as the ID attribute value.
71
     * @param        boolean     $bOverride      (optional) If true, only one message will be shown in the next page load. false: do not override when there is a message of the same id. true: override the previous one.
72
     * @return       void
73
     */
74
    public function setSubmitNotice( $sMessage, $sType='error', $asAttributes=array(), $bOverride=true ) {
75
        $this->oSubmitNotice->set(
76
            $sMessage, 
77
            $sType, 
78
            $asAttributes, 
79
            $bOverride
80
        );
81
    }
82
    
83
    /**
84
     * Adds the given section definition array to the form property.
85
     * 
86
     * @since       3.0.0
87
     * @since       3.7.0       Moved from `AminPageFramework_FormDefinition`.
88
     * @return      void
89
     */
90
    public function addSection( array $aSectionset ) {
91
        
92
        // $aSectionset                 = $aSectionset + AdminPageFramework_Form_Model___FormatSectionset::$aStructure;
93
        // Pre-format
94
        $aSectionset                 = $aSectionset + array(
95
            'section_id'    => null,
96
        );
97
        $aSectionset[ 'section_id' ] = $this->sanitizeSlug( $aSectionset[ 'section_id' ] );
98
        
99
        $this->aSectionsets[ $aSectionset[ 'section_id' ] ] = $aSectionset;    
0 ignored issues
show
Bug introduced by
The property aSectionsets does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
        $this->aFieldsets[ $aSectionset[ 'section_id' ] ]   = $this->getElement(
0 ignored issues
show
Bug introduced by
The property aFieldsets does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
101
            $this->aFieldsets,  // subject array
102
            $aSectionset[ 'section_id' ], // key
103
            array()      // default
104
        );                                
105
        
106
    }
107
    
108
    /**
109
     * Removes a section definition array from the property by the given section ID.
110
     * 
111
     * @since       3.0.0
112
     * @since       3.7.0       Moved from `AminPageFramework_FormDefinition`.
113
     */
114
    public function removeSection( $sSectionID ) {
115
        
116
        if ( '_default' === $sSectionID ){ 
117
            return; 
118
        }
119
        unset( 
120
            $this->aSectionsets[ $sSectionID ],
121
            $this->aFieldsets[ $sSectionID ]
122
        );
123
        
124
    }
125
    
126
    /**
127
     * Returns the added resource items.
128
     * @since       3.7.0
129
     * @return      array
130
     */
131
    public function getResources( $sKey ) {
132
        return $this->getElement( self::$_aResources, $sKey );
133
    }
134
    
135
    /**
136
     * @since       3.8.5
137
     * @return      void
138
     */    
139
    public function unsetResources( $aKeys ) {
140
        $this->unsetDimensionalArrayElement( self::$_aResources, $aKeys );   
141
    }
142
    
143
    /**
144
     * Sets the resource items.
145
     * @return      void
146
     */
147
    public function setResources( $sKey, $mValue ) {
148
        return self::$_aResources[ $sKey ] = $mValue;
149
    }
150
    /**
151
     * @since       3.7.0
152
     * @return      void
153
     */
154
    public function addResource( $sKey, $sValue ) {
155
        self::$_aResources[ $sKey ][] = $sValue;
156
    }
157
    
158
    /**
159
     * Stores the target page slug which will be applied when no page slug is specified.
160
     * 
161
     * @since       3.0.0
162
     * @since       3.7.0      Accepts an array.
163
     * @since       3.7.0      Moved from `AminPageFramework_FormDefinition`.
164
     */
165
    protected $_asTargetSectionID = '_default';    
166
    
167
    /*
168
     * Adds the given field definition array to the form property.
169
     * 
170
     * @since       3.0.0
171
     * @since       3.7.0       Moved from `AminPageFramework_FormDefinition`.
172
     * @param       array|string            $asFieldset        A field definition array.
173
     * @return      array|string|null       If the passed field is set, it returns the set field array. If the target section id is set, the set section id is returned. Otherwise null.
174
     */    
175
    public function addField( $asFieldset ) {
176
177
        // If it is a target section, update the property and return.
178
        if ( ! $this->_isFieldsetDefinition( $asFieldset ) ) {
179
            $this->_asTargetSectionID = $this->_getTargetSectionID( $asFieldset );
180
            return $this->_asTargetSectionID;
181
        }
182
183
        $_aFieldset = $asFieldset;
184
        
185
        // Set the target section ID
186
        $this->_asTargetSectionID = $this->getElement(
187
            $_aFieldset,  // subject array
0 ignored issues
show
Bug introduced by
It seems like $_aFieldset defined by $asFieldset on line 183 can also be of type string; however, AdminPageFramework_Utili...rayGetter::getElement() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
188
            'section_id', // key
189
            $this->_asTargetSectionID // default
190
        );                               
191
192
        // Required Keys - 3.8.0+ Now 'type' can be omitted.
193
        if ( ! isset( $_aFieldset[ 'field_id' ] ) ) { 
194
            return null; 
195
        }         
196
                
197
        // Update the fieldset property
198
        $this->_setFieldset( $_aFieldset );
0 ignored issues
show
Bug introduced by
It seems like $_aFieldset defined by $asFieldset on line 183 can also be of type string; however, AdminPageFramework_Form_Controller::_setFieldset() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
199
200
        return $_aFieldset;
201
        
202
    }    
203
        /**
204
         * @return      void
205
         * @since       3.7.0
206
         */
207
        private function _setFieldset( array $aFieldset ) {
208
            
209
            // Pre-format
210
            $aFieldset = array( 
211
                    '_fields_type'    => $this->aArguments[ 'structure_type' ], // @todo deprecate this item.
0 ignored issues
show
Bug introduced by
The property aArguments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
212
                    '_structure_type' => $this->aArguments[ 'structure_type' ],
213
                )
214
                + $aFieldset
215
                + array( 
216
                    'section_id'      => $this->_asTargetSectionID,
217
                    'class_name'      => $this->aArguments[ 'caller_id' ], // for backward-compatibility
218
                )
219
                // + self::$_aStructure_Field // @deprecated 3.6.0 as the field will be formatted later anyway.
220
                ;         
221
        
222
            // Sanitize the IDs since they are used as a callback method name.
223
            $aFieldset[ 'field_id' ]     = $this->getIDSanitized( $aFieldset[ 'field_id' ] );
224
            $aFieldset[ 'section_id' ]   = $this->getIDSanitized( $aFieldset[ 'section_id' ] );
225
            
226
            // 3.7.0+ A section path (e.g. parent_section|nested_section|more_nested_section) will be stored in the key.
227
            // Also in the fieldsets dimension, a field path is stored in the key.
228
            $_aSectionPath    = $this->getAsArray( $aFieldset[ 'section_id' ] );
229
            $_sSectionPath    = implode( '|', $_aSectionPath );
230
            
231
            $_aFieldPath      = $this->getAsArray( $aFieldset[ 'field_id' ] );
232
            $_sFieldPath      = implode( '|', $_aFieldPath );
233
            
234
            $this->aFieldsets[ $_sSectionPath ][ $_sFieldPath ] = $aFieldset;
235
            
236
        }
237
238
        /**
239
         * Checks if the given item is a fieldset definition or not.
240
         * @since       3.7.0
241
         * @return      boolean
242
         */
243
        private function _isFieldsetDefinition( $asFieldset ) {
244
            
245
            if ( is_scalar( $asFieldset ) ) {
246
                return false;
247
            }
248
            // if ( ! is_array( $asFieldset ) ) {
249
                // return false;
250
            // }
251
            return $this->isAssociative( $asFieldset );
252
            
253
        }
254
        /**
255
         * @return      string
256
         */
257
        private function _getTargetSectionID( $asTargetSectionID ) {
258
            
259
            if ( is_scalar( $asTargetSectionID ) ) {
260
                return $asTargetSectionID;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $asTargetSectionID; of type integer|double|string|boolean is incompatible with the return type documented by AdminPageFramework_Form_...er::_getTargetSectionID of type string as it can also be of type boolean which is not included in this return type.
Loading history...
261
            }
262
            return $asTargetSectionID;
263
            // return implode( '|', $asTargetSectionID );
264
            
265
        }
266
        
267
    /**
268
     * Removes a field definition array from the property array by the given field ID.
269
     * 
270
     *  The structure of the aFields property array looks like this:
271
     *  <code>    array( 
272
     *          'my_sec_a' => array(
273
     *              'my_field_a' => array( ... ),
274
     *              'my_field_b' => array( ... ),
275
     *              'my_field_c' => array( ... ),
276
     *          ),
277
     *          'my_sec_b' => array(
278
     *              'my_field_a' => array( ... ),
279
     *              'my_field_b' => array( ... ),
280
     *              1 => array(
281
     *                  'my_field_a' => array( ... ),
282
     *                  'my_field_b' => array( ... ),
283
     *              )
284
     *              2 => array(
285
     *                  'my_field_a' => array( ... ),
286
     *                  'my_field_b' => array( ... ),
287
     *              )     
288
     *          )
289
     *      )</code>
290
     * 
291
     * @since       3.0.0
292
     * @since       3.7.0       Moved from `AminPageFramework_FormDefinition`.
293
     */     
294
    public function removeField( $sFieldID ) {
295
               
296
        foreach( $this->aFieldsets as $_sSectionID => $_aSubSectionsOrFields ) {
297
298
            if ( array_key_exists( $sFieldID, $_aSubSectionsOrFields ) ) {
299
                unset( $this->aFieldsets[ $_sSectionID ][ $sFieldID ] );
300
            }
301
            
302
            // Check sub-sections.
303
            foreach ( $_aSubSectionsOrFields as $_sIndexOrFieldID => $_aSubSectionOrFields ) {
304
                
305
                // if it's a sub-section
306
                if ( $this->isNumericInteger( $_sIndexOrFieldID ) ) {
307
                    if ( array_key_exists( $sFieldID, $_aSubSectionOrFields ) ) {
308
                        unset( $this->aFieldsets[ $_sSectionID ][ $_sIndexOrFieldID ] );
309
                    }
310
                    continue;
311
                }
312
                
313
            }
314
        }
315
        
316
    }
317
        
318
}
319