JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
messages should only be displayed once
[wfpl.git] / messages.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22
23 # This file is useful for putting message boxe(s) on the screen.
24 #
25 # Just call message("message here") whenever you have something to report.
26 #
27 # Once a template is loaded, call display_messages(). Your template should have
28 # a <!--~message_box start~--> section with ~message_text.html~ tag in it.
29 #
30 # If you want a divider (any text between message boxes when there are multiple
31 # boxes) provide a sub-template section named "message_divider" INSIDE
32 # "message_box" at the begining of it.
33 #
34 # If you'd like something around the group of all message boxes, you can put
35 # the whole thing in a sub-template section called "message_container"
36
37 # Simple example:
38 #
39 #    <!--~message_box start~-->
40 #        <p>~message_text.html~</p>
41 #    <!--~end~-->
42
43 # Full-featured example:
44 #
45 #    <!--~message_container start~-->
46 #         <div style="border: 2px solid red; background: #f88; padding: 5px">
47 #         <!--~message_box start~-->
48 #             <!--~message_divider start~-->
49 #                 <hr />
50 #             <!--~end~-->
51 #             <p style="font-size: 120%">~message_text.html~</p>
52 #         <!--~end~-->
53 #         </div>
54 #    <!--~end~-->
55
56 require_once('code/wfpl/template.php');
57
58 function message($msg) {
59         if(!isset($GLOBALS['wfpl_messages'])) {
60                 $GLOBALS['wfpl_messages'] = array();
61         }
62
63         $GLOBALS['wfpl_messages'][] = $msg;
64 }
65
66 # if you want the messages in a template other than the default one, pass it like so:
67 #
68 # display_messages(ref($my_template));
69 function display_messages($template = 0) {
70         $first = true;
71         if($template === 0) {
72                 $template = &$GLOBALS['wfpl_template'];
73         } else {
74                 $template = &$template->ref;
75         }
76
77         if(function_exists('session_restore_messages')) {
78                 session_restore_messages();
79         }
80
81         if($GLOBALS['wfpl_messages']) {
82                 foreach($GLOBALS['wfpl_messages'] as $msg) {
83                         if($first) {
84                                 $first = false;
85                         } else {
86                                 $template->sub('message_divider');
87                         }
88                         $template->set('message_text', $msg);
89                         $template->sub('message_box');
90                 }
91                 $template->sub('message_container');
92                 unset($GLOBALS['wfpl_messages']);
93         }
94 }
95
96 ?>