統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / View / Helper / SessionHelper.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (5.561 KB)

1
<?php
2
/**
3
 * Session Helper provides access to the Session in the Views.
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.View.Helper
15
 * @since         CakePHP(tm) v 1.1.7.3328
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18

    
19
App::uses('AppHelper', 'View/Helper');
20
App::uses('CakeSession', 'Model/Datasource');
21

    
22
/**
23
 * Session Helper.
24
 *
25
 * Session reading from the view.
26
 *
27
 * @package       Cake.View.Helper
28
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
29
 */
30
class SessionHelper extends AppHelper {
31

    
32
/**
33
 * Reads a session value for a key or returns values for all keys.
34
 *
35
 * In your view: `$this->Session->read('Controller.sessKey');`
36
 * Calling the method without a param will return all session vars
37
 *
38
 * @param string $name the name of the session key you want to read
39
 * @return mixed values from the session vars
40
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::read
41
 */
42
        public function read($name = null) {
43
                return CakeSession::read($name);
44
        }
45

    
46
/**
47
 * Reads and deletes a session value for a key.
48
 *
49
 * In your view: `$this->Session->consume('Controller.sessKey');`
50
 *
51
 * @param string $name the name of the session key you want to read
52
 * @return mixed values from the session vars
53
 */
54
        public function consume($name) {
55
                return CakeSession::consume($name);
56
        }
57

    
58
/**
59
 * Checks if a session key has been set.
60
 *
61
 * In your view: `$this->Session->check('Controller.sessKey');`
62
 *
63
 * @param string $name Session key to check.
64
 * @return bool
65
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::check
66
 */
67
        public function check($name) {
68
                return CakeSession::check($name);
69
        }
70

    
71
/**
72
 * Returns last error encountered in a session
73
 *
74
 * In your view: `$this->Session->error();`
75
 *
76
 * @return string last error
77
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages
78
 */
79
        public function error() {
80
                return CakeSession::error();
81
        }
82

    
83
/**
84
 * Used to render the message set in Controller::Session::setFlash()
85
 *
86
 * In your view: $this->Session->flash('somekey');
87
 * Will default to flash if no param is passed
88
 *
89
 * You can pass additional information into the flash message generation. This allows you
90
 * to consolidate all the parameters for a given type of flash message into the view.
91
 *
92
 * ```
93
 * echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
94
 * ```
95
 *
96
 * The above would generate a flash message with a custom class name. Using $attrs['params'] you
97
 * can pass additional data into the element rendering that will be made available as local variables
98
 * when the element is rendered:
99
 *
100
 * ```
101
 * echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
102
 * ```
103
 *
104
 * This would pass the current user's name into the flash message, so you could create personalized
105
 * messages without the controller needing access to that data.
106
 *
107
 * Lastly you can choose the element that is rendered when creating the flash message. Using
108
 * custom elements allows you to fully customize how flash messages are generated.
109
 *
110
 * ```
111
 * echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
112
 * ```
113
 *
114
 * If you want to use an element from a plugin for rendering your flash message you can do that using the
115
 * plugin param:
116
 *
117
 * ```
118
 * echo $this->Session->flash('flash', array(
119
 *                'element' => 'my_custom_element',
120
 *                'params' => array('plugin' => 'my_plugin')
121
 * ));
122
 * ```
123
 *
124
 * @param string $key The [Message.]key you are rendering in the view.
125
 * @param array $attrs Additional attributes to use for the creation of this flash message.
126
 *    Supports the 'params', and 'element' keys that are used in the helper.
127
 * @return string
128
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
129
 * @deprecated 3.0.0 Since 2.7, use FlashHelper::render() instead.
130
 */
131
        public function flash($key = 'flash', $attrs = array()) {
132
                $out = false;
133

    
134
                if (CakeSession::check('Message.' . $key)) {
135
                        $flash = CakeSession::read('Message.' . $key);
136
                        CakeSession::delete('Message.' . $key);
137
                        $message = $flash['message'];
138
                        unset($flash['message']);
139

    
140
                        if (!empty($attrs)) {
141
                                $flash = array_merge($flash, $attrs);
142
                        }
143

    
144
                        if ($flash['element'] === 'default') {
145
                                $class = 'message';
146
                                if (!empty($flash['params']['class'])) {
147
                                        $class = $flash['params']['class'];
148
                                }
149
                                $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
150
                        } elseif (!$flash['element']) {
151
                                $out = $message;
152
                        } else {
153
                                $options = array();
154
                                if (isset($flash['params']['plugin'])) {
155
                                        $options['plugin'] = $flash['params']['plugin'];
156
                                }
157
                                $tmpVars = $flash['params'];
158
                                $tmpVars['message'] = $message;
159
                                $tmpVars['key'] = $key;
160
                                $out = $this->_View->element($flash['element'], $tmpVars, $options);
161
                        }
162
                }
163
                return $out;
164
        }
165

    
166
/**
167
 * Used to check is a session is valid in a view
168
 *
169
 * @return bool
170
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::valid
171
 */
172
        public function valid() {
173
                return CakeSession::valid();
174
        }
175

    
176
}