You are here

function template_preprocess_views_views_json_style_simple in Views Datasource 7

Same name and namespace in other branches
  1. 6 theme/views_views_json_style.theme.inc \template_preprocess_views_views_json_style_simple()

@file Views theme to render view fields as JSON.

  • $view: The view in use.
  • $rows: Array of row objects as rendered by _views_json_render_fields
  • $attachment: Not used currently
  • $options: The options for the style passed in from the UI.

See also

views_json.views.inc

1 call to template_preprocess_views_views_json_style_simple()
template_preprocess_views_views_json_style_simple_object in views/theme/views_views_json_style.theme.inc
Theme preprocess for "single simple object" format.

File

views/theme/views_views_json_style.theme.inc, line 15
Views theme to render view fields as JSON.

Code

function template_preprocess_views_views_json_style_simple(&$vars) {
  $view = $vars["view"];
  $rows = $vars["rows"];
  $options = $vars["options"];
  $base = $view->base_table;
  $root_object = $options["root_object"];
  $top_child_object = $options["top_child_object"];
  $plaintext_output = $options["plaintext_output"];
  $objects = array();

  // Create bitmask for json_encode.
  $option_defs = $vars['view']->style_plugin
    ->option_definition();
  $bitmasks = $option_defs['encoding']['contains'];
  $bitmask = NULL;
  foreach ($bitmasks as $mask_key => $_bitmask) {
    if (isset($options[$mask_key]) && $options[$mask_key] && !is_array($options[$mask_key])) {
      $bitmask = $bitmask | constant($_bitmask['bitmask']);
    }
  }
  $vars['bitmask'] = $bitmask;
  foreach ($rows as $row) {
    $object = array();

    /* Convert the $rows into a hierachial key=>value array */
    foreach ($row as $field) {
      if ($options["field_output"] == "normal") {
        if ($field->label) {
          $label = $plaintext_output ? strip_tags($field->label) : $field->label;
        }
        else {
          $label = $plaintext_output ? strip_tags($field->id) : $field->id;
        }
        if (!$field->is_multiple) {
          if (is_array($field->content)) {
            $content = array();
            foreach ($field->content as $key => $value) {
              $safe = $plaintext_output ? strip_tags(html_entity_decode($value, ENT_QUOTES)) : $value;
              $safe = mb_check_encoding($safe, 'UTF-8') ? $safe : utf8_encode($safe);
              $content[$key] = $safe;
            }
          }
          else {
            $content = $plaintext_output ? strip_tags(html_entity_decode($field->content, ENT_QUOTES)) : $field->content;
            $content = mb_check_encoding($content, 'UTF-8') ? $content : utf8_encode($content);
          }
        }
        else {
          $content = array();
          foreach ($field->content as $n => $oc) {
            if (is_array($oc)) {
              foreach ($oc as $key => $value) {
                $content[$n][$key] = $plaintext_output ? strip_tags($value) : $value;
              }
            }
            else {
              $content[$n] = $plaintext_output ? strip_tags($oc) : $oc;
            }
          }
        }
      }
      elseif ($options["field_output"] == "raw") {
        $label = $plaintext_output ? strip_tags($field->id) : $field->id;
        if (!$field->is_multiple) {
          $content = $plaintext_output ? strip_tags($field->raw) : $field->raw;
        }
        else {
          $content = array();
          foreach ($field->raw as $n => $oc) {
            if (is_array($oc)) {
              foreach ($oc as $key => $value) {
                $content[$n][$key] = $plaintext_output ? strip_tags($value) : $value;
              }
            }
            else {
              $content[$n] = $plaintext_output ? strip_tags($oc) : $oc;
            }
          }
        }
      }

      // check if user wants nested arrays
      if (strlen($top_child_object) != 0) {
        $object[$top_child_object][$label] = $content;
      }
      else {
        $object[$label] = $content;
      }
    }
    $objects[] = $object;
  }

  // check if user wants nested arrays
  $vars["rows"] = strlen($root_object) != 0 ? array(
    $root_object => $objects,
  ) : $objects;

  // For pagination, output the total number of pages, the current page, the
  // total result count, and the limit per page.
  global $pager_total, $pager_page_array, $pager_total_items, $pager_limits;
  if (isset($pager_total[0])) {
    $pager_object = array(
      'pages' => $pager_total[0],
      'page' => $pager_page_array[0],
      'count' => intval($pager_total_items[0]),
      'limit' => intval($pager_limits[0]),
    );
    $vars["rows"]["pager"] = $pager_object;
  }
}