Anonymous Anonymous - 2 months ago
330 0

Flash based message from http://mikeeverhart.net/

Legacy edition

PHP

messages.php

<?php

class Messages
{
    private $msgId;
    private $msgTypes = array('info', 'warning', 'error', 'success');
    private $msgClassPrepend = 'messages';
    private $msgClass = 'ec-messages';
    private $msgWrapper = "<div class='%s %s' id='%s'><a href='#' class='dismiss-messages dismiss-messages-processed'></a>%s</div>";
    private $msgBefore = '';
    private $msgAfter = '<br>';
	
	private static $instance;
	
    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            self::$instance = new Messages();
        }
        return self::$instance;
    }
	
    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, should exists bc login
        if (!array_key_exists('flash_messages', $_SESSION))
            $_SESSION['flash_messages'] = array();
    }
    /**
     * Add
     *
     * Creates a new SESSION message
     *
     * @param string $type
     * @param string $message
     * @return bool
     *
     */
    public function add($type, $message)
    {
        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(
                'i',
                'w',
                'e',
                's'
            ), array(
                'info',
                'warning',
                'error',
                'success'
            ), $type);
            // backwards compatibility
        } elseif ($type == 'information') {
            $type = '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;
        }
        return true;
    }
    /**
     * Display
     *
     * Displays session message
     *
     * UPDATE 2014: Added filtered arrays to remove duplicates!
     *
     * @param string $type
     * @param bool $print
     * @return bool or $data
     *
     */
    public function display($type = 'all', $print = true)
    {
        $messages = '';
        $data = '';
        if (!isset($_SESSION['flash_messages'])) {
            return false;
        }
        // print a certain type of message?
        if (in_array($type, $this->msgTypes)) {
            $filtered = array_unique($_SESSION['flash_messages'][$type]);
            foreach ($filtered as $msg) {
                $messages .= $this->msgBefore . $msg . $this->msgAfter;
            }
            $data .= sprintf($this->msgWrapper, $this->msgClass, $this->msgClassPrepend . '-' . $type, str_replace('messages', 'autoclose', $this->msgClassPrepend . '-' . $type), $messages);
            // clear the viewed messages
            $this->clear($type);
            // print ALL queued messages
        } elseif ($type == 'all') {
            $counter = 1;
            // http://stackoverflow.com/questions/22950233/avoid-duplicates-in-flash-based-messaging
            foreach ($_SESSION['flash_messages'] as $type => $msgArray) {
                $count = $counter++;
                $messages = '';
                $filtered = array_unique($msgArray);
                foreach ($filtered as $msg) {
                    $messages .= $this->msgBefore . $msg . $this->msgAfter;
                }
                $data .= sprintf($this->msgWrapper, $this->msgClass, $this->msgClassPrepend . '-' . $type, str_replace('messages', 'autoclose', $this->msgClassPrepend . '-' . $type), $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;
        }
    }
    /**
     * Errors
     *
     * Determines if an error occured
     *
     * @return bool or $data
     *
     */
    public function hasErrors()
    {
        return empty($_SESSION['flash_messages']['error']) ? false : true;
    }
    /**
     * Messages
     *
     * Checks to see if there are queued messages of any kind
     *
     * @param string $type
     * @return bool or $data
     *
     */
    public function hasMessages($type = null)
    {
        if (!is_null($type)) {
            if (!empty($_SESSION['flash_messages'][$type]))
                return $_SESSION['flash_messages'][$type];
        } else {
            foreach ($this->msgTypes as $type) {
                if (!empty($_SESSION['flash_messages']))
                    return true;
            }
        }
        return false;
    }
    /**
     * To String
     *
     * "magic" method that will, in this case, return the result from $this->hasMessages()
     *
     * @param string $type
     * @return hasMessages()
     *
     */
    public function __toString()
    {
        return $this->hasMessages();
    }
    /**
     * Clear
     *
     * Clears messages
     *
     * @param string $type
     * @return bool true
     *
     */
	public function __clone () {
		
	}
    public function clear($type = 'all')
    {
        if ($type == 'all') {
            unset($_SESSION['flash_messages']);
        } else {
            unset($_SESSION['flash_messages'][$type]);
        }
        return true; 
    }
    public function __destruct()
    {
        //$this->clear();
    }
}
?>