<?php
/**
* Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
* Copyright (C) 2002-2004 The Nucleus Group
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* (see nucleus/documentation/index.html#license for more info)
*
*/
class NP_Age extends NucleusPlugin {
function getEventList() { return array(); }
function getName() { return 'Age'; }
function getAuthor() { return 'Trent Adams'; }
function getURL() { return 'http://www.trentadams.com/'; }
function getVersion() { return '1.0'; }
function install() {
$this->createOption('FormatStart','Format Start Number Calculation: HTML or CSS Tags','text','<b>');
$this->createOption('FormatFinish','Format Finish Number Calculation: HTML or CSS Tags','text','</b>');
$this->createOption('FormatStartText','Format Start Text: HTML or CSS Tags','text','<i>');
$this->createOption('FormatFinishText','Format Finish Text: HTML or CSS Tags','text','</i>');
}
function getDescription() {
return 'This template plugin can be used to calculate how old the poster was relative to the item timestamp. Please look at http://wakka.xiffy.nl/Age/ for details on usage and installation.';
}
var $itemtime;
function doTemplateVar(&$item, $look) {
global $manager, $blog, $CONF;
$params = func_get_args();
$b =& $blog;
if ($params[1]){
$detail = $params[1];
} else {
$detail = "need detail level,";
}
if ($params[2]){
$year = $params[2];
} else {
$year = "need year,";
}
if ($params[3]){
$month = $params[3];
} else {
$month = "need month,";
}
if ($params[4]){
$day = $params[4];
} else {
$day = "need day,";
}
if ($params[5]){
$event = $params[5];
} else {
$event = "need event,";
}
$itemtime = $item->timestamp;
define("OFFSET", 0);
define("YSECS", 365*24*60*60);
define("DSECS", 24*60*60);
define("HSECS", 60*60);
define("MSECS", 60);
echo $this->getOption(FormatStartText);
echo $event;
echo $this->getOption(FormatFinishText);
echo $this->getOption(FormatStart);
$this->age($item->timestamp,$detail, $year, $month, $day);
echo $this->getOption(FormatFinish);
}
function age($itemtime, $detail, $year, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0) {
$years = $days = $hours = $minutes = $seconds = 0;
$born = mktime($hour, $minute, $second, $month, $day, $year);
$itemtime = $itemtime + OFFSET*60*60;
if ($born < 0) {
$cdown = $itemtime + $born * -1;
} else {
$cdown = abs($itemtime - $born);
}
if ($detail == 1) $years = round($cdown/YSECS);
else $years = floor($cdown/YSECS);
$cdown %= YSECS;
if ($detail == 2) $days = round($cdown/DSECS);
else $days = floor($cdown/DSECS);
$cdown %= DSECS;
if ($detail == 3) $hours = round($cdown/HSECS);
else $hours = floor($cdown/HSECS);
$cdown %= HSECS;
if ($detail == 4) $minutes = round($cdown/MSECS);
else $minutes = floor($cdown/MSECS);
$cdown %= MSECS;
$seconds = $cdown;
$tnums = array($years, $days, $hours, $minutes, $seconds);
$ttext = array("year", "day", "hour", "minute", "second");
$shown = 0;
for ($i=0;$i<$detail;$i++) {
if ($tnums[$i]) {
echo "$tnums[$i] $ttext[$i]";
$shown++;
if ($tnums[$i] != 1) echo "s";
$count = 0;
for ($j=$i+1;$j<$detail;$j++) {
if ($tnums[$j]) $count++;
}
switch($count) {
case 0: break 2;
case 1: if ($shown>1) echo ","; echo " and "; break;
default: echo ", "; break;
}
}
}
if ($itemtime > $born) echo " old";
if ($itemtime == $born) echo "old";
}
}
?>
itemage::Plugins