• Модуль: rpa
  • Путь к файлу: ~/bitrix/modules/rpa/lib/controller/stage.php
  • Класс: BitrixRpaControllerStage
  • Вызов: Stage::getSortByPreviousStageId
protected function getSortByPreviousStageId(BitrixRpaModelStage $newStage, int $stageId): int
{
	$stages = $newStage->getType()->getStages()->getAll();
	$nextStage = $sort = null;
	if($stageId === 0)
	{
		$sort = static::SORT_STEP;
		if(isset($stages[0]))
		{
			$nextStage = $stages[0];
		}
	}
	else
	{
		foreach($stages as $index => $stage)
		{
			if($stage->getId() === $stageId)
			{
				if(isset($stages[$index + 1]))
				{
					$nextStage = $stages[$index + 1];
					$sort = $stage->getSort();
				}
				else
				{
					$sort = $stage->getSort() + static::SORT_STEP;
				}
			}
		}
	}
	if($nextStage)
	{
		$sort = floor((($nextStage->getSort() - $sort) / 2) + $sort);
		if($nextStage->getSort() <= $sort)
		{
			$nextStageSort = $this->getSortByPreviousStageId($nextStage, $nextStage->getId());
			$nextStage->setSort($nextStageSort);
			$nextStage->save();
			$sort = floor((($nextStage->getSort() - $sort) / 2) + $sort);
		}
	}

	return $sort;
}