Passed
Push — master ( ef4891...235f7d )
by Stefan
01:23
created

ContactDetails.php (1 issue)

Severity
1
<?php
2
declare(strict_types=1);
3
4
use SKien\Google\GClient;
5
use SKien\Google\GContact;
6
use SKien\Google\GContactGroups;
7
use SKien\Google\GContacts;
8
use SKien\Google\GSecrets;
9
10
require_once 'autoloader.php';
11
require_once 'displayApiError.php';
12
13
/**
14
 * This example is only intended to demonstrate the use of the package. The UI
15
 * is only coded 'quick and dirty', contains no validations and should only be
16
 * used as a starting point for your own implementation.
17
 *
18
 * @author Stefanius <[email protected]>
19
 * @copyright MIT License - see the LICENSE file for details
20
 */
21
22
$oSecrets = new GSecrets(-1, GSecrets::TOKEN_FILE);
23
$oClient = new GClient();
24
$oClient->setAccessToken($oSecrets->getAccessToken());
25
if ($oClient->isAccessTokenExpired()) {
26
    // try to refresh the accesstoken
27
    $strRefreshToken = $oSecrets->getRefreshToken();
28
    if (empty($strRefreshToken)) {
29
        // no refresh token available - redirect to google login
30
        header('Location: ./GoogleLogin.php');
31
        exit;
32
    }
33
    $oClient->setOAuthClient($oSecrets->getClientSecrets());
34
    $oSecrets->saveAccessToken($oClient->refreshAccessToken($strRefreshToken));
35
}
36
37
$oContacts = new GContacts($oClient);
38
$oContacts->addPersonFields(GContacts::DEF_DETAIL_PERSON_FIELDS);
39
$strResourceName = rawurldecode($_GET['res'] ?? '');
40
41
if (empty($strResourceName)) {
42
    $oContact = GContact::createEmpty();
43
} else {
44
    $oContact = $oContacts->getContact($strResourceName);
45
    if ($oContact === false) {
46
        displayApiError(
47
            'reading contact',
48
            $strResourceName,
49
            $oClient->getLastResponseCode(),
50
            $oClient->getLastError(),
51
            $oClient->getLastStatus()
52
            );
53
        exit;
54
    }
55
}
56
$uxtsLastModified = $oContact->getLastModified();
57
58
$oGroups = new GContactGroups($oClient);
59
$aGroups = $oGroups->list(GContactGroups::GT_ALL_CONTACT_GROUPS, GContactGroups::DATA_LIST);
60
if ($aGroups === false) {
0 ignored issues
show
The condition $aGroups === false is always true.
Loading history...
61
    displayApiError(
62
        'list contact groups',
63
        '',
64
        $oClient->getLastResponseCode(),
65
        $oClient->getLastError(),
66
        $oClient->getLastStatus()
67
        );
68
    exit;
69
}
70
71
/*
72
$strGroups = '';
73
foreach ($oContact['memberships'] as $aMembership) {
74
    if (isset($aMembership['contactGroupMembership'])) {
75
        $strGroupRes = $aMembership['contactGroupMembership']['contactGroupResourceName'];
76
        if (isset($aGroups[$strGroupRes])) {
77
            $strGroups .= '                    <li>' . $aGroups[$strGroupRes] . '</li>' . PHP_EOL;
78
        }
79
    }
80
}
81
*/
82
83
if (file_exists('data')) {
84
    // this is just for debugging/testing to easy lock at the complete response data...
85
    file_put_contents('./data/' . str_replace(' ', '_', $oContact->getDisplayName()) . '.json', json_encode($oContact, JSON_PRETTY_PRINT));
86
}
87
88
$strTitle = $oContact->getDisplayName();
89
if ($oContact->isStarred()) {
90
    $strTitle = '&#x2b50;&nbsp;' . $strTitle;
91
}
92
93
?>
94
<html>
95
<head>
96
<style>
97
body, form {
98
    font-family: Sans-Serif;
99
    font-size: 12px;
100
}
101
form fieldset {
102
    line-height: 200%;
103
}
104
form label {
105
    display: inline-block;
106
    width: 150px;
107
}
108
form input[type=text] {
109
    width: 400px;
110
}
111
form span img {
112
    float: right;
113
}
114
form label.ptype {
115
    width: 80px;
116
    text-align: right;
117
}
118
for, input[type=tel] {
119
    width: 236px;
120
}
121
form input.ptype {
122
    width: 80px;
123
}
124
form input.mail {
125
    width: 236px;
126
}
127
form input.city {
128
    width: 316px;
129
}
130
form textarea {
131
    width: 552px;
132
}
133
form ul {
134
    margin: -10px 0 -5px 0;
135
}
136
</style>
137
<script>
138
function onPhotoFileSelected()
139
{
140
	// var file = document.getElementById('photoFile').files[document.getElementById('photoFile').files.length - 1];
141
	// alert(file.name);
142
	document.getElementById('photoForm').submit();
143
}
144
function loadPhoto()
145
{
146
	document.getElementById('photoFile').click()
147
}
148
function deletePhoto(strResourceName)
149
{
150
	if (confirm("Delete the photo?")) {
151
		window.location = './DoAction.php?action=deleteContactPhoto&res=' + encodeURI(strResourceName);
152
	}
153
}
154
</script>
155
</head>
156
<body>
157
	<a href="./ContactList.php">back to the overview</a><br/><br/>
