Gossamer Forum
Home : Products : Gossamer Links : PHP Front End :

Days_Old "plugin" hack

Quote Reply
Days_Old "plugin" hack
I've got a crude, but working version of the Perl Days_Old plugin working for using a graphic to represent new links. (As an aside, I've always wondered why this isn't built into Links as a standard feature, since it seems to be quite popular and a natural extension of the "new" superscript titles.)

I haven't done it yet for categories, just links at this point, but it should be pretty similar. In Utils.inc.php, function _print_link(), I added the following just after if (!isset($opts['return_as_string'])) at the top (before the function makes display preference adjustments to some of the necessary values:

Code:
if ($link['isNew'] == 'Yes') {
$add_date = explode("-", $link['Add_Date']);
$add_stamp = mktime(12, 0, 0, $add_date[1], $add_date[2], $add_date[0]);
$today_stamp = mktime(12, 0, 0, date("m, d, Y"));
$link['Days_Old'] = ($today_stamp - $add_stamp) / (24*60*60);
} else {
$link['Days_Old'] = '';
}

Then in link.html, you can use $Days_Old to represent the number of days old a link is, such as to assign a graphic to it corresponding to the number. For example:

Code:
<?if ($isNew) {?>
<img src="/images/new<?echo $Days_Old?>.gif">
<?}?>

It looks like a similar treatment to function _print_cat() right below it will handle the categories, but I haven't yet tested that.

There aren't any built in functions for the PHP front end like GT::Date::date_diff (sub date_diff{} in Date.pm), are there?

Dan
Quote Reply
Re: [Dan Kaplan] Days_Old "plugin" hack In reply to
Ok, I'm a bit stumped on the category side of things. function _print_cat() sure looks like it is the place to make changes, but I don't see anything there related to $Has_New_Links... All it seems to put together is the category title and URL; surely I'm missing something???

edit: Oh wait, I think I partially see what's happening. Has_New_Links is a column in the Categories table, so that must be one of the $cat_r[] variables being passed straight through to subcategory.html. In that case, using Newest_Link is probably the step I was missing.

I slightly changed the above code to move the date calculations into a function. Makes no difference unless I can get the category side of things working (to share the calculations), but it looks cleaner. I'll post that version if I get somewhere with the categories...

Dan

Last edited by:

Dan Kaplan: Jul 10, 2002, 4:56 PM
Quote Reply
Re: [Dan Kaplan] Days_Old "plugin" hack In reply to
Got it! Ok, here we go:

Utils.inc.php

add a new function:

Code:
# if no 2nd field is passed in, use today's date as the end range.
# similar to: GT::Date::date_diff ( GT::Date::date_get(), $link->{'Add_Date1'})
function _date_diff($start, $end="") {
$add_date = explode("-", $start);
$add_stamp = mktime(12, 0, 0, $add_date[1], $add_date[2], $add_date[0]);
if ($end == "") {
$today_stamp = mktime(12, 0, 0, date("m, d, Y"));
} else {
$end_date = explode("-", $end);
$today_stamp = mktime(12, 0, 0, $end_date[1], $end_date[2], $end_date[0]);
}
$Days_Old = ($today_stamp - $add_stamp) / (24*60*60);
return $Days_Old;
}

function _print_link()

add the following toward the top of the function (above if ($CFG['date_db_format'] != $CFG['date_user_format'])):

Code:
if ($link['isNew'] == 'Yes') {
$link['Days_Old'] = _date_diff($link['Add_Date']);
if ($link['Days_Old'] > $CFG['build_new_cutoff']) {
$link['isNew'] = 'No';
}
} else {
$link['Days_Old'] = '';
}

function _print_cat()

add the following somewhere within the foreach() loop (before $output .= print_page(...)):

Code:
if ($cat_r['Has_New_Links'] == 'Yes') {
$cat_r['Days_Old'] = _date_diff($cat_r['Newest_Link']);
if ($cat_r['Days_Old'] > $CFG['build_new_cutoff']) {
$cat_r['Has_New_Links'] = 'No';
}
} else {
$cat_r['Days_Old'] = '';
}

You'll notice that I added a couple of checks to make sure the $Days_Old calculation falls within the build_new_cutoff setting. I don't know if that's a problem in 2.1.1, but I remember 2.0.5 came up with some strange calculations at times.

link.html

<?if ($isNew) {?>
<img src="/images/new<?echo $Days_Old?>.gif">
<?}?>

using whatever images you want, named new1.gif, new2. gif, etc.

subcategory.html

<?if ($Has_New_Links == 'Yes') {?><img src="/images/new<?echo $Days_Old?>.gif"><?}?>

That should do it.

Dan
Quote Reply
Re: [Dan Kaplan] Days_Old "plugin" hack In reply to
Just a thought, it would probably be wise to change one line in the added _date_diff() function. Change:

$Days_Old = ($today_stamp - $add_stamp) / (24*60*60);

to:

$Days_Old = intval($today_stamp - $add_stamp) / (24*60*60));

Shouldn't come into play, as both start and end date are being compared to the same hour, minute, and second, but just to be safe...

Dan