/[drupal]/contributions/modules/filefield/filefield.theme.inc
ViewVC logotype

Contents of /contributions/modules/filefield/filefield.theme.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


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

  ViewVC Help
Powered by ViewVC 1.1.2