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