You are here

user_relationships_hooks.inc in User Relationships 5.2

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

File

user_relationships_hooks.inc
View source
<?php

/**
 * User Relationships hook implementations
 */

/**
 * Help
 */
function user_relationships_help($section) {
  switch ($section) {
    case 'admin/help#user_relationships':
      $output = '<p>' . t('This module allows you to create relationship types that users can use to connect to each other.') . '</p>';
      return $output;
    case 'admin/user/relationships':
      $output = '<p>' . t('This page lets you setup user relationship types.') . '</p>';
      return $output;
  }
}

/**
 * Perm
 */
function user_relationships_perm() {
  return array(
    'administer user relationships',
    'maintain relationships',
    'view user relationships',
    'can have relationship',
  );
}

/**
 * Menu
*/
function user_relationships_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/user/relationships',
      'title' => t('Relationships'),
      'description' => t('Create relationship types'),
      'access' => user_access('administer user relationships'),
      'callback' => 'user_relationships_types_list_page',
    );
    $items[] = array(
      'path' => 'admin/user/relationships/list',
      'title' => t('List'),
      'callback' => 'user_relationships_types_list_page',
      'access' => user_access('administer user relationships'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/user/relationships/add',
      'title' => t('Add type'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'user_relationships_type_edit',
      ),
      'access' => user_access('administer user relationships'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
    );
    $items[] = array(
      'path' => 'admin/user/relationships/edit',
      'title' => t('Edit type'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'user_relationships_type_edit',
      ),
      'access' => user_access('administer user relationships'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/user/relationships/delete',
      'title' => t('Delete type'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'user_relationships_type_delete',
      ),
      'access' => user_access('administer user relationships'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/user/relationships/settings',
      'title' => t('Settings'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'user_relationships_settings',
      ),
      'access' => user_access('administer user relationships'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 2,
    );
    $items[] = array(
      'path' => 'relationship_types/autocomplete',
      'title' => t('User Relationships Autocomplete'),
      'callback' => '_user_relationships_autocomplete_types',
      'access' => user_access('administer user relationships'),
      'type' => MENU_CALLBACK,
    );
  }
  else {
    global $user;
    $id = is_numeric(arg(1)) ? arg(1) : $user->uid;
    $edit_access = $id == $user->uid && user_access('maintain relationships') || user_access('administer users');
    $view_access = $id == $user->uid && user_access('maintain relationships') || user_access('view user relationships');
    $items[] = array(
      'path' => 'relationships',
      'title' => t('My relationships'),
      'access' => $id && user_access('maintain relationships'),
      'type' => MENU_NORMAL_ITEM,
      'callback' => 'theme',
      'callback arguments' => array(
        'user_relationships_page',
        $user->uid,
      ),
    );
    $items[] = array(
      'path' => "relationships/list",
      'title' => t('All'),
      'access' => $view_access && $user->uid,
      'type' => MENU_NORMAL_ITEM | MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    if (true || $approval_required && $edit_access) {
      $items[] = array(
        'path' => "relationships/requests",
        'title' => t('Pending'),
        'access' => $edit_access,
        'callback' => 'theme',
        'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK,
        'weight' => -9,
        'callback arguments' => array(
          'user_relationships_pending_requests_page',
          $user->uid,
        ),
      );
    }
    if (arg(0) == 'relationships' && is_numeric(arg(1))) {
      $relationships = user_relationships_types_load();
      foreach ($relationships as $rtid => $relationship) {
        $items[] = array(
          'path' => "relationships/{$rtid}",
          'title' => t($relationship->name),
          'access' => $view_access,
          'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK,
          'callback' => 'theme',
          'callback arguments' => array(
            'user_relationships_page',
            $id,
            $rtid,
          ),
        );
      }
    }
    $items[] = array(
      'path' => "relationships/remove",
      'title' => t('Remove relationship'),
      'access' => $edit_access,
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'user_relationships_remove',
        $id,
        arg(2),
      ),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => "relationship/request",
      'title' => t('Create a relationship'),
      'access' => $edit_access,
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'user_relationships_request',
        arg(2),
      ),
      'type' => MENU_CALLBACK,
    );
    if (arg(0) == 'user' && $id && arg(2) == 'relationships') {
      if ($id != $user->uid) {
        $access = user_access('administer user relationships') || user_access('administer users');
        $items[] = array(
          'path' => "user/{$id}/relationships",
          'title' => t('Relationships'),
          'access' => $access,
          'callback' => 'theme',
          'callback arguments' => array(
            'user_relationships_page',
            $id,
          ),
        );
        $items[] = array(
          'path' => "user/{$id}/relationships/list",
          'title' => t('All'),
          'access' => $access,
          'type' => MENU_NORMAL_ITEM | MENU_DEFAULT_LOCAL_TASK,
          'weight' => -10,
        );
        if (true || $approval_required && $edit_access) {
          $items[] = array(
            'path' => "user/{$id}/relationships/requests",
            'title' => t('Pending'),
            'access' => $access,
            'callback' => 'theme',
            'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK,
            'weight' => -9,
            'callback arguments' => array(
              'user_relationships_pending_requests_page',
              $id,
            ),
          );
        }
        if (arg(0) == 'user' && arg(2) == 'relationships' && is_numeric(arg(3))) {
          $relationships = user_relationships_types_load();
          foreach ($relationships as $rtid => $relationship) {
            $items[] = array(
              'path' => "user/{$id}/relationships/{$rtid}",
              'title' => t($relationship->name),
              'access' => $access,
              'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK,
              'callback' => 'theme',
              'callback arguments' => array(
                'user_relationships_page',
                $id,
                $rtid,
              ),
            );
          }
        }
      }
      else {
        if ($_REQUEST['destination']) {
          $query = "destination={$_REQUEST['destination']}";
          unset($_REQUEST['destination']);
        }
        $items[] = array(
          'path' => "user/{$id}/relationships",
          'type' => MENU_CALLBACK,
          'access' => TRUE,
          'callback' => 'drupal_goto',
          'callback arguments' => array(
            str_replace("user/{$id}/", '', $_GET['q']),
            $query,
          ),
        );
      }
      if (in_array(arg(4), array(
        'approve',
        'disapprove',
        'cancel',
      ))) {
        $items[] = array(
          'path' => "user/{$id}/relationships/requested",
          'title' => t('Approve Relationship'),
          'access' => $access || $id == $user->uid,
          'type' => MENU_CALLBACK,
          'callback' => 'drupal_get_form',
          'callback arguments' => array(
            'user_relationships_pending_requested',
            arg(4),
            $id,
            arg(5),
          ),
        );
      }
      $items[] = array(
        'path' => "user/{$id}/relationships/remove",
        'title' => t('Remove relationship'),
        'access' => $access || $id == $user->uid,
        'callback' => 'drupal_get_form',
        'callback arguments' => array(
          'user_relationships_remove',
          $id,
          arg(4),
        ),
        'type' => MENU_CALLBACK,
      );
    }
  }
  return $items;
}

/**
 * User
 */
function user_relationships_user($type, &$edit, &$account, $category = NULL) {
  switch ($type) {
    case 'login':
      if (user_access('maintain relationships')) {
        _user_relationships_set_notifications($account);
      }
      break;
    case 'view':
      global $user;
      $viewer =& $user;
      if ($account->uid == $user->uid && user_access('maintain relationships')) {
        _user_relationships_set_notifications($account);
        break;
      }
      $output = array();
      if ($list = _user_relationships_between($viewer, $account)) {
        $output[] = array(
          'title' => t('Your relationships to this user'),
          'value' => theme('item_list', $list),
          'class' => 'user_relationships',
        );
      }
      if ($actions = _user_relationships_actions_between($viewer, $account)) {
        $output[] = array(
          'title' => t('Relationship actions'),
          'value' => theme('item_list', $actions),
          'class' => 'user_relationships_actions',
        );
      }
      if (sizeof($output)) {
        return array(
          t('Relationships') => $output,
        );
      }
      break;
    case 'delete':
      db_query("DELETE FROM {user_relationships} WHERE requester_id = %d OR requestee_id = %d", $account->uid, $account->uid);
      break;
    case 'form':
      if ($category == 'account' && variable_get('user_relationships_require_approval', TRUE) && user_access('maintain relationships', $account)) {
        $form['user_relationships_settings'] = array(
          '#type' => 'fieldset',
          '#title' => t('Relationship settings'),
          '#weight' => 5,
        );
        if (variable_get('user_relationships_allow_auto_approve', FALSE) && ($relationships = user_relationships_types_load())) {
          $options = array();
          foreach ($relationships as $relationship) {
            if ($relationship->requires_approval) {
              $options[$relationship->rtid] = $relationship->name;
            }
          }
          $form['user_relationships_settings']['user_relationships_auto_approve'] = array(
            '#type' => 'checkboxes',
            '#title' => t('Automatically approve the following relationship requests'),
            '#options' => $options,
            '#default_value' => $edit['user_relationships_auto_approve'],
            '#description' => t("Check off the types of relationships you'd like to automatically approve."),
          );
        }
      }

      // No options have been set so don't display it
      if (sizeof($form['user_relationships_settings']) == 3) {
        unset($form['user_relationships_settings']);
      }
      return $form;
  }
}

/**
 * Cron
 */
function user_relationships_cron() {
  $now = time();

  // only expire relationships once a day
  $last_cron = variable_get('user_relationships_last_expire', 0);
  if ($now < $last_cron + 86400) {
    return FALSE;
  }
  $result = db_query(" SELECT ur.rid\n      FROM {user_relationships} ur\n        INNER JOIN {user_relationship_types} urt ON ur.rtid = urt.rtid\n        WHERE ur.approved = 0\n          AND urt.expires_val > 0\n        AND ur.updated_at < (%d - (urt.expires_val * 86400))\n      GROUP BY ur.rid", $now);
  $expired_requests = array();
  while ($request = db_fetch_object($result)) {
    $expired_requests[$request->rid] = $request;
  }
  if ($expired_requests) {
    $placeholders = implode(', ', array_fill(0, count($expired_requests), '%d'));
    db_query('DELETE FROM {user_relationships} WHERE rid IN (' . $placeholders . ')', $expired_requests);
    _user_relationships_invoke('delete', $expired_requests);
  }

  // remember when we last expired relationships
  variable_set('user_relationships_last_expire', $now);
  return TRUE;
}