13 public static function add(array $fields, array $products = []): Main\
Result
17 if (empty($fields[
'TITLE']))
19 $fields[
'TITLE'] =
'';
22 if (empty($fields[
'CREATED_BY']))
24 $fields[
'CREATED_BY'] = Main\Engine\CurrentUser::get()->getId();
27 $files = isset($fields[
'FILES']) && is_array($fields[
'FILES']) ? $fields[
'FILES'] : [];
28 unset($fields[
'FILES']);
30 $addResult = Catalog\AgentContractTable::add($fields);
31 if ($addResult->isSuccess())
33 $id = $addResult->getId();
34 $result->setData([
'ID' => $id]);
38 $addProductsResult = self::addProducts($id, $products);
39 if (!$addProductsResult->isSuccess())
41 $result->addErrors($addProductsResult->getErrors());
47 self::saveFiles($id, $files);
52 $result->addErrors($addResult->getErrors());
58 public static function update(
int $id, array $fields, ?array $products =
null): Main\
Result
62 if (empty($fields[
'MODIFIED_BY']))
64 $fields[
'MODIFIED_BY'] = Main\Engine\CurrentUser::get()->getId();
69 $files[
'FILES'] = $fields[
'FILES'];
70 $files[
'FILES_del'] = $fields[
'FILES_del'] ?? [];
71 unset($fields[
'FILES'], $fields[
'FILES_del']);
73 $files = self::prepareFilesToUpdate($files);
75 $updateResult = Catalog\AgentContractTable::update($id, $fields);
76 if ($updateResult->isSuccess())
78 if (!is_null($products))
80 $deleteProductsResult = self::deleteProductsByContractId($id);
81 if (!$deleteProductsResult->isSuccess())
83 $result->addErrors($deleteProductsResult->getErrors());
88 $addProductsResult = self::addProducts($id, $products);
89 if (!$addProductsResult->isSuccess())
91 $result->addErrors($addProductsResult->getErrors());
98 self::saveFiles($id, $files);
103 $result->addErrors($updateResult->getErrors());
109 public static function delete(
int $id): Main\
Result
113 $deleteProductsResult = self::deleteProductsByContractId($id);
114 if (!$deleteProductsResult->isSuccess())
116 $result->addErrors($deleteProductsResult->getErrors());
119 if ($result->isSuccess())
121 $deleteResult = Catalog\AgentContractTable::delete($id);
122 if (!$deleteResult->isSuccess())
124 $result->addErrors($deleteResult->getErrors());
131 private static function addProducts(
int $id, array $products): Main\
Result
135 $products = array_map(
136 static function ($product) use ($id)
138 $product[
'CONTRACT_ID'] = $id;
144 $addProductResult = Catalog\AgentProductTable::addMulti($products,
true);
145 if (!$addProductResult->isSuccess())
147 $result->addErrors($addProductResult->getErrors());
153 private static function deleteProductsByContractId(
int $id): Main\Result
157 $agentProductIterator = Catalog\AgentProductTable::getList([
159 'filter' => [
'=CONTRACT_ID' => $id],
161 while ($agentProduct = $agentProductIterator->fetch())
163 $deleteProductResult = Catalog\AgentProductTable::delete($agentProduct[
'ID']);
164 if (!$deleteProductResult->isSuccess())
166 $result->addErrors($deleteProductResult->getErrors());
173 public static function get(
int $id): Main\
Result
177 $agentContract = Catalog\AgentContractTable::getList([
187 'filter' => [
'=ID' => $id],
197 $agentProductIterator = Catalog\AgentProductTable::getList([
204 '=CONTRACT_ID' => $id,
207 while ($agentProduct = $agentProductIterator->fetch())
211 $sectionIds[] = $agentProduct[
'PRODUCT_ID'];
216 $productsIds[] = $agentProduct[
'PRODUCT_ID'];
219 $agentProduct[
'PRODUCT_NAME'] =
'';
220 $products[] = $agentProduct;
231 $sectionIterator = Iblock\SectionTable::getList([
232 'select' => [
'ID',
'NAME',
'PICTURE'],
233 'filter' => [
'=ID' => array_unique($sectionIds)],
235 while ($sectionData = $sectionIterator->fetch())
237 $sectionNames[$sectionData[
'ID']] = $sectionData[
'NAME'];
238 if (!empty($sectionData[
'PICTURE']))
240 $sectionImages[$sectionData[
'ID']] = self::getImageSource((
int)$sectionData[
'PICTURE']);
246 $iblockProductMorePhotoMap = [];
249 $elementIterator = Iblock\ElementTable::getList([
250 'select' => [
'ID',
'NAME',
'IBLOCK_ID',
'PREVIEW_PICTURE',
'DETAIL_PICTURE'],
251 'filter' => [
'=ID' => array_unique($productsIds)],
253 while ($elementData = $elementIterator->fetch())
255 $elementId = $elementData[
'ID'];
256 $productNames[$elementId] = $elementData[
'NAME'];
257 if (!empty($elementData[
'PREVIEW_PICTURE']))
259 $productImages[$elementId] = self::getImageSource((
int)$elementData[
'PREVIEW_PICTURE']);
262 if (empty($element[
'IMAGE']) && !empty($elementData[
'DETAIL_PICTURE']))
264 $productImages[$elementId] = self::getImageSource((
int)$elementData[
'DETAIL_PICTURE']);
267 if (empty($element[
'IMAGE']))
269 $iblockProductMorePhotoMap[$elementData[
'IBLOCK_ID']] ??= [];
270 $iblockProductMorePhotoMap[$elementData[
'IBLOCK_ID']][] = $elementId;
274 if (!empty($iblockProductMorePhotoMap))
277 $iterator = PropertyTable::getList([
278 'select' => [
'ID',
'IBLOCK_ID'],
280 '=IBLOCK_ID' => array_keys($iblockProductMorePhotoMap),
281 '=CODE' => \CIBlockPropertyTools::CODE_MORE_PHOTO,
286 if ($row = $iterator->fetch())
288 $morePhotoIds[$row[
'IBLOCK_ID']] = $row[
'ID'];
291 foreach ($morePhotoIds as $iblockId => $propertyId)
293 $elementIds = $iblockProductMorePhotoMap[$iblockId];
294 $elementPropertyValues = array_fill_keys($elementIds, []);
296 'IBLOCK_ID' => $iblockId,
302 \CIBlockElement::GetPropertyValuesArray($elementPropertyValues, $iblockId, $offersFilter, $propertyFilter);
303 foreach ($elementPropertyValues as $productId => $properties)
305 if (empty($properties))
310 $morePhotoProperty = reset($properties);
311 $value = $morePhotoProperty[
'VALUE'] ??
null;
317 $propertyValue = is_array($value) ? reset($value) : $value[
'VALUE'];
318 if ((
int)$propertyValue > 0)
320 $productImages[$productId] = self::getImageSource((
int)$propertyValue);
328 $products = array_map(
329 static function ($product) use ($sectionNames, $productNames, $productImages, $sectionImages)
333 $product[
'PRODUCT_NAME'] = $sectionNames[$product[
'PRODUCT_ID']];
334 $product[
'IMAGE'] = $sectionImages[$product[
'PRODUCT_ID']] ??
null;
339 $product[
'PRODUCT_NAME'] = $productNames[$product[
'PRODUCT_ID']];
340 $product[
'IMAGE'] = $productImages[$product[
'PRODUCT_ID']] ??
null;
349 $files = self::getFiles($id);
355 'PRODUCTS' => $products,
365 private static function saveFiles(
int $contractId, array $files): void
375 $agentContractFileIterator = Catalog\AgentContractFileTable::getList([
376 'select' => [
'ID',
'FILE_ID'],
377 'filter' => [
'=CONTRACT_ID' => $contractId],
379 while ($agentContractFile = $agentContractFileIterator->fetch())
381 $id = (int)$agentContractFile[
'ID'];
382 $fileId = (int)$agentContractFile[
'FILE_ID'];
383 $existingFiles[$id] = [
385 'FILE_ID' => $fileId,
387 $fileMap[$fileId] = $id;
391 $files = static::convertFileList($fileMap, $files);
398 foreach (array_keys($existingFiles) as $rowId)
400 if (!isset($files[$rowId]))
402 $files[$rowId] = $existingFiles[$rowId];
408 foreach ($files as $rowId => $row)
414 && isset($existingFiles[$rowId])
420 && $row[
'DEL'] ===
'Y'
423 $resultInternal = Catalog\AgentContractFileTable::delete($rowId);
424 if ($resultInternal->isSuccess())
426 \CFile::Delete($existingFiles[$rowId][
'FILE_ID']);
431 isset($row[
'FILE_ID'])
434 if ($row[
'FILE_ID'] !== $existingFiles[$rowId][
'FILE_ID'])
436 $resultInternal = Catalog\AgentContractFileTable::update(
439 'FILE_ID' => $row[
'FILE_ID'],
442 if ($resultInternal->isSuccess())
444 \CFile::Delete($existingFiles[$rowId][
'FILE_ID']);
451 preg_match(
'/^n[0-9]+$/', $rowId, $parsed)
456 if (isset($row[
'FILE_ID']))
458 $resultInternal = Catalog\AgentContractFileTable::add([
459 'CONTRACT_ID' => $contractId,
460 'FILE_ID' => $row[
'FILE_ID'],
462 if ($resultInternal->isSuccess())
464 $id = (int)$resultInternal->getId();
465 $fileMap[$row[
'FILE_ID']] = $id;
466 $existingFiles[$id] = [
468 'FILE_ID' => $row[
'FILE_ID'],
474 isset($row[
'FILE_UPLOAD'])
475 && is_array($row[
'FILE_UPLOAD'])
478 $row[
'FILE_UPLOAD'][
'MODULE_ID'] =
'catalog';
479 $fileId = (int)\CFile::SaveFile(
487 $resultInternal = Catalog\AgentContractFileTable::add([
488 'CONTRACT_ID' => $contractId,
489 'FILE_ID' => $fileId,
491 if ($resultInternal->isSuccess())
493 $id = (int)$resultInternal->getId();
494 $fileMap[$fileId] = $id;
495 $existingFiles[$id] = [
497 'FILE_ID' => $fileId,
506 private static function convertFileList(array $fileMap, array $files): array
508 $formatArray =
false;
509 $formatOther =
false;
510 foreach ($files as $value)
512 if (is_array($value))
522 if ($formatArray && $formatOther)
533 $list = array_values(array_unique($files));
536 foreach ($list as $value)
538 if (!is_string($value))
542 if (preg_match(
'/^delete([0-9]+)$/', $value, $parsed))
544 $value = (int)$parsed[1];
545 if (isset($fileMap[$value]))
547 $id = $fileMap[$value];
553 elseif (preg_match(
'/^[0-9]+$/', $value, $parsed))
555 $value = (int)$value;
556 if (isset($fileMap[$value]))
558 $id = $fileMap[$value];
566 $id =
'n' . $counter;
579 private static function getFiles(
int $contractId): array
581 $files = Catalog\AgentContractFileTable::getList([
582 'select' => [
'FILE_ID'],
583 'filter' => [
'=CONTRACT_ID' => $contractId]
586 return array_column($files,
'FILE_ID');
589 private static function prepareFilesToUpdate(array $fields): array
591 $filesExists = isset($fields[
'FILES']) && is_array($fields[
'FILES']);
592 $filesDelete = isset($fields[
'FILES_del']) && is_array($fields[
'FILES_del']);
593 if ($filesExists || $filesDelete)
598 $fileList = $fields[
'FILES'];
599 Main\Type\Collection::normalizeArrayValuesByInt($fileList,
false);
600 $fileList = Main\UI\FileInputUtility::instance()->checkFiles(
604 foreach ($fileList as $id)
606 $result[$id] = (string)$id;
612 $deleteList = $fields[
'FILES_del'];
613 Main\Type\Collection::normalizeArrayValuesByInt($deleteList,
false);
614 foreach ($deleteList as $id)
616 $result[$id] =
'delete' . $id;
620 $fields[
'FILES'] = array_values($result);
624 return $fields[
'FILES'];
627 private static function getImageSource(
int $id): ?string
634 $file = \CFile::GetFileArray($id);
640 return Tools::getImageSrc($file,
false) ?:
null;