158
    <form id="photoForm" action="./DoAction.php?action=setContactPhoto" enctype="multipart/form-data" method="post">
159
		<input type="file" id="photoFile" name="photoFile" onchange="onPhotoFileSelected()" style="display: none">
160
		<input type="hidden" name="resourceName" value="<?=$strResourceName?>">
161
	</form>
162
    <form action="./DoAction.php?action=saveContact" method="post">
163
		<input type="hidden" name="resourceName" value="<?=$strResourceName?>">
164
		<input type="hidden" name="metadataType" value="<?=$oContact['metadata']['sources'][0]['type']?>">
165
		<input type="hidden" name="metadataId" value="<?=$oContact['metadata']['sources'][0]['id']?>">
166
		<input type="hidden" name="metadataEtag" value="<?=$oContact['metadata']['sources'][0]['etag']?>">
167
		<h2><?=$strTitle?></h2>
168
		<p>Letzte Änderung: <?php if ($uxtsLastModified > 0) echo date('d.m.Y - H:i:s', $uxtsLastModified); ?></p>
169
		<div>
170
<?php
171
$iVisibleGroups = 0;
172
$strGroups = '';
173
foreach ($aGroups as $aGroup) {
174
    $strChecked = ($oContact->belongsToGroup($aGroup['resourceName']) ? ' checked' : '');
175
    if ($aGroup['groupType'] == GContactGroups::GT_SYSTEM_CONTACT_GROUPS) {
176
        $strGroups .= '                <input type="checkbox" name="memberships[]" value="' . $aGroup['resourceName'] . '"' . $strChecked . ' style="display: none;">' . PHP_EOL;
177
    } else {
178
        $strGroups .= '                <input type="checkbox" name="memberships[]" value="' . $aGroup['resourceName'] . '"' . $strChecked . '>';
179
        $strGroups .= '&nbsp;' . $aGroup['formattedName'] . '<br/>' . PHP_EOL;
180
        $iVisibleGroups++;
181
    }
182
}
183
if ($iVisibleGroups > 0) {
184
    echo '            <fieldset>' . PHP_EOL;
185
    echo '                <legend>Member of</legend>' . PHP_EOL;
186
    echo $strGroups;
187
    echo '            </fieldset>' . PHP_EOL;
188
} else {
189
    echo $strGroups;
190
}
191
?>
192
			<fieldset>
193
				<legend>Names</legend>
194
				<div style="float: right; padding: 10px;">
195
					Photo&nbsp;
196
					<a href="javascript: loadPhoto()" title="Select photo" style="font-size: 20px; text-decoration: none; padding: 0 8px;">&#128393;</a>&nbsp;
197
					<a href="javascript: deletePhoto('<?=$strResourceName?>')" title="Remove photo" style="font-size: 20px; text-decoration: none; padding: 0 8px;">&#128465;</a>
198
					<br/>
199
					<img onclick="loadPhoto()" src="<?php echo $oContact['photos'][0]['url'] ?? ''?>">
200
    			</div>
201
    			<label for="honorificPrefix">honorificPrefix:</label>
202
    			<input type="text" id="honorificPrefix" name="names_0_honorificPrefix" value="<?php echo $oContact['names'][0]['honorificPrefix'] ?? ''?>">
203
    			<br/>
204
    			<label for="familyName">familyName:</label>
205
    			<input type="text" id="familyName" name="names_0_familyName" value="<?php echo $oContact['names'][0]['familyName'] ?? ''?>">
206
    			<br/>
207
    			<label for="givenName">givenName:</label>
208
    			<input type="text" id="givenName" name="names_0_givenName" value="<?php echo $oContact['names'][0]['givenName'] ?? ''?>">
