You are here

views_rdf.module in Views Datasource 6

Same filename and directory in other branches
  1. 5 views_rdf.module
  2. 7 views_rdf.module

Module definition for views_rdf

File

views_rdf.module
View source
<?php

/**
 * @file
 * Module definition for views_rdf
 *
 * @see views_rdf.views.inc
 */
function views_rdf_views_api() {
  return array(
    'api' => '2.0',
    'path' => drupal_get_path('module', 'views_rdf'),
  );
}

/**
 * We almost duplicate the content_handler_field_multiple::render function
 * to get the multiple rendered field values in an array
 * @param $field
 * @param $values
 * @return unknown_type
 */
function _views_rdf_render_multiple_field($field, $values) {
  $options = $field->options;

  // If this is not a grouped field, use content_handler_field::render().
  if (!$field->defer_query) {
    return $field
      ->render($values);
  }

  // We're down to a single node here, so we can retrieve the actual field
  // definition for the node type being considered.
  $content_field = content_fields($field->content_field['field_name'], $values->{$field->aliases['type']});
  $vid = $values->{$field->field_alias};
  if (isset($field->field_values[$vid])) {

    // Gather items, respecting the 'Display n values starting from m' settings.
    $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']) {

          // Grab the nid - needed for render_link().
          $nid = $item['_nid'];
          unset($item['_nid']);
          $items[] = $item;
        }
        else {
          break;
        }
      }
      $count_skipped++;
    }

    // Build a pseudo-node from the retrieved values.
    $node = drupal_clone($values);

    // content_format and formatters will need a 'type'.
    $node->type = $values->{$field->aliases['type']};
    $node->nid = $values->{$field->aliases['nid']};
    $node->vid = $values->{$field->aliases['vid']};

    // Some formatters need to behave differently depending on the build_mode
    // (for instance: preview), so we provide one.
    $node->build_mode = NODE_BUILD_NORMAL;

    // Render items.
    $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) {

        // Single-value formatter.
        $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 {

        // Multiple values formatter.
        $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) {

        // TODO: could we use generic field display ?

        //return theme('content_view_multiple_field', $rendered, $content_field, $values);
        return $rendered;
      }
      elseif ($rendered) {
        return $rendered[1];
      }
    }
  }
  return '';
}

/**
 * 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 _views_rdf_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 = _views_rdf_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']]
          ->theme($row);
      }
    }
    else {
      $field_output = $view->field[$field->options['id']]
        ->theme($row);
      $field_raw = isset($view->field[$id]->field_alias) && isset($row->{$view->field[$id]->field_alias}) ? $row->{$view->field[$id]->field_alias} : NULL;
    }
    $img_match = array();
    $src_match = array();
    if (is_array($field_output)) {
      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_match[2];
          }
        }
      }
    }
    else {
      if (preg_match("/<img[^>]+>/i", $field_output, $img_match)) {
        if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
          $field_output = $src_match[2];
        }
      }
    }
    if (empty($field->options['exclude']) && $field_output != "" && !empty($field_output)) {
      $object = new stdClass();
      $object->id = $id;
      $object->content = $field_output;
      $object->raw = $field_raw;
      $object->class = views_css_safe($id);
      $object->label = check_plain($view->field[$id]
        ->label());
      $object->is_multiple = $field_is_multiple;
      $rendered_fields[$id] = $object;
    }
  }
  return $rendered_fields;
}

/**
 * Replaces illegal characters in a XML attribute value string with their encoded entities as well as the " char.
 *
 * @param $input
 *   String to process.
 * @return
 *   String with illegal characters stripped away and entities encoded.
 */
function _views_rdf_strip_illegal_xml_attribute_value_chars($input) {
  $output = str_replace('<', '&lt;', $input);

  // encode left-angled bracket
  $output = str_replace('>', '&gt;', $output);

  // encode right-angled bracket
  $output = str_replace('"', '&quot;', $output);

  // encode quote
  return $output;
}

/**
 * Strips characters not matching the XML Name production: 
 *
 * @param $input
 *   String to process.
 * @return
 *   String with illegal characters stripped.
 */
function _views_rdf_strip_illegal_xml_name_chars($input) {
  $output = preg_replace("/(^xml)|([^A-Za-z0-9_\\-\\.:])/", "", $input);
  return $output;
}

/**
 * Creates an author object to use in the headers of OPML and Atom documents
 * @param $username
 * @return unknown_type
 */
function _views_rdf_format_author($username) {
  $author = array();
  if (!$username) {
    $author["name"] = variable_get('anonymous', 'Anonymous');
    $author["email"] = variable_get('site_mail', 'none@none.info');
    return $author;
  }
  if (is_numeric($username)) {
    $user = user_load(array(
      'uid' => $username,
    ));
  }
  else {
    $user = user_load(array(
      'name' => $username,
    ));
  }
  if (!$user) {

    //$author["name"] = variable_get('anonymous', ('Anonymous'));
    $author["name"] = variable_get('anonymous', 'Anonymous');
    $author["email"] = variable_get('site_mail', 'none@none.info');
    return $author;
  }
}

/**
 * Renders a single user from a view in the
 */

// function views_rdf_sioc_render_xml_user($view_result) {
//
// }

/**
 * Strips illegal Unicode characters and encodes entities in string.
 *
 * @param $input
 *   String to process.
 * @return
 *   String with illegal characters stripped away and entities encoded.
 */
function views_rdf_strip_illegal_chars($input) {
  return check_plain(strip_tags($input));
}

/**
 * If input is a serialized date array, return a date string
 *
 * @param $input
 *   Input to check.
 * @return
 *   Either the original input or a date string.
 */
function views_rdf_is_date($input) {
  if (strpos($input, 'a:3:{s:5:"month"') !== 0) {
    return $input;
  }
  else {

    // serialized date array
    $date = unserialize($input);
    return format_date(mktime(0, 0, 0, $date['month'], $date['day'], $date['year']), 'custom', DATE_ISO8601);
  }
}
function _views_rdf_debug_stop($var) {
  var_dump($var);
  module_Invoke_all('exit');
  exit;
}

Functions

Namesort descending Description
views_rdf_is_date If input is a serialized date array, return a date string
views_rdf_strip_illegal_chars Strips illegal Unicode characters and encodes entities in string.
views_rdf_views_api @file Module definition for views_rdf
_views_rdf_debug_stop
_views_rdf_format_author Creates an author object to use in the headers of OPML and Atom documents
_views_rdf_render_fields Takes each field from a row object and renders the field as determined by the field's theme
_views_rdf_render_multiple_field We almost duplicate the content_handler_field_multiple::render function to get the multiple rendered field values in an array
_views_rdf_strip_illegal_xml_attribute_value_chars Replaces illegal characters in a XML attribute value string with their encoded entities as well as the " char.
_views_rdf_strip_illegal_xml_name_chars Strips characters not matching the XML Name production: