JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
xhtml -> html5
[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 # This file is useful for putting message box(es) on the screen. If you include
20 # session_messages.php these messages can even persist accross http redirects
21 # (such as you should do after receiving a POST.
22 #
23 # Just call message("message here") whenever you have something to report.
24 #
25 #
26 # Once a template is loaded, call display_messages(). run.php will do that
27 # automatically after your main function returns, so long as you require_once
28 # this file.
29 #
30 # Just make sure your template has a <!--~message {~--> section with a
31 # ~value html~ tag in it.
32 #
33 # Simple example:
34 #
35 #    <!--~messages {~-->
36 #      <p>~value html~</p>
37 #    <!--~}~-->
38 #
39 # Full-featured example:
40 #
41 #    <!--~wfpl_messages {~-->
42 #      <!--~ first {~-->
43 #        <div style="border: 2px solid red; background: #fbb; padding: 5px; margin: 20px 0px">
44 #      <!--~}~-->
45 #          <p style="font-size: 120%; padding: 5px; margin: 0px">~data html~</p>
46 #      <!--~ sep {~-->
47 #          <hr>
48 #      <!--~}~-->
49 #      <!--~ last {~-->
50 #        </div>
51 #      <!--~}~-->
52 #    <!--~}~-->
53
54 require_once('code/wfpl/template.php');
55
56 function message($msg) {
57         if(!isset($GLOBALS['wfpl_messages'])) {
58                 $GLOBALS['wfpl_messages'] = array();
59         }
60
61         $GLOBALS['wfpl_messages'][] = $msg;
62 }
63
64 function get_messages() {
65         if(!isset($GLOBALS['wfpl_messages'])) {
66                 $messages = array();
67         } else {
68                 $messages = $GLOBALS['wfpl_messages'];
69                 unset($GLOBALS['wfpl_messages']);
70         }
71
72         if(function_exists('session_restore_messages')) {
73                 $messages = array_merge(session_restore_messages(), $messages);
74         }
75
76         return $messages;
77 }
78
79 # for old-style templates
80 function display_messages(&$tem = NULL, $key = 'wfpl_messages') {
81         if($tem) {
82                 $tem->data[$key] = columnize(get_messages());
83         } else {
84                 $GLOBALS['wfpl_template']->data[$key] = columnize(get_messages());
85         }
86 }