Notifications all over the place!

Place to place any code snippets, completed games, or even uncompleted games for IR users to use.
Post Reply
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Notifications all over the place!

Post by Xaleph »

Ok, well i`m guessing this will not be as usefull for some but it`s a simple Notifications object which you can use. If you include the file somewhere at the top of your pages, you can use the Notify object everywhere.

Code: Select all

<?php

class Notify {
    
    private static $error = array();
    private static $success = array();
    private static $info = array();
    private static $log = array();
    private static $instance;
 
    private function __construct() {
        self::log("Class Notify has been loaded.");
        if(isset($_SESSION['success'])){
              foreach($_SESSION['success'] as $message){
                    self::addSuccess($message);
              }
              unset($_SESSION['success']);
        }
        if(isset($_SESSION['error'])){
              foreach($_SESSION['error'] as $message){
                    self::addError($message);
              }
              unset($_SESSION['error']);
        }
        if(isset($_SESSION['info'])){
              foreach($_SESSION['info'] as $message){
                    self::addInfo($message);
              }
              unset($_SESSION['info']);
        }
    }
    
    /**
     * This is the method that wil be used to create a singleton of this object. 
     * @return type 
     */
    public function getInstance(){
        if(!self::$instance instanceof self){
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * This is a simple helper method to return a given stack. The currently 
     * accepted stacks are: success, error and info. The $type has a default of
     * info. 
     * @param type $type 
     */
    public function getStack($type='info'){
        switch($type){
            case 'log':
                self::getLog();
                break;
            case 'success':
                self::getSuccess();
                break;
            case 'error':
                self::getError();
                break;
            case 'info':default:
                self::getInfo();
                break;
        }
    }
    
    /** 
     * This method will return the log stack
     * @return type 
     */
    public function getLog(){
        return self::$log;
    }
    
    /**
     * This method will return the error stack
     * @return type 
     */
    public function getError(){
        return self::$error;
    }
    
    /** 
     * This method will return the success stack
     * @return type 
     */
    public function getSuccess(){
        return self::$success;
    }
    
    /**
     * This method will return the info stack.
     * @return type 
     */
    public function getInfo(){
        return self::$info;
    }
    
    
    /**
     * This method will add a message to corresponding stack, which
     * can be provided in the $type. $type currently accepts 3 different
     * types: success, error, info. The default will be info. 
     * @param type $message
     * @param type $type 
     */
    public function add($message, $type = 'info'){
        switch($type){
            case 'log':
                self::addLog($message);
                break;
            case 'success':
                self::addSuccess($message);
                break;
            case 'error':
                self::addError($message);
                break;
            case 'info':default:
                self::addInfo($message);
                break;
        }
    }
    
    /**
     * This method will ad a message to the log stack
     * @param type $message 
     */
    public function addLog($message){
        array_push(self::$log, $message);
    }
    public function log($message){
        self::addLog($message);
    }
    
    /**
     * This method will ad a message to the info stack
     * @param type $message 
     */
    public function addInfo($message){
        array_push(self::$info, $message);
    }
    public function info($message){
        self::addInfo($message);
    }
    
    /**
     * This method will add a message to the success stack
     * @param type $message 
     */
    public function addSuccess($message){
        array_push(self::$success, $message);
    }
    public function success($message){
        self::addSuccess($message);
    }
    
    /**
     * This method will add a message to the error stack
     * @param type $message 
     */
    public function addError($message){
        array_push(self::$error, $message);
    }
    public function error($message){
        self::addError($message);
    }
}
To use it, save the file to your project ( for example as Notify.php )

To use it:

Code: Select all

require "path/to/Notify.php";
$notify = Notify::getInstance();
And to add messages to the stack:

Code: Select all

Notify::success("This message will be added to the success stack!");
// or
$notify->success("This message will also be added to the success stack.");
To get all success messages, just use:

Code: Select all

$successMessages = Notify::getSuccess();
// or
$successMessages = $notify->getStack("success");
Anyway, it doesn`t do that much, but it`s a nice abstraction to use notifications all over the place. You could use it if a player registers or something like that.

Also, if you want to redirect after something, but still want to add a message to the stack, you can use sessions:

Code: Select all

session_start(); // should be on obviously
$_SESSION['success'][0] = "This is the first message given!";
$_SESSION['success'][1] = "This is the second message given!";
header("Location: index.php");
edit: to show the actual messages:

Code: Select all

$successMessages = Notify::getSuccess();

foreach($successMessages as $message){
      echo "<p>". $message . "</p>";
}

// or use it like this
foreach(Notify::getSuccess() as $message){
     echo "<p>" . $message . "</p>";
}
Post Reply

Return to “Code Sharing”