HOWTO: Top-of-every-page announcements

Do you find this fudge helpful?

Yes! I'm going to use it on my site.
Yes, but I probably will not use it.
Indifferent
No

Post

Posted
Rating:
Item has a rating of 5 (Liked by Chris GrahamLiked by Jason Verhagen)
#285 (In Topic #66)
Avatar
Site director
Patrick Schmalstig is in the usergroup ‘Administrators’

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.

  • Need support for version 10? The core development team is no-longer offering it for free (unless it's a critical bug that breaks your entire site or a serious security hole). Please consider hiring me instead if you need v10 support or a non-critical bug fix. Or, ask the community in the forums!
  • Do you enjoy Composr? Please consider contributing your talent to the project or recommending Composr to others. Even small contributions make a big impact in the Composr community.
  • Do you have feedback for us? You can report bugs, suggest features, or give feedback on the Free support options page.
  • Do you need professional service with your Composr website? Please consider contracting me for your needs through my company, PDStig, LLC. Doing so will also help fund Composr development.
  • Want to watch live streams of me developing Composr CMS? Please subscribe to me on Twitch to be notified when I stream. Composr development streams are usually spontaneous / not scheduled in advance as work priorities come first.
Online now: No Back to the top
1 guest and 0 members have just viewed this.

Statistics

Forum statistics:
  • 2,053 topics, 7,195 posts, 10,839 members
  • Our newest member is PurplewaveIndia
Birthdays:
Back to Top