CCrmExternalChannelRestProxy::processMethodRequest

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmExternalChannelRestProxy
  4. processMethodRequest
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
  • Класс: \CCrmExternalChannelRestProxy
  • Вызов: CCrmExternalChannelRestProxy::processMethodRequest
public function processMethodRequest($name, $nameDetails, $arParams, $nav, $server)
{
	$connector = new Rest\CCrmExternalChannelConnector();

	$name = mb_strtoupper($name);

	if ($name === 'COMPANY' || $name === 'CONTACT' || $name === 'ACTIVITY')
	{
		$resultImport = array();
		$errorBatch = array();

		$isRegistered = false;
		$methodParams = $this->resolveArrayParam($arParams, 'params');
		if(($channel_id = $methodParams['CHANNEL_ID']) && $channel_id <> '')
		{
			$connector->setChannelId($channel_id);
			$isRegistered = $connector->isRegistered();
			$originator_id = $connector->getOriginatorId();
		}

		if(!$isRegistered)
		{
			throw new RestException('Connector not found!', self::ERROR_CONNECTOR_NOT_FOUND, CRestServer::STATUS_NOT_FOUND);
		}
		elseif(empty($originator_id) || $originator_id === '')
		{
			throw new RestException('Connector is invalid!', self::ERROR_CONNECTOR_INVALID, CRestServer::STATUS_FORBIDDEN);
		}
		else
		{
			$nameSuffix = mb_strtoupper(!empty($nameDetails)? implode('_', $nameDetails) : '');

			if($name === 'COMPANY' || $nameSuffix === 'COMPANY')
				$entity = new CCrmCompanyRestProxy();
			else
				$entity = new CCrmContactRestProxy();

			$preset = new Rest\CCrmExternalChannelImportPreset();
			$preset->setOwnerEntity($entity);

			if(!$this->isValidID(EntityRequisite::getDefaultPresetId($entity->getOwnerTypeID())))
			{
				throw new RestException("Preset is not defined.", self::ERROR_PRESET_NOT_FOUND, CRestServer::STATUS_FORBIDDEN);
			}

			$import = new Rest\CCrmExternalChannelImport($connector, $preset);

			$batch = $import->resolveParamsBatch($arParams);

			if (!is_array($batch) || count($batch) === 0)
			{
				throw new RestException("Batch is not defined.", self::ERROR_IMPORT_BATCH, CRestServer::STATUS_WRONG_REQUEST);
			}
			elseif(count($batch)>\CRestUtil::BATCH_MAX_LENGTH)
			{
				throw new RestException("Max batch length exceeded ".\CRestUtil::BATCH_MAX_LENGTH, \CRestProvider::ERROR_BATCH_LENGTH_EXCEEDED, CRestServer::STATUS_WRONG_REQUEST);
			}
			else
			{
				$added = 0;
				$updated = 0;
				$skipped = 0;
				$errorList = array();
				$errorBatchDetails = array();

				foreach($batch as $num => $items)
				{
					$errors = array();
					$activity = new Rest\CCrmExternalChannelImportActivity();
					$agent = 	new Rest\CCrmExternalChannelImportAgent();

					$activity->setOwnerEntity($entity);
					$agent->setEntity($entity);

					$activity->import = $import;
					$agent->import = $import;

					$import->setRawData($items);

					$isNewVersionAgent = false;

					$bAgentAdd = false;
					$bAgentUpd = false;
					$bAgentSkip = false;
					$bActivityAdd = false;
					$bActivityUpd = false;

					if(($agentFields = $items[Rest\CCrmExternalChannelImport::AGENT]) && count($agentFields)>0)
					{
						if($import->checkLicense('tariff'))
						{
							if($import->checkLicense('accepted'))
							{
								if((is_set($agentFields, Rest\CCrmExternalChannelImport::FIELDS) && count($agentFields[Rest\CCrmExternalChannelImport::FIELDS])>0) ||
									(is_set($agentFields, Rest\CCrmExternalChannelImport::EXTERNAL_FIELDS) && count($agentFields[Rest\CCrmExternalChannelImport::EXTERNAL_FIELDS])>0))
								{
									$r = $agent->checkFields($agentFields[Rest\CCrmExternalChannelImport::FIELDS]);

									if($r->getErrors())
									{
										$errorList[$num] = $import->formatErrorsPackage(implode('; ', $r->getErrors()), $num);
										$errorBatch[$num] = $r->getErrors();
									}
									else
									{
										$r = $agent->tryGetOwnerInfos($agentFields[Rest\CCrmExternalChannelImport::FIELDS]);
										$resOwnerInfos = $r->getData();
										$ownerInfo = $resOwnerInfos['RESULT'];

										$isNewVersionAgent = ($ownerInfo['ORIGIN_VERSION'] == '' ||
											$ownerInfo['ORIGIN_VERSION'] !== $agentFields[Rest\CCrmExternalChannelImport::FIELDS]['ORIGIN_VERSION']);

										if($isNewVersionAgent)
										{
											if($r->getErrors())
											{
												$errorList[$num] = $import->formatErrorsPackage(implode('; ', $r->getErrors()), $num);
												$errorBatch[$num] = $r->getErrors();
											}
											else
											{
												$resultAgent = array();
												$r = $agent->modify($ownerInfo, $agentFields, $resultAgent);

												$agentId = $resultAgent['id'];

												if($r->getErrors())
												{
													$errorList[$num] = $import->formatErrorsPackage($r->getErrors(), $num);
													$errorBatch[$num] = $r->getErrors();
													$errorBatchDetails[$num] = $r->getData();
												}
												elseif(!$this->isValidID($agentId))
												{
													$errorList[$num] = $import->formatErrorsPackage("Agent is not created", $num);
													$errorBatch[$num][] = new Main\Error("Agent is not created", 1001);
												}
												else
												{
													$bAgentAdd = $resultAgent['process']['add'];
													$bAgentUpd = $resultAgent['process']['upd'];
													$activity->setOwnerEntityId($agentId);
												}
											}
										}
										else
										{
											$activity->setOwnerEntityId($ownerInfo['ID']);
											$bAgentSkip = true;
										}
									}
								}
								else
								{
									$errorList[$num] = $import->formatErrorsPackage("Agent fields or external fields is not defined.", $num);
									$errorBatch[$num][] = new Main\Error("Agent fields or external fields is not defined.", 1002);
								}

								if(empty($errorList[$num]))
								{
									if($name === 'ACTIVITY')
									{
										if(($activityInfo = $items[Rest\CCrmExternalChannelImport::ACTIVITY]) && count($activityInfo)>0)
										{
											$resultActivity = array();

											$typeId = Rest\CCrmExternalChannelImport::resolveTypeIdActivityByFields($activityInfo);

											if($typeId>0)
											{
												$activity->setTypeActivity(Rest\CCrmExternalChannelActivityType::resolveName($typeId));

												$r = $activity->import($activityInfo, $resultActivity, $fileds);

												$activityId = $resultActivity['id'];

												if($r->getErrors())
												{
													$errorList[$num] = $import->formatErrorsPackage($r->getErrors(), $num);
													$errorBatch[$num] = $r->getErrors();
												}
												elseif(!$this->isValidID($activityId))
												{
													$errorList[$num] = $import->formatErrorsPackage("Activity is not imported", $num);
													$errorBatch[$num][] = new Main\Error("Activity is not imported", 1003);
												}
												else
												{
													$bActivityAdd = $resultActivity['process']['add'];
													$bActivityUpd = $resultActivity['process']['upd'];
												}
											}
											else
											{
												$errorList[$num] = $import->formatErrorsPackage("Type activity is not defined.", $num);
												$errorBatch[$num][] = new Main\Error("Type activity is not defined.", 1004);
											}
										}
										else
										{
											$errorList[$num] = $import->formatErrorsPackage("Activity is not defined.", $num);
											$errorBatch[$num][] = new Main\Error("Activity is not defined.", 1005);
										}
									}
									else
									{
										if($isNewVersionAgent)
										{
											$fields = array();

											$activity->setTypeActivity(Rest\CCrmExternalChannelActivityType::ImportAgentName);

											$activity->fillEmptyFields($fields, $agentFields);

											$items[Rest\CCrmExternalChannelImport::ACTIVITY] = array();

											$activity->fillFields($fields);

											$activityId = $activity->getEntity()->innerAdd($fields, $errors);

											if(count($errors)>0)
											{
												$errorList[$num] = $import->formatErrorsPackage(implode('; ', $errors), $num);
												$errorBatch[$num][] = new Main\Error(implode('; ', $errors), 1006);
											}
											elseif(!$this->isValidID($activityId))
											{
												$errorList[$num] = $import->formatErrorsPackage("Activity is not created", $num);
												$errorBatch[$num][] = new Main\Error("Activity is not created", 1007);
											}
											else
											{
												$activity->registerActivityInChannel($activityId, $connector);
											}
										}
									}
								}
							}
							else
							{
								throw new RestException("License agreement not accepted", self::ERROR_BAD_LICENSE_AGREEMENT, CRestServer::STATUS_FORBIDDEN);
							}
						}
						else
						{
							throw new RestException("License restricted", self::ERROR_LICENSE_RESTRICTED, CRestServer::STATUS_FORBIDDEN);
						}
					}
					else
						$errorList[$num] = $import->formatErrorsPackage("Agent is not defined.", $num);

					if($name === 'COMPANY' || $name === 'CONTACT')
					{
						if($bAgentAdd) $added++;
						if($bAgentUpd) $updated++;
						if($bAgentSkip) $skipped++;
					}
					elseif($name === 'ACTIVITY')
					{
						if($bActivityAdd) $added++;
						if($bActivityUpd) $updated++;
					}
				}
			}

			if($added>0)
				$resultImport['added'] = $added;
			if($updated>0)
				$resultImport['updated'] = $updated;
			if($skipped>0)
				$resultImport['skipped'] = $skipped;
			if(count($errorList)>0)
				$resultImport['result_error'] = implode(';', $errorList);

			$detail = array();
			$resultErrors = array();
			if(count($errorBatch)>0)
			{

				foreach($errorBatch as $num=>$errors)
				{
					$detail[$num]['id'] = $num;

					foreach($errors as $error)
					{
						/**@var $error Error */
						$resultErrors[] = array(
							'code' => $error->getCode(),
							'message' => str_replace('\\', '/', $error->getMessage()),
						);
					}

					$detail[$num]['errors'] = $resultErrors;
					$detail[$num]['requisites'] = $this->prepareDetailErrors($errorBatchDetails[$num]['requisites']);
				}

				foreach($detail as $errors)
				{
					$resultImport['batch'][] = $errors;
				}
			}

			return $resultImport;
		}
	}

	throw new RestException("Resource '{$name}' is not supported in current context.");
}

Добавить комментарий