HOWTO: Top-of-every-page announcements

Poll
Do you find this fudge helpful?

Post

Posted
Rating:
Item has a rating of 5 Item has a rating of 5 Item has a rating of 5 Item has a rating of 5 Item has a rating of 5 (Liked by Chris GrahamLiked by Jason Verhagen)
#349 (In Topic #123)

This tutorial fudge will show you how you can incorporate global messages to show on the top of every page for everyone. An example would be a notice that you're taking the site down at X time for maintenance.

This tutorial involves modifying the Composr code. Use at your own risk!

Step 1:

Create a new database table with the name (prefix)messages_global (Prefix would be something like cms_ . Check your configuration.)

The database should have 5 columns:

ID ( int(11), PRIMARY key, AUTO_INCREMENT)
message (text, utf8_general_ci collation)
type (text, utf8_general_ci collation)
begin ( int(11), default: 0)
end ( int(11), default: 0)

message column will contain the message to be displayed to everyone.
Type is the type of message (inform, notice, or warn. Inform will make the message have a green check mark. Notice will make it have a yellow triangle with an ! in it, and warn will have an orange triangle with an ! in it.)
Begin is the unix timestamp indicating when the message should begin showing. The time should be relative to your server's timezone. Use 0 to display immediately.
End is the unix timestamp indicating when the message should stop showing. The time should be relative to your server's timezone. Use 0 to display until you remove it from the database.


Step 2:

Open the file sources/site.php , preferably using the Composr code editor found at yoursite.domain/code_editor.php . If you are not using the code editor, save a copy of site.php into sources_custom/site.php and modify that version instead. Bottom line, do not edit the original!

Step 3:

FIND

function do_site()
{
    // Any messages to output?
    if (get_param_integer('redirected', 0) == 1) {
        $messages = $GLOBALS['SITE_DB']->query_select('messages_to_render', array('r_message', 'r_type'), array('r_session_id' => get_session_id(),), 'ORDER BY r_time DESC');
        foreach ($messages as $message) {
            if ($GLOBALS['XSS_DETECT']) {
                ocp_mark_as_escaped($message['r_message']);
            }
            attach_message(protect_from_escaping($message['r_message']), $message['r_type']);
        }
        if (count($messages) != 0) {
            $GLOBALS['SITE_DB']->query('DELETE FROM ' . $GLOBALS['SITE_DB']->get_table_prefix() . 'messages_to_render WHERE ' . db_string_equal_to('r_session_id', get_session_id()) . ' OR r_time<' . strval(time() - 60 * 60));
        }
    }

Step 4:
   

After, ADD

$messages = $GLOBALS['SITE_DB']->query_select('messages_global', array('message', 'type'), NULL, 'WHERE `begin` <= ' . time() . ' AND (`end` > ' . time() . ' OR `end` = 0) ORDER BY begin DESC');
        foreach ($messages as $message) {
            if ($GLOBALS['XSS_DETECT']) {
                ocp_mark_as_escaped($message['message']);
            }
            attach_message(protect_from_escaping($message['message']), $message['type']);
        }

Enjoy! Please note you will need to manage your message directly in the database; there is no user interface. I may turn this into an addon with an adminzone interface in the future, considering people find this useful.
0 guests and 0 members have recently viewed this.