continued :)
[phpgitweb.git] / htdocs / lib / Tools.class.php
diff --git a/htdocs/lib/Tools.class.php b/htdocs/lib/Tools.class.php
new file mode 100644 (file)
index 0000000..8ae0717
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+/* Tools.class.php - phpgitweb
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * 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 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+class Tools {
+       
+       public static function age_calculate($last_change) {
+               $result = array();
+               $now = time();
+               $age = ($last_change > 0 ? ($now - $last_change) : 0);
+               
+               if ($age > 60*60*24*365*2) {
+                       $age_str = floor($age/60/60/24/365);
+                       $age_str .= " years ago";
+               } else if ($age > 60*60*24*(365/12)*2) {
+                       $age_str = floor($age/60/60/24/(365/12));
+                       $age_str .= " months ago";
+                       $max_cache = min((60*60*24*(365/12)) - ($age % (60*60*24*(365/12))), (60*60*24*365*2) - $age);
+               } else if ($age > 60*60*24*7*2) {
+                       $age_str = floor($age/60/60/24/7);
+                       $age_str .= " weeks ago";
+                       $max_cache = min((60*60*24*7) - ($age % (60*60*24*7)), (60*60*24*(365/12)*2) - $age);
+               } else if ($age > 60*60*24*2) {
+                       $age_str = floor($age/60/60/24);
+                       $age_str .= " days ago";
+                       $max_cache = min((60*60*24) - ($age % (60*60*24)), (60*60*24*7*2) - $age);
+               } else if ($age > 60*60*2) {
+                       $age_str = floor($age/60/60);
+                       $age_str .= " hours ago";
+                       $max_cache = min((60*60) - ($age % (60*60)), (60*60*24*2) - $age);
+               } else if ($age > 60*2) {
+                       $age_str = floor($age/60);
+                       $age_str .= " min ago";
+                       $max_cache = min(60 - ($age % 60), (60*60*2) - $age);
+               } else if ($age > 2) {
+                       $age_str = $age;
+                       $age_str .= " sec ago";
+                       $max_cache = 1;
+               } else if ($age >= 0) {
+                       $age_str = "right now";
+                       $max_cache = 1;
+               } else {
+                       $max_cache = -1;
+               }
+               if($age == 0) {
+                       $age_class = "noage";
+               } else if ($age < 60*60*2) {
+                       $age_class = "age0";
+               } else if ($age < 60*60*24*2) {
+                       $age_class = "age1";
+               } else {
+                       $age_class = "age2";
+               }
+               
+               return array("age_str" => $age_str, "age_class" => $age_class, "max_cache" => $max_cache);
+       }
+       
+       public static function chop_text($text, $len, $add_len) {
+               if(strlen($text) <= $len + $add_len)
+                       return $text;
+               $ctext = substr($text, 0, $len);
+               for($i = 0; $i < $add_len; $i++) {
+                       if($text[$len+$i] == ' ') 
+                               break;
+                       $ctext .= $text[$len+$i];
+               }
+               $ctext .= "...";
+               return $ctext;
+       }
+       
+}
+
+?>
\ No newline at end of file