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 85ad3d82 Assos Assos
<?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 39a181a4 Assos Assos
 *
11
 * @codingStandardsIgnoreStart
12 85ad3d82 Assos Assos
 */
13
class link_views_handler_filter_protocol extends views_handler_filter_string {
14 39a181a4 Assos Assos
15 85ad3d82 Assos Assos
  /**
16
   * Set defaults for the filter options.
17 39a181a4 Assos Assos
   *
18
   * @codingStandardsIgnoreEnd
19 85ad3d82 Assos Assos
   */
20 39a181a4 Assos Assos
  function option_definition() {
21
    $options = parent::option_definition();
22
23 85ad3d82 Assos Assos
    $options['operator'] = 'OR';
24
    $options['value'] = 'http';
25
    $options['case'] = 0;
26 39a181a4 Assos Assos
27
    return $options;
28 85ad3d82 Assos Assos
  }
29
30
  /**
31
   * Define the operators supported for protocols.
32
   */
33 39a181a4 Assos Assos
  public function operators() {
34 85ad3d82 Assos Assos
    $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 39a181a4 Assos Assos
  /**
47
   * Options form.
48
   *
49
   * @codingStandardsIgnoreStart
50
   */
51
  public function options_form(&$form, &$form_state) {
52
    //@codingStandardsIgnoreEnd
53 85ad3d82 Assos Assos
    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 39a181a4 Assos Assos
   *
63
   * @codingStandardsIgnoreStart
64 85ad3d82 Assos Assos
   */
65 39a181a4 Assos Assos
  public function value_form(&$form, &$form_state) {
66
    // @codingStandardsIgnoreEnd
67 85ad3d82 Assos Assos
    // 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 39a181a4 Assos Assos
        '#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 85ad3d82 Assos Assos
        '#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 39a181a4 Assos Assos
   *
104
   * @codingStandardsIgnoreStart
105 85ad3d82 Assos Assos
   */
106 39a181a4 Assos Assos
  public function op_protocol($field, $upper) {
107
    // @codingStandardsIgnoreEnd
108 85ad3d82 Assos Assos
    $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 39a181a4 Assos Assos
      // More complex case, no protocol specified but is automatically cleaned
118
      // up by link_cleanup_url(). RegEx is required for this search operation.
119 85ad3d82 Assos Assos
      if ($protocol == 'http') {
120 39a181a4 Assos Assos
        $link_domains = _link_domains();
121 85ad3d82 Assos Assos
        if ($db_type == 'pgsql') {
122 39a181a4 Assos Assos
          // 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 85ad3d82 Assos Assos
          // See http://www.postgresql.org/docs/8.1/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
128 39a181a4 Assos Assos
          $condition .= ' OR ' . $field . ' ~* \'' . '^(([a-z0-9]([a-z0-9\\-_]*\\.)+)(' . $link_domains . '|[a-z][a-z]))' . '\'';
129 85ad3d82 Assos Assos
        }
130
        else {
131 39a181a4 Assos Assos
          // mySQL requires backslashes to be double (triple?) escaped within
132
          // character classes.
133
          // @codingStandardsIgnoreLine
134 85ad3d82 Assos Assos
          // See http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_regexp
135 39a181a4 Assos Assos
          $condition .= ' OR ' . $field . ' REGEXP \'' . '^(([a-z0-9]([a-z0-9\\\-_]*\.)+)(' . $link_domains . '|[a-z][a-z]))' . '\'';
136 85ad3d82 Assos Assos
        }
137
      }
138
139
      $where_conditions[] = $condition;
140
    }
141
142
    $this->query->add_where($this->options['group'], implode(' ' . $this->operator . ' ', $where_conditions));
143
  }
144 39a181a4 Assos Assos
145 85ad3d82 Assos Assos
}