|
1
|
|
|
<?php |
|
2
|
|
|
require_once __DIR__ . '/vendor/autoload.php'; |
|
3
|
|
|
use EUAutomation\GraphQL\Client; |
|
4
|
|
|
|
|
5
|
|
|
elgg_register_event_handler('init', 'system', 'paas_integration_init'); |
|
6
|
|
|
|
|
7
|
|
|
function paas_integration_init() { |
|
8
|
|
|
|
|
9
|
|
|
elgg_register_action("avatar/upload", elgg_get_plugins_path() . "paas_integration/actions/avatar/upload.php"); |
|
10
|
|
|
elgg_register_plugin_hook_handler("action", "b_extended_profile/edit_profile", "edit_profile_paas_mutation"); |
|
11
|
|
|
|
|
12
|
|
|
elgg_register_action('b_extended_profile/edit_profile', elgg_get_plugins_path() . 'paas_integration/actions/b_extended_profile/edit_profile.php'); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Send modifyProfile mutation to PaaS |
|
17
|
|
|
* |
|
18
|
|
|
* @return bool |
|
19
|
|
|
*/ |
|
20
|
|
|
function edit_profile_paas_mutation($hook, $entity_type, $returnvalue, $params) { |
|
21
|
|
|
$guid = get_input('guid'); |
|
22
|
|
|
$owner = get_entity($guid); |
|
23
|
|
|
|
|
24
|
|
|
$dbprefix = elgg_get_config("dbprefix"); |
|
25
|
|
|
$service_url = elgg_get_plugin_setting("graphql_client", "paas_integration"); |
|
26
|
|
|
|
|
27
|
|
|
$session = elgg_get_session(); |
|
28
|
|
|
$token = $session->get('token'); |
|
29
|
|
|
|
|
30
|
|
|
$result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid"); |
|
31
|
|
|
if ($result->pleio_guid) { |
|
32
|
|
|
$gcID = $result->pleio_guid; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$client = new Client($service_url); |
|
36
|
|
|
|
|
37
|
|
|
$headers = [ |
|
38
|
|
|
"Authorization" => "Bearer $token" |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
$query = 'mutation ($gcID: ID!, $data: ModifyProfileInput!) { |
|
42
|
|
|
modifyProfile(gcID: $gcID, data: $data) { |
|
43
|
|
|
gcID |
|
44
|
|
|
name |
|
45
|
|
|
email |
|
46
|
|
|
titleEn |
|
47
|
|
|
titleFr |
|
48
|
|
|
avatar |
|
49
|
|
|
mobilePhone |
|
50
|
|
|
officePhone |
|
51
|
|
|
address { |
|
52
|
|
|
streetAddress |
|
53
|
|
|
city |
|
54
|
|
|
postalCode |
|
55
|
|
|
province |
|
56
|
|
|
country |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
}'; |
|
60
|
|
|
|
|
61
|
|
|
$profile_fields = get_input('profile'); |
|
62
|
|
|
|
|
63
|
|
|
$address = array( |
|
64
|
|
|
'streetAddress' => copy_value_to_object_if_defined($profile_fields['streetaddress']), |
|
65
|
|
|
'city' => copy_value_to_object_if_defined($profile_fields['city']), |
|
66
|
|
|
'province' => copy_value_to_object_if_defined($profile_fields['province']), |
|
67
|
|
|
'postalCode' => copy_value_to_object_if_defined($profile_fields['postalcode']), |
|
68
|
|
|
'country' => copy_value_to_object_if_defined($profile_fields['country']), |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$variables = array( |
|
72
|
|
|
'gcID' => $gcID, |
|
|
|
|
|
|
73
|
|
|
'data' => array( |
|
74
|
|
|
'name' => $profile_fields['name'], |
|
75
|
|
|
'titleEn' => copy_value_to_object_if_defined($profile_fields['job']), |
|
76
|
|
|
'titleFr' => copy_value_to_object_if_defined($profile_fields['jobfr']), |
|
77
|
|
|
'mobilePhone' => copy_value_to_object_if_defined($profile_fields['mobile']), |
|
78
|
|
|
'officePhone' => copy_value_to_object_if_defined($profile_fields['phone']), |
|
79
|
|
|
'address' => address_validation($address), |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
// Send data to PaaS |
|
84
|
|
|
$response = $client->response($query, $variables, $headers); |
|
85
|
|
|
|
|
86
|
|
|
// Error check |
|
87
|
|
|
if($response->errors()){ |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Contact details |
|
92
|
|
|
$owner->name = $response->modifyProfile->name; |
|
93
|
|
|
$owner->job = $response->modifyProfile->titleEn; |
|
94
|
|
|
$owner->jobfr = $response->modifyProfile->titleFr; |
|
95
|
|
|
$owner->mobile = $response->modifyProfile->mobilePhone; |
|
96
|
|
|
$owner->phone = $response->modifyProfile->officePhone; |
|
97
|
|
|
|
|
98
|
|
|
// Address details |
|
99
|
|
|
$owner->streetaddress = $response->modifyProfile->address->streetAddress; |
|
100
|
|
|
$owner->city = $response->modifyProfile->address->city; |
|
101
|
|
|
$owner->province = $response->modifyProfile->address->province; |
|
102
|
|
|
$owner->postalcode = $response->modifyProfile->address->postalCode; |
|
103
|
|
|
$owner->country = $response->modifyProfile->address->country; |
|
104
|
|
|
|
|
105
|
|
|
$owner->save(); |
|
106
|
|
|
|
|
107
|
|
|
// Mutation has gone through |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Set value to null if not set |
|
112
|
|
|
function copy_value_to_object_if_defined($value){ |
|
113
|
|
|
if($value != null && $value != "undefined"){ |
|
114
|
|
|
return $value; |
|
115
|
|
|
} else { |
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Set array to null if all fields are null |
|
121
|
|
|
function address_validation($address) { |
|
122
|
|
|
|
|
123
|
|
|
foreach($address as $k => $v ) { |
|
124
|
|
|
if($v != null) { |
|
125
|
|
|
return $address; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return null; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
function waiting_on_approval($guid){ |
|
133
|
|
|
|
|
134
|
|
|
$dbprefix = elgg_get_config("dbprefix"); |
|
135
|
|
|
$service_url = elgg_get_plugin_setting("graphql_client", "paas_integration"); |
|
136
|
|
|
|
|
137
|
|
|
$result = get_data_row("SELECT pleio_guid FROM {$dbprefix}users_entity WHERE guid = $guid"); |
|
138
|
|
|
if ($result->pleio_guid) { |
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
$gcID = $result->pleio_guid; |
|
142
|
|
|
|
|
143
|
|
|
$client = new Client($service_url); |
|
144
|
|
|
|
|
145
|
|
|
$headers = []; |
|
146
|
|
|
|
|
147
|
|
|
$query = 'query getYourApproval($gcIDSubmitter: gcIDProfileInput!) { |
|
148
|
|
|
approvals( |
|
149
|
|
|
gcIDSubmitter: $gcIDSubmitter, |
|
150
|
|
|
status: Pending, |
|
151
|
|
|
changeType: Informational, |
|
152
|
|
|
) { |
|
153
|
|
|
id |
|
154
|
|
|
requestedChange { |
|
155
|
|
|
titleEn |
|
156
|
|
|
titleFr |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
}'; |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
$variables = array( |
|
163
|
|
|
'gcIDSubmitter' => array( |
|
164
|
|
|
'gcID' => $gcID |
|
165
|
|
|
), |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
// Send data to PaaS |
|
169
|
|
|
$response = $client->response($query, $variables, $headers); |
|
170
|
|
|
|
|
171
|
|
|
// Error check |
|
172
|
|
|
if($response->errors()){ |
|
173
|
|
|
return null; |
|
174
|
|
|
} else if(!$response->approvals[0]->requestedChange) { |
|
175
|
|
|
return null; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return array( |
|
179
|
|
|
"titleEn" => $response->approvals[0]->requestedChange->titleEn, |
|
180
|
|
|
"titleFr" => $response->approvals[0]->requestedChange->titleFr |
|
181
|
|
|
); |
|
182
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: