1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Theme functions used for normal file output. |
---|
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 | |
---|
11 | /** |
---|
12 | * Return an image with an appropriate icon for the given file. |
---|
13 | * |
---|
14 | * @param $file |
---|
15 | * A file object for which to make an icon. |
---|
16 | */ |
---|
17 | function theme_filefield_icon($file) { |
---|
18 | if (is_object($file)) { |
---|
19 | $file = (array) $file; |
---|
20 | } |
---|
21 | $mime = check_plain($file['filemime']); |
---|
22 | |
---|
23 | $dashed_mime = strtr($mime, array('/' => '-', '+' => '-', '.' => '-')); |
---|
24 | |
---|
25 | if ($icon_url = filefield_icon_url($file)) { |
---|
26 | return '<img class="filefield-icon field-icon-'. $dashed_mime .'" alt="'. t('@mime icon', array('@mime' => $mime)) .'" src="'. $icon_url .'" />'; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Given a file object, create a URL to a matching icon. |
---|
32 | * |
---|
33 | * @param $file |
---|
34 | * A file object. |
---|
35 | * @param $theme |
---|
36 | * Optional. The theme to be used for the icon. Defaults to the value of |
---|
37 | * the "filefield_icon_theme" variable. |
---|
38 | * @return |
---|
39 | * A URL string to the icon, or FALSE if an appropriate icon could not be |
---|
40 | * found. |
---|
41 | */ |
---|
42 | function _filefield_icon_url($file, $theme = NULL) { |
---|
43 | global $base_url; |
---|
44 | |
---|
45 | if ($icon_path = _filefield_icon_path($file, $theme)) { |
---|
46 | return $base_url .'/'. $icon_path; |
---|
47 | } |
---|
48 | return FALSE; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Given a file object, create a URL to a matching icon. |
---|
53 | * |
---|
54 | * @param $file |
---|
55 | * A file object. |
---|
56 | * @param $theme |
---|
57 | * Optional. The theme to be used for the icon. Defaults to the value of |
---|
58 | * the "filefield_icon_theme" variable. |
---|
59 | * @return |
---|
60 | * A string to the icon as a local path, or FALSE if an appropriate icon could |
---|
61 | * not be found. |
---|
62 | */ |
---|
63 | function _filefield_icon_path($file, $theme = NULL) { |
---|
64 | if (!isset($theme)) { |
---|
65 | $theme = variable_get('filefield_icon_theme', 'default'); |
---|
66 | } |
---|
67 | |
---|
68 | // If there's an icon matching the exact mimetype, go for it. |
---|
69 | $dashed_mime = strtr($file['filemime'], array('/' => '-')); |
---|
70 | if ($icon_path = _filefield_create_icon_path($dashed_mime, $theme)) { |
---|
71 | return $icon_path; |
---|
72 | } |
---|
73 | // For a couple of mimetypes, we can "manually" tell a generic icon. |
---|
74 | if ($generic_name = _filefield_generic_icon_map($file)) { |
---|
75 | if ($icon_path = _filefield_create_icon_path($generic_name, $theme)) { |
---|
76 | return $icon_path; |
---|
77 | } |
---|
78 | } |
---|
79 | // Use generic icons for each category that provides such icons. |
---|
80 | foreach (array('audio', 'image', 'text', 'video') as $category) { |
---|
81 | if (strpos($file['filemime'], $category .'/') === 0) { |
---|
82 | if ($icon_path = _filefield_create_icon_path($category .'-x-generic', $theme)) { |
---|
83 | return $icon_path; |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | // Try application-octet-stream as last fallback. |
---|
88 | if ($icon_path = _filefield_create_icon_path('application-octet-stream', $theme)) { |
---|
89 | return $icon_path; |
---|
90 | } |
---|
91 | // Sorry, no icon can be found... |
---|
92 | return FALSE; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Internal function to convert a file icon theme name to a directory. |
---|
97 | */ |
---|
98 | function _filefield_icon_directory($theme = NULL) { |
---|
99 | static $sets; |
---|
100 | |
---|
101 | if (!isset($sets)) { |
---|
102 | $sets = module_invoke_all('filefield_icon_sets'); |
---|
103 | drupal_alter('filefield_icon_sets', $sets); |
---|
104 | } |
---|
105 | |
---|
106 | if (!isset($theme) || !isset($sets[$theme])) { |
---|
107 | $theme = 'default'; |
---|
108 | } |
---|
109 | |
---|
110 | return $sets[$theme]; |
---|
111 | } |
---|
112 | |
---|
113 | function _filefield_create_icon_path($icon_name, $theme = NULL) { |
---|
114 | $icons_directory = _filefield_icon_directory($theme); |
---|
115 | $icon_path = $icons_directory .'/'. $icon_name .'.png'; |
---|
116 | |
---|
117 | if (file_exists($icon_path)) { |
---|
118 | return $icon_path; |
---|
119 | } |
---|
120 | return FALSE; |
---|
121 | } |
---|
122 | |
---|
123 | function _filefield_generic_icon_map($file) { |
---|
124 | switch ($file['filemime']) { |
---|
125 | // Word document types. |
---|
126 | case 'application/msword': |
---|
127 | case 'application/vnd.ms-word.document.macroEnabled.12': |
---|
128 | case 'application/vnd.oasis.opendocument.text': |
---|
129 | case 'application/vnd.oasis.opendocument.text-template': |
---|
130 | case 'application/vnd.oasis.opendocument.text-master': |
---|
131 | case 'application/vnd.oasis.opendocument.text-web': |
---|
132 | case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': |
---|
133 | case 'application/vnd.stardivision.writer': |
---|
134 | case 'application/vnd.sun.xml.writer': |
---|
135 | case 'application/vnd.sun.xml.writer.template': |
---|
136 | case 'application/vnd.sun.xml.writer.global': |
---|
137 | case 'application/vnd.wordperfect': |
---|
138 | case 'application/x-abiword': |
---|
139 | case 'application/x-applix-word': |
---|
140 | case 'application/x-kword': |
---|
141 | case 'application/x-kword-crypt': |
---|
142 | return 'x-office-document'; |
---|
143 | |
---|
144 | // Spreadsheet document types. |
---|
145 | case 'application/vnd.ms-excel': |
---|
146 | case 'application/vnd.ms-excel.sheet.macroEnabled.12': |
---|
147 | case 'application/vnd.oasis.opendocument.spreadsheet': |
---|
148 | case 'application/vnd.oasis.opendocument.spreadsheet-template': |
---|
149 | case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': |
---|
150 | case 'application/vnd.stardivision.calc': |
---|
151 | case 'application/vnd.sun.xml.calc': |
---|
152 | case 'application/vnd.sun.xml.calc.template': |
---|
153 | case 'application/vnd.lotus-1-2-3': |
---|
154 | case 'application/x-applix-spreadsheet': |
---|
155 | case 'application/x-gnumeric': |
---|
156 | case 'application/x-kspread': |
---|
157 | case 'application/x-kspread-crypt': |
---|
158 | return 'x-office-spreadsheet'; |
---|
159 | |
---|
160 | // Presentation document types. |
---|
161 | case 'application/vnd.ms-powerpoint': |
---|
162 | case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12': |
---|
163 | case 'application/vnd.oasis.opendocument.presentation': |
---|
164 | case 'application/vnd.oasis.opendocument.presentation-template': |
---|
165 | case 'application/vnd.openxmlformats-officedocument.presentationml.presentation': |
---|
166 | case 'application/vnd.openxmlformats-officedocument.presentationml.slideshow': |
---|
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 | // RTF files. |
---|
231 | case 'application/rtf': |
---|
232 | return 'text-rtf'; |
---|
233 | |
---|
234 | // Google earth files. |
---|
235 | case 'application/vnd.google-earth.kml+xml': |
---|
236 | case 'application/vnd.google-earth.kmz': |
---|
237 | return 'application-google-earth'; |
---|
238 | |
---|
239 | // Executable types. |
---|
240 | case 'application/x-macbinary': |
---|
241 | case 'application/x-ms-dos-executable': |
---|
242 | case 'application/x-pef-executable': |
---|
243 | return 'application-x-executable'; |
---|
244 | |
---|
245 | default: |
---|
246 | return FALSE; |
---|
247 | } |
---|
248 | } |
---|