Branched the code to 6.x-2.x properly.
[project/link.git] / views / link_views_handler_filter_protocol.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Contains filter handlers for protocol filters with views.
7 */
8
9 /**
10 * Filter handler for limiting a view to URLs of a certain protocol.
11 */
12 class link_views_handler_filter_protocol extends views_handler_filter_string {
13 /**
14 * Set defaults for the filter options.
15 */
16 function options(&$options) {
17 parent::options($options);
18 $options['operator'] = 'OR';
19 $options['value'] = 'http';
20 $options['case'] = 0;
21 }
22
23 /**
24 * Define the operators supported for protocols.
25 */
26 function operators() {
27 $operators = array(
28 'OR' => array(
29 'title' => t('Is one of'),
30 'short' => t('='),
31 'method' => 'op_protocol',
32 'values' => 1,
33 ),
34 );
35
36 return $operators;
37 }
38
39 function options_form(&$form, &$form_state) {
40 parent::options_form($form, $form_state);
41 $form['case'] = array(
42 '#type' => 'value',
43 '#value' => 0,
44 );
45 }
46
47 /**
48 * Provide a select list to choose the desired protocols.
49 */
50 function value_form(&$form, &$form_state) {
51 // We have to make some choices when creating this as an exposed
52 // filter form. For example, if the operator is locked and thus
53 // not rendered, we can't render dependencies; instead we only
54 // render the form items we need.
55 $which = 'all';
56 if (!empty($form_state['exposed']) && empty($this->options['expose']['operator'])) {
57 $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
58 }
59
60 if ($which == 'all' || $which == 'value') {
61 $form['value'] = array(
62 '#type' => 'select',
63 '#title' => t('Protocol'),
64 '#default_value' => $this->value,
65 '#options' => drupal_map_assoc(variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'))),
66 '#multiple' => 1,
67 '#size' => 4,
68 '#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.'),
69 );
70 }
71 }
72
73 /**
74 * Filter down the query to include only the selected protocols.
75 */
76 function op_protocol($field, $upper) {
77 global $db_type;
78
79 $protocols = $this->value;
80
81 $where_conditions = array();
82 foreach ($protocols as $protocol) {
83 // Simple case, the URL begins with the specified protocol.
84 $condition = $field .' LIKE \''. $protocol .'%\'';
85
86 // More complex case, no protocol specified but is automatically cleaned up
87 // by link_cleanup_url(). RegEx is required for this search operation.
88 if ($protocol == 'http') {
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 }