user_relationship_defaults.module in User Relationships 7
Same filename and directory in other branches
Drupal Module: User Relationship Defaults
@author Jeff Smick (creator) @author Alex Karshakevich (maintainer) http://drupal.org/user/183217
Automatically create a relationship when a new user joins
File
user_relationship_defaults/user_relationship_defaults.moduleView source
<?php
/**
* Drupal Module: User Relationship Defaults
*
* @author Jeff Smick (creator)
* @author Alex Karshakevich (maintainer) http://drupal.org/user/183217
* @file
* Automatically create a relationship when a new user joins
*/
/**
* Public API for retrieving a default relationship.
*
* @param $param
* The rdid or an associative array of attributes to search for in selecting the
* default, such as user_name or name. Attributes must match column names
* in the user_relationship_defaults or user_relationship_types table.
*
* @return
* object of the default relationship with the user object and relationship_type object
*
*/
function user_relationship_default_load($param = array()) {
$defaults = user_relationship_defaults_load();
if (is_numeric($param)) {
return $defaults[$param];
}
foreach ($defaults as $default) {
$found = TRUE;
foreach ($param as $column => $value) {
$column = drupal_strtolower($column);
if ($column == 'name' || $column == 'plural_name') {
$value = drupal_strtolower($value);
$col_val = drupal_strtolower($default->{$column});
}
else {
$col_val = $default->{$column};
}
// mismatch, move to the next type
if ($col_val != $value) {
$found = FALSE;
break;
}
}
if ($found) {
return $type;
}
}
}
/**
* Public API to retrieve the full list of default relationships.
*
* @return
* array of default relationship objects
*/
function user_relationship_defaults_load($reset = FALSE) {
static $defaults = array();
if ($reset || !$defaults) {
$result = db_query("SELECT * FROM {user_relationship_defaults}");
foreach ($result as $default) {
$default->user = user_load($default->uid);
$default->user_name = $default->user->name;
$default->relationship_type = user_relationships_type_load($default->rtid);
// Ignore default if relationship type does not exist.
if (empty($default->relationship_type)) {
continue;
}
foreach ((array) $default->relationship_type as $column => $value) {
$default->{$column} = $value;
}
$defaults[$default->rdid] = $default;
}
}
return $defaults;
}
/**
* Implements hook_menu().
*/
function user_relationship_defaults_menu() {
$items = array();
$items['admin/config/people/relationships/defaults/add'] = array(
'title' => 'Add default relationship',
'access arguments' => array(
'administer user relationships',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'user_relationship_defaults_add_form',
),
'type' => MENU_LOCAL_ACTION,
'weight' => 2,
);
$items['admin/config/people/relationships/defaults/%user_relationship_default/delete'] = array(
'title' => 'Delete default',
'access arguments' => array(
'administer user relationships',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'user_relationship_defaults_delete_form',
5,
),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Add new default relationship form.
*/
function user_relationship_defaults_add_form($form, &$form_state) {
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#maxlength' => 255,
'#default_value' => isset($form_state['username']) ? $form_state['username'] : NULL,
'#description' => t('Start typing the name of a user to automatically create a relationship to'),
'#required' => TRUE,
'#autocomplete_path' => 'user/autocomplete',
);
$form['relationship_type_name'] = array(
'#type' => 'textfield',
'#title' => t('Relationship'),
'#maxlength' => 255,
'#default_value' => isset($form_state['relationship_type_name']) ? $form_state['relationship_type_name'] : NULL,
'#description' => t('Start typing the name of a relationship to use'),
'#required' => TRUE,
'#autocomplete_path' => 'admin/user_relationships/autocomplete/types',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Validate the default relationship add page form submission.
*/
function user_relationship_defaults_add_form_validate($form, &$form_state) {
$users = user_load_multiple(array(), array(
'name' => $form_state['values']['username'],
));
if (!empty($form_state['values']['username']) && empty($users)) {
form_set_error('username', t("You must enter an existing user's name."));
}
if (!empty($form_state['values']['relationship_type_name'])) {
if (!user_relationships_type_load(array(
'name' => $form_state['values']['relationship_type_name'],
))) {
form_set_error('relationship_type_name', t("You must enter an existing relationship's name."));
}
}
}
/**
* Process default relationship add form submission.
*/
function user_relationship_defaults_add_form_submit($form, &$form_state) {
$users = user_load_multiple(array(), array(
'name' => $form_state['values']['username'],
));
$user = array_shift($users);
$relationship_type = user_relationships_type_load(array(
'name' => $form_state['values']['relationship_type_name'],
));
$new_default = array(
'uid' => $user->uid,
'rtid' => $relationship_type->rtid,
);
drupal_write_record('user_relationship_defaults', $new_default);
$message_p = array(
'%username' => $user->name,
'%relationship_name' => user_relationships_type_get_name($relationship_type),
);
drupal_set_message(t('Default relationship %relationship_name of %username has been added.', $message_p));
watchdog('u_relationship', 'Default relationship %relationship_name of %username has been added.', $message_p, WATCHDOG_NOTICE, l(t('view'), 'admin/config/people/relationships'));
$form_state['redirect'] = 'admin/config/people/relationships';
}
/**
* Default Relationship delete confirmation.
*/
function user_relationship_defaults_delete_form($form, &$form_state, $default_relationship) {
$form['rdid'] = array(
'#type' => 'value',
'#value' => $default_relationship->rdid,
);
$form = confirm_form($form, t('Are you sure you want to delete the default relationship %rel_name of %name?', array(
'%name' => $default_relationship->user->name,
) + user_relationships_type_translations($default_relationship)), 'admin/config/people/relationships', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
return $form;
}
/**
* Process default relationship delete form submission.
*/
function user_relationship_defaults_delete_form_submit($form, &$form_state) {
$default_relationship = user_relationship_default_load($form_state['values']['rdid']);
db_delete('user_relationship_defaults')
->condition('rdid', $default_relationship->rdid)
->execute();
$message_p = array(
'%name' => $default_relationship->user_name,
'%relationship' => user_relationships_type_get_name($default_relationship),
);
drupal_set_message(t('Default relationship %relationship of %name has been deleted.', $message_p));
watchdog('u_relationship', 'Default relationship %relationship of %name has been deleted.', $message_p, WATCHDOG_NOTICE);
user_relationship_defaults_load(TRUE);
$form_state['redirect'] = 'admin/config/people/relationships';
}
/**
* Implements hook_user_insert().
*/
function user_relationship_defaults_user_insert(&$edit, $account) {
if ($account->status) {
_user_relationship_defaults_create_default_relationships($account);
}
}
/**
* Implements hook_user_update().
*/
function user_relationship_defaults_user_update(&$edit, $account) {
if ($account->status && !$account->login) {
_user_relationship_defaults_create_default_relationships($account);
}
}
/**
* Implements hook_user_delete().
*/
function user_relationship_defaults_user_delete($account) {
db_delete('user_relationship_defaults')
->condition('uid', $account->uid)
->execute();
}
function _user_relationship_defaults_create_default_relationships($account) {
//note that if a new account is approved, then blocked and unblocked before the user logged in once,
//it'll attempt to create default relationships again (there won't be any errors if they already exist, though)
$defaults = user_relationship_defaults_load();
foreach ($defaults as $default) {
user_relationships_request_relationship($default->user, $account, $default->relationship_type, TRUE);
}
}
/**
* hook_user_relationships_admin_page_alter()
*/
function user_relationship_defaults_user_relationships_types_list_alter(&$page) {
$defaults = user_relationship_defaults_load();
$default_rows = array();
foreach ($defaults as $default) {
$default_rows[] = array(
check_plain(user_relationships_type_get_name($default->relationship_type)),
theme('username', array(
'account' => $default->user,
)),
l(t('delete'), "admin/config/people/relationships/defaults/{$default->rdid}/delete"),
);
}
$page['defaults'] = array(
'#type' => 'fieldset',
'#title' => t('Default Relationships'),
'#weight' => 2,
);
$page['defaults']['list'] = array(
'#theme' => 'table',
'#header' => array(
t('Relationship type'),
t('User'),
t('Operations'),
),
'#rows' => $default_rows,
'#empty' => t('No default relationships available.'),
);
}
/**
* hook_user_relationships()
*/
function user_relationship_defaults_user_relationships_type_delete($relationship_type) {
db_delete('user_relationship_defaults')
->condition('rtid', $relationship_type->rtid)
->execute();
}