Fixed SessionHelper not handling stacked messages

This commit is contained in:
Jeremy Harris 2017-08-22 10:06:37 -05:00
parent cdf00a9956
commit 2e75f12a4f
No known key found for this signature in database
GPG key ID: C90F85CC81D949C8
2 changed files with 57 additions and 35 deletions

View file

@ -47,24 +47,32 @@ class SessionHelperTest extends CakeTestCase {
'test' => 'info', 'test' => 'info',
'Message' => array( 'Message' => array(
'flash' => array( 'flash' => array(
'element' => 'default', array(
'params' => array(), 'element' => 'default',
'message' => 'This is a calling' 'params' => array(),
'message' => 'This is a calling'
),
), ),
'notification' => array( 'notification' => array(
'element' => 'session_helper', array(
'params' => array('title' => 'Notice!', 'name' => 'Alert!'), 'element' => 'session_helper',
'message' => 'This is a test of the emergency broadcasting system', 'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
'message' => 'This is a test of the emergency broadcasting system',
),
), ),
'classy' => array( 'classy' => array(
'element' => 'default', array(
'params' => array('class' => 'positive'), 'element' => 'default',
'message' => 'Recorded' 'params' => array('class' => 'positive'),
'message' => 'Recorded'
),
), ),
'bare' => array( 'bare' => array(
'element' => null, array(
'message' => 'Bare message', 'element' => null,
'params' => array(), 'message' => 'Bare message',
'params' => array(),
),
), ),
), ),
'Deeply' => array('nested' => array('key' => 'value')), 'Deeply' => array('nested' => array('key' => 'value')),
@ -104,7 +112,7 @@ class SessionHelperTest extends CakeTestCase {
public function testCheck() { public function testCheck() {
$this->assertTrue($this->Session->check('test')); $this->assertTrue($this->Session->check('test'));
$this->assertTrue($this->Session->check('Message.flash.element')); $this->assertTrue($this->Session->check('Message.flash.0.element'));
$this->assertFalse($this->Session->check('Does.not.exist')); $this->assertFalse($this->Session->check('Does.not.exist'));

View file

@ -134,30 +134,14 @@ class SessionHelper extends AppHelper {
if (CakeSession::check('Message.' . $key)) { if (CakeSession::check('Message.' . $key)) {
$flash = CakeSession::read('Message.' . $key); $flash = CakeSession::read('Message.' . $key);
CakeSession::delete('Message.' . $key); CakeSession::delete('Message.' . $key);
$message = $flash['message'];
unset($flash['message']);
if (!empty($attrs)) { $out = '';
$flash = array_merge($flash, $attrs); foreach ($flash as $flashArray) {
} if (!empty($attrs)) {
$flashArray = array_merge($flashArray, $attrs);
if ($flash['element'] === 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
} }
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>'; $flashArray['key'] = $key;
} elseif (!$flash['element']) { $out .= $this->_render($flashArray);
$out = $message;
} else {
$options = array();
if (isset($flash['params']['plugin'])) {
$options['plugin'] = $flash['params']['plugin'];
}
$tmpVars = $flash['params'];
$tmpVars['message'] = $message;
$tmpVars['key'] = $key;
$out = $this->_View->element($flash['element'], $tmpVars, $options);
} }
} }
return $out; return $out;
@ -173,4 +157,34 @@ class SessionHelper extends AppHelper {
return CakeSession::valid(); return CakeSession::valid();
} }
/**
* Renders a flash message
*
* @param array $flash Flash message array
* @return string
*/
protected function _render($flash) {
$message = $flash['message'];
unset($flash['message']);
if ($flash['element'] === 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
}
$out = '<div id="' . $flash['key'] . 'Message" class="' . $class . '">' . $message . '</div>';
} elseif (!$flash['element']) {
$out = $message;
} else {
$options = array();
if (isset($flash['params']['plugin'])) {
$options['plugin'] = $flash['params']['plugin'];
}
$tmpVars = $flash['params'];
$tmpVars['message'] = $message;
$tmpVars['key'] = $flash['key'];
$out = $this->_View->element($flash['element'], $tmpVars, $options);
}
return $out;
}
} }