link_views_handler_argument_target.inc 4.6 KB

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