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