You are here

user_relationship_views.module in User Relationships 5.2

File

plugins/user_relationship_views/user_relationship_views.module
View source
<?php

/**
 * views_tables_hook()
 */
function user_relationship_views_views_tables() {
  $tables['user_relationships_users'] = array(
    'name' => 'users',
    'join' => array(
      'left' => array(
        'table' => 'user_relationships',
        'field' => 'requester_id',
      ),
      'right' => array(
        'field' => 'uid',
      ),
    ),
  );
  $tables['user_relationships'] = array(
    'name' => 'user_relationships',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'uid',
      ),
      'right' => array(
        'field' => 'requestee_id',
      ),
    ),
    'fields' => array(
      'author_relationships_to_current_user' => array(
        'name' => t("User Relationships: Author's Relationships to Current User"),
        'handler' => 'user_relationship_views_relationships_field_handler',
        'option' => array(
          '#type' => 'select',
          '#options' => array(
            'any' => t('Any'),
            1 => t('Only Approved'),
            0 => t('Only Non-Approved'),
          ),
        ),
        'order' => array(
          'requestee_id',
          'requester_id',
        ),
        'notafield' => TRUE,
        'help' => t('This will display the relationships between the author and the current user.'),
      ),
      'current_user_relationships_to_author' => array(
        'name' => t("User Relationships: Current User's Relationships to Author"),
        'handler' => 'user_relationship_views_relationships_field_handler',
        'option' => array(
          '#type' => 'select',
          '#options' => array(
            'any' => t('Any'),
            1 => t('Only Approved'),
            0 => t('Only Non-Approved'),
          ),
        ),
        'order' => array(
          'requester_id',
          'requestee_id',
        ),
        'notafield' => TRUE,
        'help' => t('This will display the relationships between the current user and the author.'),
      ),
      'number_relationships_of_author' => array(
        'name' => t("User Relationships: Number of Relationships held by Particular User"),
        'handler' => 'user_relationship_views_number_relationships_field_handler',
        'option' => array(
          '#type' => 'select',
          '#options' => array(
            'user' => t('Any'),
            'requester_id' => t('User is Requestee'),
            'requestee_id' => t('User is Requester'),
          ),
        ),
        'notafield' => TRUE,
        'help' => t('This will display the total number of relationships held by a particular user.'),
      ),
      'approved' => array(
        'name' => t('User Relationships: Approval Status'),
        'handler' => 'user_relationship_views_approved_field_handler',
        'sortable' => TRUE,
        'help' => t("Display the approval state of the relationship (really only useful if you've filtered by a relationship)."),
      ),
      'created_at' => array(
        'name' => t('User Relationships: Relationship Creation Date'),
        'handler' => views_handler_field_dates(),
        'sortable' => TRUE,
        'help' => t("Display the date the relationship was created (really only useful if you've filtered by a relationship)."),
      ),
    ),
    'filters' => array(
      'username_rtid' => array(
        'field' => 'rtid',
        'name' => t("User Relationships: Author's relationship with Username"),
        'help' => t('Allows you to filter by how the author relates to the user (provided in "options").'),
        'handler' => 'user_relationship_views_username_rtid_filter_handler',
        'operator' => 'user_relationship_views_handler_operator_eqneq',
        'value' => array(
          '#type' => 'select',
          '#options' => 'user_relationship_views_relationship_types',
        ),
        'option' => array(
          '#type' => 'textfield',
          '#size' => 20,
          '#autocomplete_path' => 'user/autocomplete',
        ),
      ),
      'current_user_rtid' => array(
        'field' => 'rtid',
        'name' => t("User Relationships: Author's relationship with the Current User"),
        'help' => t("Allows you to filter by the author's relationship to the currently logged in user."),
        'handler' => 'user_relationship_views_current_user_rtid_filter_handler',
        'operator' => 'user_relationship_views_handler_operator_eqneq',
        'value' => array(
          '#type' => 'select',
          '#options' => 'user_relationship_views_relationship_types',
        ),
        'cacheable' => 'no',
      ),
    ),
  );
  return $tables;
}