209
    			<br/>
210
    			<label for="middleName">middleName:</label>
211
    			<input type="text" id="middleName" name="names_0_middleName" value="<?php echo $oContact['names'][0]['middleName'] ?? ''?>">
212
    			<br/>
213
    			<label for="honorificSuffix">honorificSuffix:</label>
214
    			<input type="text" id="honorificSuffix" name="names_0_honorificSuffix" value="<?php echo $oContact['names'][0]['honorificSuffix'] ?? ''?>">
215
    			<br/>
216
<?php
217
$i = 0;
218
foreach ($oContact['nicknames'] as $aNickname) {
219
    $strName = 'nicknames_' . $i . '_value';
220
    $strField = 'nickName' . ++$i;
221
    echo '                <label for="' . $strField . '">' . $strField . ':</label>' . PHP_EOL;
222
    echo '                <input type="text" id="' . $strField . '" name="' . $strName . '" value="' . $aNickname['value'] . '">' . PHP_EOL;
223
    echo '                <br/>' . PHP_EOL;
224
}
225
?>
226
    			<label for="birthday">birthday:</label>
227
    			<input type="date" id="birthday" name="birthday" value="<?=$oContact->getDateOfBirth(GContact::DT_STRING)?>">
228
    			<label class="ptype" for="gender">gender:</label>
229
    			<select id="gender" name="genders_0_value">
230
    				<option value=""></option>
231
    				<option value="male"<?php echo ($oContact['genders'][0]['value'] ?? '') == 'male' ? ' selected' : '';?>>male</option>
232
    				<option value="female"<?php echo ($oContact['genders'][0]['value'] ?? '') == 'female' ? ' selected' : '';?>>female</option>
233
    				<option value="unspecified"<?php echo ($oContact['genders'][0]['value'] ?? '') == 'unspecified' ? ' selected' : '';?>>unspecified</option>
234
    			</select>
235
    			<br/>
236
			</fieldset>
237
			<fieldset>
238
				<legend>Phone numbers</legend>
239
<?php
240
$i = 0;
241
foreach ($oContact['phoneNumbers'] as $aPhone) {
242
    $strFieldName = 'phoneNumbers_' . $i . '_value';
243
    $strTypeName = 'phoneNumbers_' . $i . '_type';
244
    $strField = 'phoneNumber' . ++$i;
245
    $strType = 'phoneType' . $i;
246
    $strPrimary = $oContact->isPrimaryItem($aPhone) ? ' checked' : '';
247
    echo '                <label for="' . $strField . '">' . $strField . ':</label>' . PHP_EOL;
248
    echo '                <input type="tel" id="' . $strField . '" name="' . $strFieldName . '" value="' . ($aPhone['value'] ?? '') . '">' . PHP_EOL;
249
    echo '                <label class="ptype" for="' . $strType . '">type:</label>' . PHP_EOL;
250
    echo '                <input class="ptype" type="text" id="' . $strType . '" name="' . $strTypeName . '" value="' . ($aPhone['type'] ?? 'other') . '">' . PHP_EOL;
251
    echo '                <input type="radio" name="phoneNumbers" value="' . ($i-1) . '"' . $strPrimary . '>&nbsp;primary phone' . PHP_EOL;
252
    echo '                <br/>' . PHP_EOL;
253
}
254
?>
255
			</fieldset>
256
			<fieldset>
257
				<legend>Mailaddresses</legend>
258
<?php
259
$i = 0;
260
foreach ($oContact['emailAddresses'] as $aMail) {
261
    $strFieldName = 'emailAddresses_' . $i . '_value';
262
    $strTypeName = 'emailAddresses_' . $i . '_type';
263
    $strField = 'emailAddress' . ++$i;
264
    $strType = 'emailType' . $i;
265
    $strPrimary = $oContact->isPrimaryItem($aMail) ? ' checked' : '';
266
    echo '                <label for="' . $strField . '">' . $strField . ':</label>' . PHP_EOL;
267
    echo '                <input class="mail" type="text" id="' . $strField . '" name="' . $strFieldName . '" value="' . ($aMail['value'] ?? '') . '">' . PHP_EOL;
268
    echo '                <label class="ptype" for="' . $strType . '">type:</label>' . PHP_EOL;
269
    echo '                <input class="ptype" type="text" id="' . $strType . '" name="' . $strTypeName . '" value="' . ($aMail['type'] ?? 'other') . '">' . PHP_EOL;
270
    echo '                <input type="radio" name="emailAddresses" value="' . ($i-1) . '"' . $strPrimary . '>&nbsp;primary email' . PHP_EOL;
271
    echo '                <br/>' . PHP_EOL;
272
}
273
?>
274
			</fieldset>
