link_views_handler_filter_protocol.inc 4.3 KB

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