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',
'Message' => array(
'flash' => array(
'element' => 'default',
'params' => array(),
'message' => 'This is a calling'
array(
'element' => 'default',
'params' => array(),
'message' => 'This is a calling'
),
),
'notification' => array(
'element' => 'session_helper',
'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
'message' => 'This is a test of the emergency broadcasting system',
array(
'element' => 'session_helper',
'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
'message' => 'This is a test of the emergency broadcasting system',
),
),
'classy' => array(
'element' => 'default',
'params' => array('class' => 'positive'),
'message' => 'Recorded'
array(
'element' => 'default',
'params' => array('class' => 'positive'),
'message' => 'Recorded'
),
),
'bare' => array(
'element' => null,
'message' => 'Bare message',
'params' => array(),
array(
'element' => null,
'message' => 'Bare message',
'params' => array(),
),
),
),
'Deeply' => array('nested' => array('key' => 'value')),
@ -104,7 +112,7 @@ class SessionHelperTest extends CakeTestCase {
public function testCheck() {
$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'));

View file

@ -134,30 +134,14 @@ class SessionHelper extends AppHelper {
if (CakeSession::check('Message.' . $key)) {
$flash = CakeSession::read('Message.' . $key);
CakeSession::delete('Message.' . $key);
$message = $flash['message'];
unset($flash['message']);
if (!empty($attrs)) {
$flash = array_merge($flash, $attrs);
}
if ($flash['element'] === 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
$out = '';
foreach ($flash as $flashArray) {
if (!empty($attrs)) {
$flashArray = array_merge($flashArray, $attrs);
}
$out = '<div id="' . $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'] = $key;
$out = $this->_View->element($flash['element'], $tmpVars, $options);
$flashArray['key'] = $key;
$out .= $this->_render($flashArray);
}
}
return $out;
@ -173,4 +157,34 @@ class SessionHelper extends AppHelper {
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;
}
}