Change Your MODX Web Site with the Seasons

A utility snippet to help change your web site automatically 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 I used to do that for people who want to do something similar. I've tested this snippet in MODX Revolution, but I think it would still run in Evolution if you changed tag syntax.

This snippet allows 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. */

——

Comments (0)


Please login to comment.

  (Login)