Clarifying PHPdoc for theme_filefield_widget_file().
[project/filefield.git] / filefield.theme.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 common theme functions.
11 */
12
13/**
9412a7cc 14 * Return an image with an appropriate icon for the given file.
512e7193
NH
15 *
16 * @param $file
17 * A file object for which to make an icon.
9412a7cc
JP
18 */
19function theme_filefield_icon($file) {
20 if (is_object($file)) {
21 $file = (array) $file;
22 }
2354a202
DP
23 $mime = check_plain($file['filemime']);
24
25 $dashed_mime = strtr($mime, array('/' => '-'));
9412a7cc 26
512e7193 27 if ($icon_url = filefield_icon_url($file)) {
2354a202 28 $icon = '<img class="field-icon-'. $dashed_mime .'" alt="'. $mime .' icon" src="'. $icon_url .'" />';
9412a7cc
JP
29 }
30 return '<div class="filefield-icon field-icon-'. $dashed_mime .'">'. $icon .'</div>';
31}
32
512e7193
NH
33/**
34 * Given a file object, create a URL to a matching icon.
35 *
36 * @param $file
37 * A file object.
38 * @param $theme
39 * Optional. The theme to be used for the icon. Defaults to the value of
40 * the "filefield_icon_theme" variable.
41 * @return
42 * A URL string to the icon, or FALSE if an appropriate icon could not be
43 * found.
44 */
2c8026f2 45function _filefield_icon_url($file, $theme = NULL) {
9412a7cc 46 global $base_url;
9412a7cc 47
2c8026f2 48 if ($iconpath = _filefield_icon_path($file, $theme)) {
9412a7cc
JP
49 return $base_url .'/'. $iconpath;
50 }
51 return FALSE;
52}
53
512e7193
NH
54/**
55 * Given a file object, create a URL to a matching icon.
56 *
57 * @param $file
58 * A file object.
59 * @param $theme
60 * Optional. The theme to be used for the icon. Defaults to the value of
61 * the "filefield_icon_theme" variable.
62 * @return
63 * A string to the icon as a local path, or FALSE if an appropriate icon could
64 * not be found.
65 */
2c8026f2 66function _filefield_icon_path($file, $theme = NULL) {
512e7193
NH
67 if (!isset($theme)) {
68 $theme = variable_get('filefield_icon_theme', 'protocons');
69 }
70
9412a7cc
JP
71 // If there's an icon matching the exact mimetype, go for it.
72 $dashed_mime = strtr($file['filemime'], array('/' => '-'));
73 if ($iconpath = _filefield_create_icon_path($dashed_mime, $theme)) {
74 return $iconpath;
75 }
76 // For a couple of mimetypes, we can "manually" tell a generic icon.
77 if ($generic_name = _filefield_generic_icon_map($file)) {
78 if ($iconpath = _filefield_create_icon_path($generic_name, $theme)) {
79 return $iconpath;
80 }
81 }
82 // Use generic icons for each category that provides such icons.
83 foreach (array('audio', 'image', 'text', 'video') as $category) {
84 if (strpos($file['filemime'], $category .'/') === 0) {
85 if ($iconpath = _filefield_create_icon_path($category .'-x-generic', $theme)) {
86 return $iconpath;
87 }
88 }
89 }
90 // Try application-octet-stream as last fallback.
91 if ($iconpath = _filefield_create_icon_path('application-octet-stream', $theme)) {
92 return $iconpath;
93 }
94 // Sorry, no icon can be found...
95 return FALSE;
96}
97
98function _filefield_create_icon_path($iconname, $theme = 'protocons') {
99 $iconpath = drupal_get_path('module', 'filefield')
100 .'/icons/'. $theme .'/16x16/mimetypes/'. $iconname .'.png';
101 if (file_exists($iconpath)) {
102 return $iconpath;
103 }
104 return FALSE;
105}
106
107function _filefield_generic_icon_map($file) {
108 switch ($file['filemime']) {
109 // Word document types.
110 case 'application/msword':
111 case 'application/vnd.ms-word.document.macroEnabled.12':
112 case 'application/vnd.oasis.opendocument.text':
113 case 'application/vnd.oasis.opendocument.text-template':
114 case 'application/vnd.oasis.opendocument.text-master':
115 case 'application/vnd.oasis.opendocument.text-web':
116 case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
117 case 'application/vnd.stardivision.writer':
118 case 'application/vnd.sun.xml.writer':
119 case 'application/vnd.sun.xml.writer.template':
120 case 'application/vnd.sun.xml.writer.global':
121 case 'application/vnd.wordperfect':
122 case 'application/x-abiword':
123 case 'application/x-applix-word':
124 case 'application/x-kword':
125 case 'application/x-kword-crypt':
126 return 'x-office-document';
127
128 // Spreadsheet document types.
129 case 'application/vnd.ms-excel':
130 case 'application/vnd.ms-excel.sheet.macroEnabled.12':
131 case 'application/vnd.oasis.opendocument.spreadsheet':
132 case 'application/vnd.oasis.opendocument.spreadsheet-template':
133 case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
134 case 'application/vnd.stardivision.calc':
135 case 'application/vnd.sun.xml.calc':
136 case 'application/vnd.sun.xml.calc.template':
137 case 'application/vnd.lotus-1-2-3':
138 case 'application/x-applix-spreadsheet':
139 case 'application/x-gnumeric':
140 case 'application/x-kspread':
141 case 'application/x-kspread-crypt':
142 return 'x-office-spreadsheet';
143
144 // Presentation document types.
145 case 'application/vnd.ms-powerpoint':
146 case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
147 case 'application/vnd.oasis.opendocument.presentation':
148 case 'application/vnd.oasis.opendocument.presentation-template':
149 case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
150 case 'application/vnd.stardivision.impress':
151 case 'application/vnd.sun.xml.impress':
152 case 'application/vnd.sun.xml.impress.template':
153 case 'application/x-kpresenter':
154 return 'x-office-presentation';
155
156 // Compressed archive types.
157 case 'application/zip':
158 case 'application/x-zip':
159 case 'application/stuffit':
160 case 'application/x-stuffit':
161 case 'application/x-7z-compressed':
162 case 'application/x-ace':
163 case 'application/x-arj':
164 case 'application/x-bzip':
165 case 'application/x-bzip-compressed-tar':
166 case 'application/x-compress':
167 case 'application/x-compressed-tar':
168 case 'application/x-cpio-compressed':
169 case 'application/x-deb':
170 case 'application/x-gzip':
171 case 'application/x-java-archive':
172 case 'application/x-lha':
173 case 'application/x-lhz':
174 case 'application/x-lzop':
175 case 'application/x-rar':
176 case 'application/x-rpm':
177 case 'application/x-tzo':
178 case 'application/x-tar':
179 case 'application/x-tarz':
180 case 'application/x-tgz':
181 return 'package-x-generic';
182
183 // Script file types.
184 case 'application/ecmascript':
185 case 'application/javascript':
186 case 'application/mathematica':
187 case 'application/vnd.mozilla.xul+xml':
188 case 'application/x-asp':
189 case 'application/x-awk':
190 case 'application/x-cgi':
191 case 'application/x-csh':
192 case 'application/x-m4':
193 case 'application/x-perl':
194 case 'application/x-php':
195 case 'application/x-ruby':
196 case 'application/x-shellscript':
197 case 'text/vnd.wap.wmlscript':
198 case 'text/x-emacs-lisp':
199 case 'text/x-haskell':
200 case 'text/x-literate-haskell':
201 case 'text/x-lua':
202 case 'text/x-makefile':
203 case 'text/x-matlab':
204 case 'text/x-python':
205 case 'text/x-sql':
206 case 'text/x-tcl':
207 return 'text-x-script';
208
209 // HTML aliases.
210 case 'application/xhtml+xml':
211 return 'text-html';
212
213 // Executable types.
214 case 'application/x-macbinary':
215 case 'application/x-ms-dos-executable':
216 case 'application/x-pef-executable':
217 return 'application-x-executable';
218
219 default:
220 return FALSE;
221 }
222}