Change Your MODX Web Site with the Seasons

A long time ago in MODX Evolution, I had a client who wanted to show photos related to the current season and offer four different seasonal coupons. I thought I’d provide an updated version of the snippet for people who want to do something similar.

This snippet allow you to automatically produce seasonal changes on your web site. It can be used to simply display the name of the current season or to pull in chunks that "seasonalize" the site in various ways. It can be used multiple times on the same page. If you’re using the chunk version, you can create four chunks called summer, fall, winter, and spring (these are the default chunk names). Optionally, you can send the names of the four chunks as properties.

Usage

This version will return just the name of the season:

[[!Season? &nameOnly=`1`]]

This one will return the content of the appropriate chunk if the chunks are called summer, fall, winter, and spring:

[[!Season]]

This version sets the names of the chunks to use for each season:

[[!Season?
    &summerChunk=`MySummerChunk`
    &fallChunk=`MyFallChunk`
    &winterChunk=`MyWinterChunk`
    &springChunk=`MySpringChunk`
]]

You could use this snippet to change the CSS with the seasons by putting the path to a different CSS file in each parameter and using the snippet call in your template where the CSS file is specified.

Using an image reference, it could be used to change an image or background image with the seasons.

The Snippet

Paste this code into a snippet called Season and save it.

<?php
/* Season snippet */

if (! function_exists('season')) {
    function season() {
        $limits = array(
            '/12/21' => 'winter',
            '/09/21' => 'fall',
            '/06/21' => 'summer',
            '/03/21' => 'spring',
            '/01/01' => 'winter'
        );
        $adate = date("M d Y"); /* set $adate to today */

        foreach ($limits AS $key => $value) {
            $limit = date("Y") . $key;
            if (strtotime($adate) >= strtotime($limit)) {
                return $value;
            }
        }
    }
}

$summerChunk = $modx->getOption('summerChunk', $scriptProperties, 'summer');
$fallChunk = $modx->getOption('fallChunk', $scriptProperties, 'fall');
$winterChunk = $modx->getOption('winterChunk', $scriptProperties, 'winter');
$springChunk = $modx->getOption('springChunk', $scriptProperties, 'spring');

$nameOnly = $modx->getOption('nameOnly', $scriptProperties, false);

/* Get the name of the current season */
$season = season();

/* Figure out what to return */
if (!empty($nameOnly)) {
    /* Return just the season name */
    $output = $season;
} else {
    /* Return the content of the appropriate chunk */
    $output = $modx->getChunk(${$season . 'Chunk'});
}
return $output;

Internationalization

You can change the season names wherever they appear in the code, and if you’re using chunks, change the names of your chunks accordingly or just send their names in the snippet properties. Be sure to change all of them and preserve the case of each season. You don’t need to change the date formatting, since the date is never displayed and only digits are used for the calculation.

The date ranges are in the form /month/day and correspond to the official seasons in the USA. You may wish to change them for other locales.

If want to use the snippet on a multi-language site, you can create lexicon files. You’ll need to send an &language property in the snippet tag with the two-letter language code and add these lines just below the function in order to load the appropriate lexicon file:

$language = $modx->getOption('language', $scriptProperties, 'en');
$modx->setOption('cultureKey', $language);
$modx->lexicon->load('season:default');

Change the line that returns just the season name to:

$output = $modx->lexicon($season);

Then, in the lexicon file (say at, core/components/season/lexicon/fr/default.inc.php), do this:

$_lang['spring'] = 'printemps';
$_lang['winter'] = 'hiver';
/* etc. */

 


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

One Comment on Change Your MODX Web Site with the Seasons

  1. Google.com says:

    Hey there! Someone in my Facebook group shared this website with us so I
    came to look it over. I’m definitely enjoying the information.
    I’m book-marking and will be tweeting this to my followers!
    Great blog and fantastic style and design.

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 6 = 7

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>