Projet

Général

Profil

Paste
Télécharger (3,66 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / link / views / link_views_handler_filter_protocol.inc @ c8740e19

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains filter handlers for protocol filters with views.
6
 */
7

    
8
/**
9
 * Filter handler for limiting a view to URLs of a certain protocol.
10
 */
11
class link_views_handler_filter_protocol extends views_handler_filter_string {
12
  /**
13
   * Set defaults for the filter options.
14
   */
15
  function options(&$options) {
16
    parent::options($options);
17
    $options['operator'] = 'OR';
18
    $options['value'] = 'http';
19
    $options['case'] = 0;
20
  }
21

    
22
  /**
23
   * Define the operators supported for protocols.
24
   */
25
  function operators() {
26
    $operators = array(
27
      'OR' => array(
28
        'title' => t('Is one of'),
29
        'short' => t('='),
30
        'method' => 'op_protocol',
31
        'values' => 1,
32
      ),
33
    );
34

    
35
    return $operators;
36
  }
37

    
38
  function options_form(&$form, &$form_state) {
39
    parent::options_form($form, $form_state);
40
    $form['case'] = array(
41
      '#type' => 'value',
42
      '#value' => 0,
43
    );
44
  }
45

    
46
  /**
47
   * Provide a select list to choose the desired protocols.
48
   */
49
  function value_form(&$form, &$form_state) {
50
    // We have to make some choices when creating this as an exposed
51
    // filter form. For example, if the operator is locked and thus
52
    // not rendered, we can't render dependencies; instead we only
53
    // render the form items we need.
54
    $which = 'all';
55
    if (!empty($form_state['exposed']) && empty($this->options['expose']['operator'])) {
56
      $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
57
    }
58

    
59
    if ($which == 'all' || $which == 'value') {
60
      $form['value'] = array(
61
        '#type' => 'select',
62
        '#title' => t('Protocol'),
63
        '#default_value' => $this->value,
64
        '#options' => drupal_map_assoc(variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'))),
65
        '#multiple' => 1,
66
        '#size' => 4,
67
        '#description' => t('The protocols displayed here are those globally available. You may add more protocols by modifying the <em>filter_allowed_protocols</em> variable in your installation.'),
68
      );
69
    }
70
  }
71

    
72
  /**
73
   * Filter down the query to include only the selected protocols.
74
   */
75
  function op_protocol($field, $upper) {
76
    $db_type = db_driver();
77

    
78
    $protocols = $this->value;
79

    
80
    $where_conditions = array();
81
    foreach ($protocols as $protocol) {
82
      // Simple case, the URL begins with the specified protocol.
83
      $condition = $field . ' LIKE \'' . $protocol . '%\'';
84

    
85
      // More complex case, no protocol specified but is automatically cleaned up
86
      // by link_cleanup_url(). RegEx is required for this search operation.
87
      if ($protocol == 'http') {
88
        $LINK_DOMAINS = _link_domains();
89
        if ($db_type == 'pgsql') {
90
          // PostGreSQL code has NOT been tested. Please report any problems to the link issue queue.
91
          // pgSQL requires all slashes to be double escaped in regular expressions.
92
          // See http://www.postgresql.org/docs/8.1/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
93
          $condition .= ' OR ' . $field . ' ~* \'' . '^(([a-z0-9]([a-z0-9\\-_]*\\.)+)(' . $LINK_DOMAINS . '|[a-z][a-z]))' . '\'';
94
        }
95
        else {
96
          // mySQL requires backslashes to be double (triple?) escaped within character classes.
97
          // See http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_regexp
98
          $condition .= ' OR ' . $field . ' REGEXP \'' . '^(([a-z0-9]([a-z0-9\\\-_]*\.)+)(' . $LINK_DOMAINS . '|[a-z][a-z]))' . '\'';
99
        }
100
      }
101

    
102
      $where_conditions[] = $condition;
103
    }
104

    
105
    $this->query->add_where($this->options['group'], implode(' ' . $this->operator . ' ', $where_conditions));
106
  }
107
}