1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | ini_set('display_errors', 1); |
||
5 | ini_set('display_startup_errors', 1); |
||
6 | error_reporting(E_ALL); |
||
7 | |||
8 | use SKien\Google\GClient; |
||
9 | use SKien\Google\GContact; |
||
10 | use SKien\Google\GContactGroups; |
||
11 | use SKien\Google\GContacts; |
||
12 | use SKien\Google\GSecrets; |
||
13 | |||
14 | require_once 'autoloader.php'; |
||
15 | require_once 'displayApiError.php'; |
||
16 | |||
17 | /** |
||
18 | * This example is only intended to demonstrate the use of the package. The UI |
||
19 | * is only coded 'quick and dirty', contains no validations and should only be |
||
20 | * used as a starting point for your own implementation. |
||
21 | * |
||
22 | * @author Stefanius <[email protected]> |
||
23 | * @copyright MIT License - see the LICENSE file for details |
||
24 | */ |
||
25 | |||
26 | $oSecrets = new GSecrets(-1, GSecrets::TOKEN_FILE); |
||
27 | $oClient = new GClient(); |
||
28 | $oClient->setAccessToken($oSecrets->getAccessToken()); |
||
29 | if ($oClient->isAccessTokenExpired()) { |
||
30 | // try to refresh the accesstoken |
||
31 | $strRefreshToken = $oSecrets->getRefreshToken(); |
||
32 | if (empty($strRefreshToken)) { |
||
33 | // no refresh token available - redirect to google login |
||
34 | header('Location: ./GoogleLogin.php'); |
||
35 | exit; |
||
36 | } |
||
37 | $oClient->setOAuthClient($oSecrets->getClientSecrets()); |
||
38 | $oSecrets->saveAccessToken($oClient->refreshAccessToken($strRefreshToken)); |
||
39 | } |
||
40 | |||
41 | $strSearch = $_REQUEST['search'] ?? ''; |
||
42 | $strGroup = $_REQUEST['group'] ?? ''; |
||
43 | |||
44 | // load available user defined contact groups |
||
45 | $oGroups = new GContactGroups($oClient); |
||
46 | $aGroups = $oGroups->list(GContactGroups::GT_USER_CONTACT_GROUPS); |
||
47 | if ($aGroups === false) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
48 | displayApiError( |
||
49 | 'list contact groups', |
||
50 | '', |
||
51 | $oClient->getLastResponseCode(), |
||
52 | $oClient->getLastError(), |
||
53 | $oClient->getLastStatus() |
||
54 | ); |
||
55 | exit; |
||
56 | } |
||
57 | $strGroupSelect = '<select id="group" onchange="onSelectGroup(this);">' . PHP_EOL; |
||
58 | $strGroupSelect .= ' <option value=""><all contacts></option>' . PHP_EOL; |
||
59 | foreach ($aGroups as $strGroupResourceName => $strGroupName) { |
||
60 | $strSelected = ($strGroup == $strGroupResourceName ? ' selected' : ''); |
||
61 | $strGroupSelect .= ' <option value="' . $strGroupResourceName . '"' . $strSelected . '>' . $strGroupName . '</option>' . PHP_EOL; |
||
62 | } |
||
63 | $strGroupSelect .= ' </select>' . PHP_EOL; |
||
64 | |||
65 | $oContacts = new GContacts($oClient); |
||
66 | $oContacts->addPersonFields(GContacts::DEF_LIST_PERSON_FIELDS); |
||
67 | |||
68 | $aContactList = []; |
||
69 | $strTitle = ''; |
||
70 | if (!empty($strSearch)) { |
||
71 | $oContacts->setPageSize(50); |
||
72 | $aContactList = $oContacts->search($strSearch); |
||
73 | $strTitle = ' - Search for <b><i>[' . $strSearch . ']</i></b>'; |
||
74 | if ($aContactList === false) { |
||
75 | displayApiError( |
||
76 | 'search contacts', |
||
77 | 'search: ' . $strSearch, |
||
78 | $oClient->getLastResponseCode(), |
||
79 | $oClient->getLastError(), |
||
80 | $oClient->getLastStatus() |
||
81 | ); |
||
82 | exit; |
||
83 | } |
||
84 | } else { |
||
85 | $aContactList = $oContacts->list(GContacts::SO_LAST_NAME_ASCENDING, $strGroup); |
||
86 | if ($aContactList === false) { |
||
87 | displayApiError( |
||
88 | 'list contacts', |
||
89 | 'group: ' . $strGroup, |
||
90 | $oClient->getLastResponseCode(), |
||
91 | $oClient->getLastError(), |
||
92 | $oClient->getLastStatus() |
||
93 | ); |
||
94 | exit; |
||
95 | } |
||
96 | } |
||
97 | $strTitle = count($aContactList) . ' Contacts' . $strTitle; |
||
98 | ?> |
||
99 | <html> |
||
100 | <head> |
||
101 | <style> |
||
102 | body, table { |
||
103 | font-family: Sans-Serif; |
||
104 | font-size: 12px; |
||
105 | } |
||
106 | a, a:visited { |
||
107 | color: blue; |
||
108 | } |
||
109 | label { |
||
110 | display: inline-block; |
||
111 | width: 100px; |
||
112 | } |
||
113 | table { |
||
114 | border-spacing: 0; |
||
115 | border-collapse: collapse; |
||
116 | } |
||
117 | th { |
||
118 | color: white; |
||
119 | background-color: #777; |
||
120 | border: 1px solid #333; |
||
121 | padding: 2px 4px; |
||
122 | } |
||
123 | td { |
||
124 | border: 1px solid #ccc; |
||
125 | padding: 2px 4px; |
||
126 | } |
||
127 | tr:nth-child(2n+1) { |
||
128 | background-color: #eee; |
||
129 | } |
||
130 | td:nth-child(9) { |
||
131 | text-align: center; |
||
132 | } |
||
133 | a.trash { |
||
134 | color: #999; |
||
135 | font-size: 16px; |
||
136 | font-weight: bold; |
||
137 | text-decoration: none; |
||
138 | } |
||
139 | a.trash:hover { |
||
140 | color: #009; |
||
141 | } |
||
142 | a.starred, |
||
143 | a.unstarred { |
||
144 | font-size: 16px; |
||
145 | font-weight: bold; |
||
146 | text-decoration: none; |
||
147 | } |
||
148 | a.starred, |
||
149 | a.unstarred:hover { |
||
150 | color: goldenrod; |
||
151 | } |
||
152 | a.starred:hover, |
||
153 | a.unstarred { |
||
154 | color: #ccc; |
||
155 | } |
||
156 | </style> |
||
157 | <script> |
||
158 | function onSelectGroup(oSelect) |
||
159 | { |
||
160 | window.location = './ContactList.php?group=' + encodeURI(oSelect.value); |
||
161 | } |
||
162 | function deleteContact(strResourceName) |
||
163 | { |
||
164 | if (confirm("Delete the contact?")) { |
||
165 | window.location = './DoAction.php?action=deleteContact&res=' + encodeURI(strResourceName); |
||
166 | } |
||
167 | } |
||
168 | function createGroup() |
||
169 | { |
||
170 | var strGroupName = prompt('Enter new group name', ''); |
||
171 | if (strGroupName !== null) { |
||
172 | window.location = './DoAction.php?action=saveGroup&name=' + encodeURI(strGroupName); |
||
173 | } |
||
174 | } |
||
175 | function deleteGroup() |
||
176 | { |
||
177 | var strGroupName = prompt('Enter name of group to delete', ''); |
||
178 | if (strGroupName !== null) { |
||
179 | window.location = './DoAction.php?action=deleteGroup&name=' + encodeURI(strGroupName); |
||
180 | } |
||
181 | } |
||
182 | </script> |
||
183 | </head> |
||
184 | <body> |
||
185 | <form action="./ContactList.php" method="post"> |
||
186 | <div> |
||
187 | <label for="search">Query:</label> |
||
188 | <input id="search" type="text" name="search" /> |
||
189 | <input type="submit" value="Search" /> |
||
190 | <br/><br/> |
||
191 | <label for="group">or select group:</label> |
||
192 | <?=$strGroupSelect?> |
||
193 | </div> |
||
194 | </form> |
||
195 | <h2><?=$strTitle?></h2> |
||
196 | <a href="./ContactDetails.php">Add new contact</a> |
||
197 | <a href="javascript: createGroup()">Create new contactgroup</a> |
||
198 | <a href="javascript: deleteGroup()">Delete existing contactgroup</a> |
||
199 | <a href="./ImportVCard.php">VCard Import</a> |
||
200 | <br/><br/> |
||
201 | <table style="width: 100%"> |
||
202 | <tbody> |
||
203 | <tr> |
||
204 | <th style="width: 4%">#</th> |
||
205 | <th style="width: 3%"> </th> |
||
206 | <th style="width: 22%">Name</th> |
||
207 | <th style="width: 18%" colspan="2">Phone 1</th> |
||
208 | <th style="width: 18%" colspan="2">Phone 2</th> |
||
209 | <th style="width: 25%">e-Mail</th> |
||
210 | <th style="width: 7%"> </th> |
||
211 | <th style="width: 3%"> </th> |
||
212 | </tr> |
||
213 | <?php |
||
214 | $i = 0; |
||
215 | foreach ($aContactList as $aContact) { |
||
216 | $i++; |
||
217 | $oContact = GContact::fromArray($aContact); |
||
218 | $strResourceName = rawurlencode($aContact['resourceName']); |
||
219 | $strStarrURL = './DoAction.php?action=starreContact&res=' . $strResourceName . '&setstarred='; |
||
220 | $strDeleteFunc = "javascript: deleteContact('" . $strResourceName . "')"; |
||
221 | $strName = '[not set]'; |
||
222 | if (isset($aContact['names'][0])) { |
||
223 | $strName = $aContact['names'][0]['displayNameLastFirst']; |
||
224 | } else if (isset($aContact['organizations'][0])) { |
||
225 | $strName = $aContact['organizations'][0]['name']; |
||
226 | } |
||
227 | echo ' <tr id="' . $aContact['resourceName'] . '">' . PHP_EOL; |
||
228 | echo ' <td>' . $i . '</td>' . PHP_EOL; |
||
229 | if ($oContact->isStarred()) { |
||
230 | $strStarrURL; |
||
231 | echo ' <td><a class="starred" href="' . $strStarrURL . 'false" title="unmark">★</a></td>' . PHP_EOL; |
||
232 | } else { |
||
233 | echo ' <td><a class="unstarred" href="' . $strStarrURL . 'true" title="mark starred">☆</a></td>' . PHP_EOL; |
||
234 | } |
||
235 | echo ' <td><a href="./ContactDetails.php?res=' . $strResourceName . '">' . $strName . '</a></td>' . PHP_EOL; |
||
236 | echo ' <td>' . (isset($aContact['phoneNumbers'][0]) ? $aContact['phoneNumbers'][0]['type'] : '') . '</td>' . PHP_EOL; |
||
237 | echo ' <td>' . (isset($aContact['phoneNumbers'][0]) ? $aContact['phoneNumbers'][0]['value'] : '') . '</td>' . PHP_EOL; |
||
238 | echo ' <td>' . (isset($aContact['phoneNumbers'][1]) ? $aContact['phoneNumbers'][1]['type'] : '') . '</td>' . PHP_EOL; |
||
239 | echo ' <td>' . (isset($aContact['phoneNumbers'][1]) ? $aContact['phoneNumbers'][1]['value'] : '') . '</td>' . PHP_EOL; |
||
240 | echo ' <td>' . (isset($aContact['emailAddresses'][0]) ? $aContact['emailAddresses'][0]['value'] : '') . '</td>' . PHP_EOL; |
||
241 | echo ' <td><a href="./ExportVCard.php?res=' . $strResourceName . '" target="_blank">VCard</a></td>' . PHP_EOL; |
||
242 | echo ' <td><a class="trash" href="' . $strDeleteFunc . '" title="delete contact">🗑</a></td>' . PHP_EOL; |
||
243 | echo ' </tr>' . PHP_EOL; |
||
244 | } |
||
245 | ?> |
||
246 | </tbody> |
||
247 | </table> |
||
248 | </body> |
||
249 | </html> |
||
250 |