-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.php
More file actions
86 lines (71 loc) · 2.42 KB
/
resources.php
File metadata and controls
86 lines (71 loc) · 2.42 KB
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
<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../lib/handler.php';
/**
* Resources page controller
*/
class Resources extends BaseHandler {
public function getSearch(){
if(isset($_GET['search']) && trim($_GET['search']) != ''){
return strip_tags(trim($_GET['search']));
}
return '';
}
public function getPage(){
if(isset($_GET['page']) && trim($_GET['page']) != ''){
return strip_tags(trim($_GET['page']));
}
return 0;
}
public function searchResources($search = '', $page = 0) {
$url = sprintf('%s/api/resource/search', $this->getConfig('resources_url'));
$resp = $this->callAPI('get', $url, [
'search' => $search,
'page' => $page,
]);
if($resp['status_code'] > 299){
throw new Exception('there was an error on the resource server');
}
return json_decode($resp['result'], true);
}
public function renderSearchResults(array $resources = []) {
return $this->render('resource/result_list.html', [
'resources' => $resources,
]);
}
public function prepare(){
$this->ensureSettingOr500('resources_url');
}
/**
* the get method will take care of both loading the page and searching
* for resources via an ajax call. If it is an ajax call, an array
* containing the search results and a flag stating if there are more
* results will be retunred. Otherwise, the whole page is rendered
*/
public function get(){
$search = $this->getSearch();
$page = $this->getPage();
$resources = $this->searchResources($search, $page);
$results = $this->renderSearchResults($resources);
// p($resources);
if($this->isAjax()){
return $this->json([
'id' => 'result_set-' . $resources['data']['id'],
'results' => $results,
'all_tags' => $resources['data']['all_tags'],
]);
}
$page = $this->render('resource/page.html', [
'page' => $page,
'search' => $search,
'content' => $results,
'all_tags' => $resources['data']['all_tags'],
]);
return $this->page($page);
}
}
class ResourceView extends BaseHandler {
public function get($resource_id){
var_dump("viewing", $resource_id);
}
}