Ticket #644: jmessage.patch

File jmessage.patch, 4.5 kB (added by bballizlife, 4 months ago)

jMessage class + jmesage jtpl plugin

  • build/manifests/jelix-lib.mn

    old new  
    239239* jJsonRpc.class.php 
    240240  jLog.class.php 
    241241  jMailer.class.php 
     242  jMessage.class.php 
    242243  jSmtp.class.php 
    243244  jTcpdf.class.php 
    244245* jWiki.class.php 
     
    452453  block.swfjs.php 
    453454  function.link_to_remote.php 
    454455  function.breadcrumb.php 
     456  function.jmessage.php 
    455457 
    456458cd lib/jelix/plugins/tpl/ltx2pdf 
    457459  function.jlocale.php 
  • lib/jelix/plugins/tpl/html/function.jmessage.php

    old new  
     1<?php 
     2/** 
     3* @package     Jelix 
     4* @subpackage  jtpl_plugin 
     5* @author      Loic Mathaud 
     6* @copyright   2008 Loic Mathaud 
     7* @link        http://www.jelix.org 
     8* @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
     9*/ 
     10 
     11/** 
     12* function plugin :  Display messages from jMessage 
     13*/ 
     14 
     15function jtpl_function_html_jmessage($tpl, $type = '') { 
     16    // Get messages 
     17    if ($type == '') { 
     18        $messages = jMessage::getAll(); 
     19    } else { 
     20        $messages = jMessage::get($type); 
     21    } 
     22    // Not messages, quit 
     23    if (!$messages) { 
     24        return; 
     25    } 
     26 
     27    // Display messages 
     28    if ($type == '') { 
     29        echo '<ul class="jelix-msg">'; 
     30        foreach ($messages as $types) { 
     31            foreach ($types as $type => $msg) { 
     32                echo '<li class="jelix-msg-item-'.$type.'">'.htmlspecialchars($msg).'</li>'; 
     33            } 
     34        } 
     35    } else { 
     36        echo '<ul class="jelix-msg-'. $type .'">'; 
     37        foreach ($messages as $msg) { 
     38            echo '<li class="jelix-msg-item-'.$type.'">'.htmlspecialchars($msg).'</li>'; 
     39        } 
     40    } 
     41    echo '</ul>'; 
     42 
     43    if ($type == '') { 
     44        jMessage::clearAll();; 
     45    } else { 
     46        jMessage::clear($type); 
     47    } 
     48     
     49} 
  • lib/jelix/utils/jMessage.class.php

    old new  
     1<?php 
     2/** 
     3* @package    jelix 
     4* @subpackage utils 
     5* @author     Loic Mathaud 
     6* @copyright  2008 Loic Mathaud 
     7* @link       http://www.jelix.org 
     8* @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
     9*/ 
     10 
     11/** 
     12* Utility class to log some message in session in order to be displayed in a template 
     13* @package    jelix 
     14* @subpackage utils 
     15* @static 
     16*/ 
     17class jMessage { 
     18 
     19    protected static $session_name = 'JELIX_MESSAGE'; 
     20     
     21     
     22    /** 
     23    * Add a message 
     24    * @param string $message the message 
     25    * @param string $type the message type ('default' by default) 
     26    */ 
     27    public static function add($message, $type = 'default') { 
     28        if (!isset($_SESSION[self::$session_name])) { 
     29            $_SESSION[self::$session_name] = array(); 
     30        } 
     31        if (!isset($_SESSION[self::$session_name][$type])) { 
     32            $_SESSION[self::$session_name][$type] = array(); 
     33        } 
     34         
     35        // Add message 
     36        $_SESSION[self::$session_name][$type][] = $message; 
     37    } 
     38     
     39    /** 
     40    * Clear messages for the given type 
     41    * @param string $type the message type ('default' by default) 
     42    */ 
     43    public static function clear($type = 'default') { 
     44        if (isset($_SESSION[self::$session_name][$type])) { 
     45            $_SESSION[self::$session_name][$type] = array(); 
     46        } 
     47    } 
     48     
     49    /** 
     50    * Clear all messages 
     51    */ 
     52    public static function clearAll() { 
     53        if (isset($_SESSION[self::$session_name])) { 
     54            $_SESSION[self::$session_name] = array(); 
     55        } 
     56    } 
     57     
     58    /** 
     59    * Get messages for the given type 
     60    * @param string $type the message type ('default' by default) 
     61    * @return mixed array/null 
     62    */ 
     63    public static function get($type = 'default') { 
     64        if (isset($_SESSION[self::$session_name][$type])) { 
     65            return $_SESSION[self::$session_name][$type]; 
     66        } 
     67         
     68        return null; 
     69    } 
     70     
     71    /** 
     72    * Get all messages 
     73    */ 
     74    public static function getAll() { 
     75        if (isset($_SESSION[self::$session_name])) { 
     76            return $_SESSION[self::$session_name]; 
     77        } 
     78         
     79        return null; 
     80    } 
     81} 
Download in other formats: Original Format