6 * Contains filter handlers for protocol filters with views.
10 * Filter handler for limiting a view to URLs of a certain protocol.
12 class link_views_handler_filter_protocol
extends views_handler_filter_string
{
14 * Set defaults for the filter options.
16 function options(&$options) {
17 parent
::options($options);
18 $options['operator'] = 'OR';
19 $options['value'] = 'http';
24 * Define the operators supported for protocols.
26 function operators() {
29 'title' => t('Is one of'),
31 'method' => 'op_protocol',
39 function options_form(&$form, &$form_state) {
40 parent
::options_form($form, $form_state);
41 $form['case'] = array(
48 * Provide a select list to choose the desired protocols.
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.
56 if (!empty($form_state['exposed']) && empty($this->options
['expose']['operator'])) {
57 $which = in_array($this->operator
, $this->operator_values(1)) ?
'value' : 'none';
60 if ($which == 'all' || $which == 'value') {
61 $form['value'] = array(
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'))),
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.'),
74 * Filter down the query to include only the selected protocols.
76 function op_protocol($field, $upper) {
79 $protocols = $this->value
;
81 $where_conditions = array();
82 foreach ($protocols as
$protocol) {
83 // Simple case, the URL begins with the specified protocol.
84 $condition = $field .
' LIKE \''.
$protocol .
'%\'';
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]))'.
'\'';
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]))'.
'\'';
102 $where_conditions[] = $condition;
105 $this->query
->add_where($this->options
['group'], implode(' '.
$this->operator .
' ', $where_conditions));