d6f9b640acd129aee8c2e0ad6e79f6be1b9b77f9
[phpgitweb.git] / htdocs / lib / Tools.class.php
1 <?php
2 /* Tools.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 class Tools {
20         
21         public static function age_calculate($last_change) {
22                 $result = array();
23                 $now = time();
24                 $age = ($last_change > 0 ? ($now - $last_change) : 0);
25                 
26                 if ($age > 60*60*24*365*2) {
27                         $age_str = floor($age/60/60/24/365);
28                         $age_str .= " years ago";
29                         $max_cache = (60*60*24*365) - ($age % (60*60*24*365));
30                 } else if ($age > 60*60*24*(365/12)*2) {
31                         $age_str = floor($age/60/60/24/(365/12));
32                         $age_str .= " months ago";
33                         $max_cache = min((60*60*24*(365/12)) - ($age % (60*60*24*(365/12))), (60*60*24*365*2) - $age);
34                 } else if ($age > 60*60*24*7*2) {
35                         $age_str = floor($age/60/60/24/7);
36                         $age_str .= " weeks ago";
37                         $max_cache = min((60*60*24*7) - ($age % (60*60*24*7)), (60*60*24*(365/12)*2) - $age);
38                 } else if ($age > 60*60*24*2) {
39                         $age_str = floor($age/60/60/24);
40                         $age_str .= " days ago";
41                         $max_cache = min((60*60*24) - ($age % (60*60*24)), (60*60*24*7*2) - $age);
42                 } else if ($age > 60*60*2) {
43                         $age_str = floor($age/60/60);
44                         $age_str .= " hours ago";
45                         $max_cache = min((60*60) - ($age % (60*60)), (60*60*24*2) - $age);
46                 } else if ($age > 60*2) {
47                         $age_str = floor($age/60);
48                         $age_str .= " min ago";
49                         $max_cache = min(60 - ($age % 60), (60*60*2) - $age);
50                 } else if ($age > 2) {
51                         $age_str = $age;
52                         $age_str .= " sec ago";
53                         $max_cache = 1;
54                 } else if ($age >= 0) {
55                         $age_str = "right now";
56                         $max_cache = 1;
57                 } else {
58                         $max_cache = -1;
59                 }
60                 if($age == 0) {
61                         $age_class = "noage";
62                 } else if ($age < 60*60*2) {
63                         $age_class = "age0";
64                 } else if ($age < 60*60*24*2) {
65                         $age_class = "age1";
66                 } else {
67                         $age_class = "age2";
68                 }
69                 
70                 return array("age_str" => $age_str, "age_class" => $age_class, "max_cache" => $max_cache);
71         }
72         
73         public static function chop_text($text, $len, $add_len) {
74                 if(strlen($text) <= $len + $add_len)
75                         return $text;
76                 $ctext = substr($text, 0, $len);
77                 for($i = 0; $i < $add_len; $i++) {
78                         if($text[$len+$i] == ' ') 
79                                 break;
80                         $ctext .= $text[$len+$i];
81                 }
82                 $ctext .= "...";
83                 return $ctext;
84         }
85         
86 }
87
88 ?>