1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Theme related functions for processing our output style plugins. |
---|
6 | * |
---|
7 | * Views bug: http://drupal.org/node/593336 |
---|
8 | */ |
---|
9 | |
---|
10 | |
---|
11 | /** |
---|
12 | * Theme a status message |
---|
13 | */ |
---|
14 | function theme_views_data_export_message($message, $type = 'info') { |
---|
15 | $output = ''; |
---|
16 | $output .= '<div class="messages status ' . $type . '">'; |
---|
17 | $output .= $message; |
---|
18 | $output .= '</div>'; |
---|
19 | return $output; |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | * Theme a feed link. |
---|
24 | * |
---|
25 | * This theme function uses the theme pattern system to allow it to be |
---|
26 | * overidden in a more specific manner. The options for overiding this include |
---|
27 | * providing per display id; per type; per display id and per type. |
---|
28 | * |
---|
29 | * e.g. |
---|
30 | * For the view "export_test" with the display "page_1" and the type "csv" you |
---|
31 | * would have the following options. |
---|
32 | * views_data_export_feed_icon__export_test__page_1__csv |
---|
33 | * views_data_export_feed_icon__export_test__page_1 |
---|
34 | * views_data_export_feed_icon__export_test__csv |
---|
35 | * views_data_export_feed_icon__page_1__csv |
---|
36 | * views_data_export_feed_icon__page_1 |
---|
37 | * views_data_export_feed_icon__csv |
---|
38 | * views_data_export_feed_icon |
---|
39 | * |
---|
40 | * @ingroup themeable |
---|
41 | */ |
---|
42 | function theme_views_data_export_feed_icon($image_path, $url, $query = '', $text = '') { |
---|
43 | $url_options = array('html' => true); |
---|
44 | if ($query) { |
---|
45 | $url_options['query'] = $query; |
---|
46 | } |
---|
47 | $image = theme('image', $image_path, $text, $text); |
---|
48 | return l($image, $url, $url_options); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Theme callback for the export complete page. |
---|
53 | * |
---|
54 | * @param $file |
---|
55 | * Link to output file |
---|
56 | */ |
---|
57 | function theme_views_data_export_complete_page($file, $errors = array(), $return_url = '') { |
---|
58 | drupal_set_title(t('Data export successful')); |
---|
59 | drupal_set_html_head('<meta http-equiv="refresh" content="3;url='. check_plain($file) . '" />'); |
---|
60 | $output = ''; |
---|
61 | $output .= '<p>'; |
---|
62 | $output .= t('Your export has been created. View/download the file <a href="@link">here</a> (will automatically download in 3 seconds.)', array('@link' => $file)); |
---|
63 | $output .= '</p>'; |
---|
64 | |
---|
65 | if (!empty($return_url)) { |
---|
66 | $output .= '<p>'; |
---|
67 | $output .= l(t('Return to previous page'), $return_url); |
---|
68 | $output .= '</p>'; |
---|
69 | } |
---|
70 | return $output; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | function template_preprocess_views_data_export(&$vars) { |
---|
75 | $vars['header'] = $vars['rows']['header']; |
---|
76 | $vars['body'] = $vars['rows']['body']; |
---|
77 | $vars['footer'] = $vars['rows']['footer']; |
---|
78 | |
---|
79 | $view = $vars['view']; |
---|
80 | $fields = &$view->field; |
---|
81 | } |
---|
82 | |
---|
83 | function template_preprocess_views_data_export_csv_header(&$vars) { |
---|
84 | _views_data_export_header_shared_preprocess($vars); |
---|
85 | |
---|
86 | // Make sure we catch saved options that are misspelled. LEGACY |
---|
87 | if (isset($vars['options']['seperator'])) { |
---|
88 | $vars['options']['separator'] = $vars['options']['seperator']; |
---|
89 | } |
---|
90 | // Support old misspelled templates. LEGACY |
---|
91 | $vars['seperator'] = |
---|
92 | $vars['separator'] = $vars['options']['separator']; |
---|
93 | |
---|
94 | // Special handling when quoted values are involved. |
---|
95 | if ($vars['options']['quote']) { |
---|
96 | $wrap = '"'; |
---|
97 | $replace_value = '""'; |
---|
98 | } |
---|
99 | else { |
---|
100 | $wrap = ''; |
---|
101 | $replace_value = ''; |
---|
102 | } |
---|
103 | |
---|
104 | // Format header values. |
---|
105 | foreach ($vars['header'] as $key => $value) { |
---|
106 | $output = decode_entities(strip_tags($value)); |
---|
107 | if ($vars['options']['trim']) { |
---|
108 | $output = trim($output); |
---|
109 | } |
---|
110 | if (!empty($vars['options']['encoding']) && function_exists('iconv')) { |
---|
111 | switch($vars['options']['encoding']) { |
---|
112 | case 'utf8_decode': |
---|
113 | $converted = utf8_decode($output); |
---|
114 | break; |
---|
115 | default: |
---|
116 | $converted = iconv("UTF-8", $vars['options']['encoding'], $output); |
---|
117 | break; |
---|
118 | } |
---|
119 | if ($converted !== FALSE) { |
---|
120 | $output = $converted; |
---|
121 | } |
---|
122 | } |
---|
123 | $vars['header'][$key] = $wrap . str_replace('"', $replace_value, $output) . $wrap; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | function template_preprocess_views_data_export_csv_body(&$vars) { |
---|
128 | _views_data_export_body_shared_preprocess($vars); |
---|
129 | |
---|
130 | // Make sure we catch saved options that are misspelled. LEGACY |
---|
131 | if (isset($vars['options']['seperator'])) { |
---|
132 | $vars['options']['separator'] = $vars['options']['seperator']; |
---|
133 | } |
---|
134 | // Support old misspelled templates. LEGACY |
---|
135 | $vars['seperator'] = |
---|
136 | $vars['separator'] = $vars['options']['separator']; |
---|
137 | |
---|
138 | // Special handling when quoted values are involved. |
---|
139 | if ($vars['options']['quote']) { |
---|
140 | $wrap = '"'; |
---|
141 | $replace_value = '""'; |
---|
142 | } |
---|
143 | else { |
---|
144 | $wrap = ''; |
---|
145 | $replace_value = ''; |
---|
146 | } |
---|
147 | |
---|
148 | // Format row values. |
---|
149 | foreach ($vars['themed_rows'] as $i => $values) { |
---|
150 | foreach ($values as $j => $value) { |
---|
151 | $output = decode_entities(strip_tags($value)); |
---|
152 | if ($vars['options']['trim']) { |
---|
153 | $output = trim($output); |
---|
154 | } |
---|
155 | |
---|
156 | if (!empty($vars['options']['encoding']) && function_exists('iconv')) { |
---|
157 | switch($vars['options']['encoding']) { |
---|
158 | case 'utf8_decode': |
---|
159 | $converted = utf8_decode($output); |
---|
160 | break; |
---|
161 | default: |
---|
162 | $converted = iconv("UTF-8", $vars['options']['encoding'], $output); |
---|
163 | break; |
---|
164 | } |
---|
165 | if ($converted !== FALSE) { |
---|
166 | $output = $converted; |
---|
167 | } |
---|
168 | } |
---|
169 | if (!empty($vars['options']['replace_newlines'])) { |
---|
170 | $output = str_replace("\n", $vars['options']['newline_replacement'], $output); |
---|
171 | } |
---|
172 | $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, $output) . $wrap; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Preprocess csv output template. |
---|
179 | */ |
---|
180 | function template_preprocess_views_data_export_csv(&$vars) { |
---|
181 | // TODO Replace items with themed_rows. |
---|
182 | _views_data_export_shared_preprocess($vars); |
---|
183 | |
---|
184 | // Make sure we catch saved options that are misspelled. LEGACY |
---|
185 | if (isset($vars['options']['separator'])) { |
---|
186 | $vars['options']['separator'] = $vars['options']['seperator']; |
---|
187 | } |
---|
188 | // Support old misspelled templates. LEGACY |
---|
189 | $vars['seperator'] = |
---|
190 | $vars['separator'] = $vars['options']['separator']; |
---|
191 | |
---|
192 | // Special handling when quoted values are involved. |
---|
193 | if ($vars['options']['quote']) { |
---|
194 | $wrap = '"'; |
---|
195 | $replace_value = '""'; |
---|
196 | } |
---|
197 | else { |
---|
198 | $wrap = ''; |
---|
199 | $replace_value = ''; |
---|
200 | } |
---|
201 | |
---|
202 | // Format header values. |
---|
203 | foreach ($vars['header'] as $key => $value) { |
---|
204 | $output = decode_entities(strip_tags($value)); |
---|
205 | if ($vars['options']['trim']) { |
---|
206 | $output = trim($output); |
---|
207 | } |
---|
208 | if (!empty($vars['options']['encoding']) && function_exists('iconv')) { |
---|
209 | switch($vars['options']['encoding']) { |
---|
210 | case 'ASCII': |
---|
211 | $converted = iconv("UTF-8", "ASCII//TRANSLIT", $output); |
---|
212 | if ($converted !== FALSE) { |
---|
213 | $output = $converted; |
---|
214 | } |
---|
215 | break; |
---|
216 | } |
---|
217 | } |
---|
218 | $vars['header'][$key] = $wrap . str_replace('"', $replace_value, $output) . $wrap; |
---|
219 | } |
---|
220 | |
---|
221 | // Format row values. |
---|
222 | foreach ($vars['themed_rows'] as $i => $values) { |
---|
223 | foreach ($values as $j => $value) { |
---|
224 | $output = decode_entities(strip_tags($value)); |
---|
225 | if ($vars['options']['trim']) { |
---|
226 | $output = trim($output); |
---|
227 | } |
---|
228 | if (!empty($vars['options']['encoding']) && function_exists('iconv')) { |
---|
229 | switch($vars['options']['encoding']) { |
---|
230 | case 'ASCII': |
---|
231 | $converted = iconv("UTF-8", "ASCII//TRANSLIT", $output); |
---|
232 | if ($converted !== FALSE) { |
---|
233 | $output = $converted; |
---|
234 | } |
---|
235 | break; |
---|
236 | } |
---|
237 | } |
---|
238 | $vars['themed_rows'][$i][$j] = $wrap . str_replace('"', $replace_value, $output) . $wrap; |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * Preprocess txt output template. |
---|
245 | */ |
---|
246 | function template_preprocess_views_data_export_txt_body(&$vars) { |
---|
247 | _views_data_export_header_shared_preprocess($vars); |
---|
248 | _views_data_export_body_shared_preprocess($vars); |
---|
249 | } |
---|
250 | |
---|
251 | function template_preprocess_views_data_export_doc_body(&$vars) { |
---|
252 | // Pass through the generic MS Office preprocess. |
---|
253 | template_preprocess_views_data_export_msoffice_body($vars); |
---|
254 | } |
---|
255 | |
---|
256 | function template_preprocess_views_data_export_xls_body(&$vars) { |
---|
257 | // Pass through the generic MS Office preprocess. |
---|
258 | template_preprocess_views_data_export_msoffice_body($vars); |
---|
259 | } |
---|
260 | |
---|
261 | function template_preprocess_views_data_export_msoffice_body(&$vars) { |
---|
262 | _views_data_export_header_shared_preprocess($vars); |
---|
263 | _views_data_export_body_shared_preprocess($vars); |
---|
264 | |
---|
265 | // Construct the tbody of a table, see theme_table(). |
---|
266 | |
---|
267 | $header = array_values($vars['header']); |
---|
268 | $ts = tablesort_init($header); |
---|
269 | |
---|
270 | $flip = array( |
---|
271 | 'even' => 'odd', |
---|
272 | 'odd' => 'even', |
---|
273 | ); |
---|
274 | $class = 'even'; |
---|
275 | $output = ''; |
---|
276 | foreach ($vars['themed_rows'] as $number => $row) { |
---|
277 | $attributes = array(); |
---|
278 | |
---|
279 | // Check if we're dealing with a simple or complex row |
---|
280 | if (isset($row['data'])) { |
---|
281 | foreach ($row as $key => $value) { |
---|
282 | if ($key == 'data') { |
---|
283 | $cells = $value; |
---|
284 | } |
---|
285 | else { |
---|
286 | $attributes[$key] = $value; |
---|
287 | } |
---|
288 | } |
---|
289 | } |
---|
290 | else { |
---|
291 | $cells = $row; |
---|
292 | } |
---|
293 | if (count($cells)) { |
---|
294 | // Add odd/even class |
---|
295 | $class = $flip[$class]; |
---|
296 | if (isset($attributes['class'])) { |
---|
297 | $attributes['class'] .= ' ' . $class; |
---|
298 | } |
---|
299 | else { |
---|
300 | $attributes['class'] = $class; |
---|
301 | } |
---|
302 | |
---|
303 | // Build row |
---|
304 | $output .= ' <tr' . drupal_attributes($attributes) . '>'; |
---|
305 | $i = 0; |
---|
306 | foreach ($cells as $cell) { |
---|
307 | $cell = tablesort_cell($cell, $header, $ts, $i++); |
---|
308 | $output .= _theme_table_cell($cell); |
---|
309 | } |
---|
310 | $output .= " </tr>\n"; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | $vars['tbody'] = preg_replace('/<\/?(a|span) ?.*?>/', '', $output); // strip 'a' and 'span' tags |
---|
316 | |
---|
317 | } |
---|
318 | |
---|
319 | function template_preprocess_views_data_export_doc_header(&$vars) { |
---|
320 | // Pass through the generic MS Office preprocess. |
---|
321 | template_preprocess_views_data_export_msoffice_header($vars); |
---|
322 | } |
---|
323 | |
---|
324 | function template_preprocess_views_data_export_xls_header(&$vars) { |
---|
325 | // Pass through the generic MS Office preprocess. |
---|
326 | template_preprocess_views_data_export_msoffice_header($vars); |
---|
327 | } |
---|
328 | |
---|
329 | function template_preprocess_views_data_export_msoffice_header(&$vars) { |
---|
330 | _views_data_export_header_shared_preprocess($vars); |
---|
331 | |
---|
332 | // Need to do a little work to construct the table header, see theme_table(). |
---|
333 | $vars['header_row'] = ''; |
---|
334 | $vars['header_row'] .= '<thead><tr>'; |
---|
335 | |
---|
336 | $header = array_values($vars['header']); |
---|
337 | $ts = tablesort_init($header); |
---|
338 | |
---|
339 | foreach ($vars['header'] as $cell) { |
---|
340 | $cell = tablesort_header($cell, $header, $ts); |
---|
341 | $vars['header_row'] .= _theme_table_cell($cell, TRUE); |
---|
342 | } |
---|
343 | |
---|
344 | $vars['header_row'] .= '</tr></thead>'; |
---|
345 | |
---|
346 | $vars['header_row'] = preg_replace('/<\/?(a|span) ?.*?>/', '', $vars['header_row']); // strip 'a' and 'span' tags |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * Preprocess xml output template. |
---|
351 | */ |
---|
352 | function template_preprocess_views_data_export_xml_header(&$vars) { |
---|
353 | $vars['root_node'] = _views_data_export_xml_tag_clean($vars['options']['root_node']); |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | * Preprocess xml output template. |
---|
358 | */ |
---|
359 | function template_preprocess_views_data_export_xml_footer(&$vars) { |
---|
360 | $vars['root_node'] = _views_data_export_xml_tag_clean($vars['options']['root_node']); |
---|
361 | } |
---|
362 | |
---|
363 | /** |
---|
364 | * Preprocess xml output template. |
---|
365 | */ |
---|
366 | function template_preprocess_views_data_export_xml_body(&$vars) { |
---|
367 | _views_data_export_header_shared_preprocess($vars); |
---|
368 | _views_data_export_body_shared_preprocess($vars); |
---|
369 | |
---|
370 | $vars['item_node'] = _views_data_export_xml_tag_clean($vars['options']['item_node']); |
---|
371 | |
---|
372 | foreach ($vars['themed_rows'] as $num => $row) { |
---|
373 | foreach ($row as $field => $content) { |
---|
374 | // Prevent double encoding of the ampersand. Look for the entities produced by check_plain(). |
---|
375 | $content = preg_replace('/&(?!(amp|quot|#039|lt|gt);)/', '&', $content); |
---|
376 | // Convert < and > to HTML entities. |
---|
377 | $content = str_replace( |
---|
378 | array('<', '>'), |
---|
379 | array('<', '>'), |
---|
380 | $content); |
---|
381 | $vars['themed_rows'][$num][$field] = $content; |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | foreach ($vars['header'] as $field => $header) { |
---|
386 | // If there is no field label, use 'no name'. |
---|
387 | if (empty($header)) { |
---|
388 | $header = 'no name'; |
---|
389 | } |
---|
390 | if ($vars['options']['transform']) { |
---|
391 | switch ($vars['options']['transform_type']) { |
---|
392 | case 'dash': |
---|
393 | $vars['xml_tag'][$field] = str_replace(' ', '-', $header); |
---|
394 | break; |
---|
395 | case 'underline': |
---|
396 | $vars['xml_tag'][$field] = str_replace(' ', '_', $header); |
---|
397 | break; |
---|
398 | case 'camel': |
---|
399 | $vars['xml_tag'][$field] = str_replace(' ', '', ucwords(strtolower($header))); |
---|
400 | // Convert the very first character of the string to lowercase. |
---|
401 | $vars['xml_tag'][$field][0] = strtolower($vars['xml_tag'][$field][0]); |
---|
402 | break; |
---|
403 | case 'pascal': |
---|
404 | $vars['xml_tag'][$field] = str_replace(' ', '', ucwords(strtolower($header))); |
---|
405 | break; |
---|
406 | } |
---|
407 | } |
---|
408 | // We should always try to output valid XML. |
---|
409 | $vars['xml_tag'][$field] = _views_data_export_xml_tag_clean($vars['xml_tag'][$field]); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * Returns a valid XML tag formed from the given input. |
---|
415 | * |
---|
416 | * @param $tag The string that should be made into a valid XML tag. |
---|
417 | * @return The valid XML tag or an empty string if the string contained no valid |
---|
418 | * XML tag characters. |
---|
419 | */ |
---|
420 | function _views_data_export_xml_tag_clean($tag) { |
---|
421 | |
---|
422 | // This regex matches characters that are not valid in XML tags, and the |
---|
423 | // unicode ones that are. We don't bother with unicode, because it would so |
---|
424 | // the preg_replace down a lot. |
---|
425 | static $invalid_tag_chars_regex = '#[^\:A-Za-z_\-.0-9]+#'; |
---|
426 | |
---|
427 | // These characters are not valid at the start of an XML tag: |
---|
428 | static $invalid_start_chars = '-.0123456789'; |
---|
429 | |
---|
430 | // Convert invalid chars to '-': |
---|
431 | $tag = preg_replace($invalid_tag_chars_regex, '-', $tag); |
---|
432 | |
---|
433 | // Need to trim invalid characters from the start of the string: |
---|
434 | $tag = ltrim($tag, $invalid_start_chars); |
---|
435 | |
---|
436 | // As a last line of defense, if we've stripped out everything, set it to |
---|
437 | // something. |
---|
438 | if (empty($tag)) { |
---|
439 | $tag = 'invalid-tag-name'; |
---|
440 | } |
---|
441 | |
---|
442 | return $tag; |
---|
443 | } |
---|
444 | |
---|
445 | /** |
---|
446 | * Shared helper function for export preprocess functions. |
---|
447 | */ |
---|
448 | function _views_data_export_header_shared_preprocess(&$vars) { |
---|
449 | $view = $vars['view']; |
---|
450 | $fields = &$view->field; |
---|
451 | |
---|
452 | $vars['header'] = array(); |
---|
453 | foreach ($fields as $key => $field) { |
---|
454 | if (empty($field->options['exclude'])) { |
---|
455 | $vars['header'][$key] = check_plain($field->label()); |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | } |
---|
460 | |
---|
461 | /** |
---|
462 | * Shared helper function for export preprocess functions. |
---|
463 | */ |
---|
464 | function _views_data_export_body_shared_preprocess(&$vars) { |
---|
465 | $view = $vars['view']; |
---|
466 | $fields = &$view->field; |
---|
467 | |
---|
468 | $rows = $vars['rows']; |
---|
469 | |
---|
470 | $vars['themed_rows'] = array(); |
---|
471 | $keys = array_keys($fields); |
---|
472 | foreach ($rows as $num => $row) { |
---|
473 | $vars['themed_rows'][$num] = array(); |
---|
474 | |
---|
475 | foreach ($keys as $id) { |
---|
476 | if (empty($fields[$id]->options['exclude'])) { |
---|
477 | $vars['themed_rows'][$num][$id] = $view->style_plugin->rendered_fields[$num][$id]; |
---|
478 | } |
---|
479 | } |
---|
480 | } |
---|
481 | } |
---|