Как загрузить файл runComponentAction

PHP
/**
 * Импортируем файл 
 * @param array $data
 * @param null|array $params
 * @return array
 */
public function importAction(array $data, ?array $params = []): array
{
   try {
           ... 
   } catch (\Throwable $th) {
      $this->addError(new Error($th->getMessage(), $th->getCode()));
   }
   return $file;
}
JS
const data = document.getElementById();
// data = 

BX.ajax.runComponentAction('company:sale.basket.detail', 'import', { 
   mode: 'ajax',
   data: {data},
})
?.then(response => {...})
.catch(response => {...})

Как скачать сгенерированный файл runComponentAction

PHP
/**
 * Экспортирует данные в файл
 * @param int $id
 * @param string $type
 * @param null|array $params
 * @return null|BFile
 */
public function exportAction(int $id, string $type, ?array $params = []): ?BFile
{
   try {
      $pFilename = \TEMP_DIR . '/' . 'item.xls';
      \file_put_contents($pFilename, $data);
      $arFile = \CFile::MakeFileArray($pFilename, false, true);
      $file = new BFile($arFile);
   } catch (\Throwable $th) {
      $this->addError(new Error($th->getMessage(), $th->getCode()));
   }
   return $file;
}
JS
BX.ajax.runComponentAction('company:sale.basket.detail', 'export', { 
   mode: 'ajax',
   data: {id, type},
   method: 'POST',
})
?.then(response => {
   if (response.status === 'success' && response.data?.url) {
     window.location.href = response.data.url;
   }
})
.catch(response => {...})

Как скачать файл runComponentAction

PHP
/**
 * Экспортирует данные в файл
 * @param int $id
 * @param string $type
 * @param null|array $params
 * @return null|BFile
 */
public function exportAction(int $id, string $type, ?array $params = []): ?BFile
{
   try {
      $file = \Bitrix\Main\Engine\Response\BFile::createByFileId(985);
   } catch (\Throwable $th) {
      $this->addError(new Error($th->getMessage(), $th->getCode()));
   }
   return $file;
}
JS
BX.ajax.runComponentAction('company:sale.basket.detail', 'export', { 
   mode: 'ajax',
   data: {id, type},
   method: 'POST',
})
?.then(response => {
   if (response.status === 'success' && response.data?.url) {
     window.location.href = response.data.url;
   }
})
.catch(response => {...})

Bitrix GetCurPage to getRequestedPageDirectory D7

Получить путь текущий страницы БЕЗ index.php
Старое ядро:
$APPLICATION->GetCurPage();
Новое ядро:
\Bitrix\Main\Context::getCurrent()->getRequest()->getRequestedPageDirectory();
Получить путь текущий страницы с index.php
Старое ядро:
$APPLICATION->GetCurPage(true);
Новое ядро:
\Bitrix\Main\Context::getCurrent()->getRequest()->getRequestedPage();

Перевод массива меню в многомерный

Перевод массива меню в многомерный по полям DEPTH_LEVEL LEFT_MARGIN RIGHT_MARGIN

Вот пример:
$arResult['SECTIONS'] = [
    ['NAME' => 'test1', 'DEPTH_LEVEL' => 1, 'LEFT_MARGIN' => 1, 'RIGHT_MARGIN' => 14],
    ['NAME' => 'test2', 'DEPTH_LEVEL' => 2, 'LEFT_MARGIN' => 2, 'RIGHT_MARGIN' => 5],
    ['NAME' => 'test3', 'DEPTH_LEVEL' => 2, 'LEFT_MARGIN' => 6, 'RIGHT_MARGIN' => 13],
    ['NAME' => 'test4', 'DEPTH_LEVEL' => 3, 'LEFT_MARGIN' => 7, 'RIGHT_MARGIN' => 12],
    ['NAME' => 'test5', 'DEPTH_LEVEL' => 3, 'LEFT_MARGIN' => 8, 'RIGHT_MARGIN' => 11],
    ['NAME' => 'test6', 'DEPTH_LEVEL' => 3, 'LEFT_MARGIN' => 9, 'RIGHT_MARGIN' => 10],
    ['NAME' => 'test7', 'DEPTH_LEVEL' => 2, 'LEFT_MARGIN' => 14, 'RIGHT_MARGIN' => 15],
];

function buildTree($items, $left = 1, $right = PHP_INT_MAX, $depth = 1): array
{
    $outputArray = [];
    foreach ($items as $item) {
        if ($item['LEFT_MARGIN'] > $left && $item['RIGHT_MARGIN'] < $right && $item['DEPTH_LEVEL'] == $depth) {
            $item['ITEMS'] = buildTree($items, $item['LEFT_MARGIN'], $item['RIGHT_MARGIN'], $item['DEPTH_LEVEL'] + 1);
            $outputArray[] = $item;
        }
    }
    return $outputArray;
}

$arResult['SECTIONS'] = buildTree($arResult['SECTIONS']);
Страницы: 1 | 2 | 3 | 4 | 5 | ... | 7 | След.