Projet

Général

Profil

Paste
Télécharger (6,89 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / privatemsg / pm_block_user / pm_block_user.pages.inc @ e3063c4a

1
<?php
2

    
3
/**
4
 * @file
5
 * User menu callbacks for pm_block_user.module.
6
 */
7

    
8
function pm_block_user_form($form, $form_state, $author) {
9
  global $user;
10

    
11
  $form['author'] = array(
12
    '#type' => 'value',
13
    '#value' => $author,
14
  );
15
    $form['recipient'] = array(
16
    '#type' => 'value',
17
    '#value' => $user,
18
  );
19
  $form['destination'] = array(
20
    '#type' => 'value',
21
    '#value' => isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
22
  );
23
  if (pm_block_user_has_blocked($author, $user)) {
24
    $form['block_action'] = array(
25
      '#type' => 'value',
26
      '#value' => 'unblock_user',
27
    );
28
    return confirm_form($form,
29
      t('You have previously blocked "@author" from sending you any more messages. Are you sure you want to unblock this user?', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
30
      isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
31
      '',
32
      t('Unblock @author', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
33
      t('Cancel')
34
    );
35
  }
36
  else {
37
    $form['block_action'] = array(
38
      '#type' => 'value',
39
      '#value' => 'block_user',
40
    );
41
    return confirm_form($form,
42
      t('Are you sure you want to block "@author" from sending you any more messages?', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
43
      isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
44
      '',
45
      t('Block @author', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
46
      t('Cancel')
47
    );
48
  }
49
}
50

    
51
/**
52
 * Submit callback for block user confirmation form.
53
 */
54
function pm_block_user_form_submit($form, &$form_state) {
55
  if ($form_state['values']['confirm']) {
56
    switch ($form_state['values']['block_action']) {
57
      case 'block_user':
58
        db_insert('pm_block_user')
59
          ->fields(array(
60
            'author' => $form_state['values']['author']->uid,
61
            'recipient' => $form_state['values']['recipient']->uid,
62
          ))
63
          ->execute();
64
        drupal_set_message(t('@author has been blocked from sending you any further messages.', array('@author' => privatemsg_recipient_format($form_state['values']['author'], array('plain' => TRUE)))));
65
      break;
66
      case 'unblock_user':
67
        db_delete('pm_block_user')
68
          ->condition('author', $form_state['values']['author']->uid)
69
          ->condition('recipient', $form_state['values']['recipient']->uid)
70
          ->execute();
71
        drupal_set_message(t('@author is now allowed to send you new messages.', array('@author' => privatemsg_recipient_format($form_state['values']['author'], array('plain' => TRUE)))));
72
      break;
73
    }
74
  }
75
  $form_state['redirect'] = $form_state['values']['destination'];
76
}
77

    
78
/**
79
 * Formbuilder function to build a simple form to block users.
80
 */
81
function pm_block_user_list() {
82
  global $user;
83

    
84
  $form['new'] = array(
85
    '#type'   => 'fieldset',
86
    '#title'  => t('Block a user'),
87
  );
88
  $form['new']['name'] = array(
89
    '#type' => 'textfield',
90
    '#title' => t('Username'),
91
    '#autocomplete_path' => 'messages/user/autocomplete',
92
    '#description'        => t('Separate multiple names with commas.'),
93
    '#required'          => TRUE,
94
  );
95
  $form['new']['submit'] = array(
96
    '#type' => 'submit',
97
    '#value' => t('Block user'),
98
    '#validate' => array('pm_block_user_block_validate'),
99
    '#submit' => array('pm_block_user_block_submit'),
100
  );
101

    
102
  $header = array(
103
    array(
104
      'data' => t('Username'),
105
      'field' => 'u.name',
106
      'sort'  => 'asc',
107
      ),
108
    array(
109
      'data' => t('Operations'),
110
      ),
111
  );
112

    
113
  $select = db_select('pm_block_user', 'pmb')->extend('PagerDefault')->extend('TableSort')
114
    ->fields('pmb', array('author'))
115
    ->condition('pmb.recipient', $user->uid)
116
    ->limit(20)
117
    ->orderByHeader($header);
118

    
119
  // Only show existing users, therefore join users.
120
  $select->innerJoin('users', 'u', 'u.uid = pmb.author');
121

    
122
  $rows = array();
123
  foreach ($select->execute() as $row) {
124
    $rows[] = array(
125
      theme('username', array('account' => user_load($row->author))),
126
      l(t('unblock'), 'messages/block/' . $row->author, array('query' => drupal_get_destination())),
127
    );
128
  }
129
  $form['#header'] = $header;
130
  $form['#rows'] = $rows;
131
  return $form;
132
}
133

    
134
/**
135
 * Validate user names.
136
 */
137
function pm_block_user_block_validate($form, &$form_state) {
138
  global $user;
139
  list($accounts, $invalid) = _privatemsg_parse_userstring($form_state['values']['name'], array('user'));
140
  // Remove accounts that can not be blocked.
141
  if (!empty($accounts)) {
142
    foreach ($accounts as $id => $account) {
143

    
144
      // Only authors can be blocked.
145
      if ($account->type != 'user') {
146
        drupal_set_message(t('Only users can be blocked.'));
147
        unset($accounts[$id]);
148
        continue;
149
      }
150

    
151
      // Check if the user can not be blocked because of a rule.
152
      if (_pm_block_user_rule_exists($account, $user, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
153
        drupal_set_message(t('You are not allowed to block !account.', array('!account' => theme('username', array('account' => $account)))), 'warning');
154
        unset($accounts[$id]);
155
        continue;
156
      }
157
      // Check if the user is already blocked.
158
      if (pm_block_user_has_blocked($account, $user)) {
159
        drupal_set_message(t('You have already blocked !account.', array('!account' => theme('username', array('account' => $account)))), 'warning');
160
        unset($accounts[$id]);
161
        continue;
162
      }
163
      // Do not allow users to block themself.
164
      if ($user->uid == $account->uid) {
165
        drupal_set_message(t('You can not block yourself.'), 'warning');
166
        unset($accounts[$id]);
167
        continue;
168
      }
169
    }
170
  }
171
  // Display warning about invalid user names.
172
  if (!empty($invalid)) {
173
    drupal_set_message(t('The following users do not exist: @invalid.', array('@invalid' => implode(", ", $invalid))), 'warning');
174
  }
175
  // If there are no accounts left, display error.
176
  if (empty($accounts)) {
177
    form_set_error('name', t('You are either not allowed to block these users or the users do not exist.'));
178
  }
179
  else {
180
    $form_state['valid_accounts'] = $accounts;
181
  }
182
}
183

    
184
/**
185
 * Submit callback for block user form.
186
 */
187
function pm_block_user_block_submit($form, &$form_state) {
188
  global $user;
189
  $insert = db_insert('pm_block_user')->fields(array('author', 'recipient'));
190
  foreach ($form_state['valid_accounts'] as $account) {
191
    $insert->values(array(
192
      'author' => $account->uid,
193
      'recipient' => $user->uid,
194
    ));
195
    drupal_set_message(t('!author has been blocked from sending you any further messages.', array('!author' => theme('username', array('account' => $account)))));
196
  }
197
  $insert->execute();
198
}
199

    
200
/**
201
 * Theme function to theme the blocked user listing.
202
 */
203
function theme_pm_block_user_list($variables) {
204
  $form = $variables['form'];
205
  return drupal_render_children($form) . theme('table', array('header' => $form['#header'], 'rows' =>  $form['#rows'])) . theme('pager');
206
}