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