275
			<fieldset>
276
				<legend>Organization</legend>
277
				<label for="orgName">orgName:</label>
278
                <input type="text" id="orgName" name="organizations_0_name" value="<?php echo ($oContact['organizations'][0]['name'] ?? '')?>">
279
                <br/>
280
                <label for="orgTitle">orgTitle:</label>
281
                <input type="text" id="orgTitle" name="organizations_0_title" value="<?php echo ($oContact['organizations'][0]['title'] ?? '')?>">
282
                <br/>
283
                <label for="orgDepartment">orgDepartment:</label>
284
                <input type="text" id="orgDepartment" name="organizations_0_department" value="<?php echo ($oContact['organizations'][0]['department'] ?? '')?>">
285
                <br/>
286
			</fieldset>
287
			<fieldset>
288
				<legend>Addresses</legend>
289
<?php
290
$i = 0;
291
foreach ($oContact['addresses'] as $aAdr) {
292
    ++$i;
293
    if ($i > 1) {
294
        echo '                <hr>' . PHP_EOL;
295
    }
296
    $strPrimary = $oContact->isPrimaryItem($aAdr) ? ' checked' : '';
297
    echo '                <label for="adrType' . $i . '">type:</label>' . PHP_EOL;
298
    echo '                <input class="ptype" type="text" id="adrType' . $i . '" name="addresses_' . ($i-1) . '_type" value="' . ($aAdr['type'] ?? 'home') . '">' . PHP_EOL;
299
    echo '                <input type="radio" name="addresses" value="' . ($i-1) . '"' . $strPrimary . '>&nbsp;primary address' . PHP_EOL;
300
    echo '                <br/>' . PHP_EOL;
301
    echo '                <label for="adrStreet' . $i . '">adrStreet:</label>' . PHP_EOL;
302
    echo '                <input class="city" type="text" id="adrStreet' . $i . '" name="addresses_' . ($i-1) . '_streetAddress" value="' . ($aAdr['streetAddress'] ?? '') . '">' . PHP_EOL;
303
    echo '                <input class="ptype" type="text" id="extendedAddress' . $i . '" name="addresses_' . ($i-1) . '_extendedAddress" value="' . ($aAdr['extendedAddress'] ?? '') . '">' . PHP_EOL;
304
    echo '                <br/>' . PHP_EOL;
305
    echo '                <label for="adrPostcode' . $i . '">adrPostcode/adrCity:</label>' . PHP_EOL;
306
    echo '                <input class="ptype" type="text" id="adrPostcode' . $i . '" name="addresses_' . ($i-1) . '_postalCode" value="' . ($aAdr['postalCode'] ?? '') . '">' . PHP_EOL;
307
    echo '                <input class="city" type="text" id="adrCity' . $i . '" name="addresses_' . ($i-1) . '_city" value="' . ($aAdr['city'] ?? '') . '">' . PHP_EOL;
308
    echo '                <br/>' . PHP_EOL;
309
    echo '                <label for="adrCountry' . $i . '">adrCountry/Code:</label>' . PHP_EOL;
310
    echo '                <input class="city" type="text" id="adrCountry' . $i . '" name="addresses_' . ($i-1) . '_country" value="' . ($aAdr['country'] ?? '') . '">' . PHP_EOL;
311
    echo '                <input class="ptype" type="text" id="adrCountryCode' . $i . '" name="addresses_' . ($i-1) . '_countryCode" value="' . ($aAdr['countryCode'] ?? '') . '">' . PHP_EOL;
312
    echo '                <br/>' . PHP_EOL;
313
    echo '                <label for="adrPOBox' . $i . '">adrPOBox:</label>' . PHP_EOL;
314
    echo '                <input type="text" id="adrPOBox' . $i . '" name="addresses_' . ($i-1) . '_poBox" value="' . ($aAdr['poBox'] ?? '') . '">' . PHP_EOL;
315
    echo '                <br/>' . PHP_EOL;
316
}
317
?>
318
			</fieldset>
319
			<fieldset>
320
				<legend>Notes (biographies)</legend>
321
				<textarea name="biographies_0_value" rows="4"><?php echo ($oContact['biographies'][0]['value'] ?? '')?></textarea>
322
			</fieldset>
323
        </div>
324
		<div>
325
			<br/>
326
            <input type="submit" value="Save" />
327
        </div>
328
    </form>
329
</body>
330
</html>
331