-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSygefor3Requester.php
More file actions
123 lines (111 loc) · 3.57 KB
/
Copy pathSygefor3Requester.php
File metadata and controls
123 lines (111 loc) · 3.57 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
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
<?php
/**
* Make search on sygefor API
*
* Class Sygefor3Requester
*/
class Sygefor3Requester
{
/**
* @var string
*/
protected $urfistCode;
/**
* @var string
*/
protected $apiUrl;
public function __construct()
{
$this->urfistCode = get_option('sygefor3_urfist_code');
$this->apiUrl = get_option('sygefor3_api_address');
if (!$this->apiUrl || !$this->urfistCode || !get_option('sygefor3_session_list_page') || !get_option('sygefor3_training_page') || !get_option('sygefor3_calendar_page')) {
throw new Exception('You have to fill-in Sygefor3 Viewer settings');
}
}
/**
* Return sessions
*
* @param null $search
* @param null $theme
* @param null $tag
* @param $arguments
* @param int $page
*
* @return array|mixed|object
* @throws Exception
*/
public function getSessions($search = null, $theme = null, $tag = null, $arguments, $page = 1)
{
$dateBegin = new \DateTime("now", timezone_open('Europe/Paris'));
date_add($dateBegin, date_interval_create_from_date_string($arguments['dateBegin'] . ' months'));
$dateEnd = new \DateTime("now", timezone_open('Europe/Paris'));
date_add($dateEnd, date_interval_create_from_date_string($arguments['dateEnd'] . ' months'));
if ($page < 1) {
$page = 1;
}
$request = [
"sort" => ["dateBegin" => "asc"],
"size" => $arguments['size'],
"query" => [
"filtered" => [
"filter" => [
"and" => [
["term" => ["training.organization.code" => $this->urfistCode]],
["range" => ["dateBegin" => ['gte' => $dateBegin->format('Y-m-d')]]],
["range" => ["dateEnd" => ['lte' => $dateEnd->format('Y-m-d')]]]
]]
]
],
"facets" => [
"theme" => [ "terms" => ["field" => "training.theme.source", "order" => "term"]],
"tags" => [ "terms" => ["field" => "training.tags.source"]]
],
"from" => $arguments['size'] * ($page - 1)
];
if ($theme) {
$request['query']['filtered']['filter']['and'][] = ["term" => ["training.theme.source" => $theme]];
}
if ($tag) {
$request['query']['filtered']['filter']['and'][] = ["term" => ["training.tags.source" => $tag]];
}
if ($search) {
$request['query']["filtered"]["query"]["multi_match"] = ['query' => $search, "fields" => ["training.name", "training.program", "training.tags"]];
}
return $this->search("training/session", $request);
}
/**
* Return training
*
* @param $id
*
* @return mixed
*/
public function getTraining($id)
{
if (!$id) {
return null;
}
return $this->search("training/" . strval($id), array());
}
/**
* Search on API
*
* @param $address
* @param $request
*
* @return mixed|
*/
public function search($address, $request)
{
$opts = array(
'http'=>array(
'method' => "POST",
'header'=> "Content-type: application/json;charset=UTF-8",
'content' => json_encode($request)
)
);
$context = stream_context_create($opts);
$results = @file_get_contents($this->apiUrl . $address, null, $context);
return json_decode($results, true);
}
}