Issue #1090216: Add MIME type icon compatibility for kml/kmz files.
[project/filefield.git] / filefield_formatter.inc
1 <?php
2 /**
3 * @file
4 * FileField: Defines a CCK file field type.
5 *
6 * Uses content.module to store the fid and field specific metadata,
7 * and Drupal's {files} table to store the actual file data.
8 *
9 * This file contains CCK formatter related functionality.
10 */
11
12 /**
13 * Theme function for the 'default' filefield formatter.
14 */
15 function theme_filefield_formatter_default($element) {
16 $file = $element['#item'];
17 $field = content_fields($element['#field_name']);
18 $output = theme('filefield_item', $file, $field);
19 return $output;
20 }
21
22 /**
23 * Theme function for the 'path_plain' formatter.
24 */
25 function theme_filefield_formatter_path_plain($element) {
26 // Inside a View this function may be called with null data. In that case,
27 // just return.
28 if (empty($element['#item'])) {
29 return '';
30 }
31
32 $field = content_fields($element['#field_name']);
33 $item = $element['#item'];
34 // If there is no image on the database, use default.
35 if (empty($item['fid']) && $field['use_default_file']) {
36 $item = $field['default_file'];
37 }
38 if (empty($item['filepath']) && !empty($item['fid'])) {
39 $item = array_merge($item, field_file_load($item['fid']));
40 }
41 return empty($item['filepath']) ? '' : check_plain(file_create_path($item['filepath']));
42 }
43
44 /**
45 * Theme function for the 'url_plain' formatter.
46 */
47 function theme_filefield_formatter_url_plain($element) {
48 // Inside a View this function may be called with null data. In that case,
49 // just return.
50 if (empty($element['#item'])) {
51 return '';
52 }
53
54 $field = content_fields($element['#field_name']);
55 $item = $element['#item'];
56 // If there is no image on the database, use default.
57 if (empty($item['fid']) && $field['use_default_file']) {
58 $item = $field['default_file'];
59 }
60 if (empty($item['filepath']) && !empty($item['fid'])) {
61 $item = array_merge($item, field_file_load($item['fid']));
62 }
63
64 if (empty($item['filepath'])) {
65 return '';
66 }
67
68 return file_create_url(field_file_urlencode_path($item['filepath']));
69 }
70
71 /**
72 * Theme function for any file that is managed by FileField.
73 *
74 * It doesn't really format stuff by itself but rather redirects to other
75 * formatters that are telling us they want to handle the concerned file.
76 *
77 * This function checks if the file may be shown and returns an empty string
78 * if viewing the file is not allowed for any reason. If you need to display it
79 * in any case, please use theme('filefield_file') instead.
80 */
81 function theme_filefield_item($file, $field) {
82 if (filefield_view_access($field['field_name']) && filefield_file_listed($file, $field)) {
83 return theme('filefield_file', $file);
84 }
85 return '';
86 }
87
88 /**
89 * Return whether a file should be listed when viewing the node.
90 *
91 * @param $file
92 * A populated FileField item.
93 * @param $field
94 * A CCK field instance array.
95 */
96 function filefield_file_listed($file, $field) {
97 if (!empty($field['list_field'])) {
98 return !empty($file['list']);
99 }
100 return TRUE;
101 }
102
103 /**
104 * Theme function for the 'generic' single file formatter.
105 */
106 function theme_filefield_file($file) {
107 // Views may call this function with a NULL value, return an empty string.
108 if (empty($file['fid'])) {
109 return '';
110 }
111
112 $path = $file['filepath'];
113 $url = file_create_url($path);
114 $icon = theme('filefield_icon', $file);
115
116 // Set options as per anchor format described at
117 // http://microformats.org/wiki/file-format-examples
118 // TODO: Possibly move to until I move to the more complex format described
119 // at http://darrelopry.com/story/microformats-and-media-rfc-if-you-js-or-css
120 $options = array(
121 'attributes' => array(
122 'type' => $file['filemime'] . '; length=' . $file['filesize'],
123 ),
124 );
125
126 // Use the description as the link text if available.
127 if (empty($file['data']['description'])) {
128 $link_text = $file['filename'];
129 }
130 else {
131 $link_text = $file['data']['description'];
132 $options['attributes']['title'] = $file['filename'];
133 }
134
135 return '<div class="filefield-file">'. $icon . l($link_text, $url, $options) .'</div>';
136 }