Show the '#required' state of the "File widgets" table.
[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 * Theme function for a file formatter / file widget settings table.
15 */
16 function theme_filefield_draggable_settings_table($element) {
17 $settings_type = $element['#settings_type']; // 'widgets' or 'formatters'?
18 $table_id = 'filefield-file-'. $settings_type .'-table';
19 $order_class = 'filefield-file-'. $settings_type .'-weight';
20
21 $widget_info = $element['#widget_info'];
22 $title = isset($element['#title']) ? $element['#title'] : '';
23 unset($element['#title']); // the header is used instead of the regular label
24
25 $required = !empty($element['#required'])
26 ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>'
27 : '';
28
29 $title_column = t('@title: !required', array(
30 '@title' => $title, '!required' => $required,
31 ));
32 $header = array($title_column, t('Order'));
33 $rows = array();
34
35 foreach (element_children($element) as $key) {
36 $element[$key]['weight']['#attributes']['class'] = $order_class;
37 $delta_element = drupal_render($element[$key]['weight']);
38 $row = array(
39 drupal_render($element[$key]),
40 $delta_element,
41 );
42 $rows[] = array('data' => $row, 'class' => 'draggable');
43 }
44 $output = theme('table', $header, $rows, array('id' => $table_id));
45 $output = theme('form_element', $element, $output);
46
47 drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
48
49 return $output;
50 }
51
52 /**
53 * Render either an upload or edit container item so that the children elements
54 * always appear inside a nice table, whatever $field['multiple'] might be.
55 */
56 function theme_filefield_container_item($element) {
57 $field = $element['#field'];
58 $children = !empty($element['#children']) ? $element['#children'] : '';
59 $children = '<div class="ahah-new-content">'. $children .'</div>';
60
61 // CCK renders a nice table for multiple-value fields, that's just fine as is.
62 if ($field['multiple']) {
63 return $children;
64 }
65 // If the field is single-value, we still want to have a table, for the looks.
66 $header = array();
67 $rows = array(array($children));
68 $attributes = array('class' => 'filefield-file-container-table');
69 $table = theme('table', $header, $rows, $attributes);
70 return theme('form_element', $element, $table);
71 }
72
73 /**
74 * Return an image with an appropriate icon for the given file.
75 * Remember to pass a file object and not an array.
76 */
77 function theme_filefield_icon($file) {
78 if (is_object($file)) {
79 $file = (array) $file;
80 }
81 $dashed_mime = check_plain(strtr($file['filemime'], array('/' => '-')));
82
83 if ($icon_url = _filefield_icon_url($file)) {
84 $icon = '<img class="field-icon-'. $dashed_mime .'" src="'. $icon_url .'" />';
85 }
86 return '<div class="filefield-icon field-icon-'. $dashed_mime .'">'. $icon .'</div>';
87 }
88
89 function _filefield_icon_url($file) {
90 global $base_url;
91 $theme = variable_get('filefield_icon_theme', 'protocons');
92
93 if ($iconpath = _filefield_icon_path($file, $theme)) {
94 return $base_url .'/'. $iconpath;
95 }
96 return FALSE;
97 }
98
99 function _filefield_icon_path($file, $theme = 'protocons') {
100 // If there's an icon matching the exact mimetype, go for it.
101 $dashed_mime = strtr($file['filemime'], array('/' => '-'));
102 if ($iconpath = _filefield_create_icon_path($dashed_mime, $theme)) {
103 return $iconpath;
104 }
105 // For a couple of mimetypes, we can "manually" tell a generic icon.
106 if ($generic_name = _filefield_generic_icon_map($file)) {
107 if ($iconpath = _filefield_create_icon_path($generic_name, $theme)) {
108 return $iconpath;
109 }
110 }
111 // Use generic icons for each category that provides such icons.
112 foreach (array('audio', 'image', 'text', 'video') as $category) {
113 if (strpos($file['filemime'], $category .'/') === 0) {
114 if ($iconpath = _filefield_create_icon_path($category .'-x-generic', $theme)) {
115 return $iconpath;
116 }
117 }
118 }
119 // Try application-octet-stream as last fallback.
120 if ($iconpath = _filefield_create_icon_path('application-octet-stream', $theme)) {
121 return $iconpath;
122 }
123 // Sorry, no icon can be found...
124 return FALSE;
125 }
126
127 function _filefield_create_icon_path($iconname, $theme = 'protocons') {
128 $iconpath = drupal_get_path('module', 'filefield')
129 .'/icons/'. $theme .'/16x16/mimetypes/'. $iconname .'.png';
130 if (file_exists($iconpath)) {
131 return $iconpath;
132 }
133 return FALSE;
134 }
135
136 function _filefield_generic_icon_map($file) {
137 switch ($file['filemime']) {
138 // Word document types.
139 case 'application/msword':
140 case 'application/vnd.ms-word.document.macroEnabled.12':
141 case 'application/vnd.oasis.opendocument.text':
142 case 'application/vnd.oasis.opendocument.text-template':
143 case 'application/vnd.oasis.opendocument.text-master':
144 case 'application/vnd.oasis.opendocument.text-web':
145 case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
146 case 'application/vnd.stardivision.writer':
147 case 'application/vnd.sun.xml.writer':
148 case 'application/vnd.sun.xml.writer.template':
149 case 'application/vnd.sun.xml.writer.global':
150 case 'application/vnd.wordperfect':
151 case 'application/x-abiword':
152 case 'application/x-applix-word':
153 case 'application/x-kword':
154 case 'application/x-kword-crypt':
155 return 'x-office-document';
156
157 // Spreadsheet document types.
158 case 'application/vnd.ms-excel':
159 case 'application/vnd.ms-excel.sheet.macroEnabled.12':
160 case 'application/vnd.oasis.opendocument.spreadsheet':
161 case 'application/vnd.oasis.opendocument.spreadsheet-template':
162 case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
163 case 'application/vnd.stardivision.calc':
164 case 'application/vnd.sun.xml.calc':
165 case 'application/vnd.sun.xml.calc.template':
166 case 'application/vnd.lotus-1-2-3':
167 case 'application/x-applix-spreadsheet':
168 case 'application/x-gnumeric':
169 case 'application/x-kspread':
170 case 'application/x-kspread-crypt':
171 return 'x-office-spreadsheet';
172
173 // Presentation document types.
174 case 'application/vnd.ms-powerpoint':
175 case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
176 case 'application/vnd.oasis.opendocument.presentation':
177 case 'application/vnd.oasis.opendocument.presentation-template':
178 case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
179 case 'application/vnd.stardivision.impress':
180 case 'application/vnd.sun.xml.impress':
181 case 'application/vnd.sun.xml.impress.template':
182 case 'application/x-kpresenter':
183 return 'x-office-presentation';
184
185 // Compressed archive types.
186 case 'application/zip':
187 case 'application/x-zip':
188 case 'application/stuffit':
189 case 'application/x-stuffit':
190 case 'application/x-7z-compressed':
191 case 'application/x-ace':
192 case 'application/x-arj':
193 case 'application/x-bzip':
194 case 'application/x-bzip-compressed-tar':
195 case 'application/x-compress':
196 case 'application/x-compressed-tar':
197 case 'application/x-cpio-compressed':
198 case 'application/x-deb':
199 case 'application/x-gzip':
200 case 'application/x-java-archive':
201 case 'application/x-lha':
202 case 'application/x-lhz':
203 case 'application/x-lzop':
204 case 'application/x-rar':
205 case 'application/x-rpm':
206 case 'application/x-tzo':
207 case 'application/x-tar':
208 case 'application/x-tarz':
209 case 'application/x-tgz':
210 return 'package-x-generic';
211
212 // Script file types.
213 case 'application/ecmascript':
214 case 'application/javascript':
215 case 'application/mathematica':
216 case 'application/vnd.mozilla.xul+xml':
217 case 'application/x-asp':
218 case 'application/x-awk':
219 case 'application/x-cgi':
220 case 'application/x-csh':
221 case 'application/x-m4':
222 case 'application/x-perl':
223 case 'application/x-php':
224 case 'application/x-ruby':
225 case 'application/x-shellscript':
226 case 'text/vnd.wap.wmlscript':
227 case 'text/x-emacs-lisp':
228 case 'text/x-haskell':
229 case 'text/x-literate-haskell':
230 case 'text/x-lua':
231 case 'text/x-makefile':
232 case 'text/x-matlab':
233 case 'text/x-python':
234 case 'text/x-sql':
235 case 'text/x-tcl':
236 return 'text-x-script';
237
238 // HTML aliases.
239 case 'application/xhtml+xml':
240 return 'text-html';
241
242 // Executable types.
243 case 'application/x-macbinary':
244 case 'application/x-ms-dos-executable':
245 case 'application/x-pef-executable':
246 return 'application-x-executable';
247
248 default:
249 return FALSE;
250 }
251 }