Completed
Branch dev (1ad2e5)
by
unknown
05:14
created

AdminPageFramework_Form_View___FieldTitle::get()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 29
nc 3
nop 0
dl 0
loc 41
rs 8.5806
c 2
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 render a field title
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Common/Form/View/Field
15
 * @extends     AdminPageFramework_Form_Utility
16
 * @since       3.8.0 
17
 * @internal
18
 */
19
class AdminPageFramework_Form_View___FieldTitle extends AdminPageFramework_Form_Utility {
20
21
    public $aFieldset = array();
22
23
    /**
24
     * Stores additional class selectors 
25
     */
26
    public $aClassSelectors = array();
27
    
28
    public $aSavedData = array();
29
    
30
    public $aFieldErrors = array();
31
    public $aFieldTypeDefinitions = array();
32
    
33
    public $aCallbacks = array();
34
    public $oMsg = array();
35
    
36
    /**
37
     * Sets up properties.
38
     * @since       3.8.0
39
     */
40
    public function __construct( /* array $aFieldset, $aClassSelectors, $aSavedData, $aFieldErrors, $aFieldTypeDefinitions, $aCallbacks, $oMsg */ ) {
41
42
        $_aParameters = func_get_args() + array( 
43
            $this->aFieldset, 
44
            $this->aClassSelectors, 
45
            $this->aSavedData,   
46
            $this->aFieldErrors, 
47
            $this->aFieldTypeDefinitions, 
48
            $this->aCallbacks, 
49
            $this->oMsg            
50
        );
51
        $this->aFieldset                = $_aParameters[ 0 ];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aParameters[0] of type * is incompatible with the declared type array of property $aFieldset.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        $this->aClassSelectors          = $_aParameters[ 1 ];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aParameters[1] of type * is incompatible with the declared type array of property $aClassSelectors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
        $this->aSavedData               = $_aParameters[ 2 ];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aParameters[2] of type * is incompatible with the declared type array of property $aSavedData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->aFieldErrors             = $_aParameters[ 3 ];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aParameters[3] of type * is incompatible with the declared type array of property $aFieldErrors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        $this->aFieldTypeDefinitions    = $_aParameters[ 4 ];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aParameters[4] of type * is incompatible with the declared type array of property $aFieldTypeDefinitions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        $this->aCallbacks               = $_aParameters[ 5 ];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aParameters[5] of type * is incompatible with the declared type array of property $aCallbacks.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->oMsg                     = $_aParameters[ 6 ];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aParameters[6] of type * is incompatible with the declared type array of property $oMsg.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        
59
    }
60
61
    /**
62
     * Returns a field title.
63
     * 
64
     * @since       3.8.0
65
     * @return      string      The output of a field title.
66
     */
67
    public function get() {
68
        
69
        $_sOutput = '';
70
        
71
        $aField = $this->aFieldset;
72
        
73
        if ( ! $aField[ 'show_title_column' ] ) {
74
            return '';
75
        }          
76
            
77
        $_oInputTagIDGenerator = new AdminPageFramework_Form_View___Generate_FieldInputID( 
78
            $aField,
79
            0   // the first item
80
        );
81
        
82
        $_aLabelAttributes = array(
83
            'class' => $this->getClassAttribute( 'admin-page-framework-field-title', $this->aClassSelectors ),
84
            'for'   => $_oInputTagIDGenerator->get(),
85
        );
86
        $_sOutput .= $aField[ 'title' ]
87
            ? "<label " . $this->getAttributes( $_aLabelAttributes ) . "'>"
88
                    . "<a id='{$aField[ 'field_id' ]}'></a>"  // to allow the browser to link to the element.
89
                    . "<span title='" 
90
                            . esc_attr( 
91
                                strip_tags( 
92
                                    is_array( $aField[ 'description' ] )
93
                                        ? implode( '&#10;', $aField[ 'description' ] )
94
                                        : $aField[ 'description' ] 
95
                                )
96
                            ) 
97
                        . "'>"
98
                            . $aField[ 'title' ]
99
                            . $this->_getTitleColon( $aField )
100
                            . $this->_getToolTip( $aField[ 'tip' ], $aField[ 'field_id' ] )
101
                    . "</span>"
102
                . "</label>"
103
            : '';
104
        
105
        $_sOutput .= $this->_getFieldOutputsInFieldTitleAreaFromNestedFields( $aField );
106
        return $_sOutput;
107
    }    
108
109
        /**
110
         * Generates the field outputs from the nested fields with the `placement` argument of the value of `field_title`.
111
         * @return      string
112
         * @since       3.8.0
113
         * @internal
114
         */
115
        private function _getFieldOutputsInFieldTitleAreaFromNestedFields( $aField ) {
116
            
117
            if ( ! $this->hasNestedFields( $aField ) ) {
118
                return '';
119
            }
120
            
121
            $_sOutput = '';
122
            foreach( $aField[ 'content' ] as $_aNestedField ) {
123
                
124
                if ( 'field_title' !== $_aNestedField[ 'placement' ] ) {
125
                    continue;
126
                }
127
128
                $_oFieldset = new AdminPageFramework_Form_View___Fieldset( 
129
                    $_aNestedField, 
130
                    $this->aSavedData,    // passed by reference. @todo: examine why it needs to be passed by reference.
131
                    $this->aFieldErrors, 
132
                    $this->aFieldTypeDefinitions,
133
                    $this->oMsg,
0 ignored issues
show
Documentation introduced by
$this->oMsg is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
                    $this->aCallbacks // field output element callables.
135
                );
136
                $_sOutput   .= $_oFieldset->get(); // field output                
137
138
            }
139
            return $_sOutput;
140
            
141
        }
142
    
143
        /**
144
         * @return      string
145
         * @since       3.7.0
146
         */
147
        private function _getToolTip( $asTip, $sElementID ) {
148
            $_oToolTip           = new AdminPageFramework_Form_View___ToolTip(
149
                $asTip,
150
                $sElementID
151
            );            
152
            return $_oToolTip->get();
153
        }
154
        
155
        /**
156
         * @since       3.7.0
157
         * @return      string
158
         */
159
        private function _getTitleColon( $aField ) {
160
            
161
            if ( ! isset( $aField[ 'title' ] ) || '' === $aField[ 'title' ] ) {
162
                return '';
163
            }                    
164
            if ( 
165
                in_array( 
166
                    $aField[ '_structure_type' ], 
167
                    array( 'widget', 'post_meta_box', 'page_meta_box' ) 
168
                ) 
169
            ){
170
                return "<span class='title-colon'>:</span>" ;
171
            }                                                 
172
            
173
        }
174
    
175
}
176