You are here

function theme_views_oai_pmh_row_misc_fields in Views OAI-PMH 7.2

Same name and namespace in other branches
  1. 6.2 theme/views_oai_pmh.theme.inc \theme_views_oai_pmh_row_misc_fields()

This 'misc' field theme function is capable of rendering its output in any of the defined OAI formats.

File

theme/views_oai_pmh.theme.inc, line 13
Theme related functions for processing our output style plugins.

Code

function theme_views_oai_pmh_row_misc_fields($vars) {
  $view = $vars['view'];
  $metadata_format = $vars['metadata_format'];

  // Prepare a reference array for storing common XML element attributes, such as language, and their values.
  $attribute_values = array();

  // Loop through the fields for this view.
  $previous_inline = FALSE;
  $vars['fields'] = array();

  // ensure it's at least an empty array.
  $row_index = $view->row_index;
  foreach ($view->field as $id => $field) {

    // render this even if set to exclude so it can be used elsewhere.
    $field_output = $view->style_plugin
      ->get_field($row_index, $id);
    $empty = $field_output !== 0 && empty($field_output);

    // Get the label or labels for this field.
    $label_string = $view->field[$id]
      ->label();

    // See if the label contains formatting HTML that we need to remove.
    if (strpos($label_string, '<span') !== FALSE) {

      // Grab the contents of the first 'span' tag in the label.
      $label_string = substr($label_string, 0, strpos($label_string, '<span>', 1));

      // Strip the span tag, leaving us with just the labels.
      $label_string = strip_tags($label_string);
    }

    // Explode the label on commas.
    $labels = explode(', ', $label_string);
    $label = '';
    foreach ($labels as $l) {
      if (array_key_exists($l, $GLOBALS['views_oai_pmh'][$metadata_format]->elements)) {

        // This value is found in the elements list.
        $label = $l;
        break;
      }
    }

    // Check to see if this label is for an attribute. Attributes are formatted
    // like this: "(attribute)oai_xxx@language"
    $attribute_prefix = '(attribute)';
    $attribute_name = '';
    $is_attribute = FALSE;
    if (substr($label, 0, strlen($attribute_prefix)) == $attribute_prefix) {

      // Flag that this is an attribute.
      $is_attribute = TRUE;

      // Get the common name of the attribute.
      $pos = strpos($label, '@');
      if ($pos !== FALSE) {
        $attribute_name = substr($label, $pos + 1);
      }
    }
    if ($is_attribute) {

      // Store the default values for XML element attributes in our reference array.
      $attribute_values[$attribute_name] = $field_output;
    }
    if ($is_attribute || $label == '') {

      // Force the display to exclude this field - we don't want to render attributes or empty labels as XML tags.
      $field->options['exclude'] = 1;
    }
    if (empty($field->options['exclude']) && (!$empty || empty($field->options['hide_empty']) && empty($vars['options']['hide_empty']))) {

      // Explode the label to give us a node path array, e.g. "a/b/c" becomes
      // array('a', 'b', 'c').
      $xml_node_path = explode('/', $label);

      // See if any of the XML elements in this path are defined with any
      // attributes.
      //
      // Note: in a case like <a><b attr="val"><c>Hello</c><d>Hi</d></b></a>,
      // the <b> tag's attributes will be calculated twice: once for <c> and
      // again for <d>. This is necessary for processing tags in this way but
      // does add a little overhead.
      $attributes = array();
      for ($i = 0; $i <= count($xml_node_path); $i++) {
        $attributes[$i] = array();
      }
      for ($i = 0; $i < count($xml_node_path); $i++) {
        $attrs = array();

        // See if this XML node definition contains any attributes.
        $xml_node_path_attrs = explode('@', $xml_node_path[$i], 2);
        $xml_node_path[$i] = $xml_node_path_attrs[0];
        if (!empty($xml_node_path_attrs[1])) {
          $attrs = $xml_node_path_attrs[1];
        }

        // Process this XML node's attributes.
        if (!empty($attrs)) {

          // There may be multiple attributes for this one element so attempt to separate them out.
          $attrs = explode('@', $attrs);

          // Check each attribute against our reference array; if we have a value for it, use that, otherwise omit the attribute.
          foreach ($attrs as $attr) {

            // Only add the attribute to our list of attributes for this node if we have a value available for it.
            if (array_key_exists($attr, $attribute_values)) {
              $attributes[$i][$attr] = $attribute_values[$attr];
            }
          }
        }
      }

      // Add the attributes to their nodes, optionally creating the nodes if they haven't yet been created.
      for ($i = 0; $i < count($xml_node_path); $i++) {
        if (is_array($attributes[$i]) && count($attributes[$i]) > 0) {
          $vars['nodes']
            ->set_attributes_at(array_slice($xml_node_path, 0, $i + 1), $attributes[$i]);
        }
      }

      // Multiple values per field (Views 3):
      if (!empty($field->multiple)) {
        $separator = $field->options['separator'];
        foreach (explode($separator, $field_output) as $value) {
          $vars['nodes']
            ->insert_at($xml_node_path, $value, $attributes[$i]);
        }
      }
      elseif (!empty($field->content_field['multiple'])) {
        $field_nid = $view->style_plugin
          ->get_field_id($row_index)->nid;
        $field_values = $field->field_values[$field_nid];
        foreach ($field_values as $field_value) {
          if ($field_value['value'] != '') {
            $vars['nodes']
              ->insert_at($xml_node_path, $field_value['value'], $attributes[$i]);
          }
        }
      }
      elseif (!empty($field->items)) {
        $taxonomy_items = reset($field->items);
        foreach ($taxonomy_items as $taxonomy_item) {
          $vars['nodes']
            ->insert_at($xml_node_path, $taxonomy_item['name'], $attributes[$i]);
        }
      }
      else {
        $vars['nodes']
          ->insert_at($xml_node_path, $field_output, $attributes[$i]);
      }
    }
  }

  // Start the draw procedure on the XML nodes, starting at an indent level of 2
  // to make the output look pretty, and return the resulting string.
  return $vars['nodes']
    ->draw(2);
}