1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace MauticPlugin\MauticSocialBundle\Integration; |
||
13 | |||
14 | /** |
||
15 | * Class FoursquareIntegration. |
||
16 | */ |
||
17 | class FoursquareIntegration extends SocialIntegration |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | public function getName() |
||
23 | { |
||
24 | return 'Foursquare'; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function getPriority() |
||
31 | { |
||
32 | return 2; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function getIdentifierFields() |
||
39 | { |
||
40 | return [ |
||
41 | 'email', |
||
42 | 'twitter', //foursquare allows searching directly by twitter handle |
||
43 | ]; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function getAuthenticationUrl() |
||
50 | { |
||
51 | return 'https://foursquare.com/oauth2/authenticate'; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getAccessTokenUrl() |
||
58 | { |
||
59 | return 'https://foursquare.com/oauth2/access_token'; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function getAuthenticationType() |
||
66 | { |
||
67 | return 'oauth2'; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $endpoint |
||
72 | * @param string $m |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getApiUrl($endpoint, $m = 'foursquare') |
||
77 | { |
||
78 | return "https://api.foursquare.com/v2/$endpoint?v=20140806&m={$m}"; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param $url |
||
83 | * @param array $parameters |
||
84 | * @param string $method |
||
85 | * @param array $settings |
||
86 | * |
||
87 | * @return mixed|string |
||
88 | */ |
||
89 | public function makeRequest($url, $parameters = [], $method = 'GET', $settings = []) |
||
90 | { |
||
91 | $settings[$this->getAuthTokenKey()] = 'oauth_token'; |
||
92 | |||
93 | return parent::makeRequest($url, $parameters, $method, $settings); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get public data. |
||
98 | * |
||
99 | * @param $identifier |
||
100 | * @param $socialCache |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getUserData($identifier, &$socialCache) |
||
105 | { |
||
106 | if ($id = $this->getContactUserId($identifier, $socialCache)) { |
||
107 | $url = $this->getApiUrl("users/{$id}"); |
||
108 | $data = $this->makeRequest($url); |
||
109 | if (!empty($data) && isset($data->response->user)) { |
||
110 | $result = $data->response->user; |
||
111 | $socialCache['profile'] = $this->matchUpData($result); |
||
112 | if (isset($result->photo)) { |
||
113 | $socialCache['profile']['profileImage'] = $result->photo->prefix.'300x300'.$result->photo->suffix; |
||
114 | } |
||
115 | $socialCache['profile']['profileHandle'] = $id; |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | * |
||
123 | * @param $identifier |
||
124 | * @param $socialCache |
||
125 | * |
||
126 | * @return array|void |
||
127 | */ |
||
128 | public function getPublicActivity($identifier, &$socialCache) |
||
129 | { |
||
130 | if ($id = $this->getContactUserId($identifier, $socialCache)) { |
||
131 | $activity = [ |
||
132 | //'mayorships' => array(), |
||
133 | 'tips' => [], |
||
134 | //'lists' => array() |
||
135 | ]; |
||
136 | |||
137 | /* |
||
138 | //mayorships |
||
139 | $url = $this->getApiUrl("users/{$id}/mayorships"); |
||
140 | $data = $this->makeRequest($url); |
||
141 | |||
142 | if (isset($data->response->mayorships) && count($data->response->mayorships->items)) { |
||
143 | $limit = 5; |
||
144 | foreach ($data->response->mayorships->items as $m) { |
||
145 | if (empty($limit)) { |
||
146 | break; |
||
147 | } |
||
148 | //find main category of venue |
||
149 | $category = ''; |
||
150 | foreach ($m->venue->categories as $c) { |
||
151 | if ($c->primary) { |
||
152 | $category = $c->name; |
||
153 | break; |
||
154 | } |
||
155 | } |
||
156 | $contact = (!empty($m->contact->formattedPhone)) ? $m->contact->formattedPhone : ''; |
||
157 | $activity['mayorships'][] = array( |
||
158 | 'venueName' => $m->venue->name, |
||
159 | 'venueLocation' => $m->venue->location->formattedAddress, |
||
160 | 'venueContact' => $contact, |
||
161 | 'venueCategory' => $category |
||
162 | ); |
||
163 | $limit--; |
||
164 | } |
||
165 | } |
||
166 | */ |
||
167 | |||
168 | //tips |
||
169 | $url = $this->getApiUrl("users/{$id}/tips").'&limit=5&sort=recent'; |
||
170 | $data = $this->makeRequest($url); |
||
171 | |||
172 | if (isset($data->response->tips) && count($data->response->tips->items)) { |
||
173 | foreach ($data->response->tips->items as $t) { |
||
174 | //find main category of venue |
||
175 | $category = ''; |
||
176 | foreach ($t->venue->categories as $c) { |
||
177 | if ($c->primary) { |
||
178 | $category = $c->name; |
||
179 | break; |
||
180 | } |
||
181 | } |
||
182 | $contact = (!empty($t->contact->formattedPhone)) ? $t->contact->formattedPhone : ''; |
||
183 | $activity['tips'][] = [ |
||
184 | 'createdAt' => $t->createdAt, |
||
185 | 'tipText' => $t->text, |
||
186 | 'tipUrl' => $t->canonicalUrl, |
||
187 | 'venueName' => $t->venue->name, |
||
188 | 'venueLocation' => $t->venue->location->formattedAddress, |
||
189 | 'venueContact' => $contact, |
||
190 | 'venueCategory' => $category, |
||
191 | ]; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /* |
||
196 | //lists |
||
197 | $url = $this->getApiUrl("users/{$id}/lists") . "&limit=5&group=created"; |
||
198 | $data = $this->makeRequest($url); |
||
199 | |||
200 | if (isset($data->response->lists) && count($data->response->lists->items)) { |
||
201 | foreach ($data->response->lists->items as $l) { |
||
202 | if (!$l->listItems->count) { |
||
203 | continue; |
||
204 | } |
||
205 | |||
206 | $item = array( |
||
207 | 'listName' => $l->name, |
||
208 | 'listDescription' => $l->description, |
||
209 | 'listUrl' => $l->canonicalUrl, |
||
210 | 'listCreatedAt' => (isset($l->createdAt)) ? $l->createdAt : '', |
||
211 | 'listUpdatedAt' => (isset($l->updatedAt)) ? $l->updatedAt : '', |
||
212 | 'listItems' => array() |
||
213 | ); |
||
214 | |||
215 | //get a sample of the list items |
||
216 | $url = "https://api.foursquare.com/v2/lists/{$l->id}?limit=5&sort=recent&v=20140719&oauth_token={$keys['access_token']}"; |
||
217 | $listData = $this->makeRequest($url); |
||
218 | |||
219 | if (isset($listData->response->list->listItems) && count($listData->response->list->listItems->items)) { |
||
220 | foreach ($listData->response->list->listItems->items as $li) { |
||
221 | //find main category of venue |
||
222 | $category = ''; |
||
223 | foreach ($li->venue->categories as $c) { |
||
224 | if ($c->primary) { |
||
225 | $category = $c->name; |
||
226 | break; |
||
227 | } |
||
228 | } |
||
229 | $contact = (!empty($li->contact->formattedPhone)) ? $li->contact->formattedPhone : ''; |
||
230 | |||
231 | $item['listItems'][] = array( |
||
232 | 'createdAt' => $li->createdAt, |
||
233 | 'venueName' => $li->venue->name, |
||
234 | 'venueLocation' => $li->venue->location->formattedAddress, |
||
235 | 'venueContact' => $contact, |
||
236 | 'venueCategory' => $category |
||
237 | ); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | $activity['lists'][] = $item; |
||
242 | } |
||
243 | } |
||
244 | */ |
||
245 | |||
246 | if (!empty($activity)) { |
||
247 | $socialCache['activity'] = $activity; |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function getErrorsFromResponse($response) |
||
256 | { |
||
257 | if (is_object($response) && isset($response->meta->errorDetail)) { |
||
258 | return $response->meta->errorDetail.' ('.$response->meta->code.')'; |
||
259 | } |
||
260 | |||
261 | return ''; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function matchFieldName($field, $subfield = '') |
||
268 | { |
||
269 | if ('contact' == $field && in_array($subfield, ['facebook', 'twitter'])) { |
||
270 | return $subfield.'ProfileHandle'; |
||
271 | } |
||
272 | |||
273 | return parent::matchFieldName($field, $subfield); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | public function getAvailableLeadFields($settings = []) |
||
280 | { |
||
281 | return [ |
||
282 | 'profileHandle' => ['type' => 'string'], |
||
283 | 'firstName' => ['type' => 'string'], |
||
284 | 'lastName' => ['type' => 'string'], |
||
285 | 'gender' => ['type' => 'string'], |
||
286 | 'homeCity' => ['type' => 'string'], |
||
287 | 'bio' => ['type' => 'string'], |
||
288 | 'contact' => [ |
||
289 | 'type' => 'object', |
||
290 | 'fields' => [ |
||
291 | 'twitter', |
||
292 | 'facebook', |
||
293 | 'phone', |
||
294 | ], |
||
295 | ], |
||
296 | ]; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function getSupportedFeatures() |
||
303 | { |
||
304 | return [ |
||
305 | 'public_profile', |
||
306 | 'public_activity', |
||
307 | ]; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param $identifier |
||
312 | * @param $socialCache |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | private function getContactUserId(&$identifier, &$socialCache) |
||
317 | { |
||
318 | if (!empty($socialCache['id'])) { |
||
319 | return $socialCache['id']; |
||
320 | } elseif (empty($identifier)) { |
||
321 | return false; |
||
322 | } |
||
323 | |||
324 | $cleaned = $this->cleanIdentifier($identifier); |
||
325 | |||
326 | if (!is_array($cleaned)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
327 | $cleaned = [$cleaned]; |
||
328 | } |
||
329 | |||
330 | foreach ($cleaned as $type => $c) { |
||
331 | $url = $this->getApiUrl('users/search')."&{$type}={$c}"; |
||
332 | $data = $this->makeRequest($url); |
||
333 | |||
334 | if (!empty($data) && isset($data->response->results) && count($data->response->results)) { |
||
335 | $socialCache['id'] = $data->response->results[0]->id; |
||
336 | |||
337 | return $socialCache['id']; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | return false; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | public function getFormType() |
||
348 | { |
||
349 | return null; |
||
350 | } |
||
351 | } |
||
352 |