トップ  >  XCL2.2関連  >  RedirectMessageJgrowl.class.php
RedirectMessageJgrowl class php preload XCL2
2017.05
オプション追加
protected $mode = 'none'; //[block|main|none]
popup以外に、ブロックへも表示するか、しないか
(javascriptが無効時用)


<?php
/**
 * @file RedirectMessageJgrowl.class.php
 * @package For legacy Cube Legacy 2.2
 * @version $Id:RedirectMessageJgrowl.class.php ver0.01 2017/05/15  20:34:00 domifara  $
 */
/*
 * **********************************
 * preload for Xoops Cube Legacy 2
 * **********************************
 */
if (! defined ( 'XOOPS_ROOT_PATH' )){
	exit ();
}
class RedirectMessageJgrowl extends XCube_ActionFilter
{
	protected $mode = 'none'; //[block|main|none]
	protected $index = 'cc';
	protected $indexes = array(
		'l' => 0,
		'r' => 1,
		'cl' => 3,
		'cr' => 4,
		'cc' => 5
	);
	protected $blocks = array(
		'l' => 'xoops_lblocks',
		'r' => 'xoops_rblocks',
		'cl' => 'xoops_clblocks',
		'cr' => 'xoops_crblocks',
		'cc' => 'xoops_ccblocks'
	);
	protected $is_success = false;//for one time only
	protected $message = '';//save session redirect_message

