Projet

Général

Profil

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

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

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
   * @codingStandardsIgnoreEnd
19
   */
20
  function option_definition() {
21
    $options = parent::option_definition();
22

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

    
27
    return $options;
28
  }
29

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

    
43
    return $operators;
44
  }
45

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

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

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

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

    
110
    $protocols = $this->value;
111

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

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

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

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

    
145
}