Projet

Général

Profil

Paste
Télécharger (7,69 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / forum_access / forum_access.node.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * forum_access.node.inc
6
 *
7
 * Include file for forum_access.module, containing (sub-)page handling
8
 * (form_alter) code for the node and comment forms as well as code
9
 * for temporarily assigning the 'forum moderator' role and managing
10
 * the resulting rights.
11
 */
12

    
13
/**
14
 * Really implements hook_node_view_alter().
15
 *
16
 * Removes the "Add new comment" link and form from a node page if the user
17
 * is not allowed to add comments to that node.
18
 */
19
function _forum_access_node_view_alter(&$build, $tid) {
20
  global $user;
21

    
22
  //dpm($build, '_forum_access_node_view_alter() BEFORE:');
23
  $node = $build['#node'];
24
  if (forum_access_node_access($node, 'create', $user) == NODE_ACCESS_DENY) {
25
    unset($build['links']['comment']['#links']['comment-add']);
26
    unset($build['comments']['comment_form']);
27
  }
28
  //dpm($build, '_forum_access_node_view_alter() AFTER:');
29
}
30

    
31
/**
32
 * Really implements hook_comment_view_alter().
33
 *
34
 * Adds and removes action links from/to one comment.
35
 */
36
function _forum_access_comment_view_alter(&$build, $tid) {
37
  global $user;
38

    
39
  $node = $build['#node'];
40
  $comment = $build['#comment'];
41
  //dpm($build, "_forum_access_comment_view_alter() tid=[$tid] cid=[$comment->cid] BEFORE:");
42
  $links =& $build['links']['comment']['#links'];
43

    
44
  if ($user->uid != 1 && !user_access('bypass node access') && !forum_access_access('create', $tid)) {
45
    unset($links['comment-reply']);
46
  }
47

    
48
  if (!user_access('administer comments') || !empty($user->_forum_access_moderator)) {
49
    $default_link_keys = array(
50
      'create' => array('comment-reply'),
51
      'update' => array('comment-edit', 'comment-approve'),
52
      'delete' => array('comment-delete'),
53
    );
54
    forum_access_enable_moderator();
55
    $admin_links = comment_links($comment, $node);
56
    forum_access_enable_moderator(FALSE);
57
    //dpm($admin_links, "_forum_access_comment_view_alter() ADMINISTRATOR_LINKS for comment/$comment->cid:");
58
    foreach (array_keys($default_link_keys) as $op) {
59
      $access = (bool) forum_access_access($op, $tid);
60
      if ($op != 'create') {
61
        $perm = ($op == 'update' ? 'edit' : $op);
62
        $access |= ($user->uid == $comment->uid && user_access("$perm own forum content"));
63
      }
64
      $available_keys = variable_get("forum_access_allowed_comment_links_for_$op", $default_link_keys[$op]);
65
      $old_links = $links;
66
      $links = array();
67
      foreach ($default_link_keys[$op] as $key) {
68
        if ($access && array_search($key, $available_keys) !== FALSE && isset($admin_links[$key])) {
69
          //dpm($key, '_forum_access_comment_view_alter() ADDED:');
70
          $links[$key] = $admin_links[$key];
71
        }
72
        if (!$access && !empty($old_links[$key])) {
73
          //dpm($key, '_forum_access_comment_view_alter() REMOVED:');
74
          unset($old_links[$key]);
75
        }
76
      }
77
      if (is_array($old_links)) {
78
        $links = array_merge($links, $old_links);
79
      }
80
    }
81
    //dpm($build, "_forum_access_comment_view_alter() tid=[$tid] cid=[$comment->cid] AFTER:");
82
  }
83
}
84

    
85
/**
86
 * Rewrites the taxonomy item on the node form.
87
 */
88
function _forum_access_node_form(&$form, &$form_state) {
89
  global $user;
90
  $vid = _forum_access_get_vid();
91

    
92
  if (!isset($form['taxonomy_forums'])) {
93
    return;
94
  }
95

    
96
  // True node administrators are all powerful and do NOT get their forms rewritten here.
97
  if (user_access('bypass node access') && empty($user->_forum_access_moderator)) {
98
    return;
99
  }
100

    
101
  $roles = array_keys($user->roles);
102
  $tids = array();
103
  $result = db_query("SELECT tid FROM {forum_access} WHERE rid IN (:roles) AND grant_create = 1", array(
104
    ':roles' => $roles,
105
  ));
106
  foreach ($result as $obj) {
107
    $tids[$obj->tid] = $obj->tid;
108
  }
109

    
110
  // Also get all forums they happen to be able to moderate.
111
  $result = db_query("SELECT a.number AS tid FROM {acl} a INNER JOIN {acl_user} u ON a.acl_id = u.acl_id WHERE a.module = 'forum_access' AND u.uid = :uid", array(
112
    ':uid' => $user->uid,
113
  ));
114
  foreach ($result as $obj) {
115
    $tids[$obj->tid] = $obj->tid;
116
  }
117

    
118
  $nid = $form['nid']['#value'];
119
  if (!empty($nid)) {
120
    // Edit an existing node.
121
    if (!forum_access_access('update', $form['forum_tid']['#value']) && !user_access('edit any forum content') && !(user_access('edit own forum content') && $form['uid']['#value'] == $user->uid)) {
122
      drupal_access_denied();
123
      drupal_exit();
124
    }
125
    $forum_tid = $form['forum_tid']['#value'];
126
    $tids[$forum_tid] = $forum_tid;
127
  }
128
  else {
129
    // Create a new node -- ensure the forum they're trying to post to directly
130
    // is allowed, otherwise there will be much confusion.
131
    $forum_tid = arg(3);
132
    if (!empty($forum_tid) && is_numeric($forum_tid) && !isset($tids[$forum_tid])) {
133
      drupal_access_denied();
134
      drupal_exit();
135
    }
136
  }
137

    
138
  $form_options = &$form['taxonomy_forums'][$form['taxonomy_forums']['#language']]['#options'];
139
  $options = array();
140
  foreach ($form_options as $tid => $name) {
141
    if (!is_numeric($tid)) {
142
      $options[$tid] = $name;
143
    }
144
    elseif (is_object($name)) {
145
      foreach ($name->option as $sub_tid => $sub_name) {
146
        if (!empty($tids[$sub_tid])) {
147
          $options[$tid]->option[$sub_tid] = $sub_name;
148
        }
149
      }
150
    }
151
    elseif (isset($tids[$tid])) {
152
      $options[$tid] = $name;
153
    }
154
  }
155
  $form_options = $options;
156

    
157
  // Apply modifications for Moderators (by role or uid).
158
  if (!user_access('administer nodes') && forum_access_is_moderator($user, $forum_tid)) {
159
    $allowed_options = variable_get('forum_access_allowed_node_edit_options', array('status', 'sticky', 'subscriptions_notify'));
160
    foreach (element_children($form) as $key) {
161
      switch ($key) {
162
        case 'buttons':
163
          $tid = $form['taxonomy'][$vid]['#default_value'][0];
164
          if (!forum_access_access('update', $tid)) {
165
            $form['buttons']['submit']['#access'] = FALSE;
166
            $form['buttons']['preview']['#access'] = FALSE;
167
          }
168
          if (!forum_access_access('delete', $tid)) {
169
            $form['buttons']['delete']['#access'] = FALSE;
170
          }
171
          break;
172
        case 'author':
173
          $form['author']['#disabled'] = TRUE;
174
          break;
175
        case 'options':
176
          foreach (element_children($form['options']) as $key2) {
177
            if (array_search($key2, $allowed_options) === FALSE) {
178
              $form['options'][$key2]['#access'] = FALSE;
179
            }
180
          }
181
          $form['options']['#access'] = 1;
182
          break;
183
        case 'revision_information':
184
          $form['revision_information']['#access'] = 1;
185
          break;
186
        case 'comment_settings':
187
          $form['comment_settings']['#access'] = 1;
188
          break;
189
        case 'shadow':
190
          $form['shadow']['#description'] .= '<br />' . t('Note: Access to this topic and its shadows is controlled by the forum that contains the topic.');
191
          break;
192
        case 'taxonomy_forums':
193
          $desc =& $form['taxonomy_forums'][$form['taxonomy_forums']['#language']]['#description'];
194
          if (!empty($desc)) {
195
            $desc .= '<br />';
196
          }
197
          $desc .= t('Note: Moving a topic to a different forum may change its accessibility.');
198
      }
199
    }
200
  }
201
}
202

    
203
/**
204
 * Sanitizes the comment Administration options for users with Edit grants.
205
 */
206
function _forum_access_comment_form(&$form, &$form_state) {
207
  global $user;
208

    
209
  $comment = $form_state['comment'];
210
  if ($cid = $form['cid']['#value'] && isset($form['author']) && !empty($user->_forum_access_moderator)) {
211
    $editable_administration_elements = variable_get('forum_access_allowed_comment_edit_administration_elements', array('homepage', 'status', 'subscriptions_notify'));
212
    foreach (element_children($form['author']) as $key) {
213
      if (array_search($key, $editable_administration_elements) === FALSE) {
214
        $form['author'][$key]['#disabled'] = TRUE;
215
      }
216
    }
217
  }
218
}
219