| | 147 | |
|---|
| | 148 | |
|---|
| | 149 | define("NOTIFICATION_TYPE_CRITICAL", 100); |
|---|
| | 150 | define("NOTIFICATION_TYPE_SECURITY", 200); |
|---|
| | 151 | define("NOTIFICATION_TYPE_UPDATE", 300); |
|---|
| | 152 | define("NOTIFICATION_TYPE_ERROR", 400); |
|---|
| | 153 | define("NOTIFICATION_TYPE_WARNING" , 500); |
|---|
| | 154 | define("NOTIFICATION_TYPE_NOTICE", 600); |
|---|
| | 155 | |
|---|
| | 156 | class notifications { |
|---|
| | 157 | |
|---|
| | 158 | var $not_loaded = true; |
|---|
| | 159 | var $notification_table = array(); |
|---|
| | 160 | var $_db; |
|---|
| | 161 | |
|---|
| | 162 | function &create(&$db) { |
|---|
| | 163 | static $obj; |
|---|
| | 164 | if (!isset($obj)) { |
|---|
| | 165 | $obj = new notifications($db); |
|---|
| | 166 | } |
|---|
| | 167 | return $obj; |
|---|
| | 168 | } |
|---|
| | 169 | |
|---|
| | 170 | function notifications(&$db) { |
|---|
| | 171 | $this->_db =& $db; |
|---|
| | 172 | } |
|---|
| | 173 | |
|---|
| | 174 | |
|---|
| | 175 | function add_critical($module, $id, $display_text, $extended_text="", $link="", $reset=true) { |
|---|
| | 176 | $this->_add_type(NOTIFICATION_TYPE_CRITICAL, $module, $id, $display_text, $extended_text, $link, $reset); |
|---|
| | 177 | } |
|---|
| | 178 | function add_security($module, $id, $display_text, $extended_text="", $link="", $reset=true) { |
|---|
| | 179 | $this->_add_type(NOTIFICATION_TYPE_SECURITY, $module, $id, $display_text, $extended_text, $link, $reset); |
|---|
| | 180 | } |
|---|
| | 181 | function add_update($module, $id, $display_text, $extended_text="", $link="", $reset=false) { |
|---|
| | 182 | $this->_add_type(NOTIFICATION_TYPE_UPDATE, $module, $id, $display_text, $extended_text, $link, $reset); |
|---|
| | 183 | } |
|---|
| | 184 | function add_error($module, $id, $display_text, $extended_text="", $link="", $reset=false) { |
|---|
| | 185 | $this->_add_type(NOTIFICATION_TYPE_ERROR, $module, $id, $display_text, $extended_text, $link, $reset); |
|---|
| | 186 | } |
|---|
| | 187 | function add_warning($module, $id, $display_text, $extended_text="", $link="", $reset=false) { |
|---|
| | 188 | $this->_add_type(NOTIFICATION_TYPE_WARNING, $module, $id, $display_text, $extended_text, $link, $reset); |
|---|
| | 189 | } |
|---|
| | 190 | function add_notice($module, $id, $display_text, $extended_text="", $link="", $reset=false) { |
|---|
| | 191 | $this->_add_type(NOTIFICATION_TYPE_NOTICE, $module, $id, $display_text, $extended_text, $link, $reset); |
|---|
| | 192 | } |
|---|
| | 193 | |
|---|
| | 194 | |
|---|
| | 195 | function list_critical($show_reset=false) { |
|---|
| | 196 | return $this->_list(NOTIFICATION_TYPE_CRITICAL, $show_reset); |
|---|
| | 197 | } |
|---|
| | 198 | function list_security($show_reset=false) { |
|---|
| | 199 | return $this->_list(NOTIFICATION_TYPE_SECURITY, $show_reset); |
|---|
| | 200 | } |
|---|
| | 201 | function list_update($show_reset=false) { |
|---|
| | 202 | return $this->_list(NOTIFICATION_TYPE_UPDATE, $show_reset); |
|---|
| | 203 | } |
|---|
| | 204 | function list_error($show_reset=false) { |
|---|
| | 205 | return $this->_list(NOTIFICATION_TYPE_ERROR, $show_reset); |
|---|
| | 206 | } |
|---|
| | 207 | function list_warning($show_reset=false) { |
|---|
| | 208 | return $this->_list(NOTIFICATION_TYPE_WARNING, $show_reset); |
|---|
| | 209 | } |
|---|
| | 210 | function list_notice($show_reset=false) { |
|---|
| | 211 | return $this->_list(NOTIFICATION_TYPE_NOTICE, $show_reset); |
|---|
| | 212 | } |
|---|
| | 213 | function list_all($show_reset=false) { |
|---|
| | 214 | return $this->_list("", $show_reset); |
|---|
| | 215 | } |
|---|
| | 216 | |
|---|
| | 217 | |
|---|
| | 218 | function reset($module, $id) { |
|---|
| | 219 | $module = q($module); |
|---|
| | 220 | $id = q($id); |
|---|
| | 221 | |
|---|
| | 222 | $sql = "UPDATE notifications SET reset = 1 WHERE module = $module AND id = $id"; |
|---|
| | 223 | sql($sql); |
|---|
| | 224 | } |
|---|
| | 225 | |
|---|
| | 226 | function delete($module, $id) { |
|---|
| | 227 | $module = q($module); |
|---|
| | 228 | $id = q($id); |
|---|
| | 229 | |
|---|
| | 230 | $sql = "DELETE FROM notifications WHERE module = $module AND id = $id"; |
|---|
| | 231 | sql($sql); |
|---|
| | 232 | } |
|---|
| | 233 | |
|---|
| | 234 | /* Internal functions |
|---|
| | 235 | */ |
|---|
| | 236 | |
|---|
| | 237 | function _add_type($level, $module, $id, $display_text, $extended_text="", $link="", $reset=false) { |
|---|
| | 238 | if ($this->not_loaded) { |
|---|
| | 239 | $this->notification_table = $this->_list("",true); |
|---|
| | 240 | $this->not_loaded = false; |
|---|
| | 241 | } |
|---|
| | 242 | |
|---|
| | 243 | $existing_row = false; |
|---|
| | 244 | foreach ($this->notification_table as $row) { |
|---|
| | 245 | if ($row['module'] == $module && $row['id'] == $id ) { |
|---|
| | 246 | $existing_row = $row; |
|---|
| | 247 | break; |
|---|
| | 248 | } |
|---|
| | 249 | } |
|---|
| | 250 | // Found an existing row - check if anything changed or if we are suppose to reset it |
|---|
| | 251 | // |
|---|
| | 252 | if ($existing_row) { |
|---|
| | 253 | if (($reset && $existing_row['reset'] == 0) || $existing_row['level'] != $level || $existing_row['display_text'] != $display_text || $existing_row['extended_text'] != $extended_text || $existing_row['link'] != $link) { |
|---|
| | 254 | $module = q($module); |
|---|
| | 255 | $id = q($id); |
|---|
| | 256 | $level = q($level); |
|---|
| | 257 | $display_text = q($display_text); |
|---|
| | 258 | $extended_text = q($extended_text); |
|---|
| | 259 | $link = q($link); |
|---|
| | 260 | $now = time(); |
|---|
| | 261 | $sql = "UPDATE notifications SET |
|---|
| | 262 | level = $level, |
|---|
| | 263 | display_text = $display_text, |
|---|
| | 264 | extended_text = $extended_text, |
|---|
| | 265 | link = $link, |
|---|
| | 266 | reset = 0, |
|---|
| | 267 | timestamp = $now |
|---|
| | 268 | WHERE module = $module AND id = $id |
|---|
| | 269 | "; |
|---|
| | 270 | sql($sql); |
|---|
| | 271 | |
|---|
| | 272 | // TODO: I should really just add this to the internal cache, but really |
|---|
| | 273 | // how often does this get called that if is a big deal. |
|---|
| | 274 | $this->not_loaded = true; |
|---|
| | 275 | } |
|---|
| | 276 | } else { |
|---|
| | 277 | // No existing row so insert this new one |
|---|
| | 278 | // |
|---|
| | 279 | $now = time(); |
|---|
| | 280 | $module = q($module); |
|---|
| | 281 | $id = q($id); |
|---|
| | 282 | $level = q($level); |
|---|
| | 283 | $display_text = q($display_text); |
|---|
| | 284 | $extended_text = q($extended_text); |
|---|
| | 285 | $link = q($link); |
|---|
| | 286 | $sql = "INSERT INTO notifications |
|---|
| | 287 | (module, id, level, display_text, extended_text, link, reset, timestamp) |
|---|
| | 288 | VALUES |
|---|
| | 289 | ($module, $id, $level, $display_text, $extended_text, $link, 0, $now) |
|---|
| | 290 | "; |
|---|
| | 291 | sql($sql); |
|---|
| | 292 | |
|---|
| | 293 | // TODO: I should really just add this to the internal cache, but really |
|---|
| | 294 | // how often does this get called that if is a big deal. |
|---|
| | 295 | $this->not_loaded = true; |
|---|
| | 296 | } |
|---|
| | 297 | } |
|---|
| | 298 | |
|---|
| | 299 | function _list($level, $show_reset=false) { |
|---|
| | 300 | |
|---|
| | 301 | $level = q($level); |
|---|
| | 302 | $where = array(); |
|---|
| | 303 | |
|---|
| | 304 | if (!$show_reset) { |
|---|
| | 305 | $where[] = "reset = 0"; |
|---|
| | 306 | } |
|---|
| | 307 | |
|---|
| | 308 | switch ($level) { |
|---|
| | 309 | case NOTIFICATION_TYPE_CRITICAL: |
|---|
| | 310 | case NOTIFICATION_TYPE_SECURITY: |
|---|
| | 311 | case NOTIFICATION_TYPE_UPDATE: |
|---|
| | 312 | case NOTIFICATION_TYPE_ERROR: |
|---|
| | 313 | case NOTIFICATION_TYPE_WARNING: |
|---|
| | 314 | case NOTIFICATION_TYPE_NOTICE: |
|---|
| | 315 | $where[] = "level = $level "; |
|---|
| | 316 | break; |
|---|
| | 317 | default: |
|---|
| | 318 | } |
|---|
| | 319 | $sql = "SELECT * FROM notifications "; |
|---|
| | 320 | if (count($where)) { |
|---|
| | 321 | $sql .= " WHERE ".implode(" AND ", $where); |
|---|
| | 322 | } |
|---|
| | 323 | $sql .= " ORDER BY level, module"; |
|---|
| | 324 | |
|---|
| | 325 | $list = sql($sql,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| | 326 | return $list; |
|---|
| | 327 | } |
|---|
| | 328 | } |
|---|
| | 329 | |
|---|
| | 330 | |
|---|