Completed
Branch dev (869f5d)
by
unknown
04:33
created

AdminPageFramework_Form_Base::isSection()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 35
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 8
nop 1
dl 0
loc 35
rs 6.7272
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 class.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Form
15
 * @since       3.7.0
16
 */
17
abstract class AdminPageFramework_Form_Base extends AdminPageFramework_Form_Utility {
18
    
19
    /**
20
     * Stores resource items. 
21
     * 
22
     * @internal
23
     */
24
    static public $_aResources = array(
25
        'inline_styles'    => array(),
26
        'inline_styles_ie' => array(),
27
        'inline_scripts'   => array(),
28
        'src_styles'       => array(),
29
        'src_scripts'      => array(),
30
    );    
31
    
32
    /**
33
     * Checks if a given array holds fieldsets or not.
34
     * @return      boolean
35
     */
36
    public function isFieldsets( array $aItems ) {
37
        $_aItem = $this->getFirstElement( $aItems );
38
        return isset( $_aItem[ 'type' ], $_aItem[ 'field_id' ], $_aItem[ 'section_id' ] );
39
    }    
40
    
41
    /**
42
     * Determines whether the given ID is of a registered form section.
43
     * 
44
     * Consider the possibility that the given ID may be used both for a section and a field.
45
     * 
46
     * 1. Check if the given ID is not a section.
47
     * 2. Parse stored fields and check their ID. If one matches, return false.
48
     * 
49
     * @since       3.0.0
50
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Base`.
51
     */
52
    public function isSection( $sID ) {
53
// @todo Find a way for nested sections.        
54
        // Integer IDs are not accepted as they are reserved for sub-sections.
55
        if ( $this->isNumericInteger( $sID ) ) {
56
            return false;
57
        }
58
        
59
        // If the section ID is not registered, return false.
60
        if ( ! array_key_exists( $sID, $this->aSectionsets ) ) { 
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...
61
            return false; 
62
        }
63
        
64
        // the fields array's first dimension is also filled with the keys of section ids.
65
        if ( ! array_key_exists( $sID, $this->aFieldsets ) ) { 
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...
66
            return false; 
67
        }
68
        
69
        // Since numeric IDs are denied at the beginning of the method, the elements will not be sub-sections.
70
        $_bIsSeciton = false;
71
        foreach( $this->aFieldsets as $_sSectionID => $_aFields ) {    
72
        
73
            if ( $_sSectionID == $sID ) { 
74
                $_bIsSeciton = true; 
75
            }
76
            
77
            // a field using the ID is found, and it precedes a section match.     
78
            if ( array_key_exists( $sID, $_aFields ) ) { 
79
                return false; 
80
            }
81
            
82
        }
83
        
84
        return $_bIsSeciton;
85
        
86
    }        
87
    
88
    /**
89
     * Decides whether the current user including guests can view the form or not.
90
     * 
91
     * To allow guests to view the form set an empty value to it.
92
     * 
93
     * @since       3.7.0
94
     * @return      boolean
95
     */
96
    public function canUserView( $sCapability ) {
97
        
98
        if ( ! $sCapability  ) {
99
            return true;
100
        }
101
        
102
        return ( boolean ) current_user_can( $sCapability );
103
        
104
    }
105
106
    /**
107
     * Decides whether the form elements should be registered or not.
108
     * 
109
     * @access      public      A delegation class accesses this method so it must be public.
110
     * @since       3.7.0
111
     * @return      boolean
112
     */
113
    public function isInThePage() {
114
        return $this->callBack(
115
            $this->aCallbacks[ 'is_in_the_page' ], 
0 ignored issues
show
Bug introduced by
The property aCallbacks 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...
116
            true
117
        );
118
    }    
119
    
120
    /**
121
     * Calls back a user defined function
122
     * 
123
     * @remark      Set a default return value to the first element of the parameter array.
124
     * @since       3.7.0
125
     */
126
    public function callBack( $oCallable, $asParameters ) {
127
        $_aParameters   = self::getAsArray( 
128
            $asParameters, 
129
            true // preserve empty
130
        );
131
        $_mDefaultValue = self::getElement( $_aParameters, 0 );
132
        return is_callable( $oCallable )
133
            ? call_user_func_array( $oCallable, $_aParameters )
134
            : $_mDefaultValue;
135
    }
136
137
    /**
138
     * Prevents the output from getting too long when the object is dumped.
139
     *
140
     * Field definition arrays contain the factory object reference and when the debug log method tries to dump it, the output gets too long.
141
     * So shorten it here.
142
     * 
143
     * @remark      Called when the object is called as a string.
144
     * @since       3.7.0
145
     */   
146
    public function __toString() {
147
        return $this->getObjectInfo( $this );        
148
    }
149
    
150
}
151