Completed
Branch dev (1ad2e5)
by
unknown
04:37
created

isNormalPlacement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 1
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 shared methods for the form classes.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Common/Form/Utility
15
 * @since       3.7.0
16
 * @extends     AdminPageFramework_FrameworkUtility
17
 * @internal
18
 */
19
abstract class AdminPageFramework_Form_Utility extends AdminPageFramework_FrameworkUtility {
20
    
21
    /**
22
     * @since       3.7.0
23
     * @return      array
24
     */
25
    static public function getElementPathAsArray( $asPath ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
26
        if ( is_array( $asPath ) ) {
27
            return;
28
        }
29
        return explode( '|', $asPath );
30
    }
31
    
32
    /**
33
     * @since       3.7.0
34
     * @return      string      The section path. e.g. my_section|nested_section
35
     */
36
    static public function getFormElementPath( $asID ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
37
        return implode( 
38
            '|', 
39
            self::getAsArray( $asID ) 
40
        );        
41
    }
42
  
43
    /**
44
     * Sanitizes a given section or field id.
45
     * @return      array|string
46
     * @since       3.7.0
47
     */
48
    static public function getIDSanitized( $asID ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
49
        return is_scalar( $asID )
50
            ? self::sanitizeSlug( $asID )
51
            : self::getAsArray( $asID );
52
            
53
    }
54
    
55
    /**
56
     * Checks whether the given field-set definition has nested field items.
57
     * @since       3.8.0
58
     * @return      boolean
59
     */
60
    static public function hasNestedFields( array $aFieldset ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
61
        
62
        if ( ! self::hasFieldDefinitionsInContent( $aFieldset ) ) {
63
            return false;
64
        }
65
        // At this point, the `content` argument contains either the definition of nested fields or inline-mixed fields.
66
        
67
        // If it is the `inline_mixed` field type, yield false.
68
        if ( 'inline_mixed' === self::getElement( $aFieldset, 'type' ) ) {
69
            return false;
70
        }
71
        
72
        // If the first element is a string, it is a inline mixed field definition.
73
       return is_array( self::getElement( $aFieldset[ 'content' ], 0 ) );
74
       
75
    }  
76
77
    /**
78
     * Checks whether the given field-set definition has inline mixed items.
79
     * 
80
     * The first element is a string defining place holders and the rest should be field definition arrays.
81
     * 
82
     * @since       3.8.0
83
     * @return      boolean
84
     * @deprecated  3.8.0
85
     */
86
/*     static public function hasInlineMixedFields( array $aFieldset ) {
87
        
88
        return 'inline_mixed' === $aFieldset[ 'type' ];
89
90
        // @deprecated 3.8.0
91
        // if ( ! self::hasFieldDefinitionsInContent( $aFieldset ) ) {
92
            // return false;
93
        // }
94
        // return is_string( self::getElement( $aFieldset[ 'content' ], 0 ) )
95
            // && is_array( self::getElement( $aFieldset[ 'content' ], 1 ) );
96
        
97
    } */
98
    
99
    /**
100
     * Checks whether the given field-set definition has field-set definitions in the `content` argument.
101
     * @since       3.8.0
102
     * @return      boolean
103
     */
104
    static public function hasFieldDefinitionsInContent( array $aFieldset ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
105
        
106
        if ( ! isset( $aFieldset[ 'content' ] ) ) {
107
            return false;
108
        }
109
        if ( empty( $aFieldset[ 'content' ] ) ) {
110
            return false;
111
        }
112
        return is_array( $aFieldset[ 'content' ] );            
113
        
114
    }
115
    
116
    /**
117
     * Checks whether the given field has a sub-field.
118
     * @since       3.8.0
119
     * @param       array       $aFields        An array holding sub-fields.
120
     * @param       array       $aField         A field definition array. 
121
     * @return      boolean
122
     */
123
    static public function hasSubFields( array $aFields, array $aField ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
124
        
125
        if ( count( $aFields ) > 1 ) {
126
            return true;
127
        }
128
        if ( $aField[ 'repeatable' ] || $aField[ 'sortable' ] ) {
129
            return true;
130
        }
131
        return false;
132
        
133
    }
134
    
135
    /**
136
     * Checks whether the parent field is repeatable or sortable.
137
     * 
138
     * @since       3.8.0
139
     * @return      boolean
140
     * @deprecated  3.8.0
141
     */
142
    // static public function isParentFieldDynamic( $aFieldset ) {
143
// return false;        
144
    // }
145
    
146
    /**
147
     * Adds a trailing pipe (|) character to the given string value.
148
     * 
149
     * Used to construct a field path.
150
     * 
151
     * @return      string
152
     * @since       3.8.0
153
     */
154
    static public function getTrailingPipeCharacterAppended( $sString ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
155
        if ( empty( $sString ) ) {
156
            return $sString;
157
        }
158
        return $sString . '|';
159
    }    
160
    
161
    /**
162
     * Re-formats the field-set definition with the passed sub-field index. The field path and other internal keys need to be updated to insert a sub-field index.
163
     * 
164
     * It is assumed that the passed field-set definition array is already formatted as this is for sub-fields of nested field-sets.
165
     * 
166
     * This is used for nested and inline-mixed field types.
167
     * 
168
     * @internal
169
     * @since       3.8.0
170
     * @return      array
171
     */
172
    static public function getFieldsetReformattedBySubFieldIndex( array $aFieldset, $iSubFieldIndex, $bHasSubFields, array $aParentFieldset ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
173
        
174
        $_oCallerForm   = $aFieldset[ '_caller_object' ];
175
        
176
        // Add sub-field index to the parent field path for repeated nested items.
177
        $aFieldset[ '_parent_field_path' ]   = self::getAOrB(
178
            $bHasSubFields,
179
            $aFieldset[ '_parent_field_path' ] . '|' . $iSubFieldIndex,
180
            $aFieldset[ '_parent_field_path' ]
181
        );
182
        $aFieldset[ '_parent_tag_id' ]       = self::getAOrB(
183
            $bHasSubFields,
184
            $aParentFieldset[ 'tag_id' ] . '__' . $iSubFieldIndex,
185
            $aParentFieldset[ 'tag_id' ]
186
        );
187
        
188
        // Re-format the field-set definition array to re-construct field path and relevant attribute IDs and names.
189
        $_oFieldsetFormatter = new AdminPageFramework_Form_Model___Format_Fieldset(
190
            $aFieldset, 
191
            $aFieldset[ '_structure_type' ],
192
            $aFieldset[ 'capability' ], 
193
            ( integer ) $iSubFieldIndex + 1,   // 1-based count (not index)
194
            $aFieldset[ '_subsection_index' ], 
195
            $aFieldset[ '_is_section_repeatable' ],
196
            $aFieldset[ '_caller_object' ]
197
        );                        
198
        $aFieldset = $_oFieldsetFormatter->get();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $aFieldset. This often makes code more readable.
Loading history...
199
        
200
        $_oFieldsetOutputFormatter = new AdminPageFramework_Form_Model___Format_FieldsetOutput(
201
            $aFieldset,
202
            $aFieldset[ '_section_index' ],    // `_section_index` is defined in the ...FieldsetOutput class. Since this is a nested item, it should be already set.
203
            $_oCallerForm->aFieldTypeDefinitions
204
        );         
205
        return $_oFieldsetOutputFormatter->get();
206
    
207
    }    
208
    
209
    /**
210
     * Checks whether the field placement is normal.
211
     * 
212
     * @since       3.8.0
213
     * @internal
214
     * @return      boolean
215
     */
216
    static public function isNormalPlacement( array $aFieldset ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
217
        
218
        if ( 'section_title' === $aFieldset[ 'type' ] ) { 
219
            return false;
220
        }
221
        
222
        return 'normal' === $aFieldset[ 'placement' ];  
223
        
224
    }    
225
    
226
    
227
}
228