link_views_handler_filter_protocol.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @file
  4. * Contains filter handlers for protocol filters with views.
  5. */
  6. /**
  7. * Filter handler for limiting a view to URLs of a certain protocol.
  8. */
  9. class link_views_handler_filter_protocol extends views_handler_filter_string {
  10. /**
  11. * Set defaults for the filter options.
  12. */
  13. function options(&$options) {
  14. parent::options($options);
  15. $options['operator'] = 'OR';
  16. $options['value'] = 'http';
  17. $options['case'] = 0;
  18. }
  19. /**
  20. * Define the operators supported for protocols.
  21. */
  22. function operators() {
  23. $operators = array(
  24. 'OR' => array(
  25. 'title' => t('Is one of'),
  26. 'short' => t('='),
  27. 'method' => 'op_protocol',
  28. 'values' => 1,
  29. ),
  30. );
  31. return $operators;
  32. }
  33. function options_form(&$form, &$form_state) {
  34. parent::options_form($form, $form_state);
  35. $form['case'] = array(
  36. '#type' => 'value',
  37. '#value' => 0,
  38. );
  39. }
  40. /**
  41. * Provide a select list to choose the desired protocols.
  42. */
  43. function value_form(&$form, &$form_state) {
  44. // We have to make some choices when creating this as an exposed
  45. // filter form. For example, if the operator is locked and thus
  46. // not rendered, we can't render dependencies; instead we only
  47. // render the form items we need.
  48. $which = 'all';
  49. if (!empty($form_state['exposed']) && empty($this->options['expose']['operator'])) {
  50. $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
  51. }
  52. if ($which == 'all' || $which == 'value') {
  53. $form['value'] = array(
  54. '#type' => 'select',
  55. '#title' => t('Protocol'),
  56. '#default_value' => $this->value,
  57. '#options' => drupal_map_assoc(variable_get('filter_allowed_protocols', array('http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'mailto', 'irc', 'ssh', 'sftp', 'webcal'))),
  58. '#multiple' => 1,
  59. '#size' => 4,
  60. '#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.'),
  61. );
  62. }
  63. }
  64. /**
  65. * Filter down the query to include only the selected protocols.
  66. */
  67. function op_protocol($field, $upper) {
  68. $db_type = db_driver();
  69. $protocols = $this->value;
  70. $where_conditions = array();
  71. foreach ($protocols as $protocol) {
  72. // Simple case, the URL begins with the specified protocol.
  73. $condition = $field . ' LIKE \'' . $protocol . '%\'';
  74. // More complex case, no protocol specified but is automatically cleaned up
  75. // by link_cleanup_url(). RegEx is required for this search operation.
  76. if ($protocol == 'http') {
  77. $LINK_DOMAINS = _link_domains();
  78. if ($db_type == 'pgsql') {
  79. // PostGreSQL code has NOT been tested. Please report any problems to the link issue queue.
  80. // pgSQL requires all slashes to be double escaped in regular expressions.
  81. // See http://www.postgresql.org/docs/8.1/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
  82. $condition .= ' OR ' . $field . ' ~* \'' . '^(([a-z0-9]([a-z0-9\\-_]*\\.)+)(' . $LINK_DOMAINS . '|[a-z][a-z]))' . '\'';
  83. }
  84. else {
  85. // mySQL requires backslashes to be double (triple?) escaped within character classes.
  86. // See http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_regexp
  87. $condition .= ' OR ' . $field . ' REGEXP \'' . '^(([a-z0-9]([a-z0-9\\\-_]*\.)+)(' . $LINK_DOMAINS . '|[a-z][a-z]))' . '\'';
  88. }
  89. }
  90. $where_conditions[] = $condition;
  91. }
  92. $this->query->add_where($this->options['group'], implode(' ' . $this->operator . ' ', $where_conditions));
  93. }
  94. }