link_views_handler_argument_target.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Argument handler to filter results by target.
  5. */
  6. /**
  7. * Argument handler to filter results by target.
  8. *
  9. * @codingStandardsIgnoreStart
  10. */
  11. class link_views_handler_argument_target extends views_handler_argument {
  12. /**
  13. * Provide defaults for the argument when a new one is created.
  14. */
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. return $options;
  18. }
  19. /**
  20. * Provide a default options form for the argument.
  21. *
  22. * @codingStandardsIgnoreStart
  23. */
  24. public function options_form(&$form, &$form_state) {
  25. // @codingStandardsIgnoreEnd
  26. $defaults = $this->default_actions();
  27. $form['title'] = array(
  28. '#prefix' => '<div class="clear-block">',
  29. '#suffix' => '</div>',
  30. '#type' => 'textfield',
  31. '#title' => t('Title'),
  32. '#default_value' => $this->options['title'],
  33. '#description' => t('The title to use when this argument is present; it will override the title of the view and titles from previous arguments. You can use percent substitution here to replace with argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
  34. );
  35. $form['clear_start'] = array(
  36. '#value' => '<div class="clear-block">',
  37. );
  38. $form['defaults_start'] = array(
  39. '#value' => '<div class="views-left-50">',
  40. );
  41. $form['default_action'] = array(
  42. '#type' => 'radios',
  43. '#title' => t('Action to take if argument is not present'),
  44. '#default_value' => $this->options['default_action'],
  45. );
  46. $form['defaults_stop'] = array(
  47. '#value' => '</div>',
  48. );
  49. $form['wildcard'] = array(
  50. '#prefix' => '<div class="views-right-50">',
  51. // Prefix and no suffix means these two items will be grouped together.
  52. '#type' => 'textfield',
  53. '#title' => t('Wildcard'),
  54. '#size' => 20,
  55. '#default_value' => $this->options['wildcard'],
  56. '#description' => t('If this value is received as an argument, the argument will be ignored; i.e, "all values"'),
  57. );
  58. $form['wildcard_substitution'] = array(
  59. '#suffix' => '</div>',
  60. '#type' => 'textfield',
  61. '#title' => t('Wildcard title'),
  62. '#size' => 20,
  63. '#default_value' => $this->options['wildcard_substitution'],
  64. '#description' => t('The title to use for the wildcard in substitutions elsewhere.'),
  65. );
  66. $form['clear_stop'] = array(
  67. '#value' => '</div>',
  68. );
  69. $options = array();
  70. $validate_options = array();
  71. foreach ($defaults as $id => $info) {
  72. $options[$id] = $info['title'];
  73. if (empty($info['default only'])) {
  74. $validate_options[$id] = $info['title'];
  75. }
  76. if (!empty($info['form method'])) {
  77. $this->{$info['form method']}($form, $form_state);
  78. }
  79. }
  80. $form['default_action']['#options'] = $options;
  81. $form['validate_type'] = array(
  82. '#type' => 'select',
  83. '#title' => t('Validator'),
  84. '#default_value' => $this->options['validate_type'],
  85. );
  86. $validate_types = array('none' => t('- Basic validation -'));
  87. $plugins = views_fetch_plugin_data('argument validator');
  88. foreach ($plugins as $id => $info) {
  89. $valid = TRUE;
  90. if (!empty($info['type'])) {
  91. $valid = FALSE;
  92. if (empty($this->definition['validate type'])) {
  93. continue;
  94. }
  95. foreach ((array) $info['type'] as $type) {
  96. if ($type == $this->definition['validate type']) {
  97. $valid = TRUE;
  98. break;
  99. }
  100. }
  101. }
  102. // If we decide this validator is ok, add it to the list.
  103. if ($valid) {
  104. $plugin = views_get_plugin('argument validator', $id);
  105. if ($plugin) {
  106. $plugin->init($this->view, $this, $id);
  107. if ($plugin->access()) {
  108. $plugin->validate_form($form, $form_state, $id);
  109. $validate_types[$id] = $info['title'];
  110. }
  111. }
  112. }
  113. }
  114. asort($validate_types);
  115. $form['validate_type']['#options'] = $validate_types;
  116. // Show this gadget if *anything* but 'none' is selected.
  117. $form['validate_fail'] = array(
  118. '#type' => 'select',
  119. '#title' => t('Action to take if argument does not validate'),
  120. '#default_value' => $this->options['validate_fail'],
  121. '#options' => $validate_options,
  122. );
  123. }
  124. /**
  125. * Set up the query for this argument.
  126. *
  127. * The argument sent may be found at $this->argument.
  128. */
  129. public function query($group_by = FALSE) {
  130. $this->ensure_my_table();
  131. // Because attributes are stored serialized, our only option is to also
  132. // serialize the data we're searching for and use LIKE to find similar data.
  133. $this->query->add_where(0, $this->table_alias . ' . ' . $this->real_field . " LIKE '%%%s%'", serialize(array('target' => $this->argument)));
  134. }
  135. }