View source
<?php
function views_json_views_api() {
return array(
'api' => '2.0',
'path' => drupal_get_path('module', 'views_json') . '/views',
);
}
function views_json_views_pre_render(&$view) {
if (isset($view->plugin_name) && $view->plugin_name == 'views_json') {
if (!empty($view->result)) {
foreach ($view->result as $row => $result) {
foreach ($view->field as $field_name => $field) {
if ($field instanceof views_handler_field_field) {
if ($field->field_info['type'] == 'video') {
$options = $field->options;
$video = $field
->get_value($view->result[$row]);
if (empty($video)) {
continue;
}
switch ($options['type']) {
case 'video_formatter_thumbnail':
$settings = $options['settings'];
$thumbnail = $video[0]['thumbnailfile'];
if (!empty($settings['image_style'])) {
$url = image_style_url($settings['image_style'], $thumbnail->uri);
}
else {
$url = file_create_url($thumbnail->uri);
}
break;
default:
$url = file_create_url($video[0]['uri']);
break;
}
$render_array = array(
'#type' => 'markup',
'#markup' => filter_xss($url),
);
$view->result[$row]->{'field_' . $field_name}[0]['rendered'] = $render_array;
}
}
}
}
}
}
}
function _views_json_render_multiple_field($field, $values) {
$options = $field->options;
if (!$field->defer_query) {
return $field
->render($values);
}
$content_field = content_fields($field->content_field['field_name'], $values->{$field->aliases['type']});
$vid = $values->{$field->field_alias};
if (isset($field->field_values[$vid])) {
$count_skipped = 0;
$items = array();
foreach ($field->field_values[$vid] as $item) {
if (empty($options['multiple']['multiple_from']) || $count_skipped >= $options['multiple']['multiple_from']) {
if (empty($options['multiple']['multiple_number']) || count($items) < $options['multiple']['multiple_number']) {
$nid = $item['_nid'];
unset($item['_nid']);
$items[] = $item;
}
else {
break;
}
}
$count_skipped++;
}
$node = drupal_clone($values);
$node->type = $values->{$field->aliases['type']};
$node->nid = $values->{$field->aliases['nid']};
$node->vid = $values->{$field->aliases['vid']};
$node->build_mode = NODE_BUILD_NORMAL;
$formatter_name = $options['format'];
if ($items && ($formatter = _content_get_formatter($formatter_name, $content_field['type']))) {
$rendered = array();
if (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE) {
$n = 0;
foreach ($items as $item) {
$output = content_format($content_field, $item, $formatter_name, $node);
if (!empty($output)) {
$rendered[++$n] = $field
->render_link($output, (object) array(
'nid' => $nid,
));
}
}
}
else {
$output = content_format($content_field, $items, $formatter_name, $values);
if (!empty($output)) {
$rendered[++$n] = $field
->render_link($output, (object) array(
'nid' => $nid,
));
}
}
if (count($rendered) > 1) {
return $rendered;
}
elseif ($rendered) {
return $rendered[1];
}
}
}
return '';
}
function _views_json_render_fields($view, $row) {
$field_ids = array_keys($view->field);
$fields_info = $view->display_handler
->get_option('fields');
$rendered_fields = array();
foreach ($field_ids as $id) {
$field = $view->field[$id];
$field_is_multiple = FALSE;
$field_raw = array();
if (isset($field->options['multiple']['group']) && isset($field->field_values)) {
$field_output = _views_json_render_multiple_field($field, $row);
$n = 0;
if (is_array($field_output)) {
foreach ($field->field_values[$row->{$field->field_alias}] as $item) {
$field_raw[++$n] = $item["value"];
}
$field_is_multiple = TRUE;
}
else {
$field_raw = $view->field[$field->options['id']]
->advanced_render($row);
}
}
else {
$field_output = $view->field[$field->options['id']]
->advanced_render($row);
$handler = $view->field[$id]->handler_type;
$fieldname = $handler . '_' . $id;
if ($handler == 'field' && isset($row->{$fieldname})) {
$field_id = "field_{$id}";
switch ($field->field_info['type']) {
case 'taxonomy_term_reference':
$field_raw = $row->{$field_id}[0]['raw']['taxonomy_term']->name;
break;
case 'link_field':
$field_raw = $row->{$field_id}[0]['raw']['url'];
break;
case 'email':
$field_raw = $row->{$field_id}[0]['raw']['email'];
break;
case 'video':
case 'file':
case 'image':
$field_raw = file_create_url($row->{$field_id}[0]['raw']['uri']);
break;
default:
$field_raw = $row->{$field_id}[0]['raw']['value'];
break;
}
}
else {
$field_raw = $row->{$view->field[$id]->field_alias};
}
if (empty($field->base_table)) {
$entity_info = entity_get_info($field->table);
if (isset($entity_info['entity keys']['label'])) {
$label_field = $entity_info['entity keys']['label'];
if ($field->real_field == $label_field) {
$field_output = html_entity_decode($field_output, ENT_QUOTES);
$field_output = mb_check_encoding($field_output, 'UTF-8') ? $field_output : utf8_encode($field_output);
}
}
}
}
$img_match = array();
$src_match = array();
if (isset($field->options['type'])) {
if (is_array($field_output) && $field->options['type'] == 'image') {
foreach ($field_output as $i => $f) {
if (preg_match("/<img[^>]+>/i", $f, $img_match)) {
if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
$field_output[$i]['src'] = $src_match[2];
$field_output[$i]['alt'] = '';
$field_output[$i]['title'] = '';
if (preg_match('/(alt)="([^"]*)"/i', $img_match[0], $alt_match)) {
$field_output[$i]['alt'] = $alt_match[2];
}
if (preg_match('/(title)="([^"]*)"/i', $img_match[0], $alt_match)) {
$field_output[$i]['title'] = $alt_match[2];
}
}
}
}
}
else {
if ($field->options['type'] == 'image') {
if (preg_match_all("/<img[^>]+>/i", $field_output, $img_matches)) {
$field_output = array();
if (sizeof($img_matches[0]) > 1) {
$field_is_multiple = TRUE;
foreach ($img_matches[0] as $i => $img_match) {
if (preg_match('/(src)="([^"]*)"/i', $img_match, $src_match)) {
$field_output[$i]['src'] = $src_match[2];
$field_output[$i]['alt'] = '';
$field_output[$i]['title'] = '';
if (preg_match('/(alt)="([^"]*)"/i', $img_match, $alt_match)) {
$field_output[$i]['alt'] = $alt_match[2];
}
if (preg_match('/(title)="([^"]*)"/i', $img_match, $alt_match)) {
$field_output[$i]['title'] = $alt_match[2];
}
}
}
}
else {
if (preg_match('/(src)="([^"]*)"/i', $img_matches[0][0], $src_match)) {
$field_output['src'] = $src_match[2];
$field_output['alt'] = '';
$field_output['title'] = '';
if (preg_match('/(alt)="([^"]*)"/i', $img_matches[0][0], $alt_match)) {
$field_output['alt'] = $alt_match[2];
}
if (preg_match('/(title)="([^"]*)"/i', $img_matches[0][0], $alt_match)) {
$field_output['title'] = $alt_match[2];
}
}
}
}
}
}
}
if (empty($field->options['exclude'])) {
if (empty($field->options['exclude']) && !($field->options['hide_empty'] && empty($field_output))) {
$object = new stdClass();
$object->id = $id;
if (empty($field_output) && $field->options['empty']) {
$object->content = $field->options['empty'];
}
else {
$object->content = $field_output;
}
$object->raw = $field_raw;
$object->class = drupal_clean_css_identifier(strtolower($id));
if (isset($view->style_options['translate_labels']) && $view->style_options['translate_labels']) {
$label = $view->field[$id]
->label();
}
elseif (isset($fields_info) && isset($fields_info[$id]['label'])) {
$label = $fields_info[$id]['label'];
}
else {
$label = $field
->label();
}
$object->label = check_plain($label);
$object->is_multiple = $field_is_multiple;
$rendered_fields[$id] = $object;
}
}
}
return $rendered_fields;
}
function _views_json_check_label($input) {
$output = str_replace(array(
'{',
'}',
'[',
']',
':',
',',
'"',
"'",
chr(47),
chr(92),
), '', $input);
$output = preg_replace('/[\\x{80}-\\x{A0}' . '\\x{01}-\\x{1F}' . '\\x{0}]/u', '', $output);
return check_plain(strip_tags($output));
}
function views_json_get($name, $display_id = 'default', $args = array(), $raw = FALSE, $current_page = 0) {
$view = views_get_view($name);
if (!is_object($view)) {
return NULL;
}
$view
->set_current_page($current_page);
$preview = $view
->preview($display_id, $args);
$start_pos = strpos($preview, '{');
$finish_pos = strrpos($preview, '}');
$length = $finish_pos - $start_pos + 1;
$json = trim(substr($preview, $start_pos, $length));
if ($raw) {
return $json;
}
return json_decode($json);
}
function _views_json_encode_formatted($v, $options, $depth = 0) {
$base_indent = ' ';
$eol = '<br />';
$indent = str_repeat($base_indent, $depth);
switch (gettype($v)) {
case 'boolean':
return $v ? 'true' : 'false';
case 'integer':
case 'double':
return $v;
case 'resource':
case 'string':
$search = array(
'"',
chr(92),
chr(8),
chr(12),
chr(13) . chr(10),
chr(10),
chr(13),
chr(9),
);
$replace = array(
'\\"',
'\\',
'\\b',
'\\f',
'\\n',
'\\n',
'\\r',
'\\t',
);
$output = str_replace($search, $replace, $v);
if (!empty($options['numeric_strings']) && is_numeric($output)) {
return strpos($output, '.') !== FALSE ? floatval($output) : intval($output);
}
return '"' . check_plain($output) . '"';
case 'array':
if (empty($v) || array_keys($v) === range(0, sizeof($v) - 1)) {
$output = array();
foreach ($v as $val) {
$output[] = $indent . $base_indent . _views_json_encode_formatted($val, $options, $depth + 1);
}
return '[' . (!empty($output) ? $eol . implode(',' . $eol, $output) . $eol . $indent : '') . ']';
}
case 'object':
$output = array();
foreach ($v as $key => $val) {
$output[] = $indent . $base_indent . _views_json_encode_formatted(strval($key), $options) . ' : ' . _views_json_encode_formatted($val, $options, $depth + 1);
}
return '{' . (!empty($output) ? $eol . implode(',' . $eol, $output) . $eol . $indent : '') . '}';
default:
return 'null';
}
}
function _views_json_debug_stop($var, $location) {
print "Location:{$location}\n";
var_dump($var);
module_Invoke_all('exit');
exit;
}
function _views_json_json_encode($rows, $bitmask = NULL) {
if (defined('PHP_MAJOR_VERSION') && PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 3) {
$json = json_encode($rows, $bitmask);
if (PHP_MINOR_VERSION <= 4) {
$json = str_replace(array(
'\\/',
), array(
'/',
), $json);
}
}
else {
$json = json_encode($rows);
$json = str_replace(array(
'\\/',
), array(
'/',
), $json);
}
return $json;
}