]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/external_packages/smarty/plugins/modifier.fsize_format.php
commit changes
[bacula/bacula] / gui / bacula-web / external_packages / smarty / plugins / modifier.fsize_format.php
1 <?php
2 /*
3 * Smarty plugin
4 * -------------------------------------------------------------
5 * Type:    modifier
6 * Name:    fsize_format
7 * Version:    0.2
8 * Date:    2003-05-15
9 * Author:    Joscha Feth, joscha@feth.com
10 * Purpose: formats a filesize (in bytes) to human-readable format
11 * Usage:    In the template, use
12             {$filesize|fsize_format}    =>    123.45 B|KB|MB|GB|TB
13             or
14             {$filesize|fsize_format:"MB"}    =>    123.45 MB            
15             or
16             {$filesize|fsize_format:"TB":4}    =>    0.0012 TB
17 * Params:
18         int        size            the filesize in bytes
19         string    format            the format, the output shall be: B, KB, MB, GB or TB
20         int        precision        the rounding precision
21         string    dec_point        the decimal separator
22         string    thousands_sep    the thousands separator
23 * Install: Drop into the plugin directory
24 * Version:
25 *            2003-05-15    Version 0.2    - added dec_point and thousands_sep thanks to Thomas Brandl, tbrandl@barff.de
26 *                                    - made format always uppercase
27 *                                    - count sizes "on-the-fly"                                    
28 *            2003-02-21    Version 0.1    - initial release
29 * -------------------------------------------------------------
30 */
31 function smarty_modifier_fsize_format($size,$format = '',$precision = 2, $dec_point = ".", $thousands_sep = ",")
32 {
33
34     $format = strtoupper($format);            
35
36     static $sizes = array();    
37     
38     if(!count($sizes)) {    
39         $b = 1024;    
40         $sizes["B"]        =    1;
41         $sizes["KB"]    =    $sizes["B"]  * $b;
42         $sizes["MB"]    =    $sizes["KB"] * $b;
43         $sizes["GB"]    =    $sizes["MB"] * $b;
44         $sizes["TB"]    =    $sizes["GB"] * $b;
45
46         $sizes = array_reverse($sizes,true);
47     }
48
49     //~ get "human" filesize
50     foreach($sizes    AS    $unit => $bytes) {
51         if($size > $bytes || $unit == $format) {
52             //~ return formatted size
53             return    number_format($size / $bytes,$precision,$dec_point,$thousands_sep)." ".$unit;
54         } //~ end if
55     } //~ end foreach
56 } //~ end function
57 ?>
58