You are here

function views_json_exhibit_render in Views Datasource 5

1 call to views_json_exhibit_render()
theme_views_json_exhibit in ./views_json.module

File

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

Code

function views_json_exhibit_render($vid, $nodes, $type) {
  define('EXHIBIT_DATE_FORMAT', '%Y-%m-%d %H:%M:%S');
  $view = views_load_view($vid);
  $result = views_build_view('items', $view);
  $fields = _views_get_fields();
  $json = "{\n" . '"items"' . " : " . "\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(" ", 8) . "{\n";

    /*$items[] = array(
        'type'        => $node->type,
        'id'          => 'node/' . $node->nid,
        'label'       => $node->title,
        'author'      => 'user/' . $node->uid,
        'created'     => gmstrftime(EXHIBIT_DATE_FORMAT, $node->created),
        'changed'     => gmstrftime(EXHIBIT_DATE_FORMAT, $node->changed),
        'body'        => $node->teaser,
        'url'         => url('node/' . $node->nid, array('absolute' => TRUE)),
      );*/

    //$json.=str_repeat(" ", 12).'"type" '. " ".": ".'"'.(isset($node->type) ? $node->type : 'Item').'",'."\n";

    //$json.=str_repeat(" ", 12).'"label" '. " "." : ".'"'.(isset($node->title) ? $node->title : 'untitled').'",'."\n";
    $json .= str_repeat(" ", 12) . '"type" ' . " " . ": " . '"' . '##type##' . '",' . "\n";
    $json .= str_repeat(" ", 12) . '"label" ' . " " . " : " . '"' . '##label##' . '",' . "\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
        if ($label == 'type') {
          $json = str_replace('##type##', $value, $json);
        }
        elseif ($label == 'label') {
          $json = str_replace('##label##', $value, $json);
        }
        else {
          $json .= str_repeat(" ", 12) . '"' . $label . '"' . " " . ": " . '"' . $value . '"' . (++$field_count == $total_field_count ? "" : ",") . "\n";
        }
      }
    }
    if (strpos($json, '##type##') !== false) {
      $json = str_replace('##type##', isset($node->type) ? $node->type : 'Item', $json);
    }
    if (strpos($json, '##label##') !== false) {
      $json = str_replace('##label##', isset($node->title) ? $node->title : 'none', $json);
    }
    $json .= str_repeat(" ", 8) . "}" . (++$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;
}