You are here

spaces_views.inc in Spaces 5.2

Same filename and directory in other branches
  1. 5 spaces_views.inc

File

spaces_views.inc
View source
<?php

/**
 * Implementation of hook_views_tables().
 * Adds a meta-filter that provides a layer of abstraction that
 * delegates actual filtering to the implementing space type modules.
 */
function spaces_views_tables() {
  return array(
    'spaces' => array(
      'name' => 'space',
      'filters' => array(
        'type' => array(
          'name' => t('Spaces: node in current space'),
          'operator' => array(
            'all' => t('Apply at all times'),
            'active' => t('Apply only in a space'),
          ),
          'list' => 'spaces_views_handler_operator_spacetype',
          'list-type' => 'select',
          'handler' => 'spaces_handler_filter_type',
        ),
      ),
    ),
  );
}

/**
 * Views filter options handler that provides the options array for
 * the filter view value dropdown.
 */
function spaces_views_handler_operator_spacetype() {
  $options = array(
    'all' => t('Applies to any space'),
  );
  foreach (spaces_types() as $type => $info) {
    $options[$type] = t('Applies only in a !spacetype', array(
      '!spacetype' => strtolower($info['title']),
    ));
  }
  return $options;
}

/**
 * Spaces view filter handler. 
 */
function spaces_handler_filter_type($op, $filter, $filterinfo, &$query) {
  $space = spaces_get_space();
  if ($space) {
    if ($filter['value'] == $space->type || $filter['value'] == 'all') {
      $space
        ->views_filter(TRUE, $query);
    }
  }
  else {
    if ($filter['operator'] == 'all') {
      $types = spaces_types();
      foreach ($types as $type => $info) {
        call_user_func(array(
          $info['class'],
          'views_filter',
        ), FALSE, $query);
      }
    }
  }
}

/**
 *  Function that handles a variety of tasks needed when view is empty
 */
function _spaces_views_empty($feature, $block = false) {
  $features = spaces_features();

  // Check to see if user has access to this feature
  $space = spaces_get_space();
  if ($space && $space
    ->feature_access($feature) && ($types = $features[$feature]->node)) {
    $content_types = node_get_types();
    $type_name = $content_types[current($types)]->name;
    if (!$block) {
      $b = spaces_node_links();
      if ($b) {
        $m = "<p>" . t('Please click below to add your first @content.', array(
          '@content' => $type_name,
        )) . "</p>";
      }
      else {
        $m = "<p>" . t('No entries were found.', array(
          '@content' => $type_name,
        )) . "</p>";
      }
    }
    return $m . $b;
  }
  else {
    if ($block) {
      return '';
    }
    else {
      drupal_not_found();
      exit;
    }
  }
}

/**
 *  Custom view handler for filetypes
 */
function spaces_views_handler_filetype($fieldinfo, $fielddata, $value, $data) {
  $file = new stdClass();
  $file->filemime = $data->files_filemime;
  $file->filename = $data->files_filename;
  $file->filepath = $data->files_filepath;
  return theme('fileview', $file);
}

/**
 *  Custom view handler for coloration projects
 */
function spaces_views_handler_crayon_name($fieldinfo, $fielddata, $value, $data) {
  switch ($fielddata['options']) {
    case 'og':
      $og = db_fetch_object(og_get_node_groups_result($data->nid));
      $og->nid = $og->group_nid;
      return theme('crayon_popup', $og);
      break;
    case 'casetracker':
      static $nodes = array();
      $pid = $data->casetracker_case_pid;
      if (!$nodes[$pid]) {
        $nodes[$pid] = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.nid = %d"), $pid));
      }
      return theme('crayon_popup', $nodes[$pid]);
      break;
  }
}

/**
 * hook_views_style_plugins
 */
