continuing along...
[project/filefield.git] / filefield_formatter.inc
CommitLineData
9412a7cc
JP
1<?php
2// $Id$
3/**
4 * @file
5 * FileField: Defines a CCK file field type.
6 *
7 * Uses content.module to store the fid and field specific metadata,
8 * and Drupal's {files} table to store the actual file data.
9 *
10 * This file contains CCK formatter related functionality.
11 */
12
13/**
14 * Theme function for the 'default' filefield formatter.
15 */
c147cceb 16function theme_filefield_formatter_filefield_default($element) {
9412a7cc 17 $file = $element['#item'];
c147cceb 18 return 'filefiedl' . theme('filefield', $file);
9412a7cc
JP
19}
20
21/**
22 * Theme function for any file that is managed by filefield.
23 * It doesn't really format stuff by itself but rather redirects to other
24 * formatters that are telling us they want to handle the concerned file.
25 *
26 * This function checks if the file may be shown and returns an empty string
27 * if viewing the file is not allowed for any reason. If you need to display it
28 * in any case, please use theme('filefield') instead.
29 */
30function theme_filefield($file, $field) {
31 if (!filefield_view_access($field['field_name'])) {
32 return '';
33 }
34 if ($field['force_list']) {
35 $file['list'] = 1; // always show the files if that option is enabled
36 }
37 if (empty($file['list'])) {
38 return '';
39 }
40 return theme('filefield_unguarded', $file, $field);
41}
42
43/**
44 * Theme function for any file that is managed by filefield.
45 * It doesn't really format stuff by itself but rather redirects to other
46 * formatters that are telling us they want to handle the concerned file.
47 *
48 * This function does not check if the file may be shown, it returns markup
49 * in any case (except if the file doesn't exist at all). If you need to check
50 * permissions, please use theme('filefield_guarded') instead.
51 */
52function theme_filefield_unguarded($file, $field) {
53 if (empty($file['fid']) || !is_file($file['filepath'])) {
54 return '';
55 }
56 drupal_add_css(drupal_get_path('module', 'filefield') .'/filefield.css');
57
58 $file_formatter_info = filefield_formatter_for_file($file, $field);
59 if (empty($file_formatter_info)) {
60 return '<div class="filefield-item filefield-item-empty"/>';
61 }
62
63 foreach ($file_formatter_info['css'] as $css_path) {
64 drupal_add_css($css_path);
65 }
66 $settings = isset($field['file_formatters'][$file_formatter_info['key']])
67 ? $field['file_formatters'][$file_formatter_info['key']]
68 : NULL;
69
70 return '<div class="filefield-item">'.
71 theme($file_formatter_info['theme'], (object)$file, $field, $settings)
72 .'</div>';
73}
74
75
76/**
77 * Theme function for the 'generic' single file formatter.
78 */
c147cceb
DP
79function theme_filefield_file($file) {
80 $path = $file['filepath'];
9412a7cc
JP
81 $url = file_create_url($path);
82 $icon = theme('filefield_icon', $file);
c147cceb
DP
83 $desc = $file['description'];
84 return '<div class="filefield-file">'. $icon . l($desc, $url) .'</div>';
9412a7cc 85}