Trying to create a "random entry if nothing new" check

Creating and modifying plugins.
Post Reply
vincem
Regular
Posts: 59
Joined: Sat Mar 05, 2005 6:01 am
Contact:

Trying to create a "random entry if nothing new" check

Post by vincem »

Hi,

My knowledge of PHP and Smarty being rather superficial to say the least, I'm having trouble implementing my random entry functionality as follows

I've found in the entrypaging plugin that a random entry link is generated (and in my case placed at the top of the detailed entry page) but I would like to use that value (which is $link and represents an HMTL link) and inject it directly at the top of my entries.tpl if a certain condition returns TRUE (in my case, I want to check if the most recent entry is older than 2 days and if so, inject that random entry at the top of my page and then proceed with regular entries). The issue I'm having is using the entry data from $link instead of the link itself.

In other words, can I display the content of the random entry by calling it via the entrypaging plugin variable or do I need to rewrite everything? (in which case I'm doomed. ;-)

If this doesn't make any sense, it's probably because I'm trying to brew coffee with tea leaves. Sorry!

The slightest push in the right direction would be much appreciated...

Cheers,

Vince
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Trying to create a "random entry if nothing new" check

Post by garvinhicking »

Hi!

You should first scrap that idea, cause it generates more pain that it solves. The entrypaging plugin only specifies you with the link. To get the contents of that entry, you need PHP logic.

I'd go a seperate route, and either create a custom event plugin that does your job, or implement it inside your template. Since putting it into your template's config.inc.php is a lot easier, I will tell you how to use that variant.

1. Inside your config.inc.php file (if it does not exist, create it) add this code:

Code: Select all

function show_random_entry($params, &$smarty) {
    global $serendipity;

    // Check latest timestamp
    $ts = serendipity_db_query("SELECT MAX(timestamp) AS max FROM {$serendipity['dbPrefix']}entries WHERE isdraft = 'false'");

    // If the latest entry is older than 2 days...
    if (is_array($ts) && $ts[0]['max'] <= time()-(86400*2)) {
        // Get a random entry
        $random_entry = serendipity_db_query("SELECT id FROM {$serendipity['dbPrefix']}entries WHERE isdraft = 'false' ORDER BY RAND() LIMIT 1");

        if (is_array($random_entry) && isset($random_entry[0]['id'])) {

            $old_entries = $smarty->get_template_vars('entries');
            $smarty->assign('is_random_entry', true);
            $entry = array(serendipity_fetchEntry('id', $random_entry[0]['id']));
            serendipity_printEntries($entry, false, false, 'ENTRIES2', false, true, false);

            $out = serendipity_smarty_fetch('smarty_random_entry', 'entries.tpl');
            $smarty->assign('entries', $old_entries);
            return $out;

        }
    }
}
$serendipity['smarty']->register_function('show_random_entry', 'show_random_entry');
That is a function that does your logic. As you can see, this is not really trivial, you first need to get the most recent entry timestamp, then compare it to the current time, then use the s9y API to fetch and display the entries.

2. Inside your entries.tpl file you can add this on top of the file:

Code: Select all

{if !$is_random_entry}
    {show_random_entry}
{/if}
3. The actual entry is then printed using the entries.tpl file itself. You can add {if $is_random_entry} checks in there to make custom outputs when the random entry is showed, or {if !$is_random_entry} checks to only show things when NOT in random entry mode (like currently the footer will shown twice in the page, you would need to wrap the footer output {if !$is_random_entry} ... {/if} around there, if you don't want it for a random entry.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
vincem
Regular
Posts: 59
Joined: Sat Mar 05, 2005 6:01 am
Contact:

Re: Trying to create a "random entry if nothing new" check

Post by vincem »

Garvin,

That was just brilliant!!!

I've implemented it with a few tweaks. The blog now serves a random entry to any visitor (via browsers only, no RSS obviously) who arrives at least two days after the last entry was posted. It will help greatly because my posting schedule is rather erratic these days and I don't want people to get bored!

I added your php code as-is to the bottom of my template's config.inc.php.

As for entries.tpl, I added

Code: Select all

{if !$is_random_entry}
    {show_random_entry}
{/if}
at the top of the file and also implemented some checks to control the display of additional text before and after the random entry only, and some random-only CSS.

I also transferred the following code from entries.tpl to index.tpl right after the CONTENT, in order to avoid the duplicate entry count and prev/next navigation links that were showing up once after my random entry and once at the bottom of the page, as you mentionned it would:

Code: Select all

{if $footer_prev_page || $footer_next_page}
    <div class="page-nav">
    {if $footer_prev_page}
        <span class="previous-entries"><a href="{$footer_prev_page}">{$CONST.PREVIOUS_PAGE}</a></span>
    {/if}
    {if $footer_next_page}
        <span class="next-entries"><a href="{$footer_next_page}">{$CONST.NEXT_PAGE}</a></span>
    {/if}
    {serendipity_hookPlugin hook="entries_footer"}
    </div><!-- /.page-nav -->
    <div style="text-align: center;background: #8d8d8d;">{if $footer_info}({$footer_info}){/if}</div>
    {/if}
For anybody wishing to see Garvin's code in action, you'll have to wait until an actual gap of 2 days+ between my posts, which shouldn't take long! ;-)

Thanks again Garvin, outstanding support as usual!

Cheers,
Vince
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Trying to create a "random entry if nothing new" check

Post by garvinhicking »

Hi!

Great you like the idea and that it works out for you! :-)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply