Completed
Branch master (0afc36)
by
unknown
07:41 queued 03:56
created

AdminPageFramework_Form::___setSubClassObjects()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 8
nop 0
dl 0
loc 11
rs 9.2
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 to build forms.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Common/Form
15
 * @since       3.7.0
16
 * @internal
17
 */
18
class AdminPageFramework_Form extends AdminPageFramework_Form_Controller {
19
        
20
    /**
21
     * Indicates the structure type of the form.
22
     * 
23
     * The form input names and ids are constructed by this type.
24
     * 
25
     * The main routine which instantiates this class shuld have its own structure type slug 
26
     * such as `admin_page`, `widget`, `page_meta_box`, `post_meta_box`, `network` etc.
27
     * 
28
     * Each extended class should override this value.
29
     */
30
    public $sStructureType = '';
31
        
32
    /**
33
     * Stores field type definitions.
34
     * 
35
     * This will be set when fields are registered.
36
     */
37
    public $aFieldTypeDefinitions = array();
38
    
39
    /**
40
     * Stores section set definitions.
41
     */
42
    public $aSectionsets  = array(
43
        '_default' => array(
44
            'section_id'    => '_default',
45
        ),
46
    );
47
48
    /**
49
     * Stores field set definitions.
50
     */    
51
    public $aFieldsets    = array();
52
    
53
    /**
54
     * Multi-dimensional array holding the saved form data.
55
     */
56
    public $aSavedData    = array();
57
        
58
    /**
59
     * The capability level of the form.
60
     * 
61
     * This value is set via a callback before the form gets rendered.
62
     * 
63
     * Each secitonset and fieldset has individual capability. If they are not set,
64
     * This value will be applied.
65
     */            
66
    public $sCapability = '';       // default - an empty string
67
    
68
    /**
69
     * Stores callback functions.
70
     * Each value will have a callback.
71
     */
72
    public $aCallbacks    = array(
73
    
74
        /**
75
         * @return      string      The form default capability level.    
76
         */
77
        'capability'    => null,
78
        
79
        /**
80
         * Decides whether the form elements should be registered or not.
81
         * 
82
         * Registration in this context means whether to load related scripts and styles.
83
         * Also, a callback will be performed when a field is registered.
84
         * 
85
         * @return     boolean
86
         */
87
        'is_in_the_page'    => null,
88
        
89
        /**
90
         * Decides whether the fieldset should load its resources.
91
         * 
92
         * Registration in this context means whether to load related scripts and styles 
93
         * and run associated callback upon filed type initialization.
94
         * @return      boolean
95
         */
96
        'is_fieldset_registration_allowed'  => null,
97
        
98
        /**
99
         * Called when a fieldset is parsed to add resources.
100
         * The main routine can use this hook to add help pane items.
101
         * @return      void
102
         */
103
        'load_fieldset_resource'        => null,
104
            
105
        /**
106
         * The saved form data.
107
         * @return     array
108
         */
109
        'saved_data'    => null,
110
        
111
        /**
112
         * Output 
113
         */
114
        'fieldset_output'       => null,
115
        'section_head_output'   => null,
116
        
117
        /**
118
         * Called when a sectionset gets formatted. 
119
         * Some factory classes such as the admin page have specific arguments such as `page_slug` and `tab_slug`.
120
         * This callback allows factory classes modify the formatted sectionset definition array.
121
         */
122
        'sectionset_before_output'     => null,
123
        'fieldset_before_output'       => null,
124
        
125
        /**
126
         * Decides whether the section is visible or not.
127
         * This will be called when the form gets rendered.
128
         * @return      boolen
129
         */
130
        'is_sectionset_visible'    => null,        
131
        /**
132
         * Decides whether the field is visible or not.
133
         * This will be called when the form gets rendered.
134
         * @return      boolean
135
         */
136
        'is_fieldset_visible'      => null,
137
        
138
        /**
139
         * Allows the main routine to modify form element definitions including sectionsets and fieldsets.
140
         */
141
        'secitonsets_before_registration'   => null,
142
        'fieldsets_before_registration'     => null,
143
        
144
        /**
145
         * Allows the main routine modify form element definitions 
146
         */
147
        'fieldset_after_formatting'     => null,
148
        'fieldsets_before_formatting'   => null,
149
        
150
        /**
151
         * Gets triggered after the form elements are registered and before the page gets rendered.
152
         * So use this hook to handle submitted form data and validate them.
153
         */
154
        'handle_form_data'         => null,
155
        
156
        /**
157
         * Determines whether to show debug information.
158
         */
159
        'show_debug_info'           => null,
160
        
161
        /**
162
         * Applies to form field errors array.
163
         * 
164
         * This is introduced to customize the field errors passed to the form rendering method,
165
         * especially for front-end forms which does not reload the page after the form validation process.
166
         * 
167
         * @since   3.8.11
168
         */
169
        'field_errors'              => null,
170
        
171
        /**
172
         * Field elements
173
         */
174
        'hfID'                              => null, // the input id attribute
175
        'hfTagID'                           => null, // the fields & fieldset & field row container id attribute
176
        'hfName'                            => null, // the input name attribute
177
        'hfNameFlat'                        => null, // the flat input name attribute
178
        'hfInputName'                       => null, // 3.6.0+   the field input name attribute
179
        'hfInputNameFlat'                   => null, // 3.6.0+   the flat field input name                 
180
        'hfClass'                           => null, // the class attribute
181
        'hfSectionName'                     => null, // 3.6.0+                            
182
        
183
    );
184
    
185
    /**
186
     * Stores the message object.
187
     */
188
    public $oMsg;
189
    
190
    /**
191
     * Stores arguments which define the behaviour of the class object. 
192
     */
193
    public $aArguments = array(
194
    
195
        /**
196
         * A caller id
197
         * 
198
         * This is used when field types are registered and for field error transient IDs to retrieve field errors.
199
         */    
200
        'caller_id'                     => '',
201
        
202
        /**
203
         * @var      string      The structure type of the form.
204
         */
205
        'structure_type'                => 'admin_page',
206
                
207
        /**
208
         * should return the action hook name that the class calls back 
209
         * to determine the action hook for form element registration and validation.
210
         * @return     string
211
         */
212
        'action_hook_form_registration' => 'current_screen',    
213
        
214
        /**
215
         * If `true` and if the form registration (loading resources) action hook is already triggerd,
216
         * the callback will be triggered right away. If `false`, it must be dealt manually. 
217
         * This allows an option of not auto-register. This is useful for widget forms that get called multiple times
218
         * so the form object need to be initialized many times. In that case, this value can be `false` 
219
         * so that it won't reset the options (form data) right away.
220
         */
221
        'register_if_action_already_done'   => true,
222
        
223
    );
224
    
225
    /**
226
     * A submit notice object.
227
     */
228
    public $oSubmitNotice;    
229
    
230
    /**
231
     * A field error object.
232
     */
233
    public $oFieldError;
234
    
235
    /**
236
     * Last inputs handler object.
237
     */
238
    public $oLastInputs;
239
    
240
    /**
241
     * Stores sub-object class names.
242
     * 
243
     * This allows extended form classes to define their own sub-classes.
244
     * For example, for front-end forms, setting notices and field errors may not need to use transients.
245
     * In that case, use a custom class to handle them. 
246
     * 
247
     * Also set an empty string to disable those objects.
248
     * 
249
     * @since       3.8.11
250
     */
251
    public $aSubClasses = array(
252
        'submit_notice' => 'AdminPageFramework_Form___SubmitNotice',
253
        'field_error'   => 'AdminPageFramework_Form___FieldError',
254
        'last_input'    => 'AdminPageFramework_Form_Model___LastInput',
255
        'message'       => 'AdminPageFramework_Message',
256
    );
257
        
258
    /**
259
     * Sets up properties.
260
     * @since       3.7.0
261
     */
262
    public function __construct( /* array $aArguments, array $aCallbacks=array(), $oMsg */ ) {
263
        
264
        $_aParameters = func_get_args() + array(
265
            $this->aArguments,
266
            $this->aCallbacks,
267
            $this->oMsg,
268
        );
269
        $this->aArguments     = $this->___getArgumentsFormatted( $_aParameters[ 0 ] );
270
        $this->aCallbacks     = $this->getAsArray( $_aParameters[ 1 ] ) + $this->aCallbacks;
271
        $this->oMsg           = $_aParameters[ 2 ] ? $_aParameters[ 2 ] : new $this->aSubClasses[ 'message' ];
272
        
273
        // Sub-class objects
274
        $this->___setSubClassObjects();
275
                
276
        parent::__construct();
277
        
278
        $this->construct();
279
        
280
    }
281
    
282
        /**
283
         * Formats the argument array.
284
         * @return      array       The formatted argument array.
285
         * @since       3.7.0
286
         */
287
        private function ___getArgumentsFormatted( $aArguments ) {
288
            
289
            $aArguments = $this->getAsArray( $aArguments ) 
290
                + $this->aArguments;
291
            $aArguments[ 'caller_id' ] = $aArguments[ 'caller_id' ]
292
                ? $aArguments[ 'caller_id' ]
293
                : get_class( $this );  // if a caller id is empty, this class name will be used.
294
            
295
            if ( $this->sStructureType ) {
296
                $aArguments[ 'structure_type' ] = $this->sStructureType;
297
            }
298
                
299
            return $aArguments;
300
            
301
        }
302
        
303
        /**
304
         * @since       3.8.11
305
         */
306
        private function ___setSubClassObjects() {
307
            if ( class_exists( $this->aSubClasses[ 'submit_notice' ] ) ) {                
308
                $this->oSubmitNotice  = new $this->aSubClasses[ 'submit_notice' ];
309
            }
310
            if ( class_exists( $this->aSubClasses[ 'field_error' ] ) ) {                
311
                $this->oFieldError    = new $this->aSubClasses[ 'field_error' ]( $this->aArguments[ 'caller_id' ] );
312
            }
313
            if ( class_exists( $this->aSubClasses[ 'last_input' ] ) ) {                
314
                $this->oLastInputs    = new $this->aSubClasses[ 'last_input' ]( $this->aArguments[ 'caller_id' ] );
315
            }
316
        }
317
        
318
    /**
319
     * User constructor.
320
     * Extended classes override this method to do set ups.
321
     * @since       3.7.0
322
     */
323
    public function construct() {}
324
    
325
}
326