|
@@ -14,6 +14,35 @@ class FlashComponent extends Component {
|
|
|
|
|
|
|
|
public $components = array('Session');
|
|
public $components = array('Session');
|
|
|
|
|
|
|
|
|
|
+ protected $_defaultConfig = array(
|
|
|
|
|
+ 'headerOnAjax' => true,
|
|
|
|
|
+ 'transformCore' => true,
|
|
|
|
|
+ 'useElements' => false, // Set to true to use 3.x flash message rendering via Elements
|
|
|
|
|
+ 'type' => 'info',
|
|
|
|
|
+ 'typeToElement' => false, // Set to true to have a single type to Element matching
|
|
|
|
|
+ 'plugin' => null, // Only for typeToElement
|
|
|
|
|
+ 'element' => 'Tools.default',
|
|
|
|
|
+ 'params' => [],
|
|
|
|
|
+ 'escape' => false
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Constructor.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ComponentCollection $collection
|
|
|
|
|
+ * @param array $config
|
|
|
|
|
+ */
|
|
|
|
|
+ public function __construct(ComponentCollection $collection, $config = array()) {
|
|
|
|
|
+ $defaults = (array)Configure::read('Flash') + $this->_defaultConfig;
|
|
|
|
|
+ //BC
|
|
|
|
|
+ if (Configure::read('Common.messages') !== null) {
|
|
|
|
|
+ $defaults['transformCore'] = Configure::read('Common.messages');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $config += $defaults;
|
|
|
|
|
+ parent::__construct($collection, $config);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* For automatic startup
|
|
* For automatic startup
|
|
|
* for this helper the controller has to be passed as reference
|
|
* for this helper the controller has to be passed as reference
|
|
@@ -30,18 +59,22 @@ class FlashComponent extends Component {
|
|
|
* Called after the Controller::beforeRender(), after the view class is loaded, and before the
|
|
* Called after the Controller::beforeRender(), after the view class is loaded, and before the
|
|
|
* Controller::render()
|
|
* Controller::render()
|
|
|
*
|
|
*
|
|
|
|
|
+ * Unless Configure::read('Ajax.transformCore') is false, it will also transform any core ones to this plugin.
|
|
|
|
|
+ * Unless Configure::read('Ajax.headerOnAjax') is false, it will pass the messages as header to AJAX requests.
|
|
|
|
|
+ * Set it to false if other components are handling the message return in AJAX use cases already.
|
|
|
|
|
+ *
|
|
|
* @param object $Controller Controller with components to beforeRender
|
|
* @param object $Controller Controller with components to beforeRender
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
public function beforeRender(Controller $Controller) {
|
|
public function beforeRender(Controller $Controller) {
|
|
|
- if (Configure::read('Common.messages') !== false && $messages = $this->Session->read('Message')) {
|
|
|
|
|
|
|
+ if ($this->settings['transformCore'] && $messages = $this->Session->read('Message')) {
|
|
|
foreach ($messages as $message) {
|
|
foreach ($messages as $message) {
|
|
|
$this->message($message['message'], 'error');
|
|
$this->message($message['message'], 'error');
|
|
|
}
|
|
}
|
|
|
$this->Session->delete('Message');
|
|
$this->Session->delete('Message');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if ($this->Controller->request->is('ajax')) {
|
|
|
|
|
|
|
+ if ($this->settings['headerOnAjax'] && $this->Controller->request->is('ajax')) {
|
|
|
$ajaxMessages = array_merge(
|
|
$ajaxMessages = array_merge(
|
|
|
(array)$this->Session->read('messages'),
|
|
(array)$this->Session->read('messages'),
|
|
|
(array)Configure::read('messages')
|
|
(array)Configure::read('messages')
|
|
@@ -57,16 +90,21 @@ class FlashComponent extends Component {
|
|
|
* Adds a flash message.
|
|
* Adds a flash message.
|
|
|
* Updates "messages" session content (to enable multiple messages of one type).
|
|
* Updates "messages" session content (to enable multiple messages of one type).
|
|
|
*
|
|
*
|
|
|
|
|
+ * ### Options:
|
|
|
|
|
+ *
|
|
|
|
|
+ * - `element` The element used to render the flash message. Default to 'default'.
|
|
|
|
|
+ * - `params` An array of variables to make available when using an element.
|
|
|
|
|
+ * - `escape` If content should be escaped or not in the element itself or if elements are not used in the component.
|
|
|
|
|
+ *
|
|
|
* @param string $message Message to output.
|
|
* @param string $message Message to output.
|
|
|
- * @param string $type Type ('error', 'warning', 'success', 'info' or custom class).
|
|
|
|
|
|
|
+ * @param array|string $options Options or Type ('error', 'warning', 'success', 'info' or custom class).
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
- public function message($message, $type = null) {
|
|
|
|
|
- if (!$type) {
|
|
|
|
|
- $type = 'info';
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public function message($message, $options = array()) {
|
|
|
|
|
+ $message = $this->_prepMessage($message, $options);
|
|
|
|
|
|
|
|
$old = (array)$this->Session->read('messages');
|
|
$old = (array)$this->Session->read('messages');
|
|
|
|
|
+ $type = $options['type'];
|
|
|
if (isset($old[$type]) && count($old[$type]) > 99) {
|
|
if (isset($old[$type]) && count($old[$type]) > 99) {
|
|
|
array_shift($old[$type]);
|
|
array_shift($old[$type]);
|
|
|
}
|
|
}
|
|
@@ -75,20 +113,104 @@ class FlashComponent extends Component {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * FlashComponent::_prepMessage()
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $message
|
|
|
|
|
+ * @param array $options
|
|
|
|
|
+ * @return array
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function _prepMessage($message, &$options) {
|
|
|
|
|
+ if (!is_array($options)) {
|
|
|
|
|
+ $type = $options ?: $this->settings['type'];
|
|
|
|
|
+ $options = array('type' => $type);
|
|
|
|
|
+ }
|
|
|
|
|
+ $defaults = $this->settings;
|
|
|
|
|
+ $options += $defaults;
|
|
|
|
|
+
|
|
|
|
|
+ $message = array(
|
|
|
|
|
+ 'message' => $options['escape'] ? h($message) : $message,
|
|
|
|
|
+ 'params' => $options['params'],
|
|
|
|
|
+ 'escape' => $options['escape']
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->settings['useElements']) {
|
|
|
|
|
+ if ($options['typeToElement'] && $options['element'] === $defaults['element']) {
|
|
|
|
|
+ $options['element'] = ($options['plugin'] ? $options['plugin'] . '.' : '') . $options['type'];
|
|
|
|
|
+ }
|
|
|
|
|
+ list($plugin, $element) = pluginSplit($options['element']);
|
|
|
|
|
+ if ($plugin) {
|
|
|
|
|
+ $message['element'] = $plugin . '.Flash/' . $element;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $message['element'] = 'Flash/' . $element;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Simplify?
|
|
|
|
|
+ if (!$message['escape'] && !$message['params']) {
|
|
|
|
|
+ $message = $message['message'];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return $message;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* Adds a transient flash message.
|
|
* Adds a transient flash message.
|
|
|
* These flash messages that are not saved (only available for current view),
|
|
* These flash messages that are not saved (only available for current view),
|
|
|
* will be merged into the session flash ones prior to output.
|
|
* will be merged into the session flash ones prior to output.
|
|
|
*
|
|
*
|
|
|
|
|
+ * Since this method can be accessed statically, it only works with Configure configuration,
|
|
|
|
|
+ * not with runtime config as the normal message() method.
|
|
|
|
|
+ *
|
|
|
|
|
+ * ### Options:
|
|
|
|
|
+ *
|
|
|
|
|
+ * - `element` The element used to render the flash message. Default to 'default'.
|
|
|
|
|
+ * - `params` An array of variables to make available when using an element.
|
|
|
|
|
+ * - `escape` If content should be escaped or not in the element itself or if elements are not used in the component.
|
|
|
|
|
+ *
|
|
|
* @param string $message Message to output.
|
|
* @param string $message Message to output.
|
|
|
- * @param string $type Type ('error', 'warning', 'success', 'info' or custom class).
|
|
|
|
|
|
|
+ * @param array|string $options Options or Type ('error', 'warning', 'success', 'info' or custom class).
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
- public static function transientMessage($message, $type = null) {
|
|
|
|
|
- if (!$type) {
|
|
|
|
|
- $type = 'info';
|
|
|
|
|
|
|
+ public static function transientMessage($message, $options = array()) {
|
|
|
|
|
+ $defaults = (array)Configure::read('Flash') + array(
|
|
|
|
|
+ 'type' => 'info',
|
|
|
|
|
+ 'escape' => false,
|
|
|
|
|
+ 'params' => array(),
|
|
|
|
|
+ 'element' => 'Tools.default',
|
|
|
|
|
+ 'typeToElement' => false,
|
|
|
|
|
+ 'useElements' => false,
|
|
|
|
|
+ 'plugin' => null,
|
|
|
|
|
+ );
|
|
|
|
|
+ if (!is_array($options)) {
|
|
|
|
|
+ $type = $options ?: $defaults['type'];
|
|
|
|
|
+ $options = array('type' => $type);
|
|
|
|
|
+ }
|
|
|
|
|
+ $options += $defaults;
|
|
|
|
|
+
|
|
|
|
|
+ $message = array(
|
|
|
|
|
+ 'message' => $message, // $options['escape'] ? h($message) : $message,
|
|
|
|
|
+ 'params' => $options['params'],
|
|
|
|
|
+ 'escape' => $options['escape']
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if ($options['useElements']) {
|
|
|
|
|
+ if ($options['typeToElement'] && $options['element'] === $defaults['element']) {
|
|
|
|
|
+ $options['element'] = ($options['plugin'] ? $options['plugin'] . '.' : '') . $options['type'];
|
|
|
|
|
+ }
|
|
|
|
|
+ list($plugin, $element) = pluginSplit($options['element']);
|
|
|
|
|
+ if ($plugin) {
|
|
|
|
|
+ $message['element'] = $plugin . '.Flash/' . $element;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $message['element'] = 'Flash/' . $element;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Simplify?
|
|
|
|
|
+ if (!$message['escape'] && !$message['params']) {
|
|
|
|
|
+ $message = $message['message'];
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$old = (array)Configure::read('messages');
|
|
$old = (array)Configure::read('messages');
|
|
|
|
|
+ $type = $options['type'];
|
|
|
if (isset($old[$type]) && count($old[$type]) > 99) {
|
|
if (isset($old[$type]) && count($old[$type]) > 99) {
|
|
|
array_shift($old[$type]);
|
|
array_shift($old[$type]);
|
|
|
}
|
|
}
|
|
@@ -111,7 +233,12 @@ class FlashComponent extends Component {
|
|
|
throw new InternalErrorException('Flash message missing.');
|
|
throw new InternalErrorException('Flash message missing.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $this->message($args[0], $name);
|
|
|
|
|
|
|
+ $options = ['type' => Inflector::underscore($name)];
|
|
|
|
|
+ if (!empty($args[1])) {
|
|
|
|
|
+ $options += (array)$args[1];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->message($args[0], $options);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|