forked from OpenDevelopmentMekong/wp-odm_theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage-data.php
316 lines (277 loc) · 13.7 KB
/
page-data.php
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
Template Name: Data
*/
?>
<?php get_header(); ?>
<?php if(have_posts()) : the_post(); ?>
<div class="container">
<?php
if (!class_exists("WP_Odm_Solr_CKAN_Manager") || !WP_Odm_Solr_CKAN_Manager()->ping_server()): ?>
<div class="row">
<div class="sixteen columns">
<p class="error">
<?php _e("wp-odm_solr plugin is not properly configured. Please contact the system's administrator","wp-odm_solr"); ?>
</p>
</div>
</div>
<?php
else: ?>
<?php
// get following variables from URL for filtering
$param_query = !empty($_GET['query']) ? $_GET['query'] : null;
$param_license = !empty($_GET['license']) ? $_GET['license'] : null;
$param_taxonomy = isset($_GET['taxonomy']) ? $_GET['taxonomy'] : null;
$param_type = isset($_GET['type']) ? $_GET['type'] : 'all';
$param_language = isset($_GET['language']) ? $_GET['language'] : null;
$param_page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
$param_country = odm_country_manager()->get_current_country() == 'mekong' && isset($_GET['country']) ? $_GET['country'] : odm_country_manager()->get_current_country();
$param_sorting = isset($_GET['sorting']) ? $_GET['sorting'] : 'score';
//Get Datasets
$attrs = array(
'dataset_type' => '("dataset" OR "library_record" OR "laws_record" OR "agreement")',
'capacity' => 'public'
);
$control_attrs = array(
'limit' => 15,
'page' => $param_page,
'sorting' => $param_sorting
);
//================ Filter Values ===================== //
$taxonomy_list = odm_taxonomy_manager()->get_taxonomy_list();
$countries = odm_country_manager()->get_country_codes();
$languages = odm_language_manager()->get_supported_languages();
$license_list = wpckan_get_license_list();
//================ Build Filters ===================== //
//Taxonomy
if (!empty($param_taxonomy) && $param_taxonomy != 'all'):
$attrs["extras_taxonomy"] = $param_taxonomy;
endif;
//dataset type
if (!empty($param_type) && $param_type != 'all'):
$attrs["dataset_type"] = $param_type;
endif;
// Language
if (!empty($param_language) && $param_language != 'all'):
$attrs["extras_odm_language"] = $param_language;
endif;
// Country
if (!empty($param_country) && $param_country != 'mekong' && $param_country != 'all'):
$attrs["extras_odm_spatial_range"] = $countries[$param_country]['iso2'];
endif;
//License
if (!empty($param_license) && $param_license != 'all'):
$attrs['license_id'] = $param_license;
endif;
$result = WP_Odm_Solr_CKAN_Manager()->query($param_query,$attrs,$control_attrs);
$results = $result["resultset"];
$facets = $result["facets"];
//================== Pagination ======================
$request_url = $_SERVER['REQUEST_URI'];
$url_parts = parse_url($request_url);
if (isset($url_parts['query'])):
parse_str($url_parts['query'], $params);
else:
$params = [];
endif;
//Next Page Link
$next_page_params = $params;
$next_page_url_parts = $url_parts;
$next_page_params['page'] = $param_page + 1;
$next_page_url_parts['query'] = http_build_query($next_page_params);
$next_page_link = $next_page_url_parts['path'] . '?' . $next_page_url_parts['query'];
//Prev Page Link
$prev_page_params = $params;
$prev_page_url_parts = $url_parts;
$prev_page_params['page'] = $param_page - 1;
$prev_page_url_parts['query'] = http_build_query($prev_page_params);
$prev_page_link = $prev_page_url_parts['path'] . '?' . $prev_page_url_parts['query'];
$total_pages = ceil($results->getNumFound()/$control_attrs['limit']);
?>
<div class="row">
<div class="four columns">
<div class="data-advanced-filters">
<form>
<h3><i class="fa fa-filter"></i> Filters</h3>
<!-- TAXONOMY FILTER -->
<div class="single-filter">
<label for="taxonomy"><?php _e('Topic', 'odm'); ?></label>
<select id="taxonomy" name="taxonomy" class="filter_box" data-placeholder="<?php _e('Select term', 'odm'); ?>">
<option value="all" selected><?php _e('All','odm') ?></option>
<?php
foreach($taxonomy_list as $value):
if (array_key_exists("vocab_taxonomy",$facets)):
$taxonomy_facets = $facets["vocab_taxonomy"];
if (array_key_exists($value,$taxonomy_facets)):
$available_records = $taxonomy_facets[$value];
if ($available_records > 0): ?>
<option value="<?php echo $value; ?>" <?php if($value == $param_taxonomy) echo 'selected'; ?>><?php echo $value . " (" . $available_records . ")"; ?></option>
<?php
endif;
endif;
endif;
endforeach; ?>
</select>
</div>
<!-- END OF TAXONOMY FILTER -->
<!-- DATASET TYPE FILTER -->
<div class="single-filter">
<label for="type"><?php _e('Dataset type', 'odm'); ?></label>
<select id="type" name="type" class="filter_box">
<option <?php if($param_type == "all") echo 'selected'; ?> value="all"><?php _e('All','odm') ?></option>
<option <?php if($param_type == "dataset") echo 'selected'; ?> value="dataset"><?php _e('Datasets','odm') ?></option>
<option <?php if($param_type == "library_record") echo 'selected'; ?> value="library_record"><?php _e('Publications','odm') ?></option>
<option <?php if($param_type == "laws_record") echo 'selected'; ?> value="laws_record"><?php _e('Laws','odm') ?></option>
<option <?php if($param_type == "agreement") echo 'selected'; ?> value="agreement"><?php _e('Agreements','odm') ?></option>
</select>
</div>
<!-- END DATASET TYPE FILTER -->
<!-- COUNTRY FILTER -->
<?php if (odm_country_manager()->get_current_country() == 'mekong'): ?>
<div class="single-filter">
<label for="country"><?php _e('Country', 'odm'); ?></label>
<select id="country" name="country" class="filter_box" data-placeholder="<?php _e('Select country', 'odm'); ?>">
<option value="all" selected><?php _e('All','odm') ?></option>
<?php
foreach($countries as $key => $value):
if ($key != 'mekong'):
if (array_key_exists("extras_odm_spatial_range",$facets)):
$spatial_range_facets = $facets["extras_odm_spatial_range"];
$country_codes = odm_country_manager()->get_country_codes();
$country_code = $country_codes[$key]["iso2"];
if (array_key_exists($country_code,$spatial_range_facets)):
$available_records = $spatial_range_facets[$country_code];
if ($available_records > 0): ?>
<option value="<?php echo $key; ?>" <?php if($key == $param_country) echo 'selected'; ?> <?php if (odm_country_manager()->get_current_country() != 'mekong' && $key != odm_country_manager()->get_current_country()) echo 'disabled'; ?>><?php echo odm_country_manager()->get_country_name($key) . " (" . $available_records . ")"; ?></option>
<?php
endif;
endif;
endif;
endif; ?>
<?php
endforeach; ?>
</select>
</div>
<?php endif; ?>
<!-- END OF COUNTRY FILTER -->
<!-- LANGUAGE FILTER -->
<div class="single-filter">
<label for="language"><?php _e('Language', 'odm'); ?></label>
<select id="language" name="language" class="filter_box" data-placeholder="<?php _e('Select language', 'odm'); ?>">
<option value="all" selected><?php _e('All','odm') ?></option>
<?php
foreach($languages as $key => $value):
if (array_key_exists("extras_odm_language",$facets)):
$language_facets = $facets["extras_odm_language"];
if (array_key_exists($key,$language_facets)):
$available_records = $language_facets[$key];
if ($available_records > 0): ?>
<option value="<?php echo $key; ?>" <?php if($key == $param_language) echo 'selected'; ?>><?php echo $value . " (" . $available_records . ")" ?></option>
<?php
endif;
endif;
endif;
endforeach; ?>
</select>
</div>
<!-- END OF LANGUAGE FILTER -->
<!-- LICENSE FILTER -->
<div class="single-filter">
<label for="license"><?php _e('License', 'odm'); ?></label>
<select id="license" name="license" class="filter_box" data-placeholder="<?php _e('Select license', 'odm'); ?>">
<option value="all" selected><?php _e('All','odm') ?></option>
<?php
foreach($license_list as $license):
if (array_key_exists("license_id",$facets)):
$license_facets = $facets["license_id"];
if (array_key_exists($license->id,$license_facets)):
$available_records = $license_facets[$license->id];
if ($available_records > 0): ?>
<option value="<?php echo $license->id; ?>" <?php if($license->id == $param_license) echo 'selected'; ?>><?php echo $license->title . " (" . $available_records . ")" ?></option>
<?php
endif;
endif;
endif;
endforeach; ?>
</select>
</div>
<!-- END OF LICENSE FILTER -->
<!-- SORTING FUNCTION -->
<h3><i class="fa fa-sort"></i> Sorting</h3>
<div class="single-filter">
<label for="sorting"><?php _e('Sort by', 'odm'); ?></label>
<select id="sorting" name="sorting" class="filter_box" data-placeholder="<?php _e('Sort by', 'odm'); ?>">
<option <?php if($param_sorting == "score") echo 'selected'; ?> value="score"><?php _e('Relevance','odm') ?></option>
<option <?php if($param_sorting == "metadata_modified") echo 'selected'; ?> value="metadata_modified"><?php _e('Date modified','odm') ?></option>
</select>
</div>
<!-- END OF LICENSE FILTER -->
<div class="single-filter">
<input class="button" type="submit" value="<?php _e('Search Filter', 'odm'); ?>"/>
</div>
</div>
</div>
<div class="twelve columns">
<div class="search_bar">
<input id="search_field" type="text" class="full-width-search-box" name="query" placeholder="<?php _e('Type your search here', 'odm'); ?>" value="<?php echo esc_attr($param_query); ?>" data-solr-host="<?php echo $GLOBALS['wp_odm_solr_options']->get_option('wp_odm_solr_setting_solr_host'); ?>" data-solr-scheme="<?php echo $GLOBALS['wp_odm_solr_options']->get_option('wp_odm_solr_setting_solr_scheme'); ?>" data-solr-path="<?php echo $GLOBALS['wp_odm_solr_options']->get_option('wp_odm_solr_setting_solr_path'); ?>" data-solr-core-wp="<?php echo $GLOBALS['wp_odm_solr_options']->get_option('wp_odm_solr_setting_solr_core_wp'); ?>" data-solr-core-ckan="<?php echo $GLOBALS['wp_odm_solr_options']->get_option('wp_odm_solr_setting_solr_core_ckan'); ?>"></input>
</form>
</div>
<div class="results_info"><h2><?php echo $results->getNumFound() ?> records found.</h2></div>
<div class="result_container container">
<?php foreach($results as $document):
odm_get_template('solr-result-single',array(),true);
endforeach; ?>
</div>
</div>
</div>
<?php
endif;
endif; ?>
<script>
jQuery(document).ready(function() {
jQuery( ".filter_box" ).select2();
jQuery('#search_field').autocomplete({
source: function( request, response ) {
var host = jQuery('#search_field').data("solr-host");
var scheme = jQuery('#search_field').data("solr-scheme");
var path = jQuery('#search_field').data("solr-path");
var core_wp = jQuery('#search_field').data("solr-core-wp");
var core_ckan = jQuery('#search_field').data("solr-core-ckan");
var url = scheme + "://" + host + path + core_ckan + "/suggest";
console.log("pulling suggestions from: " + url);
jQuery.ajax({
url: url,
data: {'wt':'json', 'q':request.term, 'json.wrf': 'callback'},
dataType: "jsonp",
jsonpCallback: 'callback',
contentType: "application/json",
success: function( data ) {
var options = [];
if (data){
if(data.spellcheck){
var spellcheck = data.spellcheck;
if (spellcheck.suggestions){
var suggestions = spellcheck.suggestions;
if (suggestions[1]){
var suggestionObject = suggestions[1];
options = suggestionObject.suggestion;
}
}
}
}
response( options );
}
});
},
minLength: 2,
select: function( event, ui ) {
var terms = this.value.split(" ");
terms.pop();
terms.push( ui.item.value );
this.value = terms.join( " " );
return false;
}
});
});
</script>
<?php get_footer(); ?>