/[drupal]/contributions/modules/filebrowser_extensions/filebrowser_cells.php
ViewVC logotype

Contents of /contributions/modules/filebrowser_extensions/filebrowser_cells.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Mon Nov 20 13:32:03 2006 UTC (3 years ago) by dman
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +9 -5 lines
File MIME type: text/x-php
fixed function name conflict
http://drupal.org/project/comments/add/98410
1 <?php
2 // $Id: filebrowser_cells.php,v 1.5 2006/11/19 10:51:58 dman Exp $
3
4 /*
5 * Created on 15/09/2006
6 *
7 * All these filebrowser_get_cell_hook() functions return a small array suitable
8 * for using as a table TD - containing at minimum a 'data' field. Also possibly
9 * sort value column indicators.
10 */
11
12 /**
13 * Callback to render just one cell of data - the name link
14 * A filebrowser_get_cell_hook()
15 */
16 function filebrowser_get_cell_name($completepath, $file_info = NULL) {
17 if (!$completepath) {
18 return array (
19 'data' => t("Name"),
20 'field' => 'name',
21 'description' => t("A link to the short file name. Links to folders open new requests, so don't use this with the AJAX methods")
22 );
23 }
24 $label = $file_info['#title'] ? $file_info['#title'] : basename($completepath);
25 if (is_dir($completepath)) {
26 // for this link I need to prefix the filebrowser relink, BUT chop the filebrowser directory part
27 $href = filebrowser_url($file_info['#rel_path']);
28 } else {
29 $href = filebrowser_file_url($file_info['#rel_path']);
30 }
31
32 $link = l($label, $href);
33 return array (
34 'data' => $link,
35 'class' => 'filename',
36 'sv' => $file
37 );
38 }
39
40 /**
41 * Return the formatted size of this file.
42 * A filebrowser_get_cell_hook()
43 */
44 function filebrowser_get_cell_size($completepath, $file_info = NULL) {
45 if (!$completepath) {
46 return array (
47 'data' => t("Size"),
48 'field' => 'size',
49 'description' => t("The size of the file, formatted into human-readable Kb,Mb.")
50 );
51 }
52 if (is_dir($completepath)) {
53 return array ();
54 }
55 $size = filesize($completepath);
56 return array (
57 'data' => $size ? format_size($size) : '',
58 'class' => 'size',
59 'sv' => ($size ? $size : 0)
60 );
61 }
62
63 /**
64 * Return the formatted age of this file.
65 * A filebrowser_get_cell_hook()
66 */
67 function filebrowser_get_cell_age($completepath, $file_info = NULL) {
68 if (!$completepath) {
69 return array (
70 'data' => t("Age"),
71 'field' => 'age',
72 'description' => t("The age of the file since last modification. Human-readable.")
73 );
74 }
75 $age = time() - filemtime($completepath);
76 return array (
77 'class' => 'age',
78 'sv' => $age,
79 'data' => format_interval($age),
80 );
81 }
82 function filebrowser_get_cell_date($completepath) {
83 if (!$completepath) {
84 return array (
85 'data' => t("Modified"),
86 'field' => 'age',
87 'description' => t("The date of the last modification. Human-readable.")
88 );
89 }
90 return array (
91 'data' => format_date(filemtime($completepath)),
92 'class' => 'date',
93 'sv' => filemtime($completepath),
94 );
95 }
96
97 /**
98 * Return the deduced description or extended info for this file.
99 * A filebrowser_get_cell_hook()
100 */
101 function filebrowser_get_cell_info($completepath, $file_info = NULL) {
102 if (!$completepath) {
103 return array (
104 'data' => t("Info"),
105 'description' => t("metadata or description of this file as found in nearby info files")
106 );
107 }
108 static $info;
109
110 if (!isset ($info)) {
111 // The first time this cell is requested, look hard into the current directory
112 // cache results
113 $subfolder = (is_dir($completepath)) ? $completepath : dirname($completepath);
114 $infopath = $subfolder . '/descript.ion';
115 if (!file_exists($infopath)) {
116 $infopath = $subfolder . '/files.bbs';
117 }
118
119 if(is_file($infopath)){
120 $info = filebrowser_extensions_get_fileinfo($infopath, $subfolder);
121 } else {
122 $info = array ();
123 }
124 }
125
126 $file = basename($completepath);
127 // each info returned was an array. - not sure how to deal with that in one cell
128 $detail = (is_array($info[$file])) ? array_pop($info[$file]) : $info[$file];
129 return array (
130 'data' => $detail,
131 'class' => 'info'
132 );
133 }
134
135 /**
136 * Loads file metainformation from the specified file. Also
137 * allows the file to specify a *callback* with which the
138 * descriptions are parsed, so more metainformation can be
139 * presented on the output.
140 *
141 * TODO Actually use this. This func is brought over from
142 * filebrowser_extensions_get_fileinfo() for legacy reasons, but hasn't yet
143 * been used in practice
144 */
145 function filebrowser_extensions_get_fileinfo($infopath = NULL, $subfolder = '') {
146 static $metacols = array();
147 // Return (previously generated) meta column list
148 if (!isset ($infopath)) {
149 return $metacols;
150 }
151
152 // Build meta information list
153 $metainfo = array ();
154 if (is_readable($infopath) && ($file = file($infopath))) {
155 foreach ($file as $line) {
156 // Skip empty and commented lines
157 if (trim($line) == '' || strpos(trim($line), '#') === 0) {
158 continue;
159 }
160 list ($name, $description) = explode(" ", $line, 2);
161 if (isset ($metainfo[$name])) {
162 $metainfo[$name] .= trim($description) . " ";
163 } else {
164 $metainfo[$name] = trim($description) . " ";
165 }
166 }
167
168 $callback = FALSE;
169 if (isset ($metainfo['*callback*']) && function_exists(trim($metainfo['*callback*']))) {
170 $callback = trim($metainfo['*callback*']);
171 unset ($metainfo['*callback*']);
172 }
173 if (isset ($metainfo['.'])) {
174 filebrowser_help(NULL, $metainfo['.']);
175 unset ($metainfo['.']);
176 }
177
178 foreach ($metainfo as $name => $description) {
179 $metainfo[$name] = ($callback ? $callback (trim($description), $subfolder, $name) : array (
180 trim($description
181 )));
182 }
183 $metacols = ($callback ? $callback () : array (t('Description')));
184 }
185 return $metainfo;
186 }
187
188 /**
189 * @param $filepath
190 * @return a table cell
191 */
192 function filebrowser_get_cell_icon($completepath, $file_info = NULL) {
193 if (!$completepath) {
194 // return the column header
195 $cell = array (
196 'field' => 'type',
197 'description' => t("An icon representing the file type")
198 );
199 }
200
201 $parts = pathinfo($completepath);
202 $extension = $parts['extension'];
203
204 $cell = array (
205 'data' => filebrowser_get_icon($completepath),
206 'sv' => $extension
207 );
208
209 return $cell;
210 }
211
212 /**
213 * Returns the appropriate HTML code for an icon representing
214 * a file, based on the extension of the file. A specific icon
215 * can also be requested with the second parameter.
216 */
217 function filebrowser_get_icon($completepath = NULL, $iconname = NULL) {
218 if (isset ($completepath)) {
219 $parts = pathinfo($completepath);
220 $iconname = (is_dir($completepath) ? 'folder' : $parts['extension']);
221 }
222 elseif (!isset ($iconname)) {
223 $iconname = 'default';
224 }
225
226 $iconfiles = array (
227 variable_get('filebrowser_icons','') . "/file-$iconname.png", variable_get('filebrowser_icons', '') . "/file-default.png"
228 );
229 foreach ($iconfiles as $icon) {
230 if (file_exists($icon)) {
231 return theme("image", $icon, $iconname . " file", $iconname . " file");
232 }
233 }
234 return '';
235 }
236
237 /**
238 * Return a cell linked to the file.
239 * Like get_cell_name, but do NOT link directories or provide uplink.
240 * Clicking on a directory here will expand it, via js, not browse into it. This
241 * version includes the icon inline, for easier linking and layout.
242 *
243 * A filebrowser_get_cell_hook()
244 */
245 function filebrowser_get_cell_file($completepath, $file_info = NULL) {
246 if (!$completepath) {
247 return array (
248 'data' => t("Name"),
249 'field' => 'name',
250 'description' => t("The icon and file name together. Directories are not linked, but have classes embedded to make this useful for AJAX callbacks. Files are linked to launch in a new window.")
251 );
252 }
253
254 $file = basename($completepath);
255 $rel_path = $file_info['#rel_path'];
256
257 if ($file == "..") { //return; }
258 // construct uplink
259 $cell = array (
260 'data' => "<a href='" . filebrowser_expander_link(dirname(dirname($rel_path))) . "'></a> " . filebrowser_get_icon($completepath) . " up$file",
261 'class' => "fileBrowserUp filename",
262 'id' => filebrowser_id($rel_path . '_file'),
263 );
264 return $cell;
265 }
266
267 if (is_dir($completepath)) {
268 $cell = array (
269 'data' => "<a href='" . filebrowser_expander_link($rel_path) . "'></a> " . filebrowser_get_icon($completepath) . " $file",
270 'class' => "fileBrowserToggle filename",
271 'id' => filebrowser_id($rel_path . '_file'),
272 );
273 return $cell;
274 }
275 return array (
276 'data' => l(filebrowser_get_icon($rel_path) . ' ' . $file, filebrowser_file_url($rel_path), NULL, NULL, NULL, NULL, TRUE),
277 'class' => 'filename',
278 'sv' => $file
279 );
280 }
281
282 /**
283 * A sample filebrowser column callback.
284 *
285 * This trivial function is mainly here as a demo to copy if you want to develop
286 * your own cells.
287 *
288 * A filebrowser_get_cell_hook()
289 * @param $filepath
290 * @return a table cell
291 */
292 function filebrowser_get_cell_type($completepath, $file_info = NULL) {
293 if (!$completepath) {
294 // return the column header
295 $cell = array (
296 'data' => t("Type"),
297 'field' => 'type',
298 'description' => t("The file suffix. Sortable.")
299 );
300 }
301
302 // Return a cell displaying the suffix. Sortable.
303 $parts = pathinfo($completepath);
304 $extension = $parts['extension'];
305 if ($extension) {
306 $cell = array (
307 'data' => $extension,
308 'sv' => $extension
309 );
310 }
311
312 return $cell;
313 }
314
315 /**
316 * Add an ajax expando to the given row.
317 * A filebrowser_get_cell_hook()
318 */
319 function filebrowser_get_cell_expander($completepath, $file_info = NULL) {
320 // Add ajax functions to file browser (once only)
321 static $done_lib;
322 if (!$done_lib) {
323 drupal_add_js('misc/progress.js');
324 drupal_add_js(drupal_get_path('module', 'filebrowser_extensions') . '/filebrowser.js');
325 $done_lib = TRUE;
326 }
327 if (!$completepath) {
328 return array (
329 'description' => t("An ajax expando. Click to expand tree")
330 );
331 }
332
333 $rel_path = $file_info['#rel_path'];
334
335 if (basename($rel_path) == "..") {
336 $cell = array (
337 'data' => "<a href='" . filebrowser_expander_link(dirname(dirname($rel_path))) . "'></a><acronym class='expando'></acronym>",
338 'class' => "fileBrowserUp",
339 'id' => filebrowser_id($rel_path),
340 );
341 return $cell;
342 }
343
344 if (is_dir($completepath)) {
345 // Return a cell displaying the trigger.
346 $cell = array (
347 'data' => "<a href='" . filebrowser_expander_link($rel_path) . "'></a><acronym class='expando'>*</acronym>",
348 'class' => "expander fileBrowserExpand",
349 'id' => filebrowser_id($rel_path),
350 );
351 // the URL gets a randomizer on the end to prevent strange xmlhttp stalls that seem to be cache related
352 } else {
353 // need something to keep css from collapsing.
354 $cell = array (
355 'class' => "expander",
356 );
357 }
358
359 return $cell;
360 }
361
362 /**
363 * Returns a link that contains the details needed for th AJAX callback to work
364 * on a directory expander link.
365 */
366 function filebrowser_expander_link($rel_path) {
367 $profile = filebrowser_profile();
368 return url($profile['path'] . '/' . $rel_path, 'mode=raw');
369 }
370
371 /**
372 * Callback used by filebrowser to annotate the filebrowser lists with my extra
373 * action.
374 * A filebrowser_get_cell_hook()
375 * @param $filepath the full filesystem path
376 */
377 function filebrowser_get_cell_select_button($completepath, $file_info = NULL) {
378 if (!$completepath) {
379 // return the column header (blank)
380 return array (
381 'data' => 'Choose',
382 'description' => t("A button that fires a JS function selectPath(rel_path)."
383 ));
384 }
385
386 $cell = "<span
387 onclick=\"if(typeof(selectPath)=='function') selectPath('" . addslashes($file_info['#rel_path']) . "')\"
388 title=\"Select this file\" class=\"filecontrols\"
389 > select </span>";
390 return array (
391 'data' => $cell,
392 'class' => 'action'
393 );
394 }
395
396 /**
397 * Callback used by filebrowser to annotate the filebrowser lists
398 * A filebrowser_get_cell_hook()
399 * Although it looks like a cell, it doesn't return a 'data' value, so won't
400 * work out of context of a formtable. The 'form' element this cell contains
401 * must be rendered using formAPI
402 */
403 function filebrowser_get_cell_checkbox($completepath, $file_info = NULL) {
404 // Add ui functions to file browser (once only)
405 static $done_lib;
406 if (!$done_lib) {
407 drupal_add_js(drupal_get_path('module', 'filebrowser_extensions') . '/toggle_tree.js');
408 $done_lib = TRUE;
409 }
410
411 if (!$completepath) {
412 // return the column header (blank)
413 return array (
414 'description' => t("A checkbox returning an edit[filepath] parameter to the form this table is embedded in."
415 ));
416 }
417
418 $rel_path = $file_info['#rel_path'];
419
420 $cell = array (
421 $rel_path => array (
422 '#type' => 'checkbox',
423 )
424 );
425
426 // Using form API in this context was too hard
427 $cell = "\n<input type='checkbox' name='edit[filepath][]' value='$rel_path' id='checkbox_" . filebrowser_id($rel_path) . "' class='filebrowser_checkbox' />";
428 return array (
429 'data' => $cell,
430 'class' => 'checkbox'
431 );
432 }
433
434 function filebrowser_get_cell_filename($rel_path, $file_info) {
435 return $file_info['#filename'];
436 }

  ViewVC Help
Powered by ViewVC 1.1.2