5 * FileField: Defines a CCK file field type.
7 * Uses content.module to store the fid and field specific metadata,
8 * and Drupal's {files} table to store the actual file data.
10 * This file contains common theme functions.
14 * Return an image with an appropriate icon for the given file.
15 * Remember to pass a file object and not an array.
17 function theme_filefield_icon($file) {
18 if (is_object($file)) {
19 $file = (array) $file;
21 $dashed_mime = check_plain(strtr($file['filemime'], array('/' => '-')));
23 if ($icon_url = _filefield_icon_url($file)) {
24 $icon = '<img class="field-icon-'.
$dashed_mime .
'" src="'.
$icon_url .
'" />';
26 return '<div class="filefield-icon field-icon-'.
$dashed_mime .
'">'.
$icon .
'</div>';
29 function _filefield_icon_url($file) {
31 $theme = variable_get('filefield_icon_theme', 'protocons');
33 if ($iconpath = _filefield_icon_path($file, $theme)) {
34 return $base_url .
'/'.
$iconpath;
39 function _filefield_icon_path($file, $theme = 'protocons') {
40 // If there's an icon matching the exact mimetype, go for it.
41 $dashed_mime = strtr($file['filemime'], array('/' => '-'));
42 if ($iconpath = _filefield_create_icon_path($dashed_mime, $theme)) {
45 // For a couple of mimetypes, we can "manually" tell a generic icon.
46 if ($generic_name = _filefield_generic_icon_map($file)) {
47 if ($iconpath = _filefield_create_icon_path($generic_name, $theme)) {
51 // Use generic icons for each category that provides such icons.
52 foreach (array('audio', 'image', 'text', 'video') as
$category) {
53 if (strpos($file['filemime'], $category .
'/') === 0) {
54 if ($iconpath = _filefield_create_icon_path($category .
'-x-generic', $theme)) {
59 // Try application-octet-stream as last fallback.
60 if ($iconpath = _filefield_create_icon_path('application-octet-stream', $theme)) {
63 // Sorry, no icon can be found...
67 function _filefield_create_icon_path($iconname, $theme = 'protocons') {
68 $iconpath = drupal_get_path('module', 'filefield')
69 .
'/icons/'.
$theme .
'/16x16/mimetypes/'.
$iconname .
'.png';
70 if (file_exists($iconpath)) {
76 function _filefield_generic_icon_map($file) {
77 switch ($file['filemime']) {
78 // Word document types.
79 case
'application/msword':
80 case
'application/vnd.ms-word.document.macroEnabled.12':
81 case
'application/vnd.oasis.opendocument.text':
82 case
'application/vnd.oasis.opendocument.text-template':
83 case
'application/vnd.oasis.opendocument.text-master':
84 case
'application/vnd.oasis.opendocument.text-web':
85 case
'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
86 case
'application/vnd.stardivision.writer':
87 case
'application/vnd.sun.xml.writer':
88 case
'application/vnd.sun.xml.writer.template':
89 case
'application/vnd.sun.xml.writer.global':
90 case
'application/vnd.wordperfect':
91 case
'application/x-abiword':
92 case
'application/x-applix-word':
93 case
'application/x-kword':
94 case
'application/x-kword-crypt':
95 return 'x-office-document';
97 // Spreadsheet document types.
98 case
'application/vnd.ms-excel':
99 case
'application/vnd.ms-excel.sheet.macroEnabled.12':
100 case
'application/vnd.oasis.opendocument.spreadsheet':
101 case
'application/vnd.oasis.opendocument.spreadsheet-template':
102 case
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
103 case
'application/vnd.stardivision.calc':
104 case
'application/vnd.sun.xml.calc':
105 case
'application/vnd.sun.xml.calc.template':
106 case
'application/vnd.lotus-1-2-3':
107 case
'application/x-applix-spreadsheet':
108 case
'application/x-gnumeric':
109 case
'application/x-kspread':
110 case
'application/x-kspread-crypt':
111 return 'x-office-spreadsheet';
113 // Presentation document types.
114 case
'application/vnd.ms-powerpoint':
115 case
'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
116 case
'application/vnd.oasis.opendocument.presentation':
117 case
'application/vnd.oasis.opendocument.presentation-template':
118 case
'application/vnd.openxmlformats-officedocument.presentationml.presentation':
119 case
'application/vnd.stardivision.impress':
120 case
'application/vnd.sun.xml.impress':
121 case
'application/vnd.sun.xml.impress.template':
122 case
'application/x-kpresenter':
123 return 'x-office-presentation';
125 // Compressed archive types.
126 case
'application/zip':
127 case
'application/x-zip':
128 case
'application/stuffit':
129 case
'application/x-stuffit':
130 case
'application/x-7z-compressed':
131 case
'application/x-ace':
132 case
'application/x-arj':
133 case
'application/x-bzip':
134 case
'application/x-bzip-compressed-tar':
135 case
'application/x-compress':
136 case
'application/x-compressed-tar':
137 case
'application/x-cpio-compressed':
138 case
'application/x-deb':
139 case
'application/x-gzip':
140 case
'application/x-java-archive':
141 case
'application/x-lha':
142 case
'application/x-lhz':
143 case
'application/x-lzop':
144 case
'application/x-rar':
145 case
'application/x-rpm':
146 case
'application/x-tzo':
147 case
'application/x-tar':
148 case
'application/x-tarz':
149 case
'application/x-tgz':
150 return 'package-x-generic';
152 // Script file types.
153 case
'application/ecmascript':
154 case
'application/javascript':
155 case
'application/mathematica':
156 case
'application/vnd.mozilla.xul+xml':
157 case
'application/x-asp':
158 case
'application/x-awk':
159 case
'application/x-cgi':
160 case
'application/x-csh':
161 case
'application/x-m4':
162 case
'application/x-perl':
163 case
'application/x-php':
164 case
'application/x-ruby':
165 case
'application/x-shellscript':
166 case
'text/vnd.wap.wmlscript':
167 case
'text/x-emacs-lisp':
168 case
'text/x-haskell':
169 case
'text/x-literate-haskell':
171 case
'text/x-makefile':
172 case
'text/x-matlab':
173 case
'text/x-python':
176 return 'text-x-script';
179 case
'application/xhtml+xml':
183 case
'application/x-macbinary':
184 case
'application/x-ms-dos-executable':
185 case
'application/x-pef-executable':
186 return 'application-x-executable';