user_relationship_defaults.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/user_relationships/user_relationship_defaults/user_relationship_defaults.module

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

Functions & methods

NameDescription
user_relationship_defaults_add_formAdd new default relationship form
user_relationship_defaults_add_form_submitProcess default relationship add form submission.
user_relationship_defaults_add_form_validateValidate the default relationship add page form submission.
user_relationship_defaults_delete_formDefault Relationship delete confirmation.
user_relationship_defaults_delete_form_submitProcess default relationship delete form submission.
user_relationship_defaults_loadPublic API to retrieve the full list of default relationships
user_relationship_defaults_menuhook_menu()
user_relationship_defaults_userhook_user()
user_relationship_defaults_user_relationshipshook_user_relationships()
user_relationship_defaults_user_relationships_page_alterhook_user_relationships_admin_page_alter()
user_relationship_default_loadPublic API for retrieving a default relationship.

File

View source
  1. <?php
  2. /**
  3. * Drupal Module: User Relationship Defaults
  4. *
  5. * @author Jeff Smick (creator)
  6. * @author Alex Karshakevich (maintainer) http://drupal.org/user/183217
  7. * @file
  8. * Automatically create a relationship when a new user joins
  9. */
  10. /**
  11. * Public API for retrieving a default relationship.
  12. *
  13. * @param $param
  14. * The rdid or an associative array of attributes to search for in selecting the
  15. * default, such as user_name or name. Attributes must match column names
  16. * in the user_relationship_defaults or user_relationship_types table.
  17. *
  18. * @return
  19. * object of the default relationship with the user object and relationship_type object
  20. *
  21. */
  22. function user_relationship_default_load($param = array()) {
  23. $defaults = user_relationship_defaults_load();
  24. if (is_numeric($param)) {
  25. return $defaults[$param];
  26. }
  27. foreach ($defaults as $default) {
  28. $found = TRUE;
  29. foreach ($param as $column => $value) {
  30. $column = strtolower($column);
  31. if ($column == 'name' || $column == 'plural_name') {
  32. $value = strtolower($value);
  33. $col_val = strtolower($default->$column);
  34. }
  35. else {
  36. $col_val = $default->$column;
  37. }
  38. // mismatch, move to the next type
  39. if ($col_val != $value) {
  40. $found = FALSE;
  41. break;
  42. }
  43. }
  44. if ($found) {
  45. return $type;
  46. }
  47. }
  48. }
  49. /**
  50. * Public API to retrieve the full list of default relationships
  51. *
  52. * @return
  53. * array of default relationship objects
  54. */
  55. function user_relationship_defaults_load($reset = FALSE) {
  56. static $defaults = array();
  57. if ($reset || !$defaults) {
  58. $results = db_query("SELECT * FROM {user_relationship_defaults}");
  59. while ($default = db_fetch_object($results)) {
  60. $default->user = user_load($default->uid);
  61. $default->user_name = $default->user->name;
  62. $default->relationship_type = user_relationships_type_load($default->rtid);
  63. foreach ((array)$default->relationship_type as $column => $value) {
  64. $default->$column = $value;
  65. }
  66. $defaults[$default->rdid] = $default;
  67. }
  68. }
  69. return $defaults;
  70. }
  71. /**
  72. * hook_menu()
  73. */
  74. function user_relationship_defaults_menu() {
  75. $items = array();
  76. $items['admin/user/relationships/defaults/add'] = array(
  77. 'title' => 'Add default',
  78. 'access arguments' => array('administer user relationships'),
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('user_relationship_defaults_add_form'),
  81. 'type' => MENU_LOCAL_TASK,
  82. 'weight' => 2
  83. );
  84. $items['admin/user/relationships/defaults/%user_relationship_default/delete'] = array(
  85. 'title' => 'Delete default',
  86. 'access arguments' => array('administer user relationships'),
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('user_relationship_defaults_delete_form', 4),
  89. 'type' => MENU_CALLBACK,
  90. );
  91. return $items;
  92. }
  93. /**
  94. * Add new default relationship form
  95. */
  96. function user_relationship_defaults_add_form(&$form_state) {
  97. $form['username'] = array(
  98. '#type' => 'textfield',
  99. '#title' => t('Username'),
  100. '#maxlength' => 255,
  101. '#default_value' => isset($form_state['username']) ? $form_state['username'] : NULL,
  102. '#description' => t('Start typing the name of a user to automatically create a relationship to'),
  103. '#required' => TRUE,
  104. '#autocomplete_path' => 'user/autocomplete',
  105. );
  106. $form['relationship_type_name'] = array(
  107. '#type' => 'textfield',
  108. '#title' => t('Relationship'),
  109. '#maxlength' => 255,
  110. '#default_value' => isset($form_state['relationship_type_name']) ? $form_state['relationship_type_name'] : NULL,
  111. '#description' => t('Start typing the name of a relationship to use'),
  112. '#required' => TRUE,
  113. '#autocomplete_path' => 'relationship_types/autocomplete',
  114. );
  115. $form['submit'] = array(
  116. '#type' => 'submit',
  117. '#value' => t('Submit'),
  118. );
  119. return $form;
  120. }
  121. /**
  122. * Validate the default relationship add page form submission.
  123. */
  124. function user_relationship_defaults_add_form_validate($form, &$form_state) {
  125. if (!empty($form_state['values']['username']) && !user_load(array('name' => $form_state['values']['username']))) {
  126. form_set_error('username', t("You must enter an existing user's name."));
  127. }
  128. if (!empty($form_state['values']['relationship_type_name'])) {
  129. if (!user_relationships_type_load(array('name' => $form_state['values']['relationship_type_name']))) {
  130. form_set_error('relationship_type_name', t("You must enter an existing relationship's name."));
  131. }
  132. }
  133. }
  134. /**
  135. * Process default relationship add form submission.
  136. */
  137. function user_relationship_defaults_add_form_submit($form, &$form_state) {
  138. $user = user_load(array('name' => $form_state['values']['username']));
  139. $relationship_type = user_relationships_type_load(array('name' => $form_state['values']['relationship_type_name']));
  140. $new_default = array(
  141. 'uid' => $user->uid,
  142. 'rtid' => $relationship_type->rtid,
  143. );
  144. drupal_write_record('user_relationship_defaults', $new_default);
  145. $message_p = array(
  146. '%username' => $user->name,
  147. '%relationship_name' => ur_tt("user_relationships:rtid:$relationship_type->rtid:name", $relationship_type->name),
  148. );
  149. drupal_set_message(t('Default relationship %relationship_name of %username has been added.', $message_p));
  150. watchdog('u_relationship', 'Default relationship %relationship_name of %username has been added.', $message_p, WATCHDOG_NOTICE, l(t('view'), 'admin/user/relationships'));
  151. $form_state['redirect'] = 'admin/user/relationships';
  152. }
  153. /**
  154. * Default Relationship delete confirmation.
  155. */
  156. function user_relationship_defaults_delete_form(&$form_state, $default_relationship) {
  157. $form['rdid'] = array(
  158. '#type' => 'value',
  159. '#value' => $default_relationship->rdid
  160. );
  161. $form = confirm_form(
  162. $form,
  163. t('Are you sure you want to delete the default relationship %relationship of %name?', array(
  164. '%relationship' => ur_tt("user_relationships:rtid:$default_relationship->relationship_type->rtid:name", $default_relationship->relationship_type->name),
  165. '%name' => $default_relationship->user->name,
  166. )),
  167. 'admin/user/relationships',
  168. t('This action cannot be undone.'),
  169. t('Delete'), t('Cancel')
  170. );
  171. return $form;
  172. }
  173. /**
  174. * Process default relationship delete form submission.
  175. */
  176. function user_relationship_defaults_delete_form_submit($form, &$form_state) {
  177. $default_relationship = user_relationship_default_load($form_state['values']['rdid']);
  178. db_query("DELETE FROM {user_relationship_defaults} WHERE rdid = %d", $default_relationship->rdid);
  179. $message_p = array(
  180. '%name' => $default_relationship->user_name,
  181. '%relationship' => ur_tt("user_relationships:rtid:$default_relationship->rtid:name", $default_relationship->name),
  182. );
  183. drupal_set_message(t('Default relationship %relationship of %name has been deleted.', $message_p));
  184. watchdog('u_relationship', 'Default relationship %relationship of %name has been deleted.', $message_p, WATCHDOG_NOTICE);
  185. user_relationship_defaults_load(TRUE);
  186. $form_state['redirect'] = 'admin/user/relationships';
  187. }
  188. /**
  189. * hook_user()
  190. */
  191. function user_relationship_defaults_user($type, &$edit, &$account, $category = NULL) {
  192. if (
  193. ($type == 'insert' && $account->status) //if new user is already approved, create relationship
  194. || ($type == 'after_update' && $account->status && !$account->login) //#519440 if admin approves a new user account, create relationship
  195. ) {
  196. //note that if a new account is approved, then blocked and unblocked before the user logged in once,
  197. //it'll attempt to create default relationships again (there won't be any errors if they already exist, though)
  198. $defaults = user_relationship_defaults_load();
  199. foreach ($defaults as $default) {
  200. user_relationships_request_relationship($default->user, $account, $default->relationship_type, TRUE);
  201. }
  202. }
  203. //delete our default user if they are deleted from the site
  204. if ($type == 'delete') {
  205. db_query("DELETE FROM {user_relationship_defaults} WHERE uid = %d", $account->uid);
  206. }
  207. }
  208. /**
  209. * hook_user_relationships_admin_page_alter()
  210. */
  211. function user_relationship_defaults_user_relationships_page_alter($page_id, &$page, &$current_table) {
  212. switch ($page_id) {
  213. case 'types list':
  214. $defaults = user_relationship_defaults_load();
  215. $default_rows = array();
  216. foreach ($defaults as $default) {
  217. $default_rows[] = array(
  218. theme('username', $default->user),
  219. $default->relationship_type->name,
  220. l(t('delete'), "admin/user/relationships/defaults/{$default->rdid}/delete"),
  221. );
  222. }
  223. if (sizeof($default_rows) <= 0) {
  224. $default_rows[] = array(array('data' => t('No default relationships available.'), 'colspan' => 3));
  225. }
  226. $page['defaults'] = array(
  227. '#type' => 'fieldset',
  228. '#title' => t('Default Relationships'),
  229. '#weight' => 2
  230. );
  231. $page['defaults']['list'] = array(
  232. '#value' => theme('table', array(t('User'), t('Relationship'), t('Operations')), $default_rows)
  233. );
  234. break;
  235. }
  236. }
  237. /**
  238. * hook_user_relationships()
  239. */
  240. function user_relationship_defaults_user_relationships($op, &$relationship_type) {
  241. if ($op == 'delete type') {
  242. db_query("DELETE FROM {user_relationship_defaults} WHERE rtid = %d", $relationship_type->rtid);
  243. }
  244. }