/[drupal]/contributions/modules/advuser/advuser_filters.inc
ViewVC logotype

Diff of /contributions/modules/advuser/advuser_filters.inc

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

revision 1.1 by sign, Thu Jan 10 15:29:48 2008 UTC revision 1.2 by earnie, Mon Jun 9 20:18:56 2008 UTC
# Line 0  Line 1 
1    <?php
2    /**
3     * @file
4     * Advanced user module allows you to select users based on an advanced set of
5     * filtering and apply actions to block, unblock, delete or email the selected
6     * users.
7     *
8     * $Id: advuser_filters.inc,v 1.1.2.12 2008/06/09 17:26:38 earnie Exp $
9     **/
10    
11    /**
12     * Return form for advuser administration filters.
13     */
14    function advuser_filter_form() {
15      $session = &$_SESSION['advuser_overview_filter'];
16      $session = is_array($session) ? $session : array();
17      $filters = advuser_filters();
18    
19      $i = 0;
20      $form['filters'] = array(
21        '#type' => 'fieldset',
22        '#title' => t('Show only users where'),
23        '#theme' => 'advuser_filters',
24        '#collapsible' => TRUE,
25        );
26      foreach ($session as $filter) {
27        list($type, $value, $op, $qop) = $filter;
28        // Merge an array of arrays into one if necessary.
29        if ($filters[$type]['form_type'] == 'select') {
30          $options = $type == 'permission'
31                   ? call_user_func_array(
32                      'array_merge',
33                      $filters[$type]['options']
34                     )
35                   : $filters[$type]['options']
36          ;
37          $params = array(
38            '%property' => $filters[$type]['title'],
39            '%value' => $options[$value]
40          );
41        } else {
42          $params = array(
43            '%property' => $filters[$type]['title'],
44            '%value' => $value
45          );
46        }
47        if ($i++ > 0) {
48          $form['filters']['current'][] = array(
49            '#value' => t('<em>'.
50                          $op.
51                          '</em> where <strong>%property</strong> '.
52                          _qop($qop).
53                          ' <strong>%value</strong>',
54                          $params
55                         ).
56                        ($i == count($session) ? ')' : ''),
57          );
58        }
59        else {
60          $form['filters']['current'][] = array(
61            '#value' => t('(<strong>%property</strong> '.
62                          _qop($qop).
63                          ' <strong>%value</strong>',
64                          $params
65                         ).
66                        ($i == count($session) ? ')' : ''),
67          );
68        }
69      }
70    
71      foreach ($filters as $key => $filter) {
72        $names[$key] = $filter['title'];
73        switch ($filter['form_type']) {
74          case 'select': {
75            $form['filters']['status'][$key] = array(
76              '#type' => 'select',
77              '#options' => $filter['options'],
78            );
79          } break;
80          case 'date': {
81            $form['filters']['status'][$key] = array(
82              '#type' => 'textfield',
83              '#size' => 20,
84              '#maxlength' => 25,
85              '#default_value' => 'now',
86            );
87          } break;
88          case 'id': {
89            $form['filters']['status'][$key] = array(
90              '#type' => 'textfield',
91              '#size' => 5,
92              '#maxsize' => 10,
93            );
94          } break;
95          case 'textfield': {
96            $form['filters']['status'][$key] = array(
97              '#type' => 'textfield',
98              '#size' => 30,
99            );
100          } break;
101          default: {
102            $autocomplete = '';
103            if ($filter['autocomplete']) {
104              $autocomplete = "profile/autocomplete/". $filter['autocomplete'];
105            }
106            switch ($filter['type']) {
107              case 'selection': {
108                $form['filters']['status'][$key] = array(
109                  '#type' => 'select',
110                  '#options' => $filter['options'],
111                  '#autocomplete_path' => $autocomplete,
112                );
113              } break;
114              case 'checkbox': {
115                $form['filters']['status'][$key] = array(
116                  '#type' => 'checkbox',
117                );
118              } break;
119              default: {
120                $form['filters']['status'][$key] = array(
121                  '#type' => $filter['type'],
122                  '#options' => $filter['options'],
123                  '#autocomplete_path' => $autocomplete,
124                  '#size' => 20,
125                );
126              } break;
127            } //End switch ($filter['type']).
128          } break;
129        }
130      }
131    
132      $form['filters']['filter'] = array(
133        '#type' => 'radios',
134        '#options' => $names,
135      );
136      $form['filters']['buttons']['submit'] = array(
137        '#type' => 'submit',
138        '#value' => (count($session) ? t('Refine') : t('Filter'))
139      );
140      if (count($session)) {
141        $form['filters']['buttons']['undo'] = array(
142          '#type' => 'submit',
143          '#value' => t('Undo')
144        );
145        $form['filters']['buttons']['reset'] = array(
146          '#type' => 'submit',
147          '#value' => t('Reset')
148        );
149      }
150    
151      $form['filters']['filters_ops'] = array(
152        '#type' => 'select',
153        '#options' => array('AND' => t('and'), ') OR (' => t('or'))
154      );
155    
156      $form['filters']['filters_qops'] = array(
157        '#type' => 'select',
158        '#options' => array(
159          '=' => 'EQ',
160          '!=' => 'NE',
161          '<' => 'LT',
162          '>' => 'GT',
163          '<=' => 'LE',
164          '>=' => 'GE',
165          'LIKE' => 'CO',
166          'NOT LIKE' => 'NC',
167          'BEGINS WITH' => 'BE',
168          'ENDS WITH' => 'EN',
169        ),
170        '#attributes' => array('title' => t("
171          'EQ' => 'is equal to'
172          'NE' => 'is not equal to'
173          'LT' => 'is less than'
174          'GT' => 'is greater than'
175          'LE' => 'is less than or equal to'
176          'GE' => 'is greater than or equal to'
177          'CO' => 'contains'
178          'NC' => 'does not contain'
179          'BE' => 'begins with'
180          'EN' => 'ends with'")
181        ),
182      );
183    
184      return $form;
185    }
186    
187    /**
188     * Helper function for translating symbols to language
189     */
190    function _qop($qop) {
191      static $_qop = NULL;
192      if (!isset($_qop)) {
193        $_qop = array(
194          '=' => t('is equal to'),
195          '!=' => t('is not equal to'),
196          '<' => t('is less than'),
197          '>' => t('is greater than'),
198          '<=' => t('is less than or equal to'),
199          '>=' => t('is greater than or equal to'),
200          'LIKE' => t('contains'),
201          'NOT LIKE' => t('does not contain'),
202          'BEGINS WITH' => t('begins with'),
203          'ENDS WITH' => t('ends with'),
204        );
205      }
206      return isset($_qop[$qop]) ? $_qop[$qop] : $qop;
207    };
208    
209    /**
210     * Theme advuser administration filter form.
211     */
212    function theme_advuser_filter_form($form) {
213      $output = '<div id="user-admin-filter">';
214      $output .= drupal_render($form['filters']);
215      $output .= '</div>';
216      $output .= drupal_render($form);
217      return $output;
218    }
219    
220    /**
221     * Validate values entered.
222     */
223    function advuser_filter_form_validate($fid, &$fval) {
224      $ret = FALSE;
225    
226      if ($fid == 'advuser_filter_form') {
227        switch ($fval['filter']) {
228          case 'last_access': {
229            switch (strtolower($fval['last_access'])) {
230              case 'never': {
231                $fval['last_access'] = 0;
232                $ret = TRUE;
233              } break;;
234    
235              case '0': {
236                $ret = TRUE;
237              } break;
238    
239              default: {
240                if (!empty($fval['last_access']) && strtotime($fval['last_access']) <= 0) {
241                  form_set_error('date', t('You have to specify a valid date to filter by Accessed.'));
242                  $ret = FALSE;
243                }
244                else {
245                  $fval['last_access'] = strtotime($fval['last_access']);
246                  $ret = TRUE;
247                }
248              } break;
249            }
250          } break;
251    
252          case 'created': {
253            if (!empty($fval['created']) && strtotime($fval['created']) <= 0) {
254              form_set_error('date', t('You have to specify a valid date to filter by Created.'));
255              $ret = FALSE;
256            }
257            else {
258              $fval['created'] = strtotime($fval['created']);
259              $ret = TRUE;
260            }
261    
262          } break;
263        }
264      }
265      return $ret;
266    }
267    
268    /**
269     * List advuser administration filters that can be applied.
270     */
271    function advuser_filters() {
272      // Regular filters
273      $filters = array();
274      $options = array();
275      $t_module = t('module');
276      foreach (module_list() as $module) {
277        if ($permissions = module_invoke($module, 'perm')) {
278          asort($permissions);
279          foreach ($permissions as $permission) {
280            $options["$module $t_module"][$permission] = t($permission);
281          }
282        }
283      }
284    
285      ksort($options);
286      $filters['permission'] = array(
287        'title' => t('Permission'),
288        'where' => " ((u.uid %in (SELECT ur.uid FROM {users_roles} ur WHERE ur.rid %in (SELECT p.rid FROM {permission} p WHERE p.perm %op '%s'))) %andor u.uid %eq 1)",
289        'options' => $options,
290        'form_type' => 'select',
291      );
292    
293      $filters['status'] = array(
294        'title' => t('Status'),
295        'where' => "u.status %op '%s'",
296        'options' => array(1 => t('active'), 0 => t('blocked')),
297        'form_type' => 'select',
298      );
299    
300      $filters['created'] = array(
301        'title' => t('Created'),
302        'where' => "u.created %op '%s'",
303        'form_type' => 'date',
304      );
305    
306      $filters['last_access'] = array(
307        'title' => t('Accessed'),
308        'where' => "u.access %op '%s'",
309        'form_type' => 'date',
310      );
311    
312      $filters['email'] = array(
313        'title' => t('Email'),
314        'where' => "u.mail %op '%s'",
315        'form_type' => 'textfield',
316      );
317    
318      $filters['uid'] = array(
319        'title' => t('User Id'),
320        'where' => "u.uid %op %d",
321        'form_type' => 'id',
322      );
323    
324      $roles = advuser_user_roles();
325      if (count($roles)) {
326        $filters['user_roles'] = array(
327          'title' => t('Role'),
328          'where' => "ur.rid %op %d",
329          'form_type' => 'select',
330          'options' => $roles,
331        );
332      }
333    
334      $profile_fields = advuser_profile_fields();
335      foreach ($profile_fields as $field) {
336        // Build array of options if they exist
337        $opts = NULL;
338        if ( ! empty($field->options) ) {
339          $opts = array();
340          foreach ( explode("\n", $field->options) as $opt ) {
341            $opt = trim($opt);
342            $opts[$opt] = $opt;
343          }
344        }
345        // Each user defined profile field needs a unique table identifier for the
346        //  JOIN and WHERE clauses.
347        // TODO: Make sure the $field->name contains valid information for a table
348        //  identifier.  This comment is to identify the source of a problem yet
349        //  to be discovered.
350        $pv = $field->name;
351        $filters[$field->name] = array(
352          'title' => check_plain($field->title),
353          'type' => $field->type,
354          'class' => $field->name,
355          'where' => "$pv.value %op '%s' AND $pv.uid = u.uid",
356          'options' => $opts,
357          'autocomplete' => $field->autocomplete ? $field->fid : FALSE,
358        );
359      }
360      return $filters;
361    }
362    
363    /**
364     * Build query for advuser administration filters based on session.
365     */
366    function advuser_build_filter_query() {
367      $filters = advuser_filters();
368    
369      // Build query
370      $where = $args = $join = array();
371    
372      foreach ($_SESSION['advuser_overview_filter'] as $filter) {
373        list($key, $value, $op, $qop) = $filter;
374        // This checks to see if this permission filter is an enabled permission
375        // for the authenticated role.  If so, then all users would be listed, and
376        // we can skip adding it to the filter query.
377        switch ($key) {
378          case 'permission': {
379            $account = new stdClass();
380            $account->uid = 'advuser_filter';
381            $account->roles = array(DRUPAL_AUTHENTICATED_RID => 1);
382            if (user_access($value, $account)) {
383              continue;
384            }
385          } break;
386          case 'created':
387          case 'last_access': {
388            $value = strtotime($value);
389          } break;
390        }
391    
392        $arg_prefix = $arg_suffix = NULL;
393        switch ($qop) {
394          case 'NOT LIKE':
395          case 'LIKE': {
396            $arg_prefix = $arg_suffix = '%';
397          } break;
398          case 'BEGINS WITH': {
399            $qop = 'LIKE';
400            $arg_suffix = '%';
401          } break;
402          case 'ENDS WITH': {
403            $qop = 'LIKE';
404            $arg_prefix = '%';
405          } break;
406        }
407    
408        switch ($qop) {
409          case '!=':
410          case 'NOT LIKE': {
411            $in = 'NOT IN';
412            $eq = '!=';
413            $andor = 'AND';
414          } break;
415          default: {
416            $in = 'IN';
417            $eq = '=';
418            $andor = 'OR';
419          }
420        }
421    
422        $_where = $op.' '.str_ireplace("%op", $qop, $filters[$key]['where']);
423        $_where = str_ireplace("%eq", $eq, $_where);
424        $_where = str_ireplace("%andor", $andor, $_where);
425        $where[] = str_ireplace("%in", $in, $_where);
426        $args[] = $arg_prefix . $value . $arg_suffix;
427        $join[] = $filters[$key]['join'];
428      }
429    
430      $where = count($where) ? 'AND ('. implode(' ', $where) . ')' : '';
431      $join = count($join) ? ' '. implode(' ', array_unique($join)) : '';
432    
433      return array(
434        'where' => $where,
435        'join' => $join,
436        'args' => $args,
437      );
438    }
439    
440    /**
441     * Process result from user administration filter form.
442     */
443    function advuser_filter_form_submit($form_id, $form_values) {
444      $op = $form_values['op'];
445      $filters = advuser_filters();
446      $ret = 'admin/user/user/advuser';
447    
448      switch ($op) {
449        case t('Filter'):
450        case t('Refine'): {
451          if (isset($form_values['filter'])) {
452            $filter = $form_values['filter'];
453            if ($filters[$filter]['form_type'] == 'select') {
454              // Merge an array of arrays into one if necessary.
455              $options = $filter == 'permission'
456           ? call_user_func_array(
457          'array_merge',
458          $filters[$filter]['options']
459            )
460           : $filters[$filter]['options']
461        ;
462              if (isset($options[$form_values[$filter]])) {
463                $_SESSION['advuser_overview_filter'][] = array(
464            $filter,
465            $form_values[$filter],
466            $form_values['filters_ops'],
467            $form_values['filters_qops'],
468          );
469              }
470            } else {
471              if (isset($form_values[$filter])) {
472                $_SESSION['advuser_overview_filter'][] = array(
473            $filter,
474            $form_values[$filter],
475            $form_values['filters_ops'],
476            $form_values['filters_qops']
477          );
478              }
479            }
480          }
481        } break;
482        case t('Undo'): {
483          array_pop($_SESSION['advuser_overview_filter']);
484        } break;
485        case t('Reset'): {
486          $_SESSION['advuser_overview_filter'] = array();
487        } break;
488        case t('Update'): {
489          $ret = NULL;
490        } break;
491      }
492    
493      return $ret;
494    }
495    
496    /**
497     * Theme user administration filter selector.
498     */
499    function theme_advuser_filters($form) {
500    
501      $output = '<ul>';
502      if (sizeof($form['current'])) {
503        foreach (element_children($form['current']) as $key) {
504          $output .= '<li>' . drupal_render($form['current'][$key]) . '</li>';
505        }
506      }
507      $output .= '</ul>';
508    
509      $output .= '<div class="container-inline" id="user-admin-buttons">' . drupal_render($form['buttons']) . '</div>';
510      $output .= '<br/><br/>';
511      $output .= '<table class="multiselect ">';
512      foreach (element_children($form['filter']) as $key) {
513        $output .= '<tr>';
514        $output .= '<td>' . (sizeof($form['current']) ? drupal_render($form['filters_ops']) : '') . '</td>';
515        $output .= '<td class="a">' . drupal_render($form['filter'][$key]) . '</td>';
516        $output .= '<td>' . $f . drupal_render($form['filters_qops']) . '</td>';
517        $output .= '<td class="b">' . drupal_render($form['status'][$key]) . '</td>';
518        $output .= '</tr>';
519      }
520    
521      foreach (element_children($form['status']) as $key) {
522        $output .= drupal_render($form['status'][$key]);
523      }
524      $output .= '</td></tr>';
525    
526      $output .= '</table>';
527    
528      return $output;
529    }
530    
531    // vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.3