| 1 |
<?php
|
| 2 |
// $Id $
|
| 3 |
/**
|
| 4 |
* @file views-view-json.tpl.php
|
| 5 |
* View template to render view fields as JSON. Currently only supports the Exhibit format.
|
| 6 |
*
|
| 7 |
* - $view: The view in use.
|
| 8 |
* - $rows: The raw result objects from the query, with all data it fetched.
|
| 9 |
* - $options: The options for the style passed in from the UI.
|
| 10 |
*
|
| 11 |
* @ingroup views_templates
|
| 12 |
* @see views_json.views.inc
|
| 13 |
*/
|
| 14 |
|
| 15 |
//print_r($view);
|
| 16 |
//print_r($rows);
|
| 17 |
//print_r($options);
|
| 18 |
|
| 19 |
$items = array();
|
| 20 |
foreach($rows as $row) {
|
| 21 |
// print_r($row).EOL;
|
| 22 |
$items[] = explode("|", trim($row));
|
| 23 |
|
| 24 |
}
|
| 25 |
//print_r($items);
|
| 26 |
//foreach ($items as $item) {
|
| 27 |
// print_r($item).PHP_EOL;
|
| 28 |
// foreach($item as $itemfield) {
|
| 29 |
// print($itemfield);
|
| 30 |
// $itemfieldarray = explode(":", $itemfield);
|
| 31 |
// print_r($itemfieldarray).PHP_EOL;
|
| 32 |
// $label = $itemfieldarray[0]; $value=$itemfieldarray[1];
|
| 33 |
// print $label." : ".$value;
|
| 34 |
// }
|
| 35 |
//}
|
| 36 |
|
| 37 |
if ($options['format'] == 'Exhibit') json_exhibit_render($items);
|
| 38 |
|
| 39 |
function json_exhibit_render($items) {
|
| 40 |
define('EXHIBIT_DATE_FORMAT', '%Y-%m-%d %H:%M:%S');
|
| 41 |
$json = "{\n".str_repeat(" ", 4).'"items"'." : ". " [";
|
| 42 |
foreach ($items as $item) {
|
| 43 |
$json.="\n".str_repeat(" ", 8)."{\n";
|
| 44 |
foreach ($item as $itemfield) {
|
| 45 |
$itemfieldarray = explode(":", $itemfield);
|
| 46 |
$label = $itemfieldarray[0]; $value=$itemfieldarray[1];
|
| 47 |
$value = preg_replace('/<.*?>/', '', $value); // strip html tags
|
| 48 |
$value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas
|
| 49 |
$value = str_replace('"', '""', $value); // escape " characters
|
| 50 |
$value = decode_entities($value);
|
| 51 |
$json.=str_repeat(" ", 8).$label. " ".": ".'"'.$value.'"'."\n";
|
| 52 |
}
|
| 53 |
$json.=str_repeat(" ", 8)."},\n";
|
| 54 |
}
|
| 55 |
$json.=str_repeat(" ", 4)."]\n}";
|
| 56 |
/*
|
| 57 |
* The following will cause an error in a live view preview - comment out if
|
| 58 |
* debugging in there.
|
| 59 |
*/
|
| 60 |
drupal_set_header('Content-Type: text/javascript');
|
| 61 |
print $json;
|
| 62 |
module_invoke_all('exit');
|
| 63 |
exit;
|
| 64 |
}
|