-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKPSEARCH.inc
241 lines (208 loc) · 8.4 KB
/
KPSEARCH.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Provides a search for all chado stock-based Tripal Content regardless of type.
*
*/
class KPSEARCH extends ChadoCustomSearch {
/**
* Adds a KnowPulse-specific crop selector to the form.
*/
public function addCropFormElement(&$form, &$form_state) {
// Container.
$form['crops'] = array(
'#type' => 'markup',
'#prefix' => '<div class="crop-element form-item"><label>Crop</label>',
'#suffix' => '</div>',
'#weight' => -9,
);
// Crop options.
$options = unserialize(variable_get('kp_searches_crop_options', 'a:0:{}'));
// Determine the current crop, if one is selected.
$q = drupal_get_query_parameters();
if (isset($form_state['values'])) {
$genus_default = $form_state['values']['genus'];
}
elseif (is_array($q) AND isset($q['genus'])) {
$genus_default = $q['genus'];
}
else {
$class = get_called_class();
$details = $class::$info['filters']['genus'];
$genus_default = (isset($details['default'])) ? $details['default'] : NULL;
}
// Now add the form elements for each crop.
foreach ($options as $crop_key => $crop) {
// Determine whether this is the active genus or not.
$class = 'active';
if ($genus_default !== NULL AND $genus_default != $crop_key) {
$class = 'inactive';
}
// Format the title as a link to the search.
$search_url = $this::$menu['path'];
$search_q = $q;
$search_q['genus'] = $crop['genus'];
//$search_q['species'] = $crop['crop-species'];
$crop_link = l($crop['title'], $search_url, [
'query' => $search_q,
'attributes' => ['class' => [$class]]
]);
// Format the image as a link to the search.
$image = theme('image', [
'path' => url($crop['image']),
'width' => 75,
'height' => 75,
'alt' => $crop['title'],
]);
$crop_img_link = l($image, $search_url, [
'html' => TRUE,
'query' => $search_q,
'attributes' => ['class' => [$class]]
]);
// Contain both the image and title links in a container
// and add to the form.
$form['crops'][$crop_key] = [
'#type' => 'markup',
'#markup' => '<span class="crop">'
. '<div class="crop-img">' . $crop_img_link . '</div>'
. '<div class="crop-title">' . $crop_link . '</div>'
. '</span>',
];
}
}
/**
* Adds a Scientific name field with the genus set based on crop.
* @todo make this AJAX-driven.
*/
public function addSpeciesFormElement(&$form, &$form_state) {
$class = get_called_class();
$description = $class::$info['filters']['genus']['help'];
$form['scientific_name'] = array(
'#type' => 'markup',
'#prefix' => '<div id="scientific-nameform-item" class="scientific-name-element form-item"><label>Scientific Name</label>',
'#suffix' => '<div class="description">' . $description . '</div></div>',
'#weight' => -8,
);
$options = unserialize(variable_get('kp_searches_crop_options', 'a:0:{}'));
$genus_options = [];
foreach ($options as $genus => $crop_details) {
$genus_options[ $genus ] = $genus;
}
$q = drupal_get_query_parameters();
if (isset($form_state['values'])
AND isset($options[ $form_state['values']['genus'] ])) {
$species = $options[ $form_state['values']['genus'] ]['species'];
$genus_default = $form_state['values']['genus'];
$species_default = $form_state['values']['species'];
}
elseif (isset($q['genus']) AND isset($options[ $q['genus'] ])) {
$species = $options[ $q['genus'] ]['species'];
$genus_default = $q['genus'];
$species_default = $q['species'];
}
else {
$details = $class::$info['filters']['genus'];
$genus_default = (isset($details['default'])) ? $details['default'] : NULL;
$species = [];
$details = $class::$info['filters']['species'];
$species_default = (isset($details['default'])) ? $details['default'] : NULL;
}
$form['scientific_name']['genus'] = $form['genus'];
$form['scientific_name']['genus']['#required'] = TRUE;
$form['scientific_name']['genus']['#type'] = 'select';
$form['scientific_name']['genus']['#options'] = $genus_options;
$form['scientific_name']['genus']['#empty_option'] = '- Select Genus -';
$form['scientific_name']['genus']['#default_value'] = $genus_default;
$form['scientific_name']['genus']['#attributes'] = ['onchange' => 'this.form.submit();'];
unset($form['genus']);
$form['scientific_name']['species'] = $form['species'];
$form['scientific_name']['species']['#type'] = 'select';
$form['scientific_name']['species']['#options'] = $species;
$form['scientific_name']['species']['#empty_option'] = '- Select Species -';
$form['scientific_name']['species']['#default_value'] = $species_default;
$form['scientific_name']['species']['#attributes'] = ['onchange' => 'this.form.submit();'];
unset($form['species']);
}
/**
* Add field to allow filter based on germplasm collection
* or population information.
*
* Source: stockcollection, stockcollection_stock and stock tables.
*
* @param $form
* Drupal form object.
* @param $form_state
* Drupal form state object.
*/
public function addCollectionFormElement(&$form, &$form_state) {
$class = get_called_class();
$default_text = '- Select -';
// Set default to all collection.
$collection_options = [0 => $default_text];
// Default to select (option 0 above array, of the field).
$default_collection = 0;
// Get collections in chado.stockcollections.
$sql_collection = "SELECT stockcollection_id AS id, name
FROM chado.stockcollection ORDER BY name ASC";
$collections = chado_query($sql_collection);
if ($collections->rowCount() > 0) {
foreach($collections as $collection) {
// Collection id number - Collection Name: Field will return
// collection id number, NOT THE NAME.
$collection_options[ $collection->id ] = $collection->name;
}
}
$q = drupal_get_query_parameters();
// Refine collection options to only collection names
// where a registered germplasm has the genus of the selected genus.
if (isset($q) && is_array($q) && $collection_options) {
if (isset($q['genus']) && !empty($q['genus'])) {
$genus = $q['genus'];
$species = (empty($q['species'])) ? '' : $q['species'];
// A single stock matching the genus (from collection) would be enough
// to satisfy that collection has a germplasm with the genus.
$sql_new_collection = "
SELECT stock_id AS id FROM {stock} INNER JOIN {organism} USING(organism_id)
WHERE organism.genus = :genus %s AND stock.stock_id IN
(SELECT stock_id FROM {stockcollection_stock} WHERE stockcollection_id = :collection)
LIMIT 1
";
// Add filter by species. GENUS + SPECIES.
$filter = '';
$args = [];
if (!empty($species)) {
$filter = 'AND organism.species = :species';
$args[':species'] = $species;
}
$sql_new_collection = sprintf($sql_new_collection, $filter);
// When user starts selecting filters... prepare
// collection field and suggest relevant to selections.
$new_collection_options = [0 => $default_text];
foreach($collection_options as $i => $collection) {
if ($i > 0) {
// Skip default select option 0.
$args[':genus'] = $genus;
$args[':collection'] = $i;
$has_genus = chado_query($sql_new_collection, $args)
->fetchObject();
if ($has_genus && $has_genus->id > 0) {
$new_collection_options[ $i ] = $collection;
}
}
}
}
// Use currently selected collection not the default.
$default_collection = $q['collection'];
}
$options = (empty($genus) && count($new_collection_options) < 1)
? $collection_options : $new_collection_options;
// Field collection render array.
$collection_field = $class::$info['filters']['collection'];
$form['collection'] = array(
'#type' => 'select',
'#title' => t($collection_field['title']),
'#description' => t($collection_field['help']),
'#options' => $options,
'#default_value' => $default_collection
);
}
}