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