You are here

json_autocomplete.module in Search Autocomplete 6.4

Module definition for json_autocomplete

@author Miroslav Talenberg (Dominique CLAUSE) <http://www.axiomcafe.fr/contact>

File

json_autocomplete/json_autocomplete.module
View source
<?php

/**
 * @file
 * Module definition for json_autocomplete
 *
 * @author
 * Miroslav Talenberg (Dominique CLAUSE) <http://www.axiomcafe.fr/contact>
 *
 * @see json_autocomplete.views.inc
 */
function json_autocomplete_views_api() {
  return array(
    'api' => '3.0',
    'path' => drupal_get_path('module', 'json_autocomplete'),
  );
}

/**
 * Takes each field from a row object and renders the field as determined by the field's theme
 *
 * @param $view
 *   View the row belongs to
 * @param $row
 *   Row object
 * @return array
 *   Object containing all the raw and rendered fields
 */
function _json_autocomplete_render_fields($view, $row) {
  $field_ids = array_keys($view->field);
  $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 = _json_autocomplete_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);
      $field_raw = isset($view->field[$id]->field_alias) && isset($row->{$view->field[$id]->field_alias}) ? $row->{$view->field[$id]->field_alias} : NULL;
    }
    if (empty($field->options['exclude'])) {
      if (empty($field->options['exclude']) && !($field->options['hide_empty'] && empty($field_output))) {
        $object = new stdClass();
        $object->id = $id;

        // Respect the 'empty' value if empty and "No results text" is given.
        if (empty($field_output) && $field->options['empty']) {
          $object->content = $field->options['empty'];
        }
        else {
          $object->content = $field_output;
        }
        $object->raw = $field_raw;
        $object->class = views_css_safe(strtolower($id));

        //views_css_safe($id);
        $object->label = check_plain($view->field[$id]
          ->label());
        if ($object->label) {
          if ($view->field[$id]->options['element_label_colon']) {
            $object->label .= ': ';
          }
          else {
            $object->label .= ' ';
          }
        }
        $object->is_multiple = $field_is_multiple;
        $rendered_fields[$id] = $object;
      }
    }
  }
  return $rendered_fields;
}

/**
 * Encodes JSON in a pretty-printed fashion.
 *
 * Only used inside the preview on the view configuration page.
 * @deprecated The $option parameter in PHP 5.4.0 json_encode() deprecates this function.
 *
 * @see _json_autocomplete_json_encode
 */
function _json_autocomplete_encode_formatted($json, $depth = 0) {
  $base_indent = '&nbsp;&nbsp;';
  $eol = '<br />';
  $indent = str_repeat($base_indent, $depth);

  // This is based on the drupal_to_js() function.
  switch (gettype($json)) {
    case 'boolean':

      // Lowercase is necessary!
      return $json ? 'true' : 'false';
    case 'integer':
    case 'double':
      return $json;
    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, $json);
      return '"' . check_plain($output) . '"';
    case 'array':

      // Arrays in JSON can't be associative.  If the array is empty or if it
      // has sequential whole number keys starting with 0, it's not associative
      // so we can go ahead and convert it as an array.
      if (empty($json) || array_keys($json) === range(0, sizeof($json) - 1)) {
        $output = array();
        foreach ($json as $val) {
          $output[] = $indent . $base_indent . _json_autocomplete_encode_formatted($val, $depth + 1);
        }
        return '[' . (!empty($output) ? $eol . implode(',' . $eol, $output) . $eol . $indent : '') . ']';
      }

    // Otherwise, fall through to convert the array as an object.
    case 'object':
      $output = array();
      foreach ($json as $key => $val) {
        $output[] = $indent . $base_indent . _json_autocomplete_encode_formatted(strval($key)) . ' : ' . _json_autocomplete_encode_formatted($val, $depth + 1);
      }
      return '{' . (!empty($output) ? $eol . implode(',' . $eol, $output) . $eol . $indent : '') . '}';
    default:
      return 'null';
  }
}
function _json_autocomplete_debug_stop($var, $location) {
  print "Location:{$location}\n";
  var_dump($var);
  module_Invoke_all('exit');
  exit;
}

Functions

Namesort descending Description
json_autocomplete_views_api @file Module definition for json_autocomplete
_json_autocomplete_debug_stop
_json_autocomplete_encode_formatted Deprecated Encodes JSON in a pretty-printed fashion.
_json_autocomplete_render_fields Takes each field from a row object and renders the field as determined by the field's theme