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