1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Definition of shs_handler_filter_term_node_tid.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Filter by term id (including selection by simple hierarchical select).
|
10 |
|
|
*
|
11 |
|
|
* @ingroup views_filter_handlers
|
12 |
|
|
*/
|
13 |
|
|
class shs_handler_filter_term_node_tid extends views_handler_filter_term_node_tid {
|
14 |
|
|
|
15 |
|
|
function option_definition() {
|
16 |
|
|
$options = parent::option_definition();
|
17 |
|
|
|
18 |
|
|
$options['type'] = array('default' => 'shs');
|
19 |
|
|
$options['use_chosen'] = array('default' => 'never');
|
20 |
|
|
|
21 |
|
|
return $options;
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
function extra_options_form(&$form, &$form_state) {
|
25 |
|
|
parent::extra_options_form($form, $form_state);
|
26 |
|
|
|
27 |
|
|
$form['type']['#options'] += array(
|
28 |
|
|
'shs' => t('Simple hierarchical select'),
|
29 |
|
|
);
|
30 |
|
|
|
31 |
|
|
if (module_exists('chosen')) {
|
32 |
|
|
$form['use_chosen'] = array(
|
33 |
|
|
'#type' => 'select',
|
34 |
|
|
'#title' => t('Output this field with !chosen', array('!chosen' => l(t('Chosen'), 'http://drupal.org/project/chosen'))),
|
35 |
|
|
'#description' => t('Select in which cases the element will use the !chosen module for the term selection of each level.', array('!chosen' => l(t('Chosen'), 'http://drupal.org/project/chosen'))),
|
36 |
|
|
'#default_value' => empty($this->options['use_chosen']) ? 'never' : $this->options['use_chosen'],
|
37 |
|
|
'#dependency' => array('radio:options[type]' => array('shs')),
|
38 |
|
|
'#options' => array(
|
39 |
|
|
'chosen' => t('let chosen decide'),
|
40 |
|
|
'always' => t('always'),
|
41 |
|
|
'never' => t('never'),
|
42 |
|
|
),
|
43 |
|
|
);
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
function value_form(&$form, &$form_state) {
|
48 |
|
|
parent::value_form($form, $form_state);
|
49 |
|
|
|
50 |
|
|
$multiple = $this->options['expose']['multiple'];
|
51 |
|
|
$vocabulary = taxonomy_vocabulary_machine_name_load($this->options['vocabulary']);
|
52 |
|
|
if (empty($vocabulary) && $this->options['limit']) {
|
53 |
|
|
$form['markup'] = array(
|
54 |
|
|
'#markup' => '<div class="form-item">' . t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
|
55 |
|
|
);
|
56 |
|
|
return;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
if ($this->options['type'] == 'shs' && $this->options['exposed']) {
|
60 |
|
|
$identifier = $this->options['expose']['identifier'];
|
61 |
|
|
|
62 |
|
|
// Get parents of selected term.
|
63 |
|
|
$default_value = empty($this->value) ? 0 : $this->value;
|
64 |
|
|
|
65 |
|
|
if (!empty($form_state['input'][$identifier])) {
|
66 |
|
|
$default_value = $form_state['input'][$identifier];
|
67 |
|
|
if (!is_array($default_value) && $multiple) {
|
68 |
|
|
$default_value = explode('+', $default_value);
|
69 |
|
|
foreach ($default_value as $delta => $value) {
|
70 |
|
|
$default_value[$delta] = explode(',', $value);
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
$parents = array();
|
76 |
|
|
if (!empty($default_value) && $default_value != 'All') {
|
77 |
|
|
if (is_array($default_value) && $default_value[0] != 'All') {
|
78 |
|
|
foreach ($default_value as $delta => $level) {
|
79 |
|
|
foreach ($level as $tid) {
|
80 |
|
|
$parents[$delta]['tid'] = $level;
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
|
|
}
|
84 |
|
|
elseif (is_string($default_value)) {
|
85 |
|
|
$term_parents = taxonomy_get_parents_all($default_value);
|
86 |
|
|
// Remove selected term from list.
|
87 |
|
|
array_shift($term_parents);
|
88 |
|
|
foreach (array_reverse($term_parents) as $term) {
|
89 |
|
|
$parents[] = array('tid' => $term->tid);
|
90 |
|
|
}
|
91 |
|
|
// Add current term (name not needed).
|
92 |
|
|
$parents[] = array('tid' => $default_value);
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
// Add fake item for next level.
|
97 |
|
|
$parents[] = array('tid' => 0);
|
98 |
|
|
|
99 |
|
|
$element_settings = array(
|
100 |
|
|
'create_new_terms' => FALSE,
|
101 |
|
|
'create_new_levels' => FALSE,
|
102 |
|
|
'required' => FALSE,
|
103 |
|
|
);
|
104 |
|
|
if (module_exists('chosen') && !empty($this->options['use_chosen'])) {
|
105 |
|
|
$element_settings['use_chosen'] = $this->options['use_chosen'];
|
106 |
|
|
}
|
107 |
|
|
if (!empty($this->options['exposed']) && !empty($this->options['expose']['required'])) {
|
108 |
|
|
$element_settings['required'] = TRUE;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
// Generate a random hash to avoid merging of settings by drupal_add_js.
|
112 |
|
|
// This is necessary until http://drupal.org/node/208611 lands for D7.
|
113 |
|
|
$js_hash = _shs_create_hash();
|
114 |
|
|
// Create settings needed for our js magic.
|
115 |
|
|
$settings_js = array(
|
116 |
|
|
'shs' => array(
|
117 |
|
|
$identifier => array(
|
118 |
|
|
$js_hash => array(
|
119 |
|
|
'vid' => $vocabulary->vid,
|
120 |
|
|
'settings' => $element_settings,
|
121 |
|
|
'default_value' => $default_value,
|
122 |
|
|
'parents' => $parents,
|
123 |
|
|
'multiple' => $multiple,
|
124 |
|
|
),
|
125 |
|
|
),
|
126 |
|
|
),
|
127 |
|
|
);
|
128 |
|
|
// Add settings.
|
129 |
|
|
drupal_add_js($settings_js, 'setting');
|
130 |
|
|
// Add behavior.
|
131 |
|
|
drupal_add_js(drupal_get_path('module', 'shs') . '/js/shs.js');
|
132 |
|
|
|
133 |
|
|
if (is_array($default_value) && $multiple && $default_value[0] != 'All') {
|
134 |
|
|
foreach ($default_value as $delta => $level) {
|
135 |
|
|
if ($delta == 0) {
|
136 |
|
|
$default_value_str = implode(',', $level);
|
137 |
|
|
}
|
138 |
|
|
else {
|
139 |
|
|
$default_value_str .= '+' . implode(',', $level);
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
// Rewrite default select to textfield.
|
145 |
|
|
$form['value'] = array(
|
146 |
|
|
'#type' => 'textfield',
|
147 |
|
|
'#default_value' => empty($default_value_str) ? 'All' : $default_value_str,
|
148 |
|
|
'#attributes' => array(
|
149 |
|
|
'class' => array('element-invisible', 'shs-enabled'),
|
150 |
|
|
),
|
151 |
|
|
);
|
152 |
|
|
|
153 |
|
|
if (!empty($form_state['exposed']) && isset($identifier) && empty($form_state['input'][$identifier]) || $multiple && isset($form_state['input'][$identifier][0])) {
|
154 |
|
|
$form_state['input'][$identifier] = empty($default_value_str) ? 'All' : $default_value_str;
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
if (empty($form_state['exposed'])) {
|
159 |
|
|
// Retain the helper option.
|
160 |
|
|
$this->helper->options_form($form, $form_state);
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
function admin_summary() {
|
165 |
|
|
// Set up $this->value_options for the parent summary.
|
166 |
|
|
$this->value_options = array();
|
167 |
|
|
|
168 |
|
|
if ($this->value == 'All') {
|
169 |
|
|
$this->value = NULL;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
return parent::admin_summary();
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
} |