• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/util/replicator/task.php
  • Класс: BitrixTasksUtilReplicatorTask
  • Вызов: Task::saveItemFromSource
protected function saveItemFromSource($source, $dataMixin, $userId = 0)
{
	/** @var Item $source */
	$source = $this->getSourceInstance($source, $userId);
	$converter = $this->getConverter();
	$dstInstance = null;

	$creationResult = new Result();

	$conversionResult = $source->transform($converter); // converted to the destination instance
	if($conversionResult->isSuccess()) // was able to produce an item
	{
		$dataMixin['SE_CHECKLIST'] = new ItemTaskCheckList();

		$dstInstance = $conversionResult->getInstance();
		$dstInstance->setData($dataMixin);

		$taskModel = TaskModel::createFromTaskItem($dstInstance);
		if (!TaskAccessController::can($userId, ActionDictionary::ACTION_TASK_SAVE, null, $taskModel))
		{
			$creationResult->getErrors()->add('ACCESS_DENIED.RESPONSIBLE_AND_ORIGINATOR_NOT_ALLOWED', Loc::getMessage('TASKS_REPLICATOR_ACCESS_DENIED'));
			return $creationResult;
		}

		$saveResult = $dstInstance->save();
		if(!$saveResult->isSuccess()) // but was not able to save it
		{
			$dstInstance->abortTransformation($this->getConverter()); // rolling back possible temporal data creation
		}
		else
		{
			$resultId = $dstInstance->getId();

			// save scenario
			ScenarioTable::insertIgnore($resultId, [ScenarioTable::SCENARIO_DEFAULT]);

			$commentPoster = CommentPoster::getInstance($resultId, $userId);
			$commentPoster->enableDeferredPostMode();
			$commentPoster->clearComments();

			$sourceId = $source->getId();
			$toCheckListFacade = static::getToCheckListFacade();
			$fromCheckListFacade = static::getFromCheckListFacade();

			$checkListItems = $fromCheckListFacade::getByEntityId($sourceId);
			$checkListItems = array_map(
				static function($item) {
					$item['COPIED_ID'] = $item['ID'];
					unset($item['ID']);
					return $item;
				},
				$checkListItems
			);

			$occurUserId = UtilUser::getOccurAsId();
			$checklistUserId = $occurUserId ?? $userId;

			$checkListRoots = $toCheckListFacade::getObjectStructuredRoots($checkListItems, $resultId, $checklistUserId);
			foreach ($checkListRoots as $root)
			{
				/** @var CheckList $root */
				$checkListSaveResult = $root->save();
				if (!$checkListSaveResult->isSuccess())
				{
					$saveResult->loadErrors($checkListSaveResult->getErrors());
				}
			}

			$this->sendEvents($resultId, $userId);
		}

		if(!$saveResult->getErrors()->isEmpty())
		{
			$creationResult->getErrors()->load($saveResult->getErrors());
		}
	}
	else
	{
		if(!$conversionResult->getErrors()->isEmpty())
		{
			$creationResult->getErrors()->load($conversionResult->getErrors());
		}
	}

	$creationResult->setInstance($dstInstance);

	return $creationResult;
}