Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
accountvkontakte.php
1<?
2
4
13
15{
16 const TYPE_CODE = 'vkontakte';
17
18 const MAX_ADS_EDIT = 20;
19 const CURRENCY_CODE = 'RUB';
20
21 protected static $listRowMap = array(
22 'ID' => 'ACCOUNT_ID',
23 'NAME' => 'NAME',
24 );
25
26 public function getList()
27 {
28 // https://vk.com/dev/ads.getAccounts
29
30 $result = $this->getRequest()->send([
31 'method' => 'GET',
32 'endpoint' => 'ads.getAccounts',
33 ]);
34 if ($result->isSuccess())
35 {
36 $list = [];
37 while ($item = $result->fetch())
38 {
39 if ($item['ACCOUNT_TYPE'] === 'general')
40 {
41 $list[] = $item;
42 }
43 }
44 $result->setData($list);
45 }
46
47 return $result;
48 }
49
50 public function getProfile()
51 {
52 $response = $this->getRequest()->send(array(
53 'method' => 'GET',
54 'endpoint' => 'users.get',
55 'fields' => array(
56 //'user_ids' => array(),
57 'fields' => 'photo_50,screen_name'
58 )
59 ));
60
61
62 if ($response->isSuccess())
63 {
64 $data = $response->fetch();
65 return array(
66 'ID' => $data['ID'],
67 'NAME' => $data['FIRST_NAME'] . ' ' . $data['LAST_NAME'],
68 'LINK' => 'https://vk.com/' . $data['SCREEN_NAME'],
69 'PICTURE' => $data['PHOTO_50'],
70 );
71 }
72 else
73 {
74 return null;
75 }
76 }
77
84 public function getExpenses($accountId, Date $dateFrom = null, Date $dateTo = null)
85 {
86 $result = new ResponseVkontakte();
87 $fields = [
88 'account_id' => $accountId,
89 'ids_type' => 'office',
90 'ids' => $accountId,
91 ];
92
93 if($dateFrom && $dateTo)
94 {
95 $fields['period'] = 'day';
96 $fields['date_from'] = $dateFrom->format('Y-m-d');
97 $fields['date_to'] = $dateTo->format('Y-m-d');
98 }
99 else
100 {
101 $fields['period'] = 'overall';
102 $fields['date_from'] = '0';
103 $fields['date_to'] = '0';
104 }
105 $response = $this->getRequest()->send([
106 'method' => 'GET',
107 'endpoint' => 'ads.getStatistics',
108 'fields' => $fields,
109 ]);
110 if($response->isSuccess())
111 {
112 $data = $response->getData();
113 if (isset($data[0]))
114 {
115 $data = $data[0];
116 }
117 $expenses = new Expenses();
118 foreach($data['stats'] as $stat)
119 {
120 $expenses->add([
121 'impressions' => $stat['impressions'],
122 'clicks' => $stat['clicks'],
123 'actions' => $stat['clicks'],
124 'spend' => $stat['spent'],
125 'currency' => static::CURRENCY_CODE,
126 ]);
127 }
128 $result->setData(['expenses' => $expenses]);
129 }
130 else
131 {
132 $result->addErrors($response->getErrors());
133 }
134
135 return $result;
136 }
137
141 public function hasPublicPages()
142 {
143 return true;
144 }
145
151 public function getPublicPages($accountId)
152 {
153 $adsLayoutResult = $this->getAdsLayout($accountId);
154 if(!$adsLayoutResult->isSuccess())
155 {
156 return $adsLayoutResult;
157 }
158 $groupIDs = [];
159 $ads = $adsLayoutResult->getData();
160 foreach($ads as $ad)
161 {
162 if(isset($ad['group_id']))
163 {
164 $groupIDs[] = $ad['group_id'];
165 }
166 }
167
168 $response = $this->getGroups($groupIDs);
169 if(!$response->isSuccess())
170 {
171 return $response;
172 }
173 $result = [];
174 $groups = $response->getData();
175 foreach($groups as $page)
176 {
177 $result[] = new Page([
178 'id' => $page['id'],
179 'name' => $page['name'],
180 'about' => $page['description'],
181 'image' => $page['photo_200'],
182 'phone' => $page['phone'],
183 ]);
184 }
185 $response->setData($result);
186
187 return $response;
188 }
189
197 public function updateAnalyticParams($accountId, array $params, array $publicPageIds = [])
198 {
199 $result = new ResponseVkontakte();
200 if(empty($params))
201 {
202 return $result;
203 }
204 $result = $this->getAdsLayout($accountId);
205 if(!$result->isSuccess() || empty($result->getData()))
206 {
207 return $result;
208 }
209
210 $data = [];
211 $ads = $result->getData();
212 $groupIDs = $postIDs = [];
213 foreach($ads as $ad)
214 {
215 if(isset($ad['group_id']) && in_array($ad['group_id'], $publicPageIds))
216 {
217 $groupIDs[] = $ad['group_id'];
218 }
219 if(isset($ad['post_id']))
220 {
221 $postIDs[] = $ad['post_id'];
222 }
223 if(!empty($params['url_tags']) && isset($ad['link_domain']) && !empty($ad['link_domain']) && !empty($ad['link_url']))
224 {
225 $url = new Uri($ad['link_url']);
226 $url->addParams($params['url_tags']);
227 $data[] = [
228 'ad_id' => $ad['id'],
229 'link_url' => $url->getUri(),
230 ];
231 }
232
233 if(count($data) == self::MAX_ADS_EDIT)
234 {
235 $editAdsResult = $this->editAds($accountId, $data);
236 if(!$editAdsResult->isSuccess())
237 {
238 $result->addErrors($editAdsResult->getErrors());
239 }
240 $data = [];
241 }
242 }
243
244 if(!empty($data))
245 {
246 $editAdsResult = $this->editAds($accountId, $data);
247 if(!$editAdsResult->isSuccess())
248 {
249 $result->addErrors($editAdsResult->getErrors());
250 }
251 }
252
253 //edit groups
254 if(!empty($groupIDs))
255 {
256 $result = $this->editGroupAnalyticParams($groupIDs, $params);
257 if(!$result->isSuccess())
258 {
259 return $result;
260 }
261 }
262 //edit posts
263 if(!empty($postIDs))
264 {
265 $result = $this->editPostAnalyticParams($postIDs, $params);
266 }
267
268 return $result;
269 }
270
277 protected function editAds($accountId, array $data)
278 {
279 return $this->getRequest()->send([
280 'method' => 'POST',
281 'endpoint' => 'ads.updateAds',
282 'fields' => [
283 'account_id' => $accountId,
284 'data' => Json::encode($data)
285 ]
286 ]);
287 }
288
294 protected function getAdsLayout($accountId)
295 {
296 $fields = [
297 'account_id' => $accountId,
298 'include_deleted' => '0',
299 ];
300
301 $response = $this->getRequest()->send([
302 'method' => 'GET',
303 'endpoint' => 'ads.getAdsLayout',
304 'fields' => $fields,
305 ]);
306
307 if($response->isSuccess())
308 {
309 $ads = $response->getData();
310 foreach($ads as &$ad)
311 {
312 if(isset($ad['link_url']))
313 {
314 $parsedData = $this->parseVkUrl($ad['link_url']);
315 if($parsedData)
316 {
317 $ad += $parsedData;
318 }
319 }
320 }
321 $response->setData($ads);
322 }
323
324 return $response;
325 }
326
333 protected function editGroupAnalyticParams(array $groupIDs, array $params)
334 {
335 $result = $this->getGroups(array_values($groupIDs));
336 if(!$result->isSuccess())
337 {
338 return $result;
339 }
340 $groups = $result->getData();
341 foreach($groups as $group)
342 {
343 $data = [];
344 if(isset($params['url_tags']) && !empty($params['url_tags']) && isset($group['site']))
345 {
346 $uri = new Uri($group['site']);
347 $uri->addParams($params['url_tags']);
348 $data['website'] = $uri->getUri();
349 }
350 if(isset($params['phone']) && !empty($params['phone']) && $group['phone'] != $params['phone'])
351 {
352 $data['phone'] = $params['phone'];
353 }
354 if(!empty($data))
355 {
356 $data['id'] = $group['id'];
357 $response = $this->getRequest()->send([
358 'method' => 'POST',
359 'endpoint' => 'groups.edit',
360 'fields' => $data,
361 ]);
362 if(!$response->isSuccess())
363 {
364 $result->addErrors($response->getErrors());
365 }
366 }
367
368 if(isset($params['url_tags']) && !empty($params['url_tags']) && isset($group['links']) && is_array($group['links']) && !empty($group['links']))
369 {
370 foreach($group['links'] as $link)
371 {
372 $url = new Uri($link['url']);
373 $url->addParams($params['url_tags']);
374 if($url->getUri() != $link['url'])
375 {
376 $response = $this->getRequest()->send([
377 'method' => 'POST',
378 'emdpoint' => 'groups.deleteLink',
379 'fields' => [
380 'group_id' => $group['id'],
381 'link_id' => $link['id'],
382 ]
383 ]);
384 if($response->isSuccess())
385 {
386 $response = $this->getRequest()->send([
387 'method' => 'POST',
388 'endpoint' => 'groups.addLink',
389 'fields' => [
390 'group_id' => $group['id'],
391 'text' => $link['desc'],
392 'link' => $url->getUri(),
393 ]
394 ]);
395 }
396 if(!$response->isSuccess())
397 {
398 $result->addErrors($response->getErrors());
399 }
400 }
401 }
402 }
403
404 // todo add here edit call_to_action button
405 }
406
407 return $result;
408 }
409
415 protected function getGroups(array $groupIDs)
416 {
417 return $this->getRequest()->send([
418 'method' => 'GET',
419 'endpoint' => 'groups.getById',
420 'fields' => [
421 'group_ids' => implode(',', $groupIDs),
422 'fields' => 'name,type,id,links,site,status,description,phone'
423 ]
424 ]);
425 }
426
431 protected function parseVkUrl($url)
432 {
433 if(preg_match('#vk\.com\/wall-(\d+)_(\d+)#', $url, $matches) && count($matches) > 2)
434 {
435 return [
436 'group_id' => $matches[1],
437 'post_id' => '-'.$matches[1].'_'.$matches[2],
438 ];
439 }
440
441 return false;
442 }
443
450 protected function editPostAnalyticParams(array $postIDs, array $params)
451 {
452 $result = $this->getPosts($postIDs);
453 if(!$result->isSuccess())
454 {
455 return $result;
456 }
457
458 $posts = $result->getData();
459 foreach($posts as $post)
460 {
461 if($post['post_type'] == 'post_ads' && isset($post['attachments']) && is_array($post['attachments']) && count($post['attachments']) == 1)
462 {
463 $attachment = reset($post['attachments']);
464 if($attachment['type'] != 'link' || mb_strpos($attachment['link']['url'], 'vk.com') !== false)
465 {
466 continue;
467 }
468 $data = [];
469 if(isset($params['phone']) && !empty($params['phone']) && mb_strpos($attachment['link']['url'], 'tel:') === 0)
470 {
471 $data = [
472 'owner_id' => $post['owner_id'],
473 'post_id' => $post['id'],
474 'attachments' => 'tel:'.$params['phone'],
475 ];
476 }
477 elseif(isset($params['url_tags']) && !empty($params['url_tags']))
478 {
479 $url = new Uri($attachment['link']['url']);
480 $url->addParams($params['url_tags']);
481 if($url->getUri() != $attachment['link']['url'])
482 {
483 $data = [
484 'owner_id' => $post['owner_id'],
485 'post_id' => $post['id'],
486 'attachments' => $url->getUri(),
487 ];
488 }
489 }
490 if(!empty($data))
491 {
492 $response = $this->getRequest()->send([
493 'method' => 'POST',
494 'endpoint' => 'wall.editAdsStealth',
495 'fields' => $data,
496 ]);
497 if(!$response->isSuccess())
498 {
499 $result->addErrors($response->getErrors());
500 }
501 }
502 }
503 }
504
505 return $result;
506 }
507
513 protected function getPosts(array $postIDs)
514 {
515 return $this->getRequest()->send([
516 'method' => 'GET',
517 'endpoint' => 'wall.getById',
518 'fields' => [
519 'posts' => implode(',', array_values($postIDs)),
520 'fields' => 'id,attachments',
521 ]
522 ]);
523 }
524}
editGroupAnalyticParams(array $groupIDs, array $params)
updateAnalyticParams($accountId, array $params, array $publicPageIds=[])
editPostAnalyticParams(array $postIDs, array $params)
getExpenses($accountId, Date $dateFrom=null, Date $dateTo=null)