Removing translation directories
[project/og.git] / og_access.module
CommitLineData
efe0f26b 1<?php
efe0f26b 2
3// visibility states for private groups. site admin chooses in og_access_settings()
4define('OG_PRIVATE_GROUPS_NEVER', 0);
5define('OG_PRIVATE_GROUPS_ALWAYS', 1);
6define('OG_PRIVATE_GROUPS_CHOOSE_TRUE', 2);
7define('OG_PRIVATE_GROUPS_CHOOSE_FALSE', 3);
8
9// visibility states for nodes within groups. site admin chooses in og_settings()
10define('OG_VISIBLE_GROUPONLY', 0);
11define('OG_VISIBLE_BOTH', 1);
12define('OG_VISIBLE_CHOOSE_PUBLIC', 2);
13define('OG_VISIBLE_CHOOSE_PRIVATE', 3);
14
efe0f26b 15function og_access_menu($may_cache) {
16 if ($may_cache) {
17 $items[] = array(
18 'path' => 'admin/og/og_access',
20ef89c0 19 'title' => t('Organic groups access configuration'),
efe0f26b 20 'description' => t('Choose whether new groups should be private or public'),
21 'callback' => 'drupal_get_form',
22 'callback arguments' => array('og_access_settings'),
23 );
24
25 }
26 return $items;
27}
28
29function og_access_settings() {
30 drupal_add_js(drupal_get_path('module', 'og_access'). '/og_access.js'); // load the form javascript to handle private groups / node visibility conflicts in the og access settings form.
31
20ef89c0 32 $options = array(t('Visible only within the targeted groups.'), t('Visible within the targeted groups and on other pages.'), t('Visibility chosen by author/editor using a checkbox on the posting form.') .' '. t('Checkbox defaults to @pub.', array('@pub' => t('Public'))), t('Visibility chosen by author/editor using a checkbox on the posting form. '). t('Checkbox defaults to @pri.', array('@pri' => t('Private'))));
33 $form['og_visibility'] = array('#type' => 'radios', '#title' => t('Visibility of posts'), '#default_value' => variable_get('og_visibility', 0), '#description' =>t('Determine how broadly available a given post should be when it is affiliated with a group. OG admins always see the checkbox for making a post @pub. Note that changing this setting has no effect on existing posts. Re-save those posts to acquire this new setting. If the setting you want is disabled here, check the settings under <em>Private Groups</em> setting below. You cannot set node visibility to always be public if private groups are set to always on and vice versa.', array('@pub' => t('Public'))), '#options' => $options);
efe0f26b 34
35 // private groups control
36 $options = array(t('New group home pages and default audience are always public.'),
37 t('New group home pages and default audience are always private.'),
38 t('Group administrator chooses whether her group homepage and audience are private or not. Defaults to %yes.', array('%yes' => t('private'))),
39 t('Group administrator chooses whether her group homepage and audience are private or not. Defaults to %no.', array('%no' => t('public'))),
40 );
41 $form['og_private_groups'] = array(
42 '#type' => 'radios',
43 '#title' => t('Private Groups'),
44 '#options' => $options,
45 '#default_value' => variable_get('og_private_groups', OG_PRIVATE_GROUPS_CHOOSE_FALSE),
20ef89c0 46 '#description' => '<p>'. t("A private group's group home page cannot be seen by non-members, and new posts created in the group will default to being private. This setting controls what private groups options can be used when creating a new group or editing an existing group. If you select one of the <em>group administrator chooses</em> options then it will be up to group admins whether their new groups are private or not, with the default you specify here.") .'<p><p>'. t('Note that the privacy of all <em>content</em> in the group is determined as each node is created or edited, according to the <em>Node authoring form / Visibility of Posts</em> setting on this page. Note also that changing this setting only affects the default for new groups being created, not the privacy of any existing groups! To change those you must edit the groups and their individual content nodes directly. If the setting you want is disabled here, check <em>Visibility of Posts</em> above. You cannot choose to only have private groups if node visibility is set to be always public, and vice versa.') .'</p>',
efe0f26b 47 );
48 return system_settings_form($form);
49}
50
51function og_access_alter_group_form(&$form, $node) {
52 // private groups
53 $visibility = variable_get('og_private_groups', OG_PRIVATE_GROUPS_CHOOSE_FALSE);
54 // override setting for admins - get right default
55 if (user_access('administer nodes')) {
56 $not = array(OG_PRIVATE_GROUPS_NEVER, OG_PRIVATE_GROUPS_CHOOSE_FALSE);
57 $visibility = in_array($visibility, $not) ? OG_PRIVATE_GROUPS_CHOOSE_FALSE : OG_PRIVATE_GROUPS_CHOOSE_TRUE;
58 }
59
60 $default = FALSE;
61 switch ($visibility) {
62 case OG_PRIVATE_GROUPS_NEVER :
63 $form['og_private'] = array (
64 '#type' => 'value',
65 '#value' => 0
66 );
67 break;
68
69 case OG_PRIVATE_GROUPS_ALWAYS :
70 $form['og_private'] = array (
71 '#type' => 'value',
72 '#value' => 1
73 );
74 break;
75
76 case OG_PRIVATE_GROUPS_CHOOSE_TRUE :
77 $default = TRUE;
78 // fall through
79
80 case OG_PRIVATE_GROUPS_CHOOSE_FALSE :
81 $form['og_private'] = array (
82 '#type' => 'checkbox',
20ef89c0 83 '#title' => t('Private group'),
efe0f26b 84 '#default_value' => $node->nid ? $node->og_private : $default,
20ef89c0 85 '#description' => t('Should this group be visible only to its members? Disabled if the group is set to <em>List in Directory</em> or <em>Membership requests: open</em>.'));
efe0f26b 86 break;
87 }
88}
89
90// Add public checkbox to node edit form.
91function og_access_alter_nongroup_form(&$form, $node) {
92 global $user;
93
de475380 94 if (og_is_group_post_type($node->type)) {
efe0f26b 95 // get the visibility for normal users
96 $vis = variable_get('og_visibility', 0);
97
98 // override visibility for og admins
99 if (user_access('administer organic groups')) {
100 if ($vis < 2) {
101 $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
102 }
103 }
104 elseif (!og_get_subscriptions($user->uid)) {
105 // don't show checkbox if no memberships. must be public.
106 $vis = OG_VISIBLE_BOTH;
107 }
108
78833b08 109 // We are using this form element to communicate $groups from og to og_access.
110 $groups = $form['og_initial_groups']['#value'];
111
efe0f26b 112 // If the post is to a private group, visibility must default to one of the private options.
efe0f26b 113 $selected_groups = isset($form['#post']['og_groups']) ? array_filter($form['#post']['og_groups']) : $groups;
114 if (count($selected_groups)) {
115 foreach ($selected_groups as $gid) {
116 $group_node = new stdClass();
117 $group_node->nid = $gid;
118 og_load_group($group_node);
119 if ($group_node->og_private) {
78833b08 120 // Try not to show checkbox if admin likes to reduce decisions for node authors.
efe0f26b 121 $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
122 break;
123 }
124 }
cf33c1a1 125 }
126 else {
127 // TODOL: No groups. Public must be checked if it is visible.
efe0f26b 128 }
129
130 switch ($vis) {
131 case OG_VISIBLE_BOTH:
132 $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
133 break;
134 case OG_VISIBLE_GROUPONLY:
135 $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 0);
136 break;
137
138 //user decides how public the post is.
139 case OG_VISIBLE_CHOOSE_PUBLIC:
140 $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 1, '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
141 break;
142 case OG_VISIBLE_CHOOSE_PRIVATE:
143 $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 0, '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
144 break;
145 }
73f255ef 146
147 if (count($form['og_nodeapi']['visible']) > 1) {
148 $form['og_nodeapi']['#type'] = 'fieldset';
149 $form['og_nodeapi']['#title'] = t('Groups');
150 $form['og_nodeapi']['#collapsible'] = TRUE;
151 $form['og_nodeapi']['#collapsed'] = $selected_groups ? TRUE : FALSE;
152 }
153
efe0f26b 154 }
155
156}
157
158/**
159 * Implementation of hook_form_alter().
160 */
161function og_access_form_alter($form_id, &$form) {
162 if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
78833b08 163 drupal_add_js(drupal_get_path('module', 'og_access'). '/og_access.js');
efe0f26b 164 $node = $form['#node'];
165 if (og_is_group_type($node->type)) {
166 og_access_alter_group_form($form, $node);
167 }
168 else {
169 og_access_alter_nongroup_form($form, $node);
170 }
171 }
172}
173
174/**
175 * Implementation of hook_node_grants().
176 */
177function og_access_node_grants($account, $op) {
178 if ($op == 'view') {
179 $grants['og_public'][] = 0; // everyone can see a public node
180 }
181
de475380 182 // Subscribers get an admin or non-admin grant for each subscription
efe0f26b 183 if ($subscriptions = og_get_subscriptions($account->uid)) {
184 foreach ($subscriptions as $key => $val) {
de475380 185 // Admins don't need to receive the subscriber grant since they can perform all operations.
186 if ($val['is_admin']) {
187 $grants['og_admin'][] = $key;
efe0f26b 188 }
de475380 189 else {
efe0f26b 190 $grants['og_subscriber'][] = $key;
191 }
192 }
193 }
de475380 194 return isset($grants) ? $grants : array();
efe0f26b 195}
196
197/**
de475380 198 * Implementation of hook_node_access_records.
efe0f26b 199 */
200function og_access_node_access_records($node) {
201 // don't write records if the node type is omitted or node is a group
202 if (og_is_omitted_type($node->type)) {
203 return;
204 }
205
206 if (og_is_group_type($node->type)) {
de475380 207 // This grant allows group admins to manage their group.
060723fa 208 $grants[] = array(
209 'realm' => 'og_admin',
210 'gid' => $node->nid,
211 'grant_view' => 1,
212 'grant_update' => 1,
213 'grant_delete' => 1
214 );
215
de475380 216 // If the group is not marked private, let everyone view the group homepage.
efe0f26b 217 if (!$node->og_private) {
060723fa 218 $grants[] = array (
219 'realm' => 'og_public',
220 'gid' => 0,
221 'grant_view' => 1,
222 'grant_update' => 0,
223 'grant_delete' => 0 );
224 }
225 else {
226 $grants[] = array (
227 'realm' => 'og_subscriber',
43761112 228 'gid' => $node->nid,
060723fa 229 'grant_view' => 1,
230 'grant_update' => 0,
231 'grant_delete' => 0
232 );
233
efe0f26b 234 }
235 }
236 elseif (is_array($node->og_groups)) {
de475380 237 // Applies to non group nodes.
efe0f26b 238 if ($node->og_public) {
239 $grants[] = array('realm' => 'og_public', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
240 }
241
242 foreach ($node->og_groups as $gid) {
de475380 243 // Group administrators get all operations.
244 $grants[] = array('realm' => 'og_admin', 'gid' => $gid, 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
245 // Normal subscribers just get update operation if node type is a wiki type.
246 $is_wiki = og_is_wiki_type($node->type);
247 $grants[] = array('realm' => 'og_subscriber', 'gid' => $gid, 'grant_view' => 1, 'grant_update' => $is_wiki, 'grant_delete' => 0);
efe0f26b 248 }
249 }
250
251 return $grants;
252}
253
254/**
255 * Implementation of hook_node_access_explain.
256 */
257function og_access_node_access_explain($row) {
258 if ($row->realm == 'og_public') {
20ef89c0 259 return t('All users may view this node.');
efe0f26b 260 }
261 elseif ($row->realm == 'og_subscriber') {
262 $node = node_load((int)$row->gid);
20ef89c0 263 return t('Members of !group_n may view this node.', array('!group_n' => l($node->title, "node/$row->gid")));
efe0f26b 264 }
265}