1: <?php
2: /**
3: * Flash Component
4: *
5: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
13: * @link https://cakephp.org CakePHP(tm) Project
14: * @package Cake.Controller.Component
15: * @since CakePHP(tm) v 2.7.0-dev
16: * @license https://opensource.org/licenses/mit-license.php MIT License
17: */
18:
19: App::uses('Component', 'Controller');
20: App::uses('Inflector', 'Utility');
21: App::uses('CakeSession', 'Model/Datasource');
22:
23: /**
24: * The CakePHP FlashComponent provides a way for you to write a flash variable
25: * to the session from your controllers, to be rendered in a view with the
26: * FlashHelper.
27: *
28: * @package Cake.Controller.Component
29: */
30: class FlashComponent extends Component {
31:
32: /**
33: * Default configuration
34: *
35: * @var array
36: */
37: protected $_defaultConfig = array(
38: 'key' => 'flash',
39: 'element' => 'default',
40: 'params' => array(),
41: 'clear' => false
42: );
43:
44: /**
45: * Constructor
46: *
47: * @param ComponentCollection $collection The ComponentCollection object
48: * @param array $settings Settings passed via controller
49: */
50: public function __construct(ComponentCollection $collection, $settings = array()) {
51: $this->_defaultConfig = Hash::merge($this->_defaultConfig, $settings);
52: }
53:
54: /**
55: * Used to set a session variable that can be used to output messages in the view.
56: *
57: * In your controller: $this->Flash->set('This has been saved');
58: *
59: * ### Options:
60: *
61: * - `key` The key to set under the session's Flash key
62: * - `element` The element used to render the flash message. Default to 'default'.
63: * - `params` An array of variables to make available when using an element
64: *
65: * @param string $message Message to be flashed. If an instance
66: * of Exception the exception message will be used and code will be set
67: * in params.
68: * @param array $options An array of options.
69: * @return void
70: */
71:
72: public function set($message, $options = array()) {
73: $options += $this->_defaultConfig;
74:
75: if ($message instanceof Exception) {
76: $options['params'] += array('code' => $message->getCode());
77: $message = $message->getMessage();
78: }
79:
80: list($plugin, $element) = pluginSplit($options['element'], true);
81: if (!empty($options['plugin'])) {
82: $plugin = $options['plugin'] . '.';
83: }
84: $options['element'] = $plugin . 'Flash/' . $element;
85:
86: $messages = array();
87: if ($options['clear'] === false) {
88: $messages = (array)CakeSession::read('Message.' . $options['key']);
89: }
90:
91: $newMessage = array(
92: 'message' => $message,
93: 'key' => $options['key'],
94: 'element' => $options['element'],
95: 'params' => $options['params']
96: );
97:
98: $messages[] = $newMessage;
99:
100: CakeSession::write('Message.' . $options['key'], $messages);
101: }
102:
103: /**
104: * Magic method for verbose flash methods based on element names.
105: *
106: * For example: $this->Flash->success('My message') would use the
107: * success.ctp element under `app/View/Element/Flash` for rendering the
108: * flash message.
109: *
110: * @param string $name Element name to use.
111: * @param array $args Parameters to pass when calling `FlashComponent::set()`.
112: * @return void
113: * @throws InternalErrorException If missing the flash message.
114: */
115: public function __call($name, $args) {
116: $options = array('element' => Inflector::underscore($name));
117:
118: if (count($args) < 1) {
119: throw new InternalErrorException('Flash message missing.');
120: }
121:
122: if (!empty($args[1])) {
123: $options += (array)$args[1];
124: }
125:
126: $this->set($args[0], $options);
127: }
128: }
129: