- Модуль: tasks
- Путь к файлу: ~/bitrix/modules/tasks/lib/item/replicator/task/template.php
- Класс: BitrixTasksItemReplicatorTaskTemplate
- Вызов: Template::produceReplicas
private function produceReplicas(Result $result, $source, $destination, $data, $tree, array $parameters = array(), $userId = 0)
{
$converter = $this->getConverter();
// create list of responsibles to create tasks to
$responsibles = array($source['RESPONSIBLE_ID']);
$multiTask = $source['MULTITASK'] == 'Y' && !empty($source['RESPONSIBLES']);
if($multiTask)
{
$responsibles = array_merge(array($source['CREATED_BY']), $source['RESPONSIBLES']);
}
$created = new Collection();
$wereErrors = false;
// for each responsible create a bunch of tasks
foreach($responsibles as $targetUser)
{
$subRootId = false;
$subRoot = $source->transformWith($converter);
if($subRoot->isSuccess())
{
$srInstance = $subRoot->getInstance();
$srInstance['PARENT_ID'] = $destination->getId();
$srInstance['RESPONSIBLE_ID'] = $targetUser;
$subRoot = $srInstance->save();
if($subRoot)
{
$subRootId = $srInstance->getId();
}
else
{
$srInstance->abortTransformation($converter); // rolling back possible temporal data creation
}
}
$created->push($subRoot); // add sub-root-item creation result
if(!$subRoot->isSuccess())
{
$wereErrors = true;
}
if($subRootId) // sub-root created, go further
{
$itemId2ReplicaId = array($source->getId() => $subRootId);
$cTree = $tree;
$srcId = $source->getId();
$walkQueue = array($srcId);
while(!empty($walkQueue)) // walk sub-item tree
{
$topTemplate = array_shift($walkQueue);
if(is_array($cTree[$topTemplate]))
{
foreach($cTree[$topTemplate] as $template)
{
$dataMixin = array_merge(array(
'PARENT_ID' => $itemId2ReplicaId[$topTemplate],
'RESPONSIBLE_ID' => $targetUser
), $parameters);
$creationResult = $this->createItemFromData($data[$template], $dataMixin, $userId);
if($creationResult->isSuccess())
{
$walkQueue[] = $template;
$itemId2ReplicaId[$template] = $creationResult->getInstance()->getId();
} // or else dont go to the sub-item tree of this item
else
{
$wereErrors = true;
}
$created->push($creationResult); // add sub-item creation result
}
}
unset($cTree[$topTemplate]);
}
}
}
if($wereErrors)
{
$result->addError('SUB_ITEMS_CREATION_FAILURE', 'Some of the sub-items was not properly created');
}
return $created;
}