{*
  Шаблон вычисления "Копировать запись множественно"
*}

$link_lines = explode('-', trim($line['{$link_field}']['raw'], '-'));
foreach ($link_lines as $link_line_id) {
    $new_id = data_insert({$correlation['table']}, EVENTS_ENABLE, [
{foreach from = $correlation['to_field'] key=key item=field_id}
        '{form_int_name($field_id)}' => {if $correlation['def_val'][$key] && $correlation['def_val'][$key] != 'null'}{if $correlation['type_field'][$key] == 2}'{form_eng_time($correlation['def_val'][$key])}'{else}'{$correlation['def_val'][$key]}'{/if}{elseif $correlation['cur_field'][$key] == substr($link_field, 1)}$link_line_id{elseif $correlation['cur_field'][$key]}$line['{form_int_name($correlation['cur_field'][$key])}']{if $correlation['type_field'][$key] == 5}['raw']{/if}{else}''{/if},
{/foreach}
    ]);
{foreach from = $correlation['to_field'] key=key item=field_id}
{if $correlation['type_field'][$key] == 6 || $correlation['type_field'][$key] == 9}
    $files = $line['f{$correlation['cur_field'][{$key}]}'] ? explode("\r\n", $line['f{$correlation['cur_field'][{$key}]}']) : [];
    $fieldId = {$correlation['cur_field'][{$key}]};
    $lineId = $line['id'];
    foreach ($files as $fname) {
        if(!$fname) {
            return;
        }
        // Получаем объект поля
        $fieldsRep = \CB\Data\Fields\FieldRepository::getInstance();
        $field = $fieldsRep->get($fieldId); // Находим поле по id поля. Чтобы получить id таблицы

        if (!$field) {
            return;
        }

        $tableId = $field->get('table_id'); // Получаем ID таблицы
        $commonData = \CB\Core::get(\CB\Core::COMMON_DATA); // Получаем объект работы с данными
        $tableModels = $commonData->getTable($tableId); // Получаем объект таблицы

        if (!$tableModels) {
            return;
        }

        if (!$lineId) {
            return;
        }

        $recordObject = $tableModels->getRecord($lineId); // Получаем объект записи

        if (!$recordObject) {
            return;
        }

        $curFilesObject = $recordObject->get('f' . $fieldId); // Получаем объект поля

        if (!$curFilesObject) {
            return;
        }
        save_data_file({$field_id}, $new_id, $fname, $curFilesObject->getContent($fname));
    }
{/if}
    $new_id_arr[] = $new_id;
{/foreach}
}
{if $correlation['save_field']}
$line['f{$correlation['save_field']}'] = '-' . implode('-', $new_id_arr) . '-';
data_update($table_id, EVENTS_ENABLE, ['f{$correlation['save_field']}' => $line['f{$correlation['save_field']}']], 'id = ' . $ID);
{/if}
