You are here

function views_json_simple_render in Views Datasource 5

1 call to views_json_simple_render()
theme_views_json_simple in ./views_json.module

File

./views_json.module, line 147
views_json.module - provides Views plugin for rendering node content as JSON.

Code

function views_json_simple_render($vid, $nodes, $type) {
  $view = views_load_view($vid);
  $result = views_build_view('items', $view);
  $fields = _views_get_fields();
  $json = "{\n  " . '"nodes":' . "\n" . str_repeat(" ", 4) . "[\n";
  $total_node_count = count((array) $nodes);

  //cast the nodes object to an array so we can count how many properties in itt;
  $node_count = 0;
  foreach ($nodes as $node) {
    $json .= str_repeat(" ", 6) . "{\n";
    $total_field_count = count((array) $view->field);

    //cast the field object to an array so we can count how many properties in itt;
    $field_count = 0;
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== false) {
        $label = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
        $label = trim(views_json_strip_illegal_chars(views_json_encode_special_chars($label)));
        $value = trim(views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view));
        if (is_null($value) || $value == '') {
          continue;
        }

        //      if (preg_match('/\d/', $value)) {
        //        if (strtotime($value))
        //        $value = gmstrftime(EXHIBIT_DATE_FORMAT, strtotime($value));
        //      }
        $value = views_json_encode_special_chars($value);
        $label = str_replace("Profile ", '', $label);

        //strip out Profile: from profile fields
        $json .= str_repeat(" ", 8) . '"' . $label . '"' . " " . ": " . '"' . $value . '"' . (++$field_count == $total_field_count ? "" : ",") . "\n";
      }
    }
    $json .= str_repeat(" ", 6) . "}" . (++$node_count == $total_node_count ? "" : ",") . "\n\n";
  }
  $json .= str_repeat(" ", 4) . "]\n}";
  drupal_set_header('Content-Type: text/javascript');
  print $json;
  module_invoke_all('exit');
  exit;
}