2015-02-04 12:18:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-04 15:05:40 +00:00
|
|
|
* Flash Helper
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2015-02-04 12:18:31 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2015-02-04 12:18:31 +00:00
|
|
|
* @package Cake.View.Helper
|
|
|
|
* @since CakePHP(tm) v 2.7.0-dev
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2015-02-04 12:18:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('AppHelper', 'View/Helper');
|
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FlashHelper class to render flash messages.
|
|
|
|
*
|
|
|
|
* After setting messages in your controllers with FlashComponent, you can use
|
|
|
|
* this class to output your flash messages in your views.
|
|
|
|
*
|
|
|
|
* @package Cake.View.Helper
|
|
|
|
*/
|
|
|
|
class FlashHelper extends AppHelper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to render the message set in FlashComponent::set()
|
|
|
|
*
|
|
|
|
* In your view: $this->Flash->render('somekey');
|
|
|
|
* Will default to flash if no param is passed
|
|
|
|
*
|
|
|
|
* You can pass additional information into the flash message generation. This allows you
|
|
|
|
* to consolidate all the parameters for a given type of flash message into the view.
|
|
|
|
*
|
|
|
|
* ```
|
2015-02-04 15:05:40 +00:00
|
|
|
* echo $this->Flash->render('flash', array('params' => array('name' => $user['User']['name'])));
|
2015-02-04 12:18:31 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This would pass the current user's name into the flash message, so you could create personalized
|
|
|
|
* messages without the controller needing access to that data.
|
|
|
|
*
|
|
|
|
* Lastly you can choose the element that is used for rendering the flash message. Using
|
|
|
|
* custom elements allows you to fully customize how flash messages are generated.
|
|
|
|
*
|
|
|
|
* ```
|
2015-02-04 15:05:40 +00:00
|
|
|
* echo $this->Flash->render('flash', array('element' => 'my_custom_element'));
|
2015-02-04 12:18:31 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* If you want to use an element from a plugin for rendering your flash message
|
|
|
|
* you can use the dot notation for the plugin's element name:
|
|
|
|
*
|
|
|
|
* ```
|
2015-02-04 15:05:40 +00:00
|
|
|
* echo $this->Flash->render('flash', array(
|
2015-02-04 12:18:31 +00:00
|
|
|
* 'element' => 'MyPlugin.my_custom_element',
|
2015-02-04 15:05:40 +00:00
|
|
|
* ));
|
2015-02-04 12:18:31 +00:00
|
|
|
* ```
|
|
|
|
*
|
2015-06-24 11:06:35 +00:00
|
|
|
* @param string $key The [Message.]key you are rendering in the view.
|
2015-02-04 12:18:31 +00:00
|
|
|
* @param array $options Additional options to use for the creation of this flash message.
|
|
|
|
* Supports the 'params', and 'element' keys that are used in the helper.
|
|
|
|
* @return string|null Rendered flash message or null if flash key does not exist
|
|
|
|
* in session.
|
|
|
|
* @throws UnexpectedValueException If value for flash settings key is not an array.
|
|
|
|
*/
|
|
|
|
public function render($key = 'flash', $options = array()) {
|
2015-06-24 11:06:35 +00:00
|
|
|
if (!CakeSession::check("Message.$key")) {
|
2015-09-25 15:11:20 +00:00
|
|
|
return null;
|
2015-02-04 12:18:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 11:06:35 +00:00
|
|
|
$flash = CakeSession::read("Message.$key");
|
2015-02-04 12:18:31 +00:00
|
|
|
|
|
|
|
if (!is_array($flash)) {
|
|
|
|
throw new UnexpectedValueException(sprintf(
|
|
|
|
'Value for flash setting key "%s" must be an array.',
|
|
|
|
$key
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:06:35 +00:00
|
|
|
CakeSession::delete("Message.$key");
|
|
|
|
|
2016-10-27 21:39:21 +00:00
|
|
|
$out = '';
|
|
|
|
foreach ($flash as $message) {
|
|
|
|
$message['key'] = $key;
|
|
|
|
$message = $options + $message;
|
2017-03-25 08:12:28 +00:00
|
|
|
if ($message['element'] === 'default') {
|
|
|
|
$message['element'] = 'Flash/default';
|
|
|
|
}
|
2016-10-27 21:39:21 +00:00
|
|
|
$out .= $this->_View->element($message['element'], $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
2015-02-04 12:18:31 +00:00
|
|
|
}
|
|
|
|
}
|