function spaces_views_style_plugins($value = '') {
  $items['spaces_datetitle'] = array(
    'name' => t('Spaces: Date title listing'),
    'theme' => 'spaces_datetitle_view_style',
    'validate' => 'views_ui_plugin_validate_list',
    'needs_fields' => true,
    'weight' => 0,
  );
  return $items;
}

/**
 * retrieves the field id of the $op type provided
 */
function _spaces_views_get_field($op, $view) {
  switch ($op) {
    case 'cck_date':
      $view = (object) $view;
      $fields = _views_get_fields();

      // iterate through provided fields and check for date field
      foreach ($view->field as $field_id => $field) {
        $field_key = $field['tablename'] . '.' . $field['field'];
        if ($fields[$field_key]['content_field_module'] == 'date' && $view->field[$field_id]) {
          return $field_id;
        }
      }
      return false;
  }
}
function theme_spaces_datetitle_view_style($view, $nodes, $type) {
  $fields = _views_get_fields();
  $taken = array();

  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $field_names[$id] = $field_name;
  }

  // Set up some variables that won't change.
  $base_vars = array(
    'view' => $view,
    'view_type' => $type,
  );

  // CCK fields can be named anything, so we use
  // a custom function to ransack the view for fields we might want
  $custom_fields = array();
  if (_spaces_views_get_field('cck_date', $view) !== false) {
    $custom_fields[_spaces_views_get_field('cck_date', $view)] = 'cck_date';
  }
  foreach ($nodes as $i => $node) {
    $vars = $base_vars;
    $vars['node'] = $node;
    $vars['count'] = $i;
    $vars['stripe'] = $i % 2 ? 'even' : 'odd';
    foreach ($view->field as $id => $field) {
      if (isset($custom_fields[$id])) {
        $name = $custom_fields[$id];
      }
      else {
        $name = $field_names[$id];
      }
      $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
      if (isset($field['label'])) {
        $vars[$name . '_label'] = $field['label'];
      }
    }
    $items[] = theme('datetime_view_style_item', $vars);
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

/**
 * 
 */
function theme_datetime_view_style_item($vars) {
  $item = (object) $vars;
  $_date = array(
    $item->created,
    $item->changed,
    $item->last_comment_timestamp,
    $item->cck_date,
  );
  foreach ($_date as $t) {
    if ($t) {
      $d = $t;
      break;
    }
  }
  $_user = array(
    $item->assign_to,
    $item->name,
    $item->user,
  );
  foreach ($_user as $t) {
    if ($t) {
      $u = $t;
      break;
    }
  }
  $_title = array(
    $item->subject,
    $item->node_title,
    // node title gets precedence over group title
    $item->title,
  );
  foreach ($_title as $t) {
    if ($t) {
      $title = $t;
      break;
    }
  }
  $output = "<div class='view-item clear-block'>";
  if ($item->type) {
    $output .= "<div class=\"view-type\">{$item->type}</div>";
  }
  if ($d || $u) {
    $output .= "<div class='meta'>";
    if ($d) {
      $output .= "<span class=\"view-date\">{$d}</span>";
    }
    if ($u) {
      $output .= "<span class=\"view-user\">{$u}</span>";
    }
    $output .= "</div>";
  }
  $output .= "<div class=\"view-title\">{$title}</div>";
  $output .= "</div>";
  return $output;
}

Functions

Namesort descending Description
spaces_handler_filter_type Spaces view filter handler.
spaces_views_handler_crayon_name Custom view handler for coloration projects
spaces_views_handler_filetype Custom view handler for filetypes
spaces_views_handler_operator_spacetype Views filter options handler that provides the options array for the filter view value dropdown.
spaces_views_style_plugins hook_views_style_plugins
spaces_views_tables Implementation of hook_views_tables(). Adds a meta-filter that provides a layer of abstraction that delegates actual filtering to the implementing space type modules.
theme_datetime_view_style_item
theme_spaces_datetitle_view_style
_spaces_views_empty Function that handles a variety of tasks needed when view is empty
_spaces_views_get_field retrieves the field id of the $op type provided