Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * Flash Component
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.Controller.Component
15
 * @since         CakePHP(tm) v 2.7.0-dev
16
 * @license       http://www.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
	);
42
 
43
/**
44
 * Constructor
45
 *
46
 * @param ComponentCollection $collection The ComponentCollection object
47
 * @param array $settings Settings passed via controller
48
 */
49
	public function __construct(ComponentCollection $collection, $settings = array()) {
50
		$this->_defaultConfig = Hash::merge($this->_defaultConfig, $settings);
51
	}
52
 
53
/**
54
 * Used to set a session variable that can be used to output messages in the view.
55
 *
56
 * In your controller: $this->Flash->set('This has been saved');
57
 *
58
 * ### Options:
59
 *
60
 * - `key` The key to set under the session's Flash key
61
 * - `element` The element used to render the flash message. Default to 'default'.
62
 * - `params` An array of variables to make available when using an element
63
 *
64
 * @param string $message Message to be flashed. If an instance
65
 *   of Exception the exception message will be used and code will be set
66
 *   in params.
67
 * @param array $options An array of options.
68
 * @return void
69
 */
70
 
71
	public function set($message, $options = array()) {
72
		$options += $this->_defaultConfig;
73
 
74
		if ($message instanceof Exception) {
75
			$options['params'] += array('code' => $message->getCode());
76
			$message = $message->getMessage();
77
		}
78
 
79
		list($plugin, $element) = pluginSplit($options['element']);
80
 
81
		if ($plugin) {
82
			$options['element'] = $plugin . '.Flash/' . $element;
83
		} else {
84
			$options['element'] = 'Flash/' . $element;
85
		}
86
 
87
		CakeSession::write('Message.' . $options['key'], array(
88
			'message' => $message,
89
			'key' => $options['key'],
90
			'element' => $options['element'],
91
			'params' => $options['params']
92
		));
93
	}
94
 
95
/**
96
 * Magic method for verbose flash methods based on element names.
97
 *
98
 * For example: $this->Flash->success('My message') would use the
99
 * success.ctp element under `app/View/Element/Flash` for rendering the
100
 * flash message.
101
 *
102
 * @param string $name Element name to use.
103
 * @param array $args Parameters to pass when calling `FlashComponent::set()`.
104
 * @return void
105
 * @throws InternalErrorException If missing the flash message.
106
 */
107
	public function __call($name, $args) {
108
		$options = array('element' => Inflector::underscore($name));
109
 
110
		if (count($args) < 1) {
111
			throw new InternalErrorException('Flash message missing.');
112
		}
113
 
114
		if (!empty($args[1])) {
115
			$options += (array)$args[1];
116
		}
117
 
118
		$this->set($args[0], $options);
119
	}
120
}