You are here

user_relationship_direct.module in User Relationships 5.2

File

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

function user_relationship_direct_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/user/relationships/direct',
      'title' => t('Direct relationship'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'user_relationship_direct_form',
      ),
      'access' => user_access('administer user relationships'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 4,
    );
  }
  return $items;
}
function user_relationship_direct_form() {
  $relationships = user_relationships_types_load();
  $form['user_relationship_direct'] = array(
    '#title' => t('Use direct relationship urls instead of the standard select form'),
    '#type' => 'checkbox',
    '#default_value' => (bool) variable_get('user_relationship_direct', FALSE),
  );
  $form['direct'] = array(
    '#type' => 'fieldset',
    '#title' => '',
    '#description' => t('Fill in titles for each direct relationship confirmation page.<br/> You may the following tokens: !tokens', array(
      '!tokens' => '!requestee_name',
    )),
  );
  foreach ($relationships as $relationship) {
    $form['direct']['user_relationship_direct_rtid_' . $relationship->rtid] = array(
      '#title' => $relationship->name,
      '#type' => 'textfield',
      '#default_value' => variable_get('user_relationship_direct_rtid_' . $relationship->rtid, t('Are you sure you want to setup a !type relationship with !requestee_name?', array(
        '!type' => $relationship->name,
      ))),
      '#required' => TRUE,
      '#description' => t('For development purposes: <br/>use the direct path: %path <br/>or the php function: %function', array(
        '%path' => user_relationship_direct_path('[uid]', $relationship->name),
        '%function' => "user_relationship_direct_path('[uid]', '{$relationship->name}');",
      )),
    );
  }
  return system_settings_form($form);
}
function user_relationship_direct_path($uid, $relation) {
  $relationships = user_relationships_types_load();
  foreach ($relationships as $relationship) {
    if ($relationship->name == $relation) {
      return 'relationship/request/' . $uid . '/' . $relationship->rtid;
    }
  }
}
function user_relationship_direct_form_alter($form_id, &$form) {
  if ($form_id == 'user_relationships_request' && variable_get('user_relationship_direct', FALSE)) {
    $rtid = arg(3);
    if (array_key_exists($rtid, $form['rtid']['#options'])) {
      $form['rtid']['#type'] = 'hidden';
      $form['rtid']['#default_value'] = $rtid;
      $form['actions']['submit']['#value'] = t('Yes');
      $tokens = array(
        '!requestee_name' => $form['requestee']['#value']->name,
      );
      $title = variable_get('user_relationship_direct_rtid_' . $rtid, t('Are you sure you want to setup a !type relationship with !requestee_name?', array(
        '!type' => $form['rtid']['#options'][$rtid],
      )));
      drupal_set_title(t($title, $tokens));
    }
  }
}

/**
 * Make links to create all outstanding relationships to a user
 *
 * @param $account user to create relationships to
 * @return array of themed create links
 */
function user_relationships_direct_create_relationship_links(&$account) {
  global $user;
  $result = array();
  $all_relationships = user_relationships_types_load();

  //load relationships to this user
  $existing = user_relationships_load(array(
    'requester_id' => $user->uid,
    'requestee_id' => $account->uid,
  ));

  //re-key on rtid
  $existing_relationships = array();
  foreach ($existing as $rel) {
    $existing_relationships[$rel->rtid] = $rel;
  }
  foreach ($all_relationships as $rtid => $relationship) {
    if (isset($existing_relationships[$rtid])) {
      continue;
    }
    $result[] = theme('user_relationships_request_relationship_link', $account, $relationship);
  }
  return $result;
}