-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFavouriteAgency.php
More file actions
151 lines (135 loc) · 4.34 KB
/
FavouriteAgency.php
File metadata and controls
151 lines (135 loc) · 4.34 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
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
<?php
class FavouriteAgency extends TingAgency {
public $userData;
public $orderAgency;
public function __construct($favourite) {
if (!isset($favourite['oui:agencyId'])) {
die('no agencyid in FavouriteAgency constructor');
}
parent::__construct($favourite['oui:agencyId']);
$this->userData = isset($favourite['oui:userData']) ? unserialize($favourite['oui:userData']) : NULL;
$this->orderAgency = ($favourite['oui:orderAgency'] == 'TRUE') ? TRUE : FALSE;
}
public function getOrderAgency() {
return $this->orderAgency;
}
public function getUserData() {
return $this->userData;
}
/** Get userid for favourite
*
* There are a number of possible userids. Run through userdata and return
* the first found
*
* @return boolean
*/
public function getUserId() {
$possibilities = array('cpr', 'userId', 'barcode', 'cardno', 'customId',);
foreach ($possibilities as $pos) {
if (isset($this->userData[$pos])) {
return $this->userData[$pos];
}
}
return FALSE;
}
public function getPinCode() {
return isset($this->userData['pincode']) ? $this->userData['pincode'] : FALSE;
}
public function getUserStatus() {
// check if userstatus is already in $_SESSION
if (isset($_SESSION['userStatus'][$this->getAgencyId()])) {
return $_SESSION['userStatus'][$this->getAgencyId()];
}
// get parameters
$userId = $this->getUserId();
$userPincode = $this->getPinCode();
$libraryCode = $this->getAgencyId();
if (module_exists('ting_openuserstatus')) {
// get userstatus from webservice
$response = ting_openuserstatus_do_userstatus($userId, $userPincode, $libraryCode);
if (isset($response['error'])) {
return $response;
}
// no errors found. add response to $_SESSION
$_SESSION['userStatus'][$this->getAgencyId()] = $response;
// last check
return isset($_SESSION['userStatus'][$this->getAgencyId()]) ? $_SESSION['userStatus'][$this->getAgencyId()] : FALSE;
}
// cannot retrive userstatus
return FALSE;
}
public function cancelOrder(array $orders) {
$userId = $this->getUserId();
$userPincode = $this->getPinCode();
$libraryCode = $this->getAgencyId();
if (module_exists('ting_openuserstatus')) {
$response = ting_openuserstatus_do_cancelorder($userId, $userPincode, $libraryCode, $orders);
if (empty($response['error'])) {
// an order has been cancelled. clear userstatus to update
$this->clearUserStatus();
}
return $response;
}
return FALSE;
}
public function updateOrder(array $updateOrders) {
$userId = $this->getUserId();
$userPincode = $this->getPinCode();
$libraryCode = $this->getAgencyId();
if (module_exists('ting_openuserstatus')) {
$response = ting_openuserstatus_do_update_order($userId, $userPincode, $libraryCode, $updateOrders);
if (empty($response['error'])) {
// an order has been updated. clear userstatus to update
$this->clearUserStatus();
}
return $response;
}
return FALSE;
}
public function renewLoan(array $loans) {
$userId = $this->getUserId();
$userPincode = $this->getPinCode();
$libraryCode = $this->getAgencyId();
if (module_exists('ting_openuserstatus')) {
$response = ting_openuserstatus_do_renew_loan($userId, $userPincode, $libraryCode, $loans);
if (empty($response['error'])) {
// an order has been updated. clear userstatus to update
$this->clearUserStatus();
}
return $this->parseFields($response);
}
return FALSE;
}
public function clearUserStatus() {
global $drupal_test_info;
if (isset($_SESSION['userStatus'][$this->getAgencyId()]) && !isset($drupal_test_info)) {
unset($_SESSION['userStatus'][$this->getAgencyId()]);
}
}
/**
* @param $object
* @return array
*/
private function parseFields($object) {
if (is_object($object)) {
$object = (array)$object;
}
if (is_array($object)) {
$arr = array();
foreach ($object as $key => $val) {
if ($key !== '@') {
if ($key === '$') {
$arr = $this->parseFields($val);
}
else {
$arr[$key] = $this->parseFields($val);
}
}
}
}
else {
$arr = $object;
}
return $arr;
}
}