使用php会话将图标添加到错误消息

时间:2014-12-31 15:26:20

标签: php

我有这个类用于使用PHP的session方法显示错误。

class Messages {

    //-----------------------------------------------------------------------------------------------
    // Class Variables
    //-----------------------------------------------------------------------------------------------   
    var $msgId;
    var $msgTypes = array( 'help', 'info', 'warning', 'success', 'danger' );
    var $msgClass = 'alert';
    var $msgWrapper = " <div class='alert %s-%s flashit'>
    <button class='close' aria-hidden='true' data-dismiss='alert' type='button'>×</button>
    <p><i style='vertical-align: middle;' class='%s icon-2x'></i> %s</p>
    </div>";
    var $msgBefore = '';
    var $msgAfter = "";

    public function __construct() {

        // Generate a unique ID for this user and session
        $this->msgId = md5(uniqid());

        // Create the session array if it doesnt already exist
        if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();

    }

    public function add($type, $message, $redirect_to=null) {

        if( !isset($_SESSION['flash_messages']) ) return false;

        if( !isset($type) || !isset($message[0]) ) return false;

        // Replace any shorthand codes with their full version
        if( strlen(trim($type)) == 1 ) {
            $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'danger', 'success'), $type );
            $icon = str_replace( array('h', 'i', 'w', 'e', 's'), array('fa-help', 'fa-info', 'fa-warning', 'fa-danger', 'fa-success'), $type );
        // Backwards compatibility...
        } elseif( $type == 'information' ) {
            $type = 'info';
            $icon = 'fa-info';      
        }


        // Make sure it's a valid message type
        if( !in_array($type, $this->msgTypes) ) die('"' . strip_tags($type) . '" is not a valid message type!' );

        // If the session array doesn't exist, create it
        if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();

        $_SESSION['flash_messages'][$type][] = $message;

        if( !is_null($redirect_to) ) {
            header("Location: $redirect_to");
            exit();
        }

        return true;

    }

    //-----------------------------------------------------------------------------------------------
    // display()
    // print queued messages to the screen
    //-----------------------------------------------------------------------------------------------

    public function display($type='all', $print=true) {
        $messages = '';
        $data = '';

        if( !isset($_SESSION['flash_messages']) ) return false;

        if( $type == 'g' || $type == 'growl' ) {
            $this->displayGrowlMessages();
            return true;
        }

        // Print a certain type of message?
        if( in_array($type, $this->msgTypes) ) {
            foreach( $_SESSION['flash_messages'][$type] as $msg ) {
                $messages .= $this->msgBefore . $msg . $this->msgAfter;
            }

            $data .= sprintf($this->msgWrapper, $this->msgClass, $type,$icon,$messages);

            // Clear the viewed messages
            $this->clear($type);

        // Print ALL queued messages
        } elseif( $type == 'all' ) {
            foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {
                $messages = '';

                foreach( $msgArray as $msg ) {
                    $messages .= $this->msgBefore . $msg . $this->msgAfter; 
                }
                $data .= sprintf($this->msgWrapper, $this->msgClass, $type,$icon,$messages);
            }

            // Clear ALL of the messages
            $this->clear();

        // Invalid Message Type?
        } else { 
            return false;
        }

        // Print everything to the screen or return the data
        if( $print ) { 
            echo $data; 
        } else { 
            return $data; 
        }
    }

  //..... more
}

呼叫:

$msg = new Messages();
$msg->add('i', 'This is a Information message!');
echo $msg->display();

现在输出:

<i style="vertical-align: middle;" class=" icon-2x"></i>

图标类未打印且为空:class=" icon-2x"。我该怎么办?

编辑:确实,我需要为每个类型打印一个班级名称。

0 个答案:

没有答案