/[drupal]/contributions/modules/og_audience/og_audience.module
ViewVC logotype

Contents of /contributions/modules/og_audience/og_audience.module

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


Revision 1.7 - (show annotations) (download) (as text)
Sat Jun 28 07:44:20 2008 UTC (17 months ago) by davidlesieur
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +10 -4 lines
File MIME type: text/x-php
#229010 by gustav and me: Do not allow removal of all groups when OG requires an audience.
1 <?php
2 // $Id: og_audience.module,v 1.6 2008/06/28 07:18:31 davidlesieur Exp $
3
4 /**
5 * Implementation of hook_perm().
6 */
7 function og_audience_perm() {
8 return array('change audience');
9 }
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function og_audience_menu() {
15 $items['admin/og/og-audience'] = array(
16 'title' => 'OG Audience',
17 'page callback' => 'drupal_get_form',
18 'page arguments' => array('og_audience_settings'),
19 'access arguments' => array('administer site configuration'),
20 );
21 $items['node/%node/audience'] = array(
22 'title' => 'Audience',
23 'type' => MENU_LOCAL_TASK,
24 'access callback' => 'og_audience_tab_access',
25 'access arguments' => array(1),
26 'page callback' => 'og_audience_page',
27 'page arguments' => array(1),
28 );
29 return $items;
30 }
31
32 /**
33 * Implementation of hook_block().
34 */
35 function og_audience_block($op = 'list', $delta = 0) {
36 switch ($op) {
37 case 'list':
38 $blocks = array();
39 $blocks[0]['info'] = t('Organic group audience');
40 $blocks[0]['cache'] = BLOCK_NO_CACHE;
41 return $blocks;
42
43 case 'view':
44 if (user_access('change audience')) {
45 global $user;
46
47 if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
48 $node = node_load(arg(1));
49 if ($node &&
50 og_is_group_post_type($node->type) &&
51 count($user->og_groups) &&
52 (count($node->og_groups) || variable_get('og_audience_allow_add', 1))) {
53
54 $block['subject'] = t('Audience');
55 $block['content'] = drupal_get_form('og_audience_build_form', $node);
56 return $block;
57 }
58 }
59 }
60 break;
61 }
62 }
63
64 /**
65 * Menu callback function for settings form
66 */
67 function og_audience_settings() {
68 $form['og_audience_tab_enable'] = array(
69 '#title' => t('Provide a tab'),
70 '#type' => 'checkbox',
71 '#default_value' => variable_get('og_audience_tab_enable', 1),
72 '#description' => t('If this is enabled, an additional tab will appear on posts for users with the <em>change audience</em> permission. You may wish to disable this if you have enabled the block interface.'),
73 );
74 $form['og_audience_allow_add'] = array(
75 '#title' => t('Posts with no audience may be added to a group'),
76 '#type' => 'checkbox',
77 '#default_value' => variable_get('og_audience_allow_add', 1),
78 '#description' => t('If this is enabled, a user may add to a group a post that initially had no audience. Note that the user may also be able to determine whether the post will remain public.'),
79 );
80
81 return system_settings_form($form);
82 }
83
84 /**
85 * Determine whether a user may access the audience page to perform audience
86 * changes.
87 *
88 * @param $account
89 * The user whose access needs to be verified. If empty, the current user is
90 * checked.
91 */
92 function og_audience_tab_access($node, $account = NULL) {
93 global $user;
94
95 if (empty($account)) {
96 $account = $user;
97 }
98
99 return (
100 variable_get('og_audience_tab_enable', 1) &&
101 $node &&
102 user_access('change audience') &&
103 og_is_group_post_type($node->type) &&
104 count($account->og_groups) &&
105 (count($node->og_groups) || variable_get('og_audience_allow_add', 1))
106 );
107 }
108
109 /**
110 * Provide the audience management page.
111 */
112 function og_audience_page($node) {
113 drupal_set_title(t('Audience for %title', array('%title' => $node->title)));
114 return drupal_get_form('og_audience_build_form', $node);
115 }
116
117 /**
118 * Return the audience form for the given node and user.
119 */
120 function og_audience_build_form(&$form_state, $node) {
121 global $user;
122
123 $type = node_get_types('name', $node);
124
125 if (empty($node->og_groups)) {
126 $node->og_groups = array();
127 }
128
129 $form['target_node'] = array(
130 '#type' => 'value',
131 '#value' => $node,
132 );
133
134 // Find groups the node could be added to.
135 $add_to_options = _og_audience_add_to_options($node, $user);
136
137 $form['og_audience_add'] = array(
138 '#type' => 'fieldset',
139 '#title' => t('Add'),
140 );
141
142 if ($count = count($add_to_options)) {
143 $form['og_audience_add']['add_to'] = array(
144 '#type' => $count >= 10 ? 'select' : 'checkboxes',
145 '#title' => t('Add this %type to', array('%type' => $type)),
146 '#options' => $add_to_options,
147 '#multiple' => TRUE,
148 );
149 $submit = TRUE;
150
151 // Only show the "Public" checkbox when the node is not in any group.
152 if (empty($node->og_groups) && module_exists('og_access')) {
153 og_access_alter_nongroup_form($form, $node);
154
155 // Only an author/editor should be offered the visibility override.
156 if (isset($form['og_nodeapi']['visible']['og_public']) && !node_access('update', $node) && !user_access('administer organic groups')) {
157 // Not allowed to set visibility. Ensure that the post remains public,
158 // because it was public in the first place by not belonging to any
159 // group.
160 unset($form['og_nodeapi']['visible']['og_public']);
161 $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
162 }
163 }
164 }
165 else {
166 $form['og_audience_add']['add_to'] = array(
167 '#type' => 'value',
168 '#value' => array(),
169 );
170 $form['og_audience_add']['add_to_message'] = array(
171 '#type' => 'markup',
172 '#value' => '<p>'. t('There are no groups you can add this %type to.', array('%type' => $type)) .'</p>',
173 );
174 }
175
176 // Find groups the node could be removed from.
177 $remove_from_options = _og_audience_remove_from_options($node, $user);
178
179 $form['og_audience_remove'] = array(
180 '#type' => 'fieldset',
181 '#title' => t('Remove'),
182 );
183
184 if ($count = count($remove_from_options)) {
185 $form['og_audience_remove']['remove_from'] = array(
186 '#type' => $count >= 10 ? 'select' : 'checkboxes',
187 '#title' => t('Remove this %type from', array('%type' => $type)),
188 '#options' => $remove_from_options,
189 '#multiple' => TRUE,
190 );
191 $submit = TRUE;
192 }
193 else {
194 $form['og_audience_remove']['remove_from'] = array(
195 '#type' => 'value',
196 '#value' => array(),
197 );
198 $form['og_audience_remove']['remove_from_message'] = array(
199 '#type' => 'markup',
200 '#value' => '<p>'. t('There are no groups you can remove this %type from.', array('%type' => $type)) .'</p>',
201 );
202 }
203
204 if ($submit) {
205 $form['submit'] = array(
206 '#type' => 'submit',
207 '#value' => t('Submit'),
208 );
209 }
210
211 return $form;
212 }
213
214 /**
215 * Form submit handler.
216 */
217 function og_audience_build_form_submit($form, &$form_state) {
218 global $user;
219 $changed = FALSE;
220 $node = $form_state['values']['target_node'];
221
222 // Add array_filter in case we're using checkboxes
223 $add_to = array_keys(array_filter($form_state['values']['add_to']));
224
225 $remove_from = array_keys(array_filter($form_state['values']['remove_from']));
226
227 $type = node_get_types('name', $node);
228 if (count($add_to)) {
229 foreach ($add_to as $gid) {
230 $node->og_groups[] = $gid;
231 drupal_set_message(t('The %type has been added to %group.', array('%type' => $type, '%group' => $user->og_groups[$gid]['title'])));
232 }
233 // This should only be set if the node was part of no groups previously.
234 if (isset($form_state['values']['og_public'])) {
235 $node->og_public = $form_state['values']['og_public'];
236 }
237 $changed = TRUE;
238 }
239 if (count($remove_from)) {
240 $node->og_groups = array_diff($node->og_groups, $form_state['values']['remove_from']);
241 $required = variable_get('og_audience_required', 0) && !user_access('administer nodes');
242 if (empty($node->og_groups) && $required) {
243 form_set_error('remove_form', t('You must leave at least one group selected for the %type.', array('%type' => $type)));
244 }
245 else {
246 foreach ($remove_from as $gid) {
247 drupal_set_message(t('The %type has been removed from %group.', array('%type' => $type, '%group' => $user->og_groups[$gid]['title'])));
248 }
249 $changed = TRUE;
250 }
251 }
252
253 if ($changed) {
254 og_save_ancestry($node);
255 node_access_acquire_grants($node);
256 cache_clear_all();
257 }
258 }
259
260 /**
261 * Find groups a node could be added to by the given user (assuming that the
262 * user has the permission for doing so).
263 *
264 * @return
265 * Array of options.
266 */
267 function _og_audience_add_to_options($node, $user) {
268 $options = array();
269 $groups = array_diff(array_keys($user->og_groups), $node->og_groups);
270 if (count($groups)) {
271 foreach ($groups as $gid) {
272 $options[$gid] = $user->og_groups[$gid]['title'];
273 }
274 }
275 return $options;
276 }
277
278 /**
279 * Find groups a node could be removed from by the given user.
280 *
281 * Only group administrators and the node's author are allowed to remove content
282 * from a group.
283 *
284 * @return
285 * Array of options.
286 */
287 function _og_audience_remove_from_options($node, $user) {
288 $options = array();
289 $groups = array_intersect($node->og_groups, array_keys($user->og_groups));
290 if (count($groups)) {
291 foreach ($groups as $gid) {
292 if ($user->og_groups[$gid]['is_admin'] || node_access('update', $node)) {
293 $options[$gid] = $user->og_groups[$gid]['title'];
294 }
295 }
296 }
297 return $options;
298 }

  ViewVC Help
Powered by ViewVC 1.1.2