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