Projet

Général

Profil

Paste
Télécharger (4,27 ko) Statistiques
| Branche: | Révision:

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

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
 * @codingStandardsIgnoreStart
12
 */
13
class link_views_handler_filter_protocol extends views_handler_filter_string {
14

    
15
  /**
16
   * Set defaults for the filter options.
17
   */
18
  public function option_definition() {
19
    // @codingStandardsIgnoreEnd
20
    $options = parent::option_definition();
21

    
22
    $options['operator'] = 'OR';
23
    $options['value'] = 'http';
24
    $options['case'] = 0;
25

    
26
    return $options;
27
  }
28

    
29
  /**
30
   * Define the operators supported for protocols.
31
   */
32
  public function operators() {
33
    $operators = array(
34
      'OR' => array(
35
        'title' => t('Is one of'),
36
        'short' => t('='),
37
        'method' => 'op_protocol',
38
        'values' => 1,
39
      ),
40
    );
41

    
42
    return $operators;
43
  }
44

    
45
  /**
46
   * Options form.
47
   *
48
   * @codingStandardsIgnoreStart
49
   */
50
  public function options_form(&$form, &$form_state) {
51
    //@codingStandardsIgnoreEnd
52
    parent::options_form($form, $form_state);
53
    $form['case'] = array(
54
      '#type' => 'value',
55
      '#value' => 0,
56
    );
57
  }
58

    
59
  /**
60
   * Provide a select list to choose the desired protocols.
61
   *
62
   * @codingStandardsIgnoreStart
63
   */
64
  public function value_form(&$form, &$form_state) {
65
    // @codingStandardsIgnoreEnd
66
    // We have to make some choices when creating this as an exposed
67
    // filter form. For example, if the operator is locked and thus
68
    // not rendered, we can't render dependencies; instead we only
69
    // render the form items we need.
70
    $which = 'all';
71
    if (!empty($form_state['exposed']) && empty($this->options['expose']['operator'])) {
72
      $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
73
    }
74

    
75
    if ($which == 'all' || $which == 'value') {
76
      $form['value'] = array(
77
        '#type' => 'select',
78
        '#title' => t('Protocol'),
79
        '#default_value' => $this->value,
80
        '#options' => drupal_map_assoc(variable_get('filter_allowed_protocols', array(
81
          'http',
82
          'https',
83
          'ftp',
84
          'news',
85
          'nntp',
86
          'telnet',
87
          'mailto',
88
          'irc',
89
          'ssh',
90
          'sftp',
91
          'webcal',
92
        ))),
93
        '#multiple' => 1,
94
        '#size' => 4,
95
        '#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.'),
96
      );
97
    }
98
  }
99

    
100
  /**
101
   * Filter down the query to include only the selected protocols.
102
   *
103
   * @codingStandardsIgnoreStart
104
   */
105
  public function op_protocol($field, $upper) {
106
    // @codingStandardsIgnoreEnd
107
    $db_type = db_driver();
108

    
109
    $protocols = $this->value;
110

    
111
    $where_conditions = array();
112
    foreach ($protocols as $protocol) {
113
      // Simple case, the URL begins with the specified protocol.
114
      $condition = $field . ' LIKE \'' . $protocol . '%\'';
115

    
116
      // More complex case, no protocol specified but is automatically cleaned
117
      // up by link_cleanup_url(). RegEx is required for this search operation.
118
      if ($protocol == 'http') {
119
        $link_domains = _link_domains();
120
        if ($db_type == 'pgsql') {
121
          // PostGreSQL code has NOT been tested. Please report any problems to
122
          // the link issue queue.
123
          // pgSQL requires all slashes to be double escaped in regular
124
          // expressions.
125
          // @codingStandardsIgnoreLine
126
          // See http://www.postgresql.org/docs/8.1/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
127
          $condition .= ' OR ' . $field . ' ~* \'' . '^(([a-z0-9]([a-z0-9\\-_]*\\.)+)(' . $link_domains . '|[a-z][a-z]))' . '\'';
128
        }
129
        else {
130
          // mySQL requires backslashes to be double (triple?) escaped within
131
          // character classes.
132
          // @codingStandardsIgnoreLine
133
          // See http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_regexp
134
          $condition .= ' OR ' . $field . ' REGEXP \'' . '^(([a-z0-9]([a-z0-9\\\-_]*\.)+)(' . $link_domains . '|[a-z][a-z]))' . '\'';
135
        }
136
      }
137

    
138
      $where_conditions[] = $condition;
139
    }
140

    
141
    $this->query->add_where($this->options['group'], implode(' ' . $this->operator . ' ', $where_conditions));
142
  }
143

    
144
}