	public function postFilter()
	{
		if( !isset( $_SESSION['redirect_message'] ) or empty( $_SESSION['redirect_message'] )) {
			return;
		} else {
			$this->message = $this->displayMessage($_SESSION['redirect_message']);
		}
		//*** for when not alive javascript
		if ($this->mode == 'block'){
			//*** add create new block
			$this->mRoot->mContext->mAttributes['legacy_BlockShowFlags'][$this->indexes[$this->index]]  = true;
			$this->mRoot->mContext->mAttributes['legacy_BlockContents'][$this->indexes[$this->index]][] = $this->redirect_message_block();
		}
		if ($this->mode == 'main'){
			//*** appent content to xoops_contents
			$this->mRoot->mDelegateManager->add('Legacy_RenderSystem.BeginRender',array(&$this, 'appendContents'));
		}
		//*** jGrowl Popup message
		$this->mRoot->mDelegateManager->add('Site.JQuery.AddFunction',array(&$this, 'addScript'));
	}
	/*
	 * **********************************
	 * addScript
	 * $jQuery : (object)Legacy_HeaderScript
	 * **********************************
	 */
	public function addScript(&$jQuery)
	{
		//*** need to do check
		if( !isset( $_SESSION['redirect_message'] ) or empty( $_SESSION['redirect_message'] )) {
			return;
		}
		//**** add js core
		$jgjs = '/common/jgrowl/jquery.jgrowl.min.js';
		if ( !$this->get_already_jgrowl($jQuery ,$jgjs ) ){
			$jQuery->addLibrary( $jgjs ,true);
			$jQuery->addStylesheet('/common/jgrowl/jquery.jgrowl.min.css',true);
		}
		//**** add css
		$jgcss = '/common/jgrowl/jquery.jgrowl.redirect.css';
		if ( !$this->get_already_jgrowltheme($jQuery ,'redirect' ) ){
			$jQuery->addStylesheet ( $jgcss , true );
		}
		//**** exec script
		$div_id = 'xugj_message';
		$div_class = 'center';
		$message_format = '<div id="%1$s" class="%2$s"></div>';
		$message_div = sprintf ( $message_format, $div_id, $div_class );

		$ext_head_script = <<< HTML
jQuery(document).ready(function($){
$('body').append('{$message_div}');
$('#{$div_id}').jGrowl('{$this->message}',
	{
life: 1000,
theme: 'redirect',
position: '{$div_class}',
themeState: ''
	});
});
HTML;
		$jQuery->addScript($ext_head_script , true );

		//*** template assign
		$render =& $this->mRoot->getRenderSystem($this->mRoot->mContext->mBaseRenderSystemName);
		if ($this->mRoot->mContext->mBaseRenderSystemName == 'Legacy_AdminRenderSystem'){
			$Tpl = $render->mSmarty;
		} else {
			$Tpl = $render->mXoopsTpl;
		}

		//assign tpl_vars
		$Tpl->assign( 'redirect_message',$this->message ) ;
		$Tpl->assign( 'is_redirected',true ) ;
		//clear
		unset( $_SESSION['redirect_message'] ) ;
	}
	/*
	 * *************************************************
	 * appendContents
	 * $tpl : (object) xoopsTpl
	 * *************************************************
	 */
	public function appendContents(&$tpl)
	{
		if ($this->is_success) {
			return;
		}
		if (empty($this->message)) {
			return;
		}
		//if (isset ( $tpl->_tpl_vars ['xoops_contents'] )) {
		$buffertype = $tpl->get_template_vars('legacy_buffertype');
		if ($buffertype == 'theme') {
			$tpl->assign ( 'xoops_contents', $this->redirect_message_content () . $tpl->get_template_vars('xoops_contents') );
			$this->is_success = true;
		}
	}
	/*
	 * *************************************************
	 * redirect_message_block
	 * *************************************************
	 */
	protected function redirect_message_block()
	{
		$block = array(
			'id'      => -10,
			'name'    => 'redirect_message_block',
			'title'   => '',//message
			'content' =>  $this->redirect_message_content(),
			'weight'  => 0, //top view weight=0 when in cc ,other block weight=-1 oK
		);
		return $block;
	}
	/*
	 * *************************************************
	 * redirect_message_content
	 * *************************************************
	 */
	protected function redirect_message_content()
	{
		//**** exec when jQuery is alive , hide message
		$content = <<< HTML
<div id="redirect_message_block">
<font color="red">{$this->message}</font>
</div>
<script type="text/javascript"><!--
jQuery(document).ready(function($){
	if (typeof jQuery != 'undefined') {
		$("#redirect_message_block").hide();
	}
});
//--></script>
HTML;
		return $content;
	}
	/*
	 * *************************************************
	 * get_already_jgrowl
	 * $jQuery : (object)Legacy_HeaderScript
	 * $jgjs : (string)'js/jgrowl/jquery.jgrowl.min.js'
	 * *************************************************
	 */
	protected function get_already_jgrowl(&$jQuery ,$jgjs )
	{
		$arr_libjs = $jQuery->getLibraryArr ();
		$str_patern = preg_quote(basename ( $jgjs ));
		$arr_match = preg_grep( "/".$str_patern."/" , $arr_libjs);
		if ( !empty ( $arr_match ) ) {
			return true;
		}
		return false;
	}
	/*
	 * *************************************************
	 * get_already_jgrowltheme
	 * $jQuery : (object)Legacy_HeaderScript
	 * $jgtheme : (string)'redirect'
	 * *************************************************
	 */
	protected function get_already_jgrowltheme(&$jQuery ,$jgtheme )
	{
//		$str_patern = preg_quote("theme: '".$jgtheme."'");
		$str_patern = "theme(\s)*\:(\s)*[\'|\\"]".$jgtheme."[\'|\\"]";

		$arr_scrjs = $jQuery->getScriptArr (true);
		if ( !empty ( $arr_match ) ) {
			return true;
		}
		$arr_scrjs = $jQuery->getScriptArr (false);
		$arr_match = preg_grep( "/".$str_patern."/" , $arr_scrjs);
		if ( !empty ( $arr_match ) ) {
			return true;
		}
		return false;
	}
	/*
	 * *************************************************
	 * displayMessage
	 * $message : (mix) string or array
	 * *************************************************
	 */
	protected function displayMessage($message)
	{
		$displayMessage = "";
		if (is_array($message)){
			foreach (array_keys($message) as $key) {
				$message[$key] = htmlspecialchars($message[$key], ENT_QUOTES);
			}
			$displayMessage = implode("<br/>", $message);
		}else{
			$displayMessage = htmlspecialchars($message, ENT_QUOTES);
		}
		return $displayMessage;
	}
}
?>
前
未完成:コード表示ポップアップ改 XCodePopupResizable.class.php
カテゴリートップ
XCL2.2関連
次
[メモ]DelegateManager::add関数