root

Changeset 479

Show
Ignore:
Timestamp:
01/12/09 01:15:51 (2 years ago)
Author:
wbond
Message:

Changed fMessaging::show() to accept more than one message name, or * for all messages

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fMessaging.php

    r448 r479 Hide Line Numbers
    33 * Provides session-based messaging for page-to-page communication 
    44 *  
    5  * @copyright  Copyright (c) 2007-2008 Will Bond 
     5 * @copyright  Copyright (c) 2007-2009 Will Bond 
    66 * @author     Will Bond [wb] <will@flourishlib.com> 
    77 * @license    http://flourishlib.com/license 
     
    1010 * @link       http://flourishlib.com/fMessaging 
    1111 *  
    12  * @version    1.0.0b 
    13  * @changes    1.0.0b  The initial implementation [wb, 2008-03-05] 
     12 * @version    1.0.0b2 
     13 * @changes    1.0.0b2  Changed ::show() to accept more than one message name, or * for all messages [wb, 2009-01-12] 
     14 * @changes    1.0.0b   The initial implementation [wb, 2008-03-05] 
    1415 */ 
    1516class fMessaging 
     
    8081     
    8182    /** 
    82      * Retrieves a message, removes it from the session and prints it with the CSS class specified - will not print if no content 
     83     * Retrieves a message, removes it from the session and prints it - will not print if no content 
    8384     *  
    8485     * The message will be printed in a `p` tag if it does not contain 
    8586     * any block level HTML, otherwise it will be printed in a `div` tag. 
    8687     *  
    87      * @param  string $name       The name of the message to retrieve 
     88     * @param  mixed  $name       The name or array of names of the message(s) to show, or `'*'` to show all 
    8889     * @param  string $recipient  The intended recipient 
    89      * @param  string $css_class  The CSS class to use when displaying the message - if `NULL` is passed or the parameter is ommitted, `$name` will be used instea
    90      * @return boolean  If a message was shown 
     90     * @param  string $css_class  Overrides using the `$name` as the CSS class when displaying the message - only used if a single `$name` is specifie
     91     * @return boolean  If one or more messages was shown 
    9192     */ 
    9293    static public function show($name, $recipient, $css_class=NULL) 
    9394    { 
    94         $css_class = ($css_class === NULL) ? $name : $css_class; 
    95         $message   = self::retrieve($name, $recipient); 
    96         fHTML::show($message, $css_class); 
    97         return !empty($message); 
     95        // Find all messages if * is specified 
     96        if (is_string($name) && $name == '*') { 
     97            fSession::open(); 
     98            $prefix = __CLASS__ . '::' . $recipient . '::'; 
     99            $keys   = array_keys($_SESSION); 
     100            $name   = array(); 
     101            foreach ($keys as $key) { 
     102                if (strpos($key, $prefix) === 0) { 
     103                    $name[] = substr($key, strlen($prefix)); 
     104                } 
     105            } 
     106        } 
     107         
     108        // Handle showing multiple messages 
     109        if (is_array($name)) { 
     110            $shown = FALSE; 
     111            $names = $name; 
     112            foreach ($names as $name) { 
     113                $shown = fHTML::show( 
     114                    self::retrieve($name, $recipient), 
     115                    $name 
     116                ) || $shown; 
     117            } 
     118            return $shown; 
     119        } 
     120         
     121        // Handle a single message 
     122        return fHTML::show( 
     123            self::retrieve($name, $recipient), 
     124            ($css_class === NULL) ? $name : $css_class 
     125        ); 
    98126    } 
    99127     
     
    110138 
    111139/** 
    112  * Copyright (c) 2007-2008 Will Bond <will@flourishlib.com> 
     140 * Copyright (c) 2007-2009 Will Bond <will@flourishlib.com> 
    113141 *  
    114142 * Permission is hereby granted, free of charge, to any person obtaining a copy