Skip to content

Instantly share code, notes, and snippets.

@b3457m0d3
Forked from umidjons/yii-notification-message.md
Last active August 29, 2015 14:17
Show Gist options
  • Save b3457m0d3/7754098038579fe0dd33 to your computer and use it in GitHub Desktop.
Save b3457m0d3/7754098038579fe0dd33 to your computer and use it in GitHub Desktop.

Helper class for notification messages

<?php
class Msg
{
	/**
	 * Add info message into queue.
	 * @param string $message info message
	 */
	public static function info( $message )
	{
		self::message( $message, 'info' );
	}

	/**
	 * Add success message into queue.
	 * @param string $message success message
	 */
	public static function success( $message )
	{
		self::message( $message, 'success' );
	}

	/**
	 * Add warning message into queue.
	 * @param string $message warning message
	 */
	public static function warning( $message )
	{
		self::message( $message, 'warning' );
	}

	/**
	 * Add error message(s) into queue.
	 * @param string|array $message error message(s). Can be simple string or $model->getErrors() array.
	 */
	public static function error( $message )
	{
		if ( is_array( $message ) ) // array
			foreach ( $message as $msg ) // foreach element check...
				if ( is_array( $msg ) ) // if array, then [attribute=>[err1, err2, ...], ...]
					self::error( $msg ); // recurse for element
				else
					self::message( $msg, 'danger' ); // simple string
		else
			self::message( $message, 'danger' ); // simple string
	}

	/**
	 * Add message into queue with specified type.<br>
	 * NOTE: Internal use only.
	 * @param string $message typed message text
	 * @param string $type    type of the message. Available values: info, success, warning, error.
	 */
	private static function message( $message, $type )
	{
		$_SESSION[ 'flash_messages' ][ ] = [ $message, $type ];
	}

	/**
	 * Get array of messages. Format: [ [message, type], [message, type], ... ]
	 * @return mixed array of messages.
	 */
	public static function getMessages()
	{
		return $_SESSION[ 'flash_messages' ];
	}

	/**
	 * Shows floating notifications. Also removes already shown messages from queue.
	 * To dismiss notification, just click to it.
	 */
	public static function show()
	{
		// register JS
		$js_flash_messages = "
			nextAnim($('.alert-fixed:not(:visible)'));
			function nextAnim(elems){
				elems.eq(0).slideDown(function(){
					nextAnim(elems.slice(1));
				});
			}
			$('.alert-fixed').on('click', function(__event){
				$(this).slideUp();
			});
		";

		Yii::app()->clientScript->registerScript( 'flash_messages', $js_flash_messages, CClientScript::POS_READY );

		// register CSS
		$css_flash_messages = "
			.alert-container {
			  position: fixed;
			  width: 300px;
			  top: 5px;
			  right: 5px;
			  z-index: 100;
			}
			.alert-container .alert-fixed {
			  margin: 3px;
			  float: right;
			  display: none;
			}
		";

		Yii::app()->clientScript->registerCss( 'flash_messages', $css_flash_messages );

		// show messages
		if ( isset( $_SESSION[ 'flash_messages' ] ) )
		{
			?>
			<div class="alert-container">
				<?php
				while ( $_SESSION[ 'flash_messages' ] )
				{
					list( $txt, $type ) = array_shift( $_SESSION[ 'flash_messages' ] );
					?><span class="alert alert-fixed alert-<?= $type; ?>"><?= CHtml::encode( $txt ); ?></span><?php
				}
				?>
			</div>
		<?php
		}
	}
}

Using in layouts

<body>

<?php
Msg::info( 'Information text is here' );
Msg::success( 'Success text is here' );
Msg::warning( 'Warning text is here' );
Msg::error( 'Error text is here' );
?>

<?php Msg::show(); ?>

<div class="container">
<!-- ... -->
</div>

Using in controller

<?php
if ( $model->save() )
{
	Msg::success( Yii::t( 'zr', 'Content has been duplicated.' ) );
	$this->redirect( [ 'update', 'id' => $model->id ] );
}
else
	Msg::error( $model->getErrors() ); // batch assign errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment