Completed
Branch dev (1ad2e5)
by
unknown
04:33
created

AdminPageFramework_Form___SubmitNotice   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 207
rs 10
wmc 25
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A _getTransientKey() 0 3 1
B hasNotice() 0 23 4
C set() 0 33 7
A _replyToSaveNotices() 0 9 2
A render() 0 19 4
A _getNotices() 0 15 3
A _printNotices() 0 19 3
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 handle setting notices.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Form
15
 * @since       3.7.0
16
 * @extends     AdminPageFramework_FrameworkUtility
17
 * @internal
18
 */
19
class AdminPageFramework_Form___SubmitNotice extends AdminPageFramework_FrameworkUtility {
20
    
21
    /**
22
     * Stores form submit notifications.
23
     * 
24
     * At the script termination, these will be saved as a transient in the database.
25
     */
26
    static private $_aNotices = array();
27
    
28
    public $sTransientKey;
29
    
30
    /**
31
     * Sets up properties.
32
     */
33
    public function __construct() {
34
        
35
        $this->sTransientKey = $this->_getTransientKey();
36
        
37
    }
38
        /**
39
         * @remark      Up to 40 chars
40
         * @remark      Not using the page-now value nor page slug because the user may be redirected to a different page.
41
         * @return      string
42
         */
43
        private function _getTransientKey() {
44
            return 'apf_ntc_' . get_current_user_id();
45
        }
46
    
47
    /**
48
     * Checks if a submit notice has been set in the current screen (not in the database).
49
     * 
50
     * This is used in the internal validation callback method to decide whether the system error or update notice should be added or not.
51
     * If this method yields true, the framework discards the system message and displays the user set notification message.
52
     * 
53
     * @param       string      $sType If empty, the method will check if a message exists in all types. Otherwise, it checks the existence of a message of the specified type.
54
     * @return      boolean     True if a setting notice is set; otherwise, false.
55
     */
56
    public function hasNotice( $sType='' ) {
57
                
58
        if ( ! $sType ) {
59
            return ( boolean ) count( self::$_aNotices );
60
        }
61
        
62
        // Check if there is a message of the type.
63
        foreach( self::$_aNotices as $_aNotice ) {
64
            $_sClassAttribute = $this->getElement( 
65
                $_aNotice, 
66
                array( 
67
                    'aAttributes', 
68
                    'class' 
69
                ),
70
                ''
71
            );
72
            if ( $_sClassAttribute === $sType ) {
73
                return true;
74
            }
75
        }
76
        return false;
77
        
78
    }
79
    
80
    /**
81
     * Sets the given message to be displayed in the next page load. 
82
     * 
83
     * This is used to inform users about the submitted input data, such as "Updated successfully." or "Problem occurred." etc. 
84
     * and normally used in validation callback methods.
85
     * 
86
     * <h4>Example</h4>
87
     * `
88
     * if ( ! $bVerified ) {
89
     *       $this->setFieldErrors( $aErrors );     
90
     *       $this->setSettingNotice( 'There was an error in your input.' );
91
     *       return $aOldPageOptions;
92
     * }
93
     * `
94
     * @since        3.7.0
95
     * @access       public
96
     * @param        string      $sMessage       the text message to be displayed.
97
     * @param        string      $sType          (optional) the type of the message, either "error" or "updated"  is used.
98
     * @param        array       $asAttributes   (optional) the tag attribute array applied to the message container HTML element. If a string is given, it is used as the ID attribute value.
99
     * @param        boolean     $bOverride      (optional) If true, only one message will be shown in the next page load. false: do not override when there is a message of the same id. true: override the previous one.
100
     * @return       void
101
     */
102
    public function set( $sMessage, $sType='error', $asAttributes=array(), $bOverride=true ) {
103
        
104
        // If the array is empty, schedule the task of saving the array at shutdown.
105
        if ( empty( self::$_aNotices ) ) {
106
            add_action( 'shutdown', array( $this, '_replyToSaveNotices' ) ); // the method is defined in the model class.
107
        }
108
        
109
        $_sID = md5( trim( $sMessage ) );
110
            
111
        // If the override is false and a message is already set, do not add.
112
        if ( ! $bOverride && isset( self::$_aNotices[ $_sID ] ) ) {
113
            return;
114
        }
115
116
        // If the override is true, reset the notice array.
117
        if ( $bOverride ) {
118
            self::$_aNotices = array();
119
        }
120
        
121
        $_aAttributes = $this->getAsArray( $asAttributes );
122
        if ( is_string( $asAttributes ) && ! empty( $asAttributes ) ) {
123
            $_aAttributes[ 'id' ] = $asAttributes;
124
        }
125
        self::$_aNotices[ $_sID ] = array(
126
            'sMessage'      => $sMessage,
127
            'aAttributes'   => $_aAttributes + array(
128
                'class'     => $sType,
129
                'id'        => 'form_submit_notice_' . $_sID,
130
            ),
131
        );
132
133
134
    }       
135
        /**
136
         * Saves the notification array set via the setSettingNotice() method.
137
         * 
138
         * @since       3.0.4 
139
         * @sine        3.7.0      Moved from `AdminPageFramework_Factory_Model`.
140
         * @internal
141
         * @callback    action     shutdown
142
         * @return      void
143
         */
144
        public function _replyToSaveNotices() {
145
            if ( empty( self::$_aNotices ) ) { 
146
                return; 
147
            }            
148
            $_bResult = $this->setTransient( 
0 ignored issues
show
Unused Code introduced by
$_bResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
149
                $this->sTransientKey, 
150
                self::$_aNotices
151
            );
152
        }        
153
    
154
    /**
155
     * Outputs the stored submit notices in the database.
156
     * @return      void
157
     */
158
    public function render() {
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
159
160
        // This will load scripts for the fade-in effect.
161
        new AdminPageFramework_AdminNotice( '' );
162
        
163
        // Retrieve the notifications set in a transient.
164
        $_aNotices = $this->_getNotices();
165
        if ( false === $_aNotices ) { 
166
            return; 
167
        }
168
            
169
        // By setting false to the 'settings-notice' key, it's possible to disable the notifications set with the framework.
170
        if ( isset( $_GET[ 'settings-notice' ] ) && ! $_GET[ 'settings-notice' ] ) { 
171
            return; 
172
        }
173
            
174
        $this->_printNotices( $_aNotices );                
175
        
176
    }
177
        /**
178
         * @since       3.7.8
179
         * @return      array|boolean
180
         */
181
        private function _getNotices() {
182
        
183
            if ( isset( self::$_aNoticeCaches[ $this->sTransientKey ] ) ) {
184
                return self::$_aNoticeCaches[ $this->sTransientKey ];
185
            }
186
187
            $_abNotices = $this->getTransient( $this->sTransientKey );            
188
            if ( false !== $_abNotices ) {
189
                $this->deleteTransient( $this->sTransientKey );
190
            }
191
192
            self::$_aNoticeCaches[ $this->sTransientKey ] = $_abNotices;
193
            return self::$_aNoticeCaches[ $this->sTransientKey ];
194
            
195
        }
196
            static private $_aNoticeCaches = array();
197
            
198
        /**
199
         * Displays settings notices.
200
         * @since       3.5.3
201
         * @since       3.7.0      Moved from `AdminPageFramework_Factory_View`. Renamed from `_printSettingNotices()`.
202
         * @internal
203
         * @return      void
204
         */
205
        private function _printNotices( $aNotices ) {
206
            
207
            $_aPeventDuplicates = array();
208
            foreach ( array_filter( ( array ) $aNotices, 'is_array' ) as $_aNotice ) {
209
                
210
                $_sNotificationKey = md5( serialize( $_aNotice ) );
211
                if ( isset( $_aPeventDuplicates[ $_sNotificationKey ] ) ) {
212
                    continue;
213
                }
214
                $_aPeventDuplicates[ $_sNotificationKey ] = true;
215
                
216
                new AdminPageFramework_AdminNotice(
217
                    $this->getElement( $_aNotice, 'sMessage' ),
218
                    $this->getElement( $_aNotice, 'aAttributes' )
219
                );              
220
              
221
            }            
222
            
223
        }    
224
        
225
}
226