Issue #1090216: Add MIME type icon compatibility for kml/kmz files.
[project/filefield.git] / filefield_formatter.inc
CommitLineData
9412a7cc 1<?php
9412a7cc
JP
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 */
bc63d6a9 15function theme_filefield_formatter_default($element) {
9412a7cc 16 $file = $element['#item'];
57d2397f 17 $field = content_fields($element['#field_name']);
c4c7e990 18 $output = theme('filefield_item', $file, $field);
57d2397f 19 return $output;
9412a7cc
JP
20}
21
22/**
3dd27e3f
NH
23 * Theme function for the 'path_plain' formatter.
24 */
25function 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 }
ac4e6d49 41 return empty($item['filepath']) ? '' : check_plain(file_create_path($item['filepath']));
3dd27e3f
NH
42}
43
44/**
45 * Theme function for the 'url_plain' formatter.
46 */
47function 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
ac4e6d49
NH
64 if (empty($item['filepath'])) {
65 return '';
66 }
67
242696dc 68 return file_create_url(field_file_urlencode_path($item['filepath']));
3dd27e3f
NH
69}
70
71/**
87d3e491
NH
72 * Theme function for any file that is managed by FileField.
73 *
9412a7cc
JP
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
a99f3c6d 79 * in any case, please use theme('filefield_file') instead.
9412a7cc 80 */
c4c7e990 81function theme_filefield_item($file, $field) {
7d1bd72d 82 if (filefield_view_access($field['field_name']) && filefield_file_listed($file, $field)) {
c4c7e990 83 return theme('filefield_file', $file);
9412a7cc 84 }
c4c7e990 85 return '';
9412a7cc
JP
86}
87
9412a7cc 88/**
35873ed9 89 * Return whether a file should be listed when viewing the node.
28b1c58e 90 *
35873ed9
NH
91 * @param $file
92 * A populated FileField item.
93 * @param $field
94 * A CCK field instance array.
28b1c58e
DP
95 */
96function filefield_file_listed($file, $field) {
4a03547e 97 if (!empty($field['list_field'])) {
bc70cf03 98 return !empty($file['list']);
35873ed9
NH
99 }
100 return TRUE;
28b1c58e
DP
101}
102
28b1c58e 103/**
9412a7cc
JP
104 * Theme function for the 'generic' single file formatter.
105 */
c147cceb 106function theme_filefield_file($file) {
92f90838
NH
107 // Views may call this function with a NULL value, return an empty string.
108 if (empty($file['fid'])) {
109 return '';
110 }
111
c147cceb 112 $path = $file['filepath'];
9412a7cc 113 $url = file_create_url($path);
92f90838 114 $icon = theme('filefield_icon', $file);
96f6f7a9
NH
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(
111f8ec7 122 'type' => $file['filemime'] . '; length=' . $file['filesize'],
96f6f7a9
NH
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
51d42c67 135 return '<div class="filefield-file">'. $icon . l($link_text, $url, $options) .'</div>';
9412a7cc 136}