JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed/rewrote display_messages(), added columnize
authorJason Woofenden <jason@jasonwoof.com>
Wed, 4 Nov 2009 09:29:52 +0000 (04:29 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 4 Nov 2009 15:32:15 +0000 (10:32 -0500)
messages.php
misc.php

index 2d065e3..0dc21e2 100644 (file)
 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-# This file is useful for putting message box(es) on the screen.
+# This file is useful for putting message box(es) on the screen. If you include
+# session_messages.php these messages can even persist accross http redirects
+# (such as you should do after receiving a POST.
 #
 # Just call message("message here") whenever you have something to report.
 #
-# Once a template is loaded, call display_messages(). Your template should have
-# a <!--~message?~--> section with a ~text:html~ tag in it.
 #
-# If you want a divider (any text between message boxes when there are
-# multiple boxes) provide a sub-template section named "separator"
-# INSIDE "message" at the begining of it.
+# Once a template is loaded, call display_messages(). run.php will do that
+# automatically after your main function returns, so long as you require_once
+# this file.
+#
+# Just make sure your template has a <!--~message {~--> section with a
+# ~value html~ tag in it.
 #
-# If you'd like something around the group of all message boxes, you can put
-# the whole thing in a sub-template section called "messages"
-
 # Simple example:
 #
-#    <!--~message?~-->
-#        <p>~text:html~</p>
-#    <!--~.~-->
-
+#    <!--~messages {~-->
+#      <p>~value html~</p>
+#    <!--~}~-->
+#
 # Full-featured example:
 #
-#    <!--~messages?~-->
-#         <div style="border: 2px solid red; background: #f88; padding: 5px">
-#         <!--~message?~-->
-#             <!--~separator?~-->
-#                 <hr />
-#             <!--~separator.~-->
-#             <p style="font-size: 120%">~text:html~</p>
-#         <!--~message.~-->
-#         </div>
-#    <!--~messages.~-->
+#    <!--~wfpl_messages {~-->
+#      <!--~ first {~-->
+#        <div style="border: 2px solid red; background: #fbb; padding: 5px; margin: 20px 0px">
+#      <!--~}~-->
+#          <p style="font-size: 120%; padding: 5px; margin: 0px">~data html~</p>
+#      <!--~ sep {~-->
+#          <hr />
+#      <!--~}~-->
+#      <!--~ last {~-->
+#        </div>
+#      <!--~}~-->
+#    <!--~}~-->
 
 require_once('code/wfpl/template.php');
 
 function message($msg) {
        if(!isset($GLOBALS['wfpl_messages'])) {
-               if(function_exists('session_restore_messages')) {
-                       $GLOBALS['wfpl_messages'] = session_restore_messages();
-               } else $GLOBALS['wfpl_messages'] = array();
+               $GLOBALS['wfpl_messages'] = array();
        }
 
        $GLOBALS['wfpl_messages'][] = $msg;
 }
 
-function display_messages(&$tem = NULL) {
-       if(!$GLOBALS['wfpl_messages']) return;
-       foreach($GLOBALS['wfpl_messages'] as $msg) {
-               $sub = array('text' => $msg);
-               $GLOBALS['wfpl_tem_data']['message'][] = $sub;
-               $GLOBALS['wfpl_tem_data']['messages'] = TRUE;
+function get_messages() {
+       if(!isset($GLOBALS['wfpl_messages'])) {
+               $messages = array();
+       } else {
+               $messages = $GLOBALS['wfpl_messages'];
+               unset($GLOBALS['wfpl_messages']);
+       }
+
+       if(function_exists('session_restore_messages')) {
+               $messages = array_merge(session_restore_messages(), $messages);
+       }
+
+       return $messages;
+}
+
+# for old-style templates
+function display_messages(&$tem = NULL, $key = 'wfpl_messages') {
+       if($tem) {
+               $tem->data[$key] = columnize(get_messages());
+       } else {
+               $GLOBALS['wfpl_template']->data[$key] = columnize(get_messages());
        }
 }
index b18abd4..1e9a2b7 100644 (file)
--- a/misc.php
+++ b/misc.php
@@ -102,6 +102,26 @@ function get_text_between($text, $start_text, $end_text) {
        return substr($text, 0, $end);
 }
 
+# Make it easy to insert an array into the template data structure so that each
+# element of the array gets its own row.
+#
+# passed this: columnate(array('a', 'b', 'c'), 'k');
+# it returns: array(array('k' => 'a'),
+#                   array('k' => 'b'),
+#                   array('k' => 'c'));
+# passed this: columnate(array(), 'k');
+# it returns: false
+function columnize($arr, $key = 'data') {
+       if(!$arr) {
+               return false;
+       }
+       $ret = array();
+       foreach($arr as $val) {
+               $ret[] = array($key => $val);
+       }
+       return $ret;
+}
+
 # php4 is broken, in that you cannot set a default value for a parameter that
 # is passed by reference. So, this is set up to use the following screwy
 # syntax: