b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_core/model/_messages.class.php - 295 lines - 6938 bytes - Summary - Text - Print

Description: This file implements the Messages class for displaying messages about performed actions. It additionally provides the class Log_noop that implements the same (used) methods, but as no-operation functions. This is useful to create a more resource friendly object when you don't need it (think Debuglog).

   1  <?php
   2  /**
   3   * This file implements the Messages class for displaying messages about performed actions.
   4   *
   5   * It additionally provides the class Log_noop that implements the same (used) methods, but as
   6   * no-operation functions. This is useful to create a more resource friendly object when
   7   * you don't need it (think Debuglog).
   8   *
   9   * This file is part of the evoCore framework - {@link http://evocore.net/}
  10   * See also {@link http://sourceforge.net/projects/evocms/}.
  11   *
  12   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
  13   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  14   *
  15   * {@internal License choice
  16   * - If you have received this file as part of a package, please find the license.txt file in
  17   *   the same folder or the closest folder above for complete license terms.
  18   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  19   *   then you must choose one of the following licenses before using the file:
  20   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  21   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  22   * }}
  23   *
  24   * {@internal Open Source relicensing agreement:
  25   * Daniel HAHLER grants Francois PLANQUE the right to license
  26   * Daniel HAHLER's contributions to this file and the b2evolution project
  27   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  28   * }}
  29   *
  30   * @package evocore
  31   *
  32   * @author blueyed: Daniel HAHLER
  33   * @author fplanque: Francois PLANQUE
  34   *
  35   * @version $Id: _messages.class.php 6136 2014-03-08 07:59:48Z manuel $ }}}
  36   *
  37   */
  38  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  39  
  40  
  41  /**
  42   * Messages class. For displaying notes, successful actions & errors.
  43   *
  44   * @todo CLEAN UP A LOT because of previous over factorization with Log class.
  45   * 
  46   * Messages can be logged into different categories (aka levels)
  47   * Examples: 'note', 'error'. Note: 'all' is reserved to display all categories together.
  48   * Messages can later be displayed grouped by category/level.
  49   *
  50   * @package evocore
  51   */
  52  class Messages
  53  {
  54      /**
  55       * The stored messages text.
  56       * array of Strings
  57       *
  58       * @var array
  59       */
  60      var $messages_text = array();
  61  
  62      /**
  63       * The stored messages type.
  64       * array of Strings
  65       *
  66       * @var array
  67       */
  68      var $messages_type = array();
  69  
  70      /**
  71       * The number of messages
  72       * 
  73       * @var integer
  74       */
  75      var $count = 0;
  76  
  77      /**
  78       * Error message was added or not.
  79       * 
  80       * @var boolean
  81       */
  82      var $has_errors = false;
  83  
  84      /**
  85       * Clears messages content
  86       */
  87  	function clear()
  88      {
  89          $this->messages_text = array();
  90          $this->messages_type = array();
  91          $this->count = 0;
  92          $this->has_errors = false;
  93      }
  94  
  95  
  96      /**
  97       * Add a message.
  98       *
  99       * @param string the message
 100       * @param string the message type, it can have this values: 'success', 'warning', 'error', 'note'
 101       */
 102  	function add( $text, $type = 'error' )
 103      {
 104          $this->messages_text[$this->count] = $text;
 105          $this->messages_type[$this->count] = $type;
 106          $this->count++;
 107          if( !$this->has_errors )
 108          {
 109              $this->has_errors = ( $type == 'error' );
 110          }
 111      }
 112  
 113  
 114      /**
 115       * Add a Messages object to this.
 116       *
 117       * @param Messages object
 118       */
 119  	function add_messages( $p_Messages )
 120      {
 121          $this->count = $this->count + $p_Messages->count;
 122          for( $i = 0; $i < $p_Messages->count; $i++ )
 123          {
 124              $this->messages_text[] = $p_Messages->messages_text[$i];
 125              $this->messages_type[] = $p_Messages->messages_type[$i];
 126              if( !$this->has_errors )
 127              {
 128                  $this->has_errors = ( $p_Messages->messages_type[$i] == 'error' );
 129              }
 130          }
 131      }
 132  
 133  
 134      /**
 135       * TEMPLATE TAG
 136       *
 137       * The purpose here is to have a tag which is simple yet flexible.
 138       * the display function is WAAAY too bloated.
 139       *
 140       * @todo optimize
 141       *
 142       * @param string HTML to display before the log when there is something to display
 143       * @param string HTML to display after the log when there is something to display
 144       */
 145  	function disp( $before = '<div class="action_messages">', $after = '</div>' )
 146      {
 147          if( $this->count )
 148          {
 149              global $preview;
 150              if( $preview )
 151              {
 152                  return;
 153              }
 154  
 155              $disp = $this->display( NULL, NULL, false, NULL );
 156  
 157              if( !empty( $disp ) )
 158              {
 159                  echo $before.$disp.$after;
 160              }
 161          }
 162      }
 163  
 164  
 165      /**
 166       * Display messages of the object.
 167       *
 168       * - You can output/get the messages
 169       * - Head/Foot will be displayed on top/bottom of the messages.
 170       * - You can suppress the outer div or set a css class for it (defaults to
 171       *   'log_container').
 172       *
 173       * @todo Make this simple!
 174       * start by getting rid of the $category selection and the special cases for 'all'. If you don't want to display ALL messages,
 175       * then you should not log them in the same Log object and you should instantiate separate logs instead.
 176       *
 177       * @param string|NULL Header/title
 178       * @param string|NULL Footer
 179       * @param boolean to display or return (default: display)
 180       * @param mixed the outer div, may be false
 181       * @return boolean false, if no messages; else true (and outputs if $display)
 182       */
 183  	function display( $head = NULL, $foot = NULL, $display = true, $outerdivclass = 'log_container' )
 184      {
 185          if( $this->count == 0 ) {
 186              return false;
 187          }
 188  
 189          $disp = '';
 190  
 191          if( $outerdivclass )
 192          {
 193              $disp .= "\n<div class=\"$outerdivclass\">";
 194          }
 195  
 196          if( !empty( $head ) )
 197          {
 198              $disp .= '<h3>'.$head.'</h3>';
 199          }
 200  
 201          $disp .= '<ul>';
 202          for( $i = 0; $i < $this->count; $i++ )
 203          {
 204              $disp .= "<li>\t<div class=\"log_{$this->messages_type[$i]}\"".'>'
 205                      .$this->messages_text[$i]."</div></li>\n";
 206          }
 207          $disp .= '</ul>';
 208  
 209          if( !empty( $foot ) )
 210          {
 211              $disp .= "\n<p>".$foot."</p>";
 212          }
 213  
 214          if( $outerdivclass )
 215          {
 216              $disp .= "</div>\n";
 217          }
 218  
 219          if( $display )
 220          {
 221              echo $disp;
 222              return true;
 223          }
 224          return $disp;
 225      }
 226  
 227  
 228      /**
 229       * Concatenates messages of a given category to a string
 230       *
 231       * @param string prefix of the string
 232       * @param string suffic of the string
 233       * @param string the glue
 234       * @param string result format
 235       * @return string the messages, imploded. Tags stripped.
 236       */
 237  	function get_string( $head = '', $foot = '', $implodeBy = ', ', $format = 'striptags' )
 238      {
 239          if( !$this->count )
 240          {
 241              return false;
 242          }
 243  
 244          $r = '';
 245          if( '' != $head )
 246          {
 247              $r .= $head.' ';
 248          }
 249          $r .= implode( $implodeBy, $this->messages_text );
 250          if( '' != $foot )
 251          {
 252              $r .= ' '.$foot;
 253          }
 254  
 255          switch( $format )
 256          {
 257              case 'xmlrpc':
 258                  $r = strip_tags( $r );    // get rid of <code>
 259                  $r = str_replace( '&lt;', '<', $r );
 260                  $r = str_replace( '&gt;', '>', $r );
 261                  $r = str_replace( '&quot;', '"', $r );
 262                  break;
 263  
 264              case 'striptags':
 265                  $r = strip_tags( $r );
 266                  break;
 267          }
 268  
 269          return $r;
 270      }
 271  
 272  
 273      /**
 274       * Get the number of messages
 275       *
 276       * @return number of messages
 277       */
 278  	function count()
 279      {
 280          return $this->count;
 281      }
 282  
 283  
 284      /**
 285       * Has error message in current object
 286       *
 287       * @return boolean true if error message was added, false otherwise
 288       */
 289  	function has_errors()
 290      {
 291          return $this->has_errors;
 292      }
 293  }
 294  
 295  ?>

title

Description

title

Description

title

Description

title

title

Body