/**
 * Custom equality/inequality handler to provide approval status
 */
function user_relationship_views_handler_operator_eqneq() {
  return array(
    '=' => t('Is'),
    '=|1' => t('Is (Approved)'),
    '=|0' => t('Is (Not Approved)'),
    '!=' => t('Is Not'),
    '!=|1' => t('Is Not (Approved)'),
    '!=|0' => t('Is Not (Not Approved)'),
  );
}

/**
 * Options list for relationship types
 */
function user_relationship_views_relationship_types() {
  $types = user_relationships_types_load();
  foreach ($types as $rtid => $type) {
    $options[$rtid] = $type->name;
  }
  return $options;
}

/**
 * default handler for filters
 *
 * pulls apart and process data from "user_relationship_views_handler_operator_eqneq" then passes the rest into
 * the default views filter handler
 */
function user_relationship_views_default_filter_handler($op, $filter_data, $filter_info, &$query) {

  // this is just a fun way to pull 'approved' or 'not approved' out of the operator argument
  list($filter_data['operator'], $approved) = explode('|', $filter_data['operator']);
  views_handler_filter_default($op, $filter_data, $filter_info, $query);
  if (isset($approved)) {
    $query
      ->ensure_table('user_relationships');
    $query
      ->add_where('user_relationships.approved = %d', $approved);
  }
}

/**
 * handler for filtering by author's relation to a specified user
 */
function user_relationship_views_username_rtid_filter_handler($op, $filter_data, $filter_info, &$query) {
  user_relationship_views_default_filter_handler($op, $filter_data, $filter_info, $query);
  $query
    ->ensure_table('user_relationships_users');
  $query
    ->add_where("user_relationships_users.name = '%s'", $filter_data['options']);
}

/**
 * handler for filtering by author's relation to the current user
 */
function user_relationship_views_current_user_rtid_filter_handler($op, $filter_data, $filter_info, &$query) {
  global $user;
  user_relationship_views_default_filter_handler($op, $filter_data, $filter_info, $query);
  $query
    ->ensure_table('user_relationships_users');
  $query
    ->add_where("user_relationships_users.uid = %d", intval($user->uid));
}

/**
 * handler for showing the approval status in a field
 */
function user_relationship_views_approved_field_handler($field_info, $field_data, $value, $data) {
  return theme('user_relationships_approval_status', $value);
}

/**
 * handler for displaying the number of relationships held by an author 
 */
function user_relationship_views_number_relationships_field_handler($field_info, $field_data, $value, $data) {
  $node = node_load($data->nid);
  return user_relationships_load(array(
    $field_data['options'] => $node->uid,
  ), TRUE);
}

/**
 * handler for displaying how the author relates to the current user
 * or how the current user relates to the author
 */
function user_relationship_views_relationships_field_handler($field_info, $field_data, $value, $data) {
  global $user;
  $node = node_load($data->nid);
  $order = $field_info['order'];
  $approved = $field_data['options'] != 'any' ? $field_data['options'] : NULL;
  $relationships = user_relationships_load(array(
    $order[0] => $user->uid,
    $order[1] => $node->uid,
    'approved' => $approved,
  ));
  foreach ($relationships as $relationship) {
    $relationship->type = user_relationships_type_load($relationship->rtid);
  }
  return theme('user_relationship_views_relationships_field_handler', $relationships, $field_data['options']);
}

/**
 * theme function for displaying the list of relationship types (called from the relationships field handler)
 */
function theme_user_relationship_views_relationships_field_handler($relationships, $approved) {
  $show_status = $approved == 'any';
  $out = array();
  foreach ($relationships as $relationship) {
    $out[] = $relationship->type->name . ($show_status ? ' (' . theme('user_relationships_approval_status', $relationship->approved) . ')' : '');
  }
  return implode('<br />', $out);
}

/**
 * views_arguments_hook()
 */
