Modification log added
[mediadatabase.git] / php / inc.functions.php
blob764261c11ce55c13878bff4f25365f84d9516370
1 <?php
2 function mb_get_extension ($filename) // {{{
4 return strtolower(substr(strrchr($filename,'.'),1));
5 } // }}}
6 function mb_chomp ($string) // {{{
8 return substr($string, 0, -1);
9 } // }}}
10 function mb_datetext ($timestamp) // {{{
12 global $config;
13 return date($config['datestring'],$timestamp);
14 } // }}}
15 function mb_sizetext ($sizeinbytes) // {{{
17 global $config;
18 // thresholds
19 $t_b = 1024; // use bytes when size < 1024
20 $t_kb = 1048576; // use kb when size < 1 MB
21 if (!$config['human_readable_size']) {
22 return "$sizeinbytes";
24 if ($sizeinbytes < $t_b) {
25 return "$sizeinbytes B";
26 } elseif ($sizeinbytes < $t_kb) {
27 $kb = floor($sizeinbytes / 1024);
28 return "$kb KB";
29 } else {
30 $mb = floor($sizeinbytes / 1048576);
31 return "$mb MB";
33 } // }}}
34 function mb_lengthtext ($seconds) // {{{
36 $minutes = floor($seconds/60);
37 $seconds = $seconds % 60;
38 if (strlen($seconds) == 1) { $seconds = '0'.$seconds; }
39 return "$minutes:$seconds";
40 } // }}}
41 function mb_typetext ($type) // {{{
43 switch ($type) {
44 case MB_T_AUDIO: return 'Audio CD';
45 case MB_T_DATA: return 'Data CD';
46 case MB_T_EMPTY: return 'CD';
47 default: return 'Unknown';
49 } // }}}
50 function mb_icon ($extension) // {{{
52 global $config;
53 $icon = $config['icon_prefix'];
54 // I'm planning to add icon-by-extension sometime :-)
55 switch ($extension) {
56 case '__folder': $icon .= 'folder.png'; break;
57 case '__unknown': $icon .= 'unknown.png'; break;
58 case '__category': $icon .= 'category.png'; break;
59 case '__media_cd': $icon .= 'cd.png'; break;
60 case '__media_acd': $icon .= 'acd.png'; break;
62 return "<img alt=\"icon\" src=\"$icon\">&nbsp;";
63 } // }}}
64 function mb_iconbytype ($type) // {{{
66 switch ($type) {
67 case MB_T_DATA: return mb_icon('__media_cd');
68 case MB_T_AUDIO: return mb_icon('__media_acd');
69 case MB_T_EMPTY: return mb_icon('__media_cd');
70 default: return mb_icon('__unknown');
72 } // }}}
73 function mb_getparentdir ($dir) // {{{
75 // I'm sure there's a better way to do this,
76 // I was just lazy. Sorry about that :)
77 if ($dir == '/') { return false; }
78 $parts = explode('/',$dir);
79 array_pop($parts);
80 array_pop($parts);
81 $dir = implode('/',$parts) . '/';
82 return $dir;
83 } // }}}
84 function mb_table_start () // {{{
86 global $TABLE_COLUMNS;
87 if (!(isset($TABLE_COLUMNS) && is_array($TABLE_COLUMNS))) {
88 $TABLE_COLUMNS = array();
90 $c = func_num_args();
91 array_unshift($TABLE_COLUMNS,$c);
92 echo "<table>\n";
93 echo " <tr>\n";
94 for ($x=0;$x<$c;$x++) {
95 echo " <th>".func_get_arg($x)."</th>\n";
97 echo " </tr>\n";
98 } // }}}
99 function mb_table_widecol ($content) // {{{
101 global $TABLE_COLUMNS;
102 global $TABLE_ROW;
103 global $TABLE_CSSCLASS;
105 $cols = $TABLE_COLUMNS[0];
107 if (!isset($TABLE_ROW))
109 $TABLE_ROW = 1;
112 if (($TABLE_ROW % 2) != 0)
114 $TABLE_CSSCLASS="OddTableRow";
116 else
118 $TABLE_CSSCLASS="EvenTableRow";
121 $TABLE_ROW++;
123 echo " <tr>\n";
124 echo " <td colspan=\"$cols\" class=\"".$TABLE_CSSCLASS."\">$content</td>\n";
125 echo " </tr>\n";
126 } // }}}
127 function mb_table_row () // {{{
129 echo " <tr>\n";
130 $c = func_num_args();
131 echo " <tr>\n";
132 for ($x=0;$x<$c;$x++) {
133 echo " <td>".func_get_arg($x)."</td>\n";
135 echo " </tr>\n";
136 } // }}}
137 function mb_table_col ($content)
139 global $TABLE_COLUMNS;
140 global $TABLE_COLUMN;
141 global $TABLE_ROW;
142 global $TABLE_CSSCLASS;
143 $cols = $TABLE_COLUMNS[0];
145 if (!isset($TABLE_COLUMN))
147 $TABLE_COLUMN = 1;
150 if (!isset($TABLE_ROW))
152 $TABLE_ROW = 1;
155 $curcol = $TABLE_COLUMN++;
157 if ($curcol == 1)
159 if (($TABLE_ROW % 2) != 0)
161 $TABLE_CSSCLASS="OddTableRow";
163 else
165 $TABLE_CSSCLASS="EvenTableRow";
168 $TABLE_ROW++;
169 echo "\t<tr>\n";
172 echo " <td class=\"".$TABLE_CSSCLASS."\"><nobr>$content</nobr></td>\n";
173 if ($curcol == $cols) { echo "\t</tr>\n"; $TABLE_COLUMN = 1; }
174 return;
175 } // }}}
176 function mb_table_end () // {{{
178 global $TABLE_COLUMNS;
179 array_shift($TABLE_COLUMNS);
180 echo "</table>\n\n";
181 } // }}}
182 function mb_fetch_select_categories ($indent=0) // {{{
184 global $config;
185 $indent = str_repeat("\t",$indent);
186 if (!$res = mysql_query("SELECT * FROM ".$config['tbl_categories']." ORDER BY name")) {
187 return false;
189 if (!mysql_num_rows($res)) {
190 return true;
192 while ($row = mysql_fetch_assoc($res)) {
193 echo $indent.'<option value="'.$row['catid'].'">'.$row['name']."</option>\n";
195 return true;
196 } // }}}
198 function truncate_ellipsis ($string, $max)
200 if (strlen($string) > $max + 3)
201 return substr_replace($string, "...", $max - 3);
202 else
203 return $string;