Jonas Häggqvist | 31ca70b | 2010-01-31 00:44:07 +0000 | [diff] [blame] | 1 | <?php |
Solomon Peachy | a0eef36 | 2020-04-02 10:05:23 -0400 | [diff] [blame] | 2 | /************************************************************************ |
Jonas Häggqvist | 31ca70b | 2010-01-31 00:44:07 +0000 | [diff] [blame] | 3 | * __________ __ ___. |
| 4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 8 | * \/ \/ \/ \/ \/ |
Jonas Häggqvist | 31ca70b | 2010-01-31 00:44:07 +0000 | [diff] [blame] | 9 | * Copyright (C) 2009 Jonas Häggqvist |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version 2 |
| 14 | * of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 17 | * KIND, either express or implied. |
| 18 | * |
Solomon Peachy | a0eef36 | 2020-04-02 10:05:23 -0400 | [diff] [blame] | 19 | **************************************************************************/ |
Jonas Häggqvist | 31ca70b | 2010-01-31 00:44:07 +0000 | [diff] [blame] | 20 | function qs($count) { |
| 21 | return $count == 1 ? '' : 's'; |
| 22 | } |
| 23 | |
| 24 | class templater { |
| 25 | private $s; |
| 26 | |
| 27 | public function __construct($smartydir) { |
| 28 | /* Load and set up Smarty */ |
| 29 | require_once(sprintf("%s/Smarty.class.php", $smartydir)); |
| 30 | $s = new smarty(); |
Solomon Peachy | 337902c | 2020-04-01 21:44:09 -0400 | [diff] [blame] | 31 | $s->setTemplateDir("templates"); |
| 32 | $s->setCompileDir("templates/compiled"); |
| 33 | $s->setCacheDir("templates/cache"); |
| 34 | // $s->caching = false; |
| 35 | // $s->debugging = false; |
| 36 | // $s->security = true; |
| 37 | // $s->security_settings['IF_FUNCS'] = array('array_key_exists', 'isset', 'is_array', 'count', 'file_exists'); |
| 38 | // $s->secure_dir = realpath($s->template_dir); |
| 39 | // $s->register_modifier('simple_timesince', array(&$this, 'simple_timesince')); |
| 40 | $s->registerPlugin("modifier","simple_timesince", array(&$this, "simple_timesince")); |
Jonas Häggqvist | 31ca70b | 2010-01-31 00:44:07 +0000 | [diff] [blame] | 41 | $this->s = $s; |
Jonas Häggqvist | 31ca70b | 2010-01-31 00:44:07 +0000 | [diff] [blame] | 42 | } |
Solomon Peachy | 337902c | 2020-04-01 21:44:09 -0400 | [diff] [blame] | 43 | |
Jonas Häggqvist | 31ca70b | 2010-01-31 00:44:07 +0000 | [diff] [blame] | 44 | public function simple_timesince($timestamp) { |
| 45 | $seconds = time() - $timestamp; |
| 46 | $one_hour = 60*60; |
| 47 | $one_day = 24*$one_hour; |
| 48 | if ($seconds > 60*28 && $seconds < 60*32) { |
| 49 | return "half an hour ago"; |
| 50 | } |
| 51 | elseif ($seconds < $one_hour) { |
| 52 | $minutes = floor($seconds/60); |
| 53 | return sprintf("%d minute%s ago", $minutes, qs($minutes)); |
| 54 | } |
| 55 | elseif ($seconds < $one_day) { |
| 56 | $hours = floor($seconds/$one_hour); |
| 57 | return sprintf("%d hour%s ago", $hours, qs($hours)); |
| 58 | } |
| 59 | elseif ($seconds < 2*$one_day) { |
| 60 | return "Yesterday"; |
| 61 | } |
| 62 | elseif ($seconds < 7*$one_day) { |
| 63 | $days = floor($seconds/$one_day); |
| 64 | return sprintf("%d day%s ago", $days, qs($days)); |
| 65 | } |
| 66 | elseif ($seconds < 2*30*$one_day) { |
| 67 | $weeks = floor($seconds/(7*$one_day)); |
| 68 | return sprintf("%d week%s ago", $weeks, qs($weeks)); |
| 69 | } |
| 70 | elseif ($seconds < 365*$one_day) { |
| 71 | $months = floor($seconds/(30*$one_day)); |
| 72 | return sprintf("%d month%s ago", $months, qs($months)); |
| 73 | } |
| 74 | else { |
| 75 | $years = floor($seconds/(365*$one_day)); |
| 76 | return sprintf("%d year%s ago", $years, qs($years)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | public function assign($name, $value) { |
| 82 | $this->s->assign($name, $value); |
| 83 | } |
| 84 | |
| 85 | public function render($pagename, $vars = array()) { |
| 86 | if (is_array($vars)) { |
| 87 | foreach($vars as $name => $value) { |
| 88 | $this->assign($name, $value); |
| 89 | } |
| 90 | } |
| 91 | $this->s->display($pagename); |
| 92 | } |
| 93 | } |
| 94 | ?> |