Yii Framework Forum: Bored with setFlash? Use addFlash and multiFlash! - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Bored with setFlash? Use addFlash and multiFlash! Rate Topic: -----

#1 User is offline   veris 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 03-July 09
  • Location:POLAND

Posted 03 July 2010 - 12:53 PM

I've extended CWebUser class adding functionality that enables easy adding and showing multiple flashes.

You don't have to worry about keys, just add flashes and then show them all in the current or next request!

class WebUser extends CWebUser {
	
	const MF_KEY_PREFIX = 'mf';
	const MF_MAX = 100;
	
	/**
	 * Add flash to the stack.
	 * @param string $msg
	 * @param string $class 
	 */
	public function addFlash($msg='', $class='info') {
		$key = $this->getNexMultiFlashKey();
		if ($key===false) 
			Yii::trace("Stack overflow in addFlash", 'application.webuser.addFlash()');
		else
			$this->setFlash($key, array($msg, $class));
	}
	
	/**
	 * Returns next flash key for addFlash function.
	 * @return mixed string if ok or bool false if there was stack overflow.
	 */
	protected function getNexMultiFlashKey() {
		$counters=$this->getState(self::FLASH_COUNTERS,array());
		if (empty($counters)) return self::MF_KEY_PREFIX."1";
		for ($i=1;$i<=self::MF_MAX; $i++) {
			$key = self::MF_KEY_PREFIX . (string)$i;
			if (array_key_exists($key, $counters)) continue;
			return $key;
		}
		return false;
	}
	
	/**
	 * Gets all flashes and shows them to the user.
	 * Every flash is div with css class 'flash' and another 'flash_xxx' where xxx is the $class
	 * parameter set in addFlash function.
	 * Examples:
	 * Yii::app()->user->addFlash('My text, something important!', 'warning');
	 * Yii::app()->multiFlash();
	 * Output is <div class="flash flash_warning">My text, something important!<div>
	 */
	public function multiFlash() {
		for ($i=1;$i<=self::MF_MAX; $i++) {
			$key = self::MF_KEY_PREFIX . (string)$i;
			if (!$this->hasFlash($key)) continue;
			list($msg, $class) = $this->getFlash($key);
			Yii::trace("Echoing multiFlash: $key", 'application.webuser.multiFlash()');
			echo "<div class=\"flash flash_$class\">$msg</div>\n";
		}
	}

	
}


Usage:

Adding:
Yii::app()->user->addFlash('My text, something important!', 'warning');
Yii::app()->user->addFlash('Another one!', 'info');
Yii::app()->user->addFlash('Another one!', 'error');

Showing them all:
Yii::app()->user->multiFlash();

Installation:
1. paste the code to WebUser.php
2. save it somewhere
3. change your config file in /protected/config
return array(
  'components'=>array(
    'user'=>array(
       'class'=>'application.somewhere_dir.WebUser
    ),
  )
);


I really missed the functionality of handling multiple flashes in CWebUser.
Zbigniew Gralewski
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 03 July 2010 - 01:19 PM

Just a note, CWebUser::getFlashes() was added in trunk already.
0

#3 User is offline   skippy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 05-June 09

Posted 31 August 2010 - 05:25 AM

View PostY!!, on 03 July 2010 - 01:19 PM, said:

Just a note, CWebUser::getFlashes() was added in trunk already.


Yes, it returns all flash messages + Array 'counters' compare to $counters=->getState(self::FLASH_COUNTERS); which returns only flash messages.

This is desired functionality or a bug?
0

#4 User is offline   jeremy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 17-June 09
  • Location:Massachusetts, USA

Posted 26 January 2011 - 07:04 PM

I have the same question as @skippy: Why does getFlashes() return an array element [counters] in addition to all the flashes themselves? Seems like a bug.

EDIT: logged as issue #2060
http://code.google.c.../detail?id=2060
0

#5 User is offline   loganix 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 23-June 13

Posted 23 June 2013 - 12:25 PM

I know it's old topic but i think my solution could be also good:
Add to WebUser class this method:
/**
 * Add flash to the stack.
 * @param string $key key identifying the flash message
 * @param mixed $value flash message
*/
public function addFlash($key,$value) {
	if($this->hasFlash($key)) {
		$actualVal = $this->getFlash($key);
		$array = is_array($actualVal) ? $actualVal : array($actualVal);
		$array[] = $value;
		$this->setFlash($key, $array);
	} else $this->setFlash($key, $value);
}


and then use it as normal "setFlash" method.
If you want to display messages, use (in your view) something like this:

foreach(Yii::app()->user->getFlashes() as $key => $value) {
	if(is_array($value))
		foreach($value as $message)
			echo '<div class="message message-' . $key . '">' . $message . "</div>";
	else echo '<div class="message message-' . $key . '">' . $value . "</div>";
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users