  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: message_stack.php 1497 2007-03-29 13:40:05Z hpdl $ |
| |
| 3 | + | $Id: message_stack.php 1845 2009-02-27 00:19:37Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
7 | 7 | | |
  |
8 | | - | Copyright (c) 2004 osCommerce |
| |
| 8 | + | Copyright (c) 2009 osCommerce |
|
9 | 9 | | |
| |
10 | 10 | | This program is free software; you can redistribute it and/or modify |
| |
11 | 11 | | it under the terms of the GNU General Public License v2 (1991) |
| |
12 | 12 | | as published by the Free Software Foundation. |
| |
13 | 13 | | */ |
| |
14 | 14 | | |
  |
15 | | - | class messageStack { |
| |
16 | | - | var $messages; |
| |
| 15 | + | /** |
| |
| 16 | + | * The osC_MessageStack class manages information messages to be displayed. |
| |
| 17 | + | * Messages that are shown are automatically removed from the stack. |
| |
| 18 | + | */ |
|
17 | 19 | | |
  |
18 | | - | // class constructor |
| |
19 | | - | function messageStack() { |
| |
20 | | - | $this->messages = array(); |
| |
| 20 | + | class osC_MessageStack { |
| |
| 21 | + | |
| |
| 22 | + | /** |
| |
| 23 | + | * A reference to the messages stored in the session messageToStack variable |
| |
| 24 | + | * |
| |
| 25 | + | * @var array |
| |
| 26 | + | * @access private |
| |
| 27 | + | */ |
| |
| 28 | + | |
| |
| 29 | + | private $_data = array(); |
| |
| 30 | + | |
| |
| 31 | + | /** |
| |
| 32 | + | * Constructor, references the session data to the private $_data variable |
| |
| 33 | + | * |
| |
| 34 | + | * @access public |
| |
| 35 | + | */ |
| |
| 36 | + | |
| |
| 37 | + | public function __construct() { |
| |
| 38 | + | if ( !isset($_SESSION['messageToStack']) ) { |
| |
| 39 | + | $_SESSION['messageToStack'] = array(); |
| |
| 40 | + | } |
| |
| 41 | + | |
| |
| 42 | + | $this->_data =& $_SESSION['messageToStack']; |
|
21 | 43 | | } |
| |
22 | 44 | | |
  |
23 | | - | // class methods |
| |
24 | | - | function add($class, $message, $type = 'error') { |
| |
25 | | - | $this->messages[] = array('class' => $class, 'type' => $type, 'message' => $message); |
| |
| 45 | + | /** |
| |
| 46 | + | * Add a message to the stack |
| |
| 47 | + | * |
| |
| 48 | + | * @param string $group The group the message belongs to |
| |
| 49 | + | * @param string $message The message information text |
| |
| 50 | + | * @param string $type The type of message: error, warning, success |
| |
| 51 | + | * @access public |
| |
| 52 | + | */ |
| |
| 53 | + | |
| |
| 54 | + | public function add($group, $message, $type = 'error') { |
| |
| 55 | + | $this->_data[$group][] = array('text' => $message, |
| |
| 56 | + | 'type' => $type); |
|
26 | 57 | | } |
| |
27 | 58 | | |
  |
28 | | - | function add_session($class, $message, $type = 'error') { |
| |
29 | | - | if (isset($_SESSION['messageToStack'])) { |
| |
30 | | - | $messageToStack = $_SESSION['messageToStack']; |
| |
31 | | - | } else { |
| |
32 | | - | $messageToStack = array(); |
| |
33 | | - | } |
| |
| 59 | + | /** |
| |
| 60 | + | * Reset the message stack |
| |
| 61 | + | * |
| |
| 62 | + | * @access public |
| |
| 63 | + | */ |
|
34 | 64 | | |
  |
35 | | - | $messageToStack[] = array('class' => $class, 'text' => $message, 'type' => $type); |
| |
| 65 | + | public function reset() { |
| |
| 66 | + | $this->_data = array(); |
| |
| 67 | + | } |
|
36 | 68 | | |
  |
37 | | - | $_SESSION['messageToStack'] = $messageToStack; |
| |
| 69 | + | /** |
| |
| 70 | + | * Checks to see if a group in the stack contains messages |
| |
| 71 | + | * |
| |
| 72 | + | * @param string $group The name of the group to check |
| |
| 73 | + | * @access public |
| |
| 74 | + | */ |
|
38 | 75 | | |
  |
39 | | - | $this->add($class, $message, $type); |
| |
| 76 | + | public function exists($group) { |
| |
| 77 | + | return ( isset($this->_data[$group]) && !empty($this->_data[$group]) ); |
|
40 | 78 | | } |
| |
41 | 79 | | |
  |
42 | | - | function reset() { |
| |
43 | | - | $this->messages = array(); |
| |
| 80 | + | /** |
| |
| 81 | + | * Checks to see if the message stack contains messages |
| |
| 82 | + | * |
| |
| 83 | + | * @access public |
| |
| 84 | + | */ |
| |
| 85 | + | |
| |
| 86 | + | public function hasContent() { |
| |
| 87 | + | return !empty($this->_data); |
|
44 | 88 | | } |
| |
45 | 89 | | |
  |
46 | | - | function output($class) { |
| |
47 | | - | $messages = '<ul>'; |
| |
48 | | - | for ($i=0, $n=sizeof($this->messages); $i<$n; $i++) { |
| |
49 | | - | if ($this->messages[$i]['class'] == $class) { |
| |
50 | | - | switch ($this->messages[$i]['type']) { |
| |
| 90 | + | /** |
| |
| 91 | + | * Get the messages belonging to a group. The messages are placed into an |
| |
| 92 | + | * unsorted list wrapped in a DIV element with the "messageStack" style sheet |
| |
| 93 | + | * class. |
| |
| 94 | + | * |
| |
| 95 | + | * @param string $group The name of the group to get the messages from |
| |
| 96 | + | * @access public |
| |
| 97 | + | */ |
| |
| 98 | + | |
| |
| 99 | + | public function get($group) { |
| |
| 100 | + | $result = false; |
| |
| 101 | + | |
| |
| 102 | + | if ( $this->exists($group) ) { |
| |
| 103 | + | $result = '<div class="messageStack"><ul>'; |
| |
| 104 | + | |
| |
| 105 | + | foreach ( $this->_data[$group] as $message ) { |
| |
| 106 | + | switch ( $message['type'] ) { |
|
51 | 107 | | case 'error': |
  |
52 | | - | $bullet_image = DIR_WS_IMAGES . 'icons/error.gif'; |
| |
| 108 | + | $bullet_image = 'error.gif'; |
|
53 | 109 | | break; |
  |
| 110 | + | |
|
54 | 111 | | case 'warning': |
  |
55 | | - | $bullet_image = DIR_WS_IMAGES . 'icons/warning.gif'; |
| |
| 112 | + | $bullet_image = 'warning.gif'; |
|
56 | 113 | | break; |
  |
| 114 | + | |
|
57 | 115 | | case 'success': |
  |
58 | | - | $bullet_image = DIR_WS_IMAGES . 'icons/success.gif'; |
| |
| 116 | + | $bullet_image = 'success.gif'; |
|
59 | 117 | | break; |
  |
| 118 | + | |
|
60 | 119 | | default: |
  |
61 | | - | $bullet_image = DIR_WS_IMAGES . 'icons/bullet_default.gif'; |
| |
| 120 | + | $bullet_image = 'bullet_default.gif'; |
|
62 | 121 | | } |
| |
63 | 122 | | |
  |
64 | | - | $messages .= '<li style="list-style-image: url(\'' . $bullet_image . '\')">' . osc_output_string($this->messages[$i]['message']) . '</li>'; |
| |
| 123 | + | $result .= '<li style="list-style-image: url(\'' . DIR_WS_IMAGES . 'icons/' . $bullet_image . '\')">' . osc_output_string($message['text']) . '</li>'; |
|
65 | 124 | | } |
  |
| 125 | + | |
| |
| 126 | + | $result .= '</ul></div>'; |
| |
| 127 | + | |
| |
| 128 | + | unset($this->_data[$group]); |
|
66 | 129 | | } |
  |
67 | | - | $messages .= '</ul>'; |
|
68 | 130 | | |
  |
69 | | - | return '<div class="messageStack">' . $messages . '</div>'; |
| |
| 131 | + | return $result; |
|
70 | 132 | | } |
| |
71 | 133 | | |
  |
72 | | - | function outputPlain($class) { |
| |
73 | | - | $message = false; |
| |
| 134 | + | /** |
| |
| 135 | + | * Get the messages belonging to a group. The messages are separated by a new |
| |
| 136 | + | * line character. |
| |
| 137 | + | * |
| |
| 138 | + | * @param string $group The name of the group to get the messages from |
| |
| 139 | + | * @access public |
| |
| 140 | + | */ |
|
74 | 141 | | |
  |
75 | | - | for ($i=0, $n=sizeof($this->messages); $i<$n; $i++) { |
| |
76 | | - | if ($this->messages[$i]['class'] == $class) { |
| |
77 | | - | $message = osc_output_string($this->messages[$i]['message']); |
| |
78 | | - | break; |
| |
| 142 | + | public function getRaw($group) { |
| |
| 143 | + | $result = false; |
| |
| 144 | + | |
| |
| 145 | + | if ( $this->exists($group) ) { |
| |
| 146 | + | $result = ''; |
| |
| 147 | + | |
| |
| 148 | + | foreach ( $this->_data[$group] as $message ) { |
| |
| 149 | + | $result .= osc_output_string($message['text']) . "\n"; |
|
79 | 150 | | } |
  |
| 151 | + | |
| |
| 152 | + | unset($this->_data[$group]); |
|
80 | 153 | | } |
| |
81 | 154 | | |
  |
82 | | - | return $message; |
| |
| 155 | + | return $result; |
|
83 | 156 | | } |
| |
84 | 157 | | |
  |
85 | | - | function size($class) { |
| |
86 | | - | $class_size = 0; |
| |
| 158 | + | /** |
| |
| 159 | + | * Get the message stack array data set |
| |
| 160 | + | * |
| |
| 161 | + | * @access public |
| |
| 162 | + | */ |
|
87 | 163 | | |
  |
88 | | - | for ($i=0, $n=sizeof($this->messages); $i<$n; $i++) { |
| |
89 | | - | if ($this->messages[$i]['class'] == $class) { |
| |
90 | | - | $class_size++; |
| |
91 | | - | } |
| |
92 | | - | } |
| |
93 | | - | |
| |
94 | | - | return $class_size; |
| |
| 164 | + | public function getAll() { |
| |
| 165 | + | return $this->_data; |
|
95 | 166 | | } |
| |
96 | 167 | | |
  |
97 | | - | function loadFromSession() { |
| |
98 | | - | if (isset($_SESSION['messageToStack'])) { |
| |
99 | | - | $messageToStack = $_SESSION['messageToStack']; |
| |
| 168 | + | /** |
| |
| 169 | + | * Get the number of messages belonging to a group |
| |
| 170 | + | * |
| |
| 171 | + | * @param string $group The name of the group to check |
| |
| 172 | + | * @access public |
| |
| 173 | + | */ |
|
100 | 174 | | |
  |
101 | | - | for ($i=0, $n=sizeof($messageToStack); $i<$n; $i++) { |
| |
102 | | - | $this->add($messageToStack[$i]['class'], $messageToStack[$i]['text'], $messageToStack[$i]['type']); |
| |
103 | | - | } |
| |
| 175 | + | public function size($group) { |
| |
| 176 | + | $size = 0; |
|
104 | 177 | | |
  |
105 | | - | unset($_SESSION['messageToStack']); |
| |
| 178 | + | if ( $this->exists($group) ) { |
| |
| 179 | + | $size = sizeof($this->_data[$group]); |
|
106 | 180 | | } |
  |
| 181 | + | |
| |
| 182 | + | return $size; |
  |
107 | 183 | | } |
| |
108 | 184 | | } |
| |
109 | 185 | | ?> |