Completed
Branch dev (f35023)
by
unknown
03:44
created

getModalForDisabledRepeatableElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 14
rs 9.4285
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 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 ) {
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 ) {
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 ) {
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 ) {
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 ) {
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 ) {
124
        
125
        if ( count( $aFields ) > 1 ) {
126
            return true;
127
        }
128
        if ( self::isDynamicField( $aField ) ) {
129
            return true;
130
        }
131
        return false;
132
        
133
    }
134
135
    /**
136
     * Checks whether the given field is dynamic.
137
     * @return      boolean
138
     * @since       3.8.13
139
     */
140
    static public function isDynamicField( $aField ) {
141
        if ( ! empty( $aField[ 'repeatable' ] ) ) {
142
            return true;
143
        }
144
        if ( ! empty( $aField[ 'sortable' ] ) ) {
145
            return true;
146
        }
147
        return false;
148
    }
149
    
150
    /**
151
     * Checks whether the parent field is repeatable or sortable.
152
     * 
153
     * @since       3.8.0
154
     * @return      boolean
155
     * @deprecated  3.8.0
156
     */
157
    // static public function isParentFieldDynamic( $aFieldset ) {
158
// return false;        
159
    // }
160
161
    /**
162
     * Adds a trailing pipe (|) character to the given string value.
163
     *
164
     * Used to construct a field path.
165
     *
166
     * @return      string
167
     * @since       3.8.0
168
     */
169
    static public function getTrailingPipeCharacterAppended( $sString ) {
170
        if ( empty( $sString ) ) {
171
            return $sString;
172
        }
173
        return $sString . '|';
174
    }
175
176
    /**
177
     * 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.
178
     *
179
     * It is assumed that the passed field-set definition array is already formatted as this is for sub-fields of nested field-sets.
180
     *
181
     * This is used for nested and inline-mixed field types.
182
     *
183
     * @internal
184
     * @since       3.8.0
185
     * @return      array
186
     */
187
    static public function getFieldsetReformattedBySubFieldIndex( array $aFieldset, $iSubFieldIndex, $bHasSubFields, array $aParentFieldset ) {
188
189
        $_oCallerForm   = $aFieldset[ '_caller_object' ];
190
191
        // Add sub-field index to the parent field path for repeated nested items.
192
        $aFieldset[ '_parent_field_path' ]   = self::getAOrB(
193
            $bHasSubFields,
194
            $aFieldset[ '_parent_field_path' ] . '|' . $iSubFieldIndex,
195
            $aFieldset[ '_parent_field_path' ]
196
        );
197
        $aFieldset[ '_parent_tag_id' ]       = self::getAOrB(
198
            $bHasSubFields,
199
            $aParentFieldset[ 'tag_id' ] . '__' . $iSubFieldIndex,
200
            $aParentFieldset[ 'tag_id' ]
201
        );
202
203
        // Re-format the field-set definition array to re-construct field path and relevant attribute IDs and names.
204
        $_oFieldsetFormatter = new AdminPageFramework_Form_Model___Format_Fieldset(
205
            $aFieldset,
206
            $aFieldset[ '_structure_type' ],
207
            $aFieldset[ 'capability' ],
208
            ( integer ) $iSubFieldIndex + 1,   // 1-based count (not index)
209
            $aFieldset[ '_subsection_index' ],
210
            $aFieldset[ '_is_section_repeatable' ],
211
            $aFieldset[ '_caller_object' ]
212
        );
213
        $aFieldset = $_oFieldsetFormatter->get();
214
215
        $_oFieldsetOutputFormatter = new AdminPageFramework_Form_Model___Format_FieldsetOutput(
216
            $aFieldset,
217
            $aFieldset[ '_section_index' ],    // `_section_index` is defined in the ...FieldsetOutput class. Since this is a nested item, it should be already set.
218
            $_oCallerForm->aFieldTypeDefinitions
219
        );
220
        return $_oFieldsetOutputFormatter->get();
221
222
    }
223
224
    /**
225
     * Checks whether the field placement is normal.
226
     *
227
     * @since       3.8.0
228
     * @internal
229
     * @return      boolean
230
     */
231
    static public function isNormalPlacement( array $aFieldset ) {
232
233
        if ( 'section_title' === $aFieldset[ 'type' ] ) {
234
            return false;
235
        }
236
        return 'normal' === $aFieldset[ 'placement' ];
237
238
    }
239
240
    /**
241
     * Generates an HTML element for the WordPress modal window to display user-defined message
242
     * for disabled repeatable elements including repeatable sections and fields.
243
     *
244
     * @since   3.8.13
245
     * @param   string  $sBoxElementID
246
     * @param   array   $aArguments         The `disabled` argument of a repeatable field/section.
247
     * @return  string  A generated container element for the modal window.
248
     */
249
    static public function getModalForDisabledRepeatableElement( $sBoxElementID, $aArguments ) {
250
251
        if ( empty( $aArguments ) ) {
252
            return '';
253
        }
254
        if ( self::hasBeenCalled( 'disabled_repeatable_elements_modal_' . $sBoxElementID ) ) {
255
            return '';
256
        }
257
        add_thickbox(); // to display a message to the user.
258
        return "<div id='{$sBoxElementID}' style='display:none'>"
259
                . "<p>" . $aArguments[ 'label' ] . "</p>"
260
            . "</div>";
261
262
    }
263
    
264
}
265