1: <?php
2:
3: /**
4: * ProcessWire Notices
5: *
6: * Contains notices/messages used by the application to the user.
7: *
8: * ProcessWire 2.x
9: * Copyright (C) 2010 by Ryan Cramer
10: * Licensed under GNU/GPL v2, see LICENSE.TXT
11: *
12: * http://www.processwire.com
13: * http://www.ryancramer.com
14: *
15: */
16:
17: /**
18: * Base class that holds a message, source class, and timestamp
19: *
20: */
21: abstract class Notice extends WireData {
22:
23: /**
24: * Flag indicates the notice is for when debug mode is on only
25: *
26: */
27: const debug = 2;
28: const warning = 4;
29:
30: public function __construct($text, $flags = 0) {
31: $this->set('text', $text);
32: $this->set('class', '');
33: $this->set('timestamp', time());
34: $this->set('flags', $flags);
35: }
36: }
37:
38: /**
39: * A notice that's indicated to be informational
40: *
41: */
42: class NoticeMessage extends Notice { }
43:
44: /**
45: * A notice that's indicated to be an error
46: *
47: */
48: class NoticeError extends Notice { }
49:
50: /**
51: * A class to contain multiple Notice instances, whether messages or errors
52: *
53: */
54: class Notices extends WireArray {
55:
56: public function isValidItem($item) {
57: return $item instanceof Notice;
58: }
59:
60: public function makeBlankItem() {
61: return new NoticeMessage('');
62: }
63:
64: public function add($item) {
65: if($item->flags & Notice::debug) {
66: if(!$this->fuel('config')->debug) return $this;
67: }
68: return parent::add($item);
69: }
70:
71: public function hasErrors() {
72: $numErrors = 0;
73: foreach($this as $notice) {
74: if($notice instanceof NoticeError) $numErrors++;
75: }
76: return $numErrors > 0;
77: }
78: }
79: