Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
albumadd.php
1<?php
2
4
5use \Bitrix\Main\SystemException;
10
16{
17 private static $firstRunning = true;
18 private static $apiHelper;
19 private static $isAgressive;
20
31 public function process($data = null, Timer $timer = null)
32 {
33 $logger = new Vk\Logger($this->exportId);
34
35 if (count($data) > Vk\Vk::MAX_EXECUTION_ITEMS)
36 {
37 $data = array_slice($data, 0, Vk\Vk::MAX_ALBUMS);
38 $logger->addError('TOO_MANY_SECTIONS_TO_EXPORT');
39 }
40
41// get STARTPOSITION for create next process step
42 reset($data);
43 $startPosition = current($data);
44 $startPosition = $startPosition["SECTION_ID"];
45
46
47// set STATIC variables for several cycles
48 if (self::$firstRunning)
49 {
50 self::$apiHelper = new Vk\Api\ApiHelper($this->exportId);
51 self::$isAgressive = self::$vk->isAgressiveExport($this->exportId);
52 self::$firstRunning = false;
53 }
54
55// CHECK existing albums
56 $vkExportedData = new Vk\VkExportedData($this->exportId, 'ALBUMS');
57 $albumsFromVk = $vkExportedData->getData();
58// $albumsFromVk = self::$apiHelper->getALbumsFromVk($this->vkGroupId);
59 $data = Vk\Map::checkMappingMatches($data, $albumsFromVk, $this->exportId, 'ALBUMS', self::$isAgressive);
60
61 try
62 {
63// UPLOAD photo
64// todo: need a photo mapping check before upload
65// todo: and maybe we need comments and likes
66 $logger->addLog("Upload album photo");
67
68 $photoUploader = new PhotoUploader($this->exportId, PhotoUploader::TYPE_ALBUM_PHOTO, $timer);
69 $albumPhotoSaveResults = $photoUploader->upload($data);
70
71// photos UPLOAD may be FAILED on VK side. For albums - do nothing
72 if(!array_key_exists('errors', $albumPhotoSaveResults))
73 {
74 $data = Vk\Api\ApiHelper::addResultToData($data, $albumPhotoSaveResults, "SECTION_ID");
75 }
76
77// ADD or EDIT albums
78 $logger->addLog("Add or edit albums", $data);
79 $albumsData = Vk\Api\ApiHelper::extractItemsFromArray($data,
80 array("SECTION_ID", "TITLE", "FLAG_EDIT", "PHOTO_VK_ID", "ALBUM_VK_ID"));
81 $albumsAddEditResults = $this->executer->executeMarketAlbumAddEdit(array(
82 "owner_id" => $this->vkGroupId,
83 "data" => $albumsData,
84 "count" => count($data),
85 ));
86 $data = Vk\Api\ApiHelper::addResultToData($data, $albumsAddEditResults, "SECTION_ID");
87
88
89// MAPPING for success results
90 $dataToMapping = array();
91 foreach ($albumsAddEditResults as $item)
92 {
93 if (isset($item["flag_album_add_result"]) && $item["flag_album_add_result"])
94 $dataToMapping[] = array(
95 "value_external" => $item["ALBUM_VK_ID"],
96 "value_internal" => $item["SECTION_ID"],
97 );
98 }
99// we don't need use timer in last operation. Timer will be checked in feed cycle.
100 if (!empty($dataToMapping))
101 {
102 Vk\Map::addAlbumMapping($dataToMapping, $this->exportId);
103 }
104
105
106// add saved data to CACHE to accelereate export process. Cache updated every hour (for long exports)
107 if (!empty($data))
108 {
109 $dataToCache = Vk\Api\ApiHelper::extractItemsFromArray($data, array('ALBUM_VK_ID'));
110 $dataToCache = Vk\Api\ApiHelper::changeArrayMainKey($dataToCache, 'ALBUM_VK_ID');
111 $vkExportedData->addData($dataToCache);
112 }
113
114// check timer before next step, because not-agressive export can be run very long time
115 if ($timer !== null && !$timer->check())
116 {
117 throw new TimeIsOverException();
118 }
119 }
120 catch (TimeIsOverException $e)
121 {
122 throw new TimeIsOverException("Timelimit for export is over", $startPosition);
123 }
124
125 return true;
126 }
127}