/[drupal]/contributions/modules/image_gallery_access/image_gallery_access.admin.inc
ViewVC logotype

Contents of /contributions/modules/image_gallery_access/image_gallery_access.admin.inc

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


Revision 1.20 - (show annotations) (download) (as text)
Wed Oct 28 14:25:57 2009 UTC (3 weeks, 6 days ago) by salvis
Branch: MAIN
Changes since 1.19: +45 -41 lines
File MIME type: text/x-php
#616812: Template values for new galleries were not saved, reported by vhenninot.
1 <?php
2 // $Id: image_gallery_access.admin.inc,v 1.19 2009/10/28 00:25:33 salvis Exp $
3
4 /**
5 * @file image_gallery_access.admin.inc
6 *
7 * Include file for image_gallery_access.module, containing (sub-)page handling
8 * (form_alter) and batch code.
9 *
10 */
11
12 /**
13 * Rewrite the image gallery administration page with our new access rules.
14 */
15 function _image_gallery_access_admin_form(&$form, &$form_state) {
16 $tid = (isset($form['tid']['#value']) ? $form['tid']['#value'] : NULL);
17 if (isset($tid) && !image_gallery_access_access($tid, 'view')) {
18 return; // Deny access to (i.e. hide) the IGA controls if the user doesn't have View access to the gallery.
19 }
20
21 $roles = array();
22 $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
23 while ($obj = db_fetch_object($result)) {
24 $roles[$obj->rid] = check_plain($obj->name);
25 }
26
27 if (isset($tid)) { // edit
28 $template_tid = variable_get('image_gallery_access_default_template_tid', 0);
29 $settings = _image_gallery_access_get_settings($tid);
30 }
31 else { // create
32 $template_tid = variable_get('image_gallery_access_new_template_tid', NULL);
33 $settings = _image_gallery_access_get_settings($template_tid);
34 }
35 $iga_priority = $settings['priority'];
36
37 $form['image_gallery_access'] = array(
38 '#type' => 'fieldset',
39 '#title' => t('Access control'),
40 '#collapsible' => TRUE,
41 '#tree' => TRUE,
42 );
43
44 $tr = 't';
45 $variables = array(
46 '!access_content' => '<em>'. l($tr('access content'), 'admin/user/permissions', array('fragment' => 'module-node', 'html' => TRUE)). '</em>',
47 '!access_comments' => '<em>'. l($tr('access comments'), 'admin/user/permissions', array('fragment' => 'module-comment', 'html' => TRUE)). '</em>',
48 '!create_images' => '<em>'. l($tr('create images'), 'admin/user/permissions', array('fragment' => 'module-image', 'html' => TRUE)). '</em>',
49 '!post_comments' => '<em>'. l($tr('post comments'), 'admin/user/permissions', array('fragment' => 'module-comment', 'html' => TRUE)). '</em>',
50 '!post_comments_without_approval' => '<em>'. l($tr('post comments without approval'), 'admin/user/permissions', array('fragment' => 'module-comment', 'html' => TRUE)). '</em>',
51 '!edit_own_images' => '<em>'. l($tr('edit own images'), 'admin/user/permissions', array('fragment' => 'module-image', 'html' => TRUE)). '</em>',
52 '!edit_any_images' => '<em>'. l($tr('edit any images'), 'admin/user/permissions', array('fragment' => 'module-image', 'html' => TRUE)). '</em>',
53 '!delete_own_images' => '<em>'. l($tr('delete own images'), 'admin/user/permissions', array('fragment' => 'module-image', 'html' => TRUE)). '</em>',
54 '!delete_any_images' => '<em>'. l($tr('delete any images'), 'admin/user/permissions', array('fragment' => 'module-image', 'html' => TRUE)). '</em>',
55 '!administer_comments' => '<em>'. l($tr('administer comments'), 'admin/user/permissions', array('fragment' => 'module-comment', 'html' => TRUE)). '</em>',
56 '!administer_image_galleries' => '<em>'. l($tr('administer image galleries'), 'admin/user/permissions', array('fragment' => 'module-image', 'html' => TRUE)). '</em>',
57 '!administer_nodes' => '<em>'. l($tr('administer nodes'), 'admin/user/permissions', array('fragment' => 'module-node', 'html' => TRUE)). '</em>',
58 );
59 $form['image_gallery_access']['permissions'] = array(
60 '#type' => 'fieldset',
61 '#title' => $tr('Permissions information'),
62 '#collapsible' => TRUE,
63 '#collapsed' => TRUE,
64 );
65 $form['image_gallery_access']['permissions'][] = array(
66 '#type' => 'markup',
67 '#value' => '<div>'. t('Note that users need') .'<ul style="margin-top: 0"><li>'.
68 t('the !access_content permissions <strong>AND <em>View</em></strong> to be able to see content at all and', $variables) .'</li><li>'.
69 t('the !create_images permissions <strong>AND <em>Post</em></strong> to be able to upload images;', $variables) .'</li><li>'.
70 t('the !edit_own_images or !edit_any_images permissions (<strong>OR <em>Edit</em></strong>) can be added if desired, <strong>plus</strong>', $variables) .'</li><li>'.
71 t('the !delete_own_images or !delete_any_images permissions (<strong>OR <em>Delete</em></strong>) if desired;', $variables) .'</li><li>'.
72 t('the !administer_image_galleries permission <strong>AND <em>View</em></strong> to be able to administer galleries (and change access!).', $variables) .'</li></ul></div>',
73 );
74 $form['image_gallery_access']['permissions'][] = array(
75 '#type' => 'markup',
76 '#value' => '<div>'. t('If you allow comments in your galleries, users also need') .'<ul style="margin-top: 0"><li>'.
77 t('the !access_comments permission to be able to see comments and replies, and', $variables) .'</li><li>'.
78 t('the !post_comments or !post_comments_without_approval permission to be able to post comments and replies;', $variables) .'</li><li>'.
79 t('the !administer_comments permission to be able to administer comments and replies (everywhere!).', $variables) .'</li></ul>'.
80 t('Furthermore note that content which is not published is treated in a different way by Drupal: it can be viewed only by its author or by users with the !administer_nodes permission. Unpublished comments and replies are accessible to users with the !administer_comments permission only.', $variables) .'</div>',
81 );
82
83 // Load a template:
84 $vid = variable_get('image_gallery_nav_vocabulary', '');
85 $form['image_gallery_access']['template'] = array(
86 '#type' => 'fieldset',
87 '#title' => $tr('Template'),
88 '#collapsible' => TRUE,
89 '#collapsed' => empty($template_tid),
90 );
91 $form['image_gallery_access']['template']['taxonomy'][$vid] = taxonomy_form($vid, array($template_tid), '');
92 $form['image_gallery_access']['template']['taxonomy'][$vid]['#description'] = t("Select a gallery and click !Load to retrieve that gallery's settings as a starting point for this gallery.", array('!Load' => '['. t('Load') .']'));
93 $form['image_gallery_access']['template']['load_button'] = array(
94 '#type' => 'button',
95 '#name' => 'load_template',
96 '#value' => t('Load'),
97 '#submit' => FALSE,
98 );
99 $form['image_gallery_access']['template']['template_tid'] = array(
100 '#type' => 'value',
101 '#value' => NULL,
102 );
103 $form['image_gallery_access']['template']['select_by_default'] = array(
104 '#type' => 'checkbox',
105 '#title' => t('Remember this selection.'),
106 '#default_value' => FALSE,
107 );
108 $form['image_gallery_access']['template']['load_for_new'] = array(
109 '#type' => 'checkbox',
110 '#title' => t("Use the selected gallery's settings as defaults for new galleries."),
111 '#default_value' => FALSE,
112 );
113 $form['image_gallery_access']['#after_build'][] = '_image_gallery_access_admin_form_after_build';
114
115 // Column titles:
116 $form['image_gallery_access']['headers']['view'] = array(
117 '#type' => 'item',
118 '#prefix' => '<div class="image-gallery-access-div">',
119 '#title' => t('View this gallery'),
120 '#suffix' => '</div>',
121 );
122 $form['image_gallery_access']['headers']['create'] = array(
123 '#type' => 'item',
124 '#prefix' => '<div class="image-gallery-access-div">',
125 '#title' => t('Post in this gallery'),
126 '#suffix' => '</div>',
127 );
128 $form['image_gallery_access']['headers']['update'] = array(
129 '#type' => 'item',
130 '#prefix' => '<div class="image-gallery-access-div">',
131 '#title' => t('Edit images'),
132 '#suffix' => '</div>',
133 );
134 $form['image_gallery_access']['headers']['delete'] = array(
135 '#type' => 'item',
136 '#prefix' => '<div class="image-gallery-access-div">',
137 '#title' => t('Delete images'),
138 '#suffix' => '</div>',
139 );
140 $form['image_gallery_access']['headers']['clearer'] = array(
141 '#value' => '<div class="image-gallery-access-clearer"></div>',
142 );
143
144 // Column content (checkboxes):
145 $form['image_gallery_access']['view'] = array(
146 '#type' => 'checkboxes',
147 '#prefix' => '<div class="image-gallery-access-div">',
148 '#suffix' => '</div>',
149 '#options' => $roles,
150 '#default_value' => $settings['view'],
151 '#process' => array('expand_checkboxes', '_image_gallery_access_admin_form_disable_checkboxes'),
152 );
153 $form['image_gallery_access']['create'] = array(
154 '#type' => 'checkboxes',
155 '#prefix' => '<div class="image-gallery-access-div">',
156 '#suffix' => '</div>',
157 '#options' => $roles,
158 '#default_value' => $settings['create'],
159 '#process' => array('expand_checkboxes', '_image_gallery_access_admin_form_disable_checkboxes'),
160 );
161 $form['image_gallery_access']['update'] = array(
162 '#type' => 'checkboxes',
163 '#prefix' => '<div class="image-gallery-access-div">',
164 '#suffix' => '</div>',
165 '#options' => $roles,
166 '#default_value' => $settings['update'],
167 '#process' => array('expand_checkboxes', '_image_gallery_access_admin_form_disable_checkboxes'),
168 );
169 $form['image_gallery_access']['delete'] = array(
170 '#type' => 'checkboxes',
171 '#prefix' => '<div class="image-gallery-access-div">',
172 '#suffix' => '</div>',
173 '#options' => $roles,
174 '#default_value' => $settings['delete'],
175 '#process' => array('expand_checkboxes', '_image_gallery_access_admin_form_disable_checkboxes'),
176 );
177 $form['image_gallery_access']['clearer'] = array(
178 '#value' => '<div class="image-gallery-access-clearer"></div>',
179 );
180
181 drupal_add_css(drupal_get_path('module', 'image_gallery_access') .'/image_gallery_access.css');
182
183 // Find our moderator ACL:
184 if (isset($tid)) { // edit, not new
185 $acl_id = db_result(db_query("SELECT acl_id from {acl} WHERE module = 'image_gallery_access' AND name = '%d'", $tid));
186 if (!$acl_id) { // create one
187 $acl_id = acl_create_new_acl('image_gallery_access', $tid);
188 // update every existing node in this image gallery to use this acl.
189 $result = db_query("SELECT nid FROM {term_node} WHERE tid = %d", $tid);
190 while ($node = db_fetch_object($result)) {
191 // all privs to this ACL.
192 acl_node_add_acl($node->nid, $acl_id, 1, 1, 1);
193 }
194 }
195 $form['image_gallery_access']['acl'] = acl_edit_form($acl_id, 'Moderators');
196 $form['image_gallery_access']['acl'][] = array(
197 '#type' => 'markup',
198 '#value' => '<div>'. t('Moderators receive all grants above; to moderate comments, they also need !administer_comments (which is a global permission!).', $variables) .'</div>',
199 '#weight' => -1,
200 );
201 $form['image_gallery_access']['acl']['note'] = array(
202 '#type' => 'markup',
203 '#value' => '<div>'. t('Note: Changes to moderators are not saved until you click [!Submit] below.', array('!Submit' => $tr('Submit'))) .'</div>',
204 );
205 $form['image_gallery_access']['acl']['#after_build'][] = '_image_gallery_access_admin_form_after_build_acl0';
206 $form['image_gallery_access']['acl']['#after_build'] = array_reverse($form['image_gallery_access']['acl']['#after_build']);
207 $form['image_gallery_access']['acl']['#after_build'][] = '_image_gallery_access_admin_form_after_build_acl2';
208 }
209
210 foreach (module_implements('node_access_records') as $module) {
211 $na_modules[$module] = $module;
212 }
213 unset($na_modules['image_gallery_access']);
214 unset($na_modules['acl']);
215 if (count($na_modules)) {
216 $form['image_gallery_access']['interference'] = array(
217 '#type' => 'fieldset',
218 '#title' => t('Module interference'),
219 '#collapsible' => TRUE,
220 );
221 $variables = array(
222 '%content_type' => node_get_types('name', 'image'),
223 '!Image_Gallery_Access' => 'Image Gallery Access',
224 '!Content_Access' => l('Content Access', 'http://drupal.org/project/content_access'),
225 '@Content_Access' => 'Content Access',
226 '!ACL' => 'ACL',
227 '!module_list' => '<ul><li>'. implode($na_modules, '</li><li>') .'</li></ul>',
228 );
229 $form['image_gallery_access']['interference'][] = array(
230 '#type' => 'item',
231 '#value' => '<p>'. t("Besides !Image_Gallery_Access (and !ACL) you have installed the following node access module(s): !module_list The grants of every module are combined for each node. Access can only be granted, not removed&mdash;if a certain module grants a permission, the other(s) cannot deny it.", $variables) .'</p>',
232 );
233
234 if (module_exists('content_access')) {
235 $ca_settings = variable_get('content_access_settings', array());
236 foreach (array('view', 'update', 'delete', 'per_node') as $type) {
237 $value = content_access_get_settings($type, 'image');
238 if (!empty($value)) {
239 $ca_interferes = TRUE;
240 }
241 }
242 $ca_priority = content_access_get_settings('priority', 'image');
243 $is_conflict = $ca_priority >= $iga_priority && !empty($ca_interferes) || $ca_priority > $iga_priority;
244 $variables += array(
245 '!link' => l(t('@Content_Access configuration for the %content_type type', $variables), 'admin/content/node-type/image/access', array('html' => TRUE)),
246 '%Advanced' => $tr('Advanced'),
247 );
248 $specifically = ($ca_priority == $iga_priority ? t('Specifically, any grants given by @Content_Access cannot be taken back by !Image_Gallery_Access.', $variables) : '');
249 if ($is_conflict) {
250 $form['image_gallery_access']['interference']['by_content_access'] = array(
251 '#type' => 'fieldset',
252 '#title' => 'Content Access',
253 '#collapsible' => FALSE,
254 '#attributes' => array('class' => 'error'),
255 );
256 $form['image_gallery_access']['interference']['by_content_access'][] = array(
257 '#value' => '<div>'. t('You have set the !Content_Access module to control access to content of type %content_type&mdash;this can interfere with proper operation of !Image_Gallery_Access!', $variables) ." $specifically</div>",
258 );
259 if ($ca_priority == $iga_priority) {
260 $form['image_gallery_access']['interference']['by_content_access'][] = array(
261 '#value' => '<div>'. t("Unless you really know what you're doing, we recommend that you go to the !link page and clear all checkboxes. This will instruct @Content_Access to leave the %content_type nodes alone.", $variables) .'</div>',
262 );
263 }
264 else {
265 $form['image_gallery_access']['interference']['by_content_access'][] = array(
266 '#value' => '<div>'. t("The priority of @Content_Access ($ca_priority) is higher than the priority of !Image_Gallery_Access ($iga_priority), which means the latter is <strong>completely disabled</strong>! Unless you really know what you're doing, we recommend that you go to the !link page, change the priority (under %Advanced) to 0, and clear all checkboxes.", $variables) .'</div>',
267 );
268 }
269 $form['image_gallery_access']['interference']['by_content_access'][] = array(
270 '#value' => '<div>'. t("Alternatively, you can give !Image_Gallery_Access priority over @Content_Access by either raising the priority of !Image_Gallery_Access in every gallery above the priority of @Content_Access, or by lowering the priority of @Content_Access for the %content_type content type below the priority of !Image_Gallery_Access.", $variables) .'</div>',
271 );
272 }
273 else {
274 $form['image_gallery_access']['interference'][] = array(
275 '#value' => '<p>'. t('Note: You have installed the !Content_Access module, which has the capability to grant access to content that would otherwise be protected by !Image_Gallery_Access. Be careful when configuring @Content_Access!', $variables) .'</p>',
276 );
277 }
278 }
279
280 $form['image_gallery_access']['interference']['advanced'] = array(
281 '#type' => 'fieldset',
282 '#title' => t('Advanced'),
283 '#collapsible' => TRUE,
284 '#collapsed' => TRUE,
285 );
286 $form['image_gallery_access']['interference']['advanced']['priority'] = array(
287 '#type' => 'weight',
288 '#title' => t('Priority of !Image_Gallery_Access node grants in this gallery', $variables),
289 '#default_value' => $iga_priority,
290 '#description' => t("If you have no other node access control modules installed, you should leave this at the default 0. <br /> Otherwise you can raise or lower the priority of !Image_Gallery_Access' grants. Out of all the grants contributed to a node, only those with the highest priority are used, and all others are discarded.", $variables),
291 );
292 }
293
294 $variables = array(
295 '!Image_Gallery_Access' => l('Image Gallery Access', 'http://drupal.org/project/image_gallery_access'),
296 '!ACL' => l('ACL', 'http://drupal.org/project/acl'),
297 '%Module_interference' => t('Module interference'),
298 '!Image_Gallery_Access-dev' => l('Image&nbsp;Gallery&nbsp;Access&nbsp;6.x-1.x-dev', 'http://drupal.org/node/308939', array('html' => TRUE)),
299 '!ACL-dev' => l('ACL&nbsp;6.x-1.x-dev', 'http://drupal.org/node/96794', array('html' => TRUE)),
300 '%devel_node_access' => 'devel_node_access',
301 '!Devel' => l('Devel', 'http://drupal.org/project/devel'),
302 '!DNA' => 'DNA',
303 '!debug_mode' => l('debug mode', 'admin/settings/devel', array('fragment' => 'edit-devel-node-access-debug-mode')),
304 '!dna_summary' => l('devel/node_access/summary', 'devel/node_access/summary'),
305 '!Rebuild_permissions' => '['. $tr('Rebuild permissions') .']',
306 '!Post_settings_link' => l('admin/content/node-settings', 'admin/content/node-settings'),
307 '!Image_Gallery_Access_' => l('Image Gallery Access', 'http://drupal.org/project/issues/image_gallery_access'),
308 '!ACL_' => l('ACL', 'http://drupal.org/project/issues/acl'),
309 );
310 $form['image_gallery_access']['troubleshooting'] = array(
311 '#type' => 'fieldset',
312 '#title' => t('Trouble-shooting node access'),
313 '#collapsible' => TRUE,
314 '#collapsed' => TRUE,
315 );
316 $form['image_gallery_access']['troubleshooting'][] = array(
317 '#type' => 'item',
318 '#value' => '<div>'. t("In case of problems, follow these steps until you've got it worked out:") .'<ol style="margin-top: 0"><li>'.
319 t("Update to the 'recommended' !Image_Gallery_Access and !ACL releases for your version of Drupal.", $variables) .'</li><li>'.
320 (count($na_modules) ? t("Read %Module_interference above and update your other node access modules.", $variables) .'</li><li>' : '').
321 t("Check the release notes of the development snapshots for issues that might have been fixed in !Image_Gallery_Access-dev or !ACL-dev since the latest release.", $variables) .'</li><li>'.
322 t("Install the %devel_node_access module (!DNA, part of the !Devel module) and enable its !debug_mode: !DNA will show you all the grants that actually control your nodes in a footer block on each node's page.", $variables) .'</li><li>'.
323 t("Additional insight can be gained from !dna_summary and by enabling the second !DNA block.", $variables) .'</li><li>'.
324 t("Click !Rebuild_permissions on !Post_settings_link and check DNA for changes.", $variables) .'</li><li>'.
325 t("Check the issues queues of !Image_Gallery_Access_ and !ACL_ for existing reports and possible solutions.", $variables) .'</li><li>'.
326 t("If all of this hasn't helped, then pick ONE node that is misbehaving, look at it using an account that can see the node (and that should NOT have access if that's your problem!), create a new issue in the issues queue, describe the problem... <ul><li> what did you do? </li><li> what did you expect? </li><li> what happened instead? </li></ul> ... and <strong>attach a screenshot of all the DNA records</strong> for that one node. <br /> Be sure to indicate paths (URLs) for every page and module that you mention.") .'</li></ol></div>',
327 );
328 $form['image_gallery_access']['troubleshooting'][] = array(
329 '#type' => 'item',
330 '#value' => '<div>'. t("Note: You should not keep the !Devel module enabled on a production site.", $variables) .'</div>',
331 );
332
333 if (isset($tid) && !node_access_needs_rebuild())
334 {
335 $count = db_result(db_query("SELECT COUNT(DISTINCT n.nid) FROM {node} n LEFT JOIN {term_node} tn ON tn.nid = n.nid WHERE tn.tid = %d", $tid));
336 $limit = 20; // from _node_access_rebuild_batch_operation()
337 $threshold = variable_get('image_gallery_access_batch_threshold', $limit); // change the variable if you want
338 $form['image_gallery_access']['update_limit'] = array(
339 '#type' => 'value',
340 '#value' => $limit,
341 );
342 $form['image_gallery_access']['update_choice'] = array(
343 '#type' => 'radios',
344 '#title' => 'Update the permissions',
345 '#description' => t('<em>If</em> you make any node access changes, then each node in this gallery needs to be updated. Hover over the radiobuttons for details.'),
346 '#options' => NULL,
347 0 => array(
348 '#type' => 'radio',
349 '#title' => t('for all %count nodes immediately', array('%count' => $count)),
350 '#attributes' => array('title' => t('This option is the fastest, but with many nodes it can still take considerable time and memory. If it fails, it will leave your !node_access table in an inconsistent state.', array('!node_access' => '{node_access}'))),
351 '#return_value' => 0,
352 '#default_value' => ($count <= $threshold ? 0 : 1),
353 '#parents' => array('image_gallery_access', 'update_choice'),
354 ),
355 1 => array(
356 '#type' => 'radio',
357 '#title' => t('in batches of !limit now', array('!limit' => $limit)),
358 '#attributes' => array('title' => t('The batch option will always work reliably, but it takes longer to complete.')),
359 '#return_value' => 1,
360 '#default_value' => ($count <= $threshold ? 0 : 1),
361 '#parents' => array('image_gallery_access', 'update_choice'),
362 ),
363 2 => array(
364 '#type' => 'radio',
365 '#title' => t('rebuild <strong>all</strong> permissions later'),
366 '#attributes' => array('title' => t("This option will only set a flag to remind you to rebuild all permissions later; this is useful if you want to make multiple changes to your node access settings quickly and delay the updating until you're done.")),
367 '#return_value' => 2,
368 '#default_value' => ($count <= $threshold ? 0 : 1),
369 '#parents' => array('image_gallery_access', 'update_choice'),
370 ),
371 '#attributes' => array('class' => 'image-gallery-access-flowed'),
372 );
373 }
374
375 // Move some stuff down so our block goes in a nice place.
376 $form['submit']['#weight'] = 10;
377 $form['delete']['#weight'] = 10;
378
379 $form['#validate'][] = '_image_gallery_access_form_validate';
380 $form['#submit'][] = '_image_gallery_access_form_submit';
381 }
382
383 function _image_gallery_access_admin_form_disable_checkboxes($element) {
384 global $user;
385 static $role_permissions = array();
386 $element_children = element_children($element);
387 if (empty($role_permissions)) {
388 $auth_permissions = '';
389 foreach ($element_children as $rid) {
390 $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid WHERE r.rid = %d', $rid);
391 while ($role = db_fetch_object($result)) {
392 $role_permissions[$role->rid] = $role->perm .', '. ($role->rid == DRUPAL_ANONYMOUS_RID ? '' : $auth_permissions);
393 if ($role->rid == DRUPAL_AUTHENTICATED_RID) {
394 $auth_permissions = $role->perm .', ';
395 }
396 }
397 }
398 }
399 $tr = 't';
400
401 foreach ($element_children as $rid) {
402 if (strstr($role_permissions[$rid], 'access content,') === FALSE) {
403 $element[$rid]['#disabled'] = TRUE;
404 $element[$rid]['#default_value'] = FALSE;
405 $element[$rid]['#prefix'] = '<span title="'. t("This role does not have the '@perm' permission.", array('@perm' => $tr('access content'))) .'">';
406 $element[$rid]['#suffix'] = "</span>";
407 }
408 if ($element['#parents'][1] == 'view' && strstr($role_permissions[$rid], 'administer forums,') !== FALSE) {
409 $element[$rid]['#title'] = '<em>'. $element[$rid]['#title'] .'</em>';
410 $element[$rid]['#prefix'] = '<span title="'. t("This role has the '@perm' permission, and granting '@View' enables the role holders to change the settings on this page, including @Access_control!", array('@perm' => $tr('administer forums'), '@View' => t('View'), '@Access_control' => t('Access control'))) .'">';
411 $element[$rid]['#suffix'] = "</span>";
412 }
413 elseif (strstr($role_permissions[$rid], 'administer nodes,') !== FALSE) {
414 $element[$rid]['#disabled'] = TRUE;
415 $element[$rid]['#default_value'] = TRUE;
416 $element[$rid]['#prefix'] = '<span title="'. t("This role has the '@perm' permission and thus full access to all nodes.", array('@perm' => $tr('administer nodes'))) .'">';
417 $element[$rid]['#suffix'] = "</span>";
418 }
419 }
420 return $element;
421 }
422
423 function _image_gallery_access_admin_form_after_build_acl0($form, $form_state) {
424 if (isset($form['#post']['image_gallery_access']['template']['taxonomy']))
425 {
426 // Get ACL's user_list for the template and replace it before ACL's after_build function gets its shot at it.
427 $template_tid = reset(array_values($form['#post']['image_gallery_access']['template']['taxonomy']));
428 if ($acl_id = db_result(db_query("SELECT acl_id from {acl} WHERE module = 'image_gallery_access' AND name = '%d'", $template_tid))) {
429 $f = acl_edit_form($acl_id, 'DUMMY');
430 $form['user_list']['#value'] = $f['user_list']['#default_value'];
431 }
432 }
433 return $form;
434 }
435
436 function _image_gallery_access_admin_form_after_build_acl2($form, $form_state) {
437 if (!count(unserialize($form['user_list']['#default_value'])) && !count(unserialize($form['user_list']['#value']))) {
438 $form['#collapsed'] = TRUE;
439 }
440 if ($form['user_list']['#default_value'] != $form['user_list']['#value']) {
441 $form['note']['#value'] = preg_replace('/<div>/', '<div class="warning">', $form['note']['#value']);
442 }
443 return $form;
444 }
445
446 function _image_gallery_access_admin_form_after_build($form, &$form_state) {
447 if (isset($form_state['clicked_button']['#name']) && $form_state['clicked_button']['#name'] == $form['template']['load_button']['#name'])
448 {
449 // Load a setting from a template:
450 $template_tid = reset(array_values($form['#post']['image_gallery_access']['template']['taxonomy']));
451 $form_state['values']['image_gallery_access']['template']['template_tid'] = $template_tid;
452 $form['template']['#collapsed'] = FALSE;
453
454 $settings = _image_gallery_access_get_settings($template_tid);
455 foreach (array('view', 'create', 'update', 'delete') as $grant_type) {
456 if (empty($form[$grant_type])) {
457 continue;
458 }
459 foreach (element_children($form[$grant_type]) as $tid) {
460 $checked = array_search($tid, $settings[$grant_type]) !== FALSE;
461 $form[$grant_type][$tid]['#value'] = ($checked ? $tid : 0);
462 }
463 }
464 $form['interference']['advanced']['priority']['#value'] = $settings['priority'];
465 if ($settings['priority'] != 0) {
466 $form['interference']['advanced']['#collapsed'] = FALSE;
467 }
468 }
469 elseif (is_array(reset($form_state['values']['image_gallery_access']['template']['taxonomy']))) {
470 $template_tid = reset(reset($form_state['values']['image_gallery_access']['template']['taxonomy']));
471 }
472 if (isset($template_tid)) {
473 $form['template']['select_by_default']['#value'] = ($template_tid && $template_tid == variable_get('image_gallery_access_default_template_tid', 0));
474 $form['template']['load_for_new']['#value'] = ($template_tid && $template_tid == variable_get('image_gallery_access_new_template_tid', 0));
475 }
476 return $form;
477 }
478
479 function _image_gallery_access_form_validate($form, &$form_state) {
480 global $user;
481
482 if ($user->uid == 1) {
483 return;
484 }
485 $access = $form_state['values']['image_gallery_access']; // shortcut
486 foreach ($access['view'] as $rid => $checked) {
487 if ($checked && isset($user->roles[$rid])) {
488 return;
489 }
490 }
491 form_set_error('image_gallery_access][view', t('You must assign %View access to a role that you hold.', array('%View' => 'View')));
492 }
493
494 function _image_gallery_access_form_submit($form, &$form_state) {
495 $access = $form_state['values']['image_gallery_access']; // shortcut
496
497 // Save template choice:
498 $template_tid = reset(array_values($access['template']['taxonomy']));
499 if ($access['template']['select_by_default']) {
500 variable_set('image_gallery_access_default_template_tid', $template_tid);
501 }
502 elseif (variable_get('image_gallery_access_default_template_tid', 0) == $template_tid) {
503 variable_del('image_gallery_access_default_template_tid');
504 }
505 if ($access['template']['load_for_new']) {
506 variable_set('image_gallery_access_new_template_tid', $template_tid);
507 }
508 elseif (variable_get('image_gallery_access_new_template_tid', 0) == $template_tid) {
509 variable_del('image_gallery_access_new_template_tid');
510 }
511
512 // check for changes
513 $is_changed = $is_new = strpos($_GET['q'] .'/', 'admin/content/image/add/') === 0;
514 $form_initial_values = $form; // avoid Coder warning
515 $form_initial_values = $form_initial_values['image_gallery_access'];
516 foreach (array('view', 'create', 'update', 'delete') as $grant_type) {
517 if (isset($form_initial_values[$grant_type])) {
518 $defaults = $form_initial_values[$grant_type]['#default_value'];
519 $defaults = array_flip($defaults);
520 foreach ($access[$grant_type] as $tid => $checked) {
521 $is_changed = $is_changed || (empty($form_initial_values[$grant_type][$tid]['#disabled']) && !empty($checked) != isset($defaults[$tid]));
522 }
523 }
524 }
525 if (!$is_changed && $access['acl']['user_list'] == $form_initial_values['acl']['user_list']['#default_value'] && $access['interference']['advanced']['priority'] == $form['image_gallery_access']['interference']['advanced']['priority']['#default_value']) {
526 drupal_set_message(t('The content access permissions are unchanged.'));
527 return;
528 }
529
530 db_query("DELETE FROM {image_gallery_access} WHERE tid = %d", $form_state['values']['tid']);
531
532 $iga_priority = isset($access['interference']['advanced']['priority']) ? $access['interference']['advanced']['priority'] : 0;
533 if (array_key_exists('acl', $access)) {
534 acl_save_form($access['acl'], $iga_priority);
535 }
536 foreach ($access['view'] as $rid => $checked) {
537 if (!empty($form_initial_values['view'][$rid]['#disabled'])) {
538 continue;
539 }
540 db_query("INSERT INTO {image_gallery_access} (tid, rid, grant_view, grant_update, grant_delete, grant_create, priority) VALUES (%d, %d, %d, %d, %d, %d, %d)", $form_state['values']['tid'], $rid, (bool) $checked, !empty($access['update'][$rid]), !empty($access['delete'][$rid]), !empty($access['create'][$rid]), $iga_priority);
541 }
542 $tid = $form_state['values']['tid'];
543 $link = l(t('edit'), 'admin/content/image/edit/'. $tid);
544 watchdog('access', 'Changed grants for %gallery gallery.', array('%gallery' => $form_state['values']['name']), WATCHDOG_NOTICE, $link);
545
546 if (!$is_new) {
547 if (!isset($access['update_choice']) || $access['update_choice'] == 2) {
548 node_access_needs_rebuild(TRUE);
549 }
550 elseif ($access['update_choice'] == 0) {
551 // update immediately (but use the batch functions anyway
552 $save_redirect = $form_state['redirect'];
553 $form_state['redirect'] = $_GET['q'];
554 $context = array();
555 $pending_error_messages = drupal_get_messages('error', FALSE);
556 $our_error_message_index = (isset($pending_error_messages['error']) ? count($pending_error_messages['error']) : 0);
557 _image_gallery_access_update_batch_finished(FALSE, array(), array()); // add our error message (in case we die underway)
558 _image_gallery_access_update_batch_operation($form_state['values']['tid'], 999999, 1, $context);
559 $pending_error_messages = drupal_get_messages('error', TRUE); // still alive, get and clear all 'error' messages
560 unset($pending_error_messages['error'][$our_error_message_index]); // remove our error message
561 foreach ($pending_error_messages['error'] as $message) { // replay any others
562 drupal_set_message($message, 'error');
563 }
564 _image_gallery_access_update_batch_finished(TRUE, array(), array());
565 $form_state['redirect'] = $save_redirect;
566 }
567 else {
568 // mass update in batch mode, modeled after node.module
569 $limit = $access['update_limit'];
570 $count = db_result(db_query("SELECT COUNT(DISTINCT n.nid) FROM {node} n LEFT JOIN {term_node} tn ON tn.nid = n.nid WHERE tn.tid = %d", $form_state['values']['tid']));
571 $batch = array(
572 'title' => t('Updating content access permissions'),
573 'file' => drupal_get_path('module', 'image_gallery_access') .'/image_gallery_access.admin.inc',
574 'operations' => array(
575 array('_image_gallery_access_update_batch_operation', array($form_state['values']['tid'], $limit, $count)),
576 ),
577 'finished' => '_image_gallery_access_update_batch_finished'
578 );
579 batch_set($batch);
580 }
581 }
582 }
583
584 /**
585 * Batch operation for image_gallery_access_form_submit().
586 *
587 * This is a mutlistep operation : we go through all nodes by packs of 20.
588 * The batch processing engine interrupts processing and sends progress
589 * feedback after 1 second execution time.
590 */
591 function _image_gallery_access_update_batch_operation($tid, $limit, $count, &$context) {
592 if (empty($context['sandbox'])) {
593 // Initiate multistep processing.
594 $context['sandbox']['progress'] = 0;
595 $context['sandbox']['current_node'] = 0;
596 $context['sandbox']['max'] = $count;
597 }
598
599 // Process the next 20 nodes.
600 $result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {term_node} tn ON tn.nid = n.nid WHERE n.nid > %d AND tn.tid = %d ORDER BY n.nid ASC", $context['sandbox']['current_node'], $tid, 0, $limit);
601 while ($row = db_fetch_array($result)) {
602 $loaded_node = node_load($row['nid'], NULL, TRUE);
603 // To preserve database integrity, only aquire grants if the node
604 // loads successfully.
605 if (!empty($loaded_node)) {
606 node_access_acquire_grants($loaded_node);
607 }
608 $context['sandbox']['progress']++;
609 $context['sandbox']['current_node'] = $loaded_node->nid;
610 }
611
612 // Multistep processing : report progress.
613 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
614 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
615 }
616 }
617
618 /**
619 * Post-processing for image_gallery_access_form_submit().
620 */
621 function _image_gallery_access_update_batch_finished($success, $results, $operations) {
622 if ($success) {
623 drupal_set_message(t('The content access permissions have been updated.'));
624 cache_clear_all();
625 }
626 else {
627 drupal_set_message(t('The content access permissions have not been properly updated.'), 'error');
628 }
629 }
630
631 /**
632 * Helper function to retrieve the settings for a gallery.
633 */
634 function _image_gallery_access_get_settings($tid = NULL) {
635 $return = array('view' => array(), 'create' => array(), 'update' => array(), 'delete' => array(), 'priority' => 0);
636 if (!isset($tid)) {
637 // Default to all users can read; all logged in users can post.
638 $return['view'] = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID);
639 $return['create'] = array(DRUPAL_AUTHENTICATED_RID);
640 }
641 else {
642 $result = db_query("SELECT * FROM {image_gallery_access} where tid=%d", $tid);
643 while ($access = db_fetch_object($result)) {
644 $row_received = TRUE;
645 if ($access->grant_view) {
646 $return['view'][] = $access->rid;
647 }
648 if ($access->grant_update) {
649 $return['update'][] = $access->rid;
650 }
651 if ($access->grant_delete) {
652 $return['delete'][] = $access->rid;
653 }
654 if ($access->grant_create) {
655 $return['create'][] = $access->rid;
656 }
657 if ($access->rid == DRUPAL_AUTHENTICATED_RID) { // this is our reference
658 $return['priority'] = $access->priority;
659 }
660 }
661 if (empty($row_received)) {
662 // Return default settings if no records found in database.
663 $return = _image_gallery_access_get_settings();
664 }
665 }
666 return $return;
667 }
668
669 /**
670 * Remove unusable 'edit' links from overview form.
671 * /
672 function _image_gallery_access_image_gallery_overview(&$form, &$form_state) {
673 global $user;
674 if ($user->uid == 1) {
675 return;
676 }
677 foreach ($form as $key => $value) {
678 if (preg_match('/^tid:(.*):0$/', $key, $matches)) {
679 if (!image_gallery_access_access($matches[1], 'view')) {
680 unset($form[$key]['edit']['#value']);
681 }
682 }
683 }
684 } /**/
685
686 /**
687 * We must know when a role is deleted.
688 */
689 function _image_gallery_access_user_admin_role_form(&$form, &$form_state) {
690 $form['#submit'][] = '_image_gallery_access_user_admin_role_submit';
691 }
692
693 /**
694 * If a role is deleted, we remove the grants it provided.
695 */
696 function _image_gallery_access_user_admin_role_submit(&$form, &$form_state) {
697 if ($form_state['values']['op'] == $form_state['values']['delete']) {
698 db_query("DELETE FROM {image_gallery_access} WHERE rid = %d", $form_state['values']['rid']);
699 db_query("DELETE FROM {node_access} WHERE gid = %d AND realm = 'image_gallery_access'", $form_state['values']['rid']);
700 }
701 }
702
703 /**
704 * Add warnings on Content Access admin forms where CA wants
705 * to control the same content types as we do.
706 */
707 function _image_gallery_access_content_access_admin_form() {
708 $tr = 't';
709 $variables = array(
710 '!Content_Access' => 'Content Access',
711 '!Image_Gallery_Access' => 'Image Gallery Access',
712 '!Image_Gallery_Access_link' => l('Image Gallery Access', 'admin/content/image'),
713 '%anonymous_user' => $tr('anonymous user'),
714 '%authenticated_user' => $tr('authenticated user'),
715 '%Advanced' => $tr('Advanced'),
716 '%content_type' => node_get_types('name', 'image'),
717 );
718 drupal_set_message(t('Note: In Drupal, access can only be granted, not taken away. Whatever access you grant here will not be reflected in the !Image_Gallery_Access_link settings, but !Image_Gallery_Access can only allow <i>more</i> access, not less.', $variables)
719 .'<br /><span class="error">'. t('Specifically, any rights granted to the %anonymous_user and/or the %authenticated_user will <b>override</b> the settings of !Image_Gallery_Access!', $variables) .'</span>'
720 .'<br />'. t('To avoid conflicts with !Image_Gallery_Access settings, you may want to lower the priority of !Content_Access (under %Advanced below) below the priority of !Image_Gallery_Access for the %content_type content type.', $variables), 'warning');
721 }

  ViewVC Help
Powered by ViewVC 1.1.2