Issues (23)

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