/[drupal]/contributions/modules/ucreate/ucreate_og.module
ViewVC logotype

Contents of /contributions/modules/ucreate/ucreate_og.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Aug 7 14:11:33 2008 UTC (15 months, 3 weeks ago) by yhahn
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.1: +13 -6 lines
File MIME type: text/x-php
Fixing ucreate_og bug where users weren't automatically added to the first group.
1 <?php
2 // $Id: ucreate_og.module,v 1.1 2008/06/18 21:28:05 alexb Exp $
3
4 /**
5 * Implementation of hook_form_alter().
6 *
7 * Adds group check boxes to a ucreate form.
8 */
9 function ucreate_og_form_alter($form_id, &$form) {
10 if ($form_id == 'ucreate_user_form') {
11 _ucreate_og_form($form);
12 }
13 }
14
15 /**
16 * Implementation of hook_mail_alter().
17 */
18 function ucreate_og_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers) {
19 if ($mailkey == 'ucreate-create' && $account = _ucreate_og_get_last_edit()) {
20 if (is_array($account['ucreate_og_group_titles'])) {
21 $body .=
22 "\n\n". t('We also added you as member to the group(s): !groups', array('!groups' => implode(', ', $account['ucreate_og_group_titles'])));
23 }
24 }
25 }
26 /**
27 * Implemenation of hook_user().
28 */
29 function ucreate_og_user($op, &$edit, &$account, $category = NULL) {
30 // @todo:
31 // $edit['og_register']
32 // Assume that whoever calls us here has actually permissions to sign up users :)
33 // -> ucreate users permissions circumvent og group permissions.
34 if ($op == 'insert' && $edit['og_register']) {
35 if (!is_array($edit['og_register']) && is_numeric($edit['og_register'])) {
36 $edit['og_register'] = array($edit['og_register'] => 1);
37 }
38 foreach ($edit['og_register'] as $gid => $register) {
39 if ($register) {
40 og_save_subscription($gid, $account->uid, array('is_active' => 1));
41 $group_title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $gid));
42 $edit['ucreate_og_group_titles'][] = $group_title;
43 }
44 }
45 _ucreate_og_set_last_edit($edit);
46 }
47 }
48
49 /**
50 * Set last account edit.
51 * This is a crutch for being able to access
52 * user information in ucreate_og_mail_alter().
53 */
54 function _ucreate_og_set_last_edit($account_edit = NULL) {
55 static $edit;
56 if ($account_edit) {
57 $edit = $account_edit;
58 }
59 return $edit;
60 }
61
62 /**
63 * Get last account edit.
64 * This is a crutch for being able to access
65 * user information in ucreate_og_mail_alter().
66 */
67 function _ucreate_og_get_last_edit() {
68 return _ucreate_og_set_last_edit();
69 }
70
71 /**
72 * This function returns role ids to assign to a given user based on given groups.
73 *
74 * @todo Move this to og_promote.
75 * @todo Doesn't seem to be used.
76 */
77 function _ucreate_og_roles_based_on_groups($groups) {
78 $rids = array();
79 foreach ($groups as $group_id) {
80 $type = db_result(db_query('SELECT type FROM {node} WHERE nid = %d', $group_id));
81 if ($type == 'group_organization') {
82 $rids[4] = 4; // MEMBER ROLE
83 }
84 else if ($type == 'group_campaign') {
85 $rids[7] = 7; // GUEST ROLE
86 }
87 }
88 return $rids;
89 }
90
91 /**
92 * This function adds an OG group selection fieldset and provides default settings by user/group context.
93 */
94 function _ucreate_og_form(&$form) {
95 if (isset($_GET['gids'])) {
96 og_set_group_context(node_load($_GET['gids'][0]));
97 }
98 $og = og_get_group_context();
99
100 // Set up OG register checkboxes
101 $form['og_register'] = array(
102 '#type' => 'fieldset',
103 '#title'=> t('Groups'),
104 'og_register' => array(
105 '#type' => 'checkboxes',
106 ),
107 );
108
109 // Allow user to add new user to her groups
110 global $user;
111 if (is_array($user->og_groups)) {
112 $group_options = $user->og_groups;
113 }
114 else if ($og) {
115 $group_options = array($og);
116 }
117
118 $options = array();
119 foreach ($group_options as $group) {
120 $group = (object) $group;
121 $options[$group->nid] = '<span class="og-registration-'.$group->nid.'">'. t('Subscribe to @name.', array('@name' => $group->title)). "</span>\n";
122 if ($group->selective) {
123 $options[$group->nid] .= ' '. t('(approval needed)');
124 }
125 }
126 $form['og_register']['og_register']['#options'] = $options;
127
128 // Disable OG options (and select it) if only 1 group
129 if (count($options) == 1) {
130 // We need to process this correctly (differently) on the hook_user() insert handler
131 $form['og_register']['og_register']['#type'] = 'hidden';
132 $form['og_register']['og_register']['#value'] = key($options);
133
134 $form['og_register']['message']['#type'] = 'item';
135 $group = current($group_options);
136 $form['og_register']['message']['#value'] = t('This user will be added to !group.', array('!group' => $group['title']));
137 }
138 // If group context is set, select it by default
139 else if ($og) {
140 $form['og_register']['og_register']['#default_value'] = array($og->nid);
141 }
142 }

  ViewVC Help
Powered by ViewVC 1.1.2