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