link_views_handler_filter_protocol.inc 4.3 KB

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