function user_relationship_views_views_arguments() {
  return array(
    'ur_uid' => array(
      'name' => t('User Relationships: UID is related to Author'),
      'handler' => 'user_relationship_views_arg_handler',
      'help' => t('The User ID argument allows users to filter to nodes authored by users related to the specified user ID. ' . 'Use the option to filter to a particular relationship type.'),
    ),
    'ur_rtid' => array(
      'name' => t('User Relationships: Author is related to UID through RTID'),
      'handler' => 'user_relationship_views_arg_handler',
      'help' => t('The Relationship Type ID allows users to filter to nodes whose author is related to the specified user ID ' . 'through the relationship type ID specified in the argument. This should be used in conjunction with ' . '"UID is related to Author"'),
    ),
    'ur_approved' => array(
      'name' => t('User Relationships: UID to Author is (Non-)Approved'),
      'handler' => 'user_relationship_views_arg_handler',
      'help' => t('The Approved argument allows users to filter to nodes where the relationship between the specified UID ' . 'and the Author is approved (1) or not approved (0)'),
    ),
  );
}

/**
 * handler for views argument
 */
function user_relationship_views_arg_handler($op, &$query, $arg_type, $arg = '') {
  switch ($op) {
    case 'filter':
      $query
        ->ensure_table('user_relationships', true);
      $arg = intval($arg);
      switch ($arg_type['type']) {
        case 'ur_uid':
          $field = 'requester_id';
          break;
        case 'ur_rtid':
          $field = 'rtid';
          break;
        case 'ur_approved':
          $field = 'approved';
          break;
      }
      if (isset($field)) {
        $query
          ->add_where('user_relationships.%s = %d', $field, $arg);
      }
      break;
    case 'title':
      switch ($arg_type) {
        case 'ur_uid':
          $title = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $query));
          break;
        case 'ur_rtid':
          $title = db_result(db_query("SELECT plural_name FROM {user_relationship_types} WHERE rtid = %d", $query));
          break;
        case 'ur_approved':
          $title = theme('user_relationships_approval_status', $query);
          break;
      }
      return check_plain($title);
  }
}

/*
* Generate the list of options for the views argument

function theme_user_relationship_views_arguments_list($element) {
 die("<pre>".print_r(func_get_args(), true)."</pre>");
 $list  = array('' => t('-Any-'));
 $types = user_relationship_views_relationship_types();
 foreach ($types as $rtid => $type) {
   $list[$rtid] = $type;
   $list["{$rtid}|1"] = t('@type (Approved)', array('@type' => $type));
   $list["{$rtid}|0"] = t('@type (Non-Approved)', array('@type' => $type));
 }

 $element['#type'] = 'select';
 $element['#options'] = $list;

 return drupal_render($element);
}

function user_relationship_views_arguments_list_process($element) {
 return $element;
 die("<pre>".print_r(func_get_args(), true)."</pre>");
}

function user_relationship_views_arguments_list_after_build($element) {
 return $element;
 die("<pre>".print_r(func_get_args(), true)."</pre>");
}
*/

Functions

Namesort descending Description
theme_user_relationship_views_relationships_field_handler theme function for displaying the list of relationship types (called from the relationships field handler)
user_relationship_views_approved_field_handler handler for showing the approval status in a field
user_relationship_views_arg_handler handler for views argument
user_relationship_views_current_user_rtid_filter_handler handler for filtering by author's relation to the current user
user_relationship_views_default_filter_handler default handler for filters
user_relationship_views_handler_operator_eqneq Custom equality/inequality handler to provide approval status
user_relationship_views_number_relationships_field_handler handler for displaying the number of relationships held by an author
user_relationship_views_relationships_field_handler handler for displaying how the author relates to the current user or how the current user relates to the author
user_relationship_views_relationship_types Options list for relationship types
user_relationship_views_username_rtid_filter_handler handler for filtering by author's relation to a specified user
user_relationship_views_views_arguments views_arguments_hook()
user_relationship_views_views_tables views_tables_hook()