- Модуль: tasks
- Путь к файлу: ~/bitrix/modules/tasks/lib/checklist/internals/checklisttree.php
- Класс: BitrixTasksCheckListInternalsCheckListTree
- Вызов: CheckListTree::delete
static function delete($id, array $parameters = [])
{
$result = new Result();
if (!static::isNodeExist($id))
{
$result = static::addErrorToResult($result, ['#ID#' => $id], __METHOD__, 'NODE_NOT_FOUND');
}
else if ($parameters['DELETE_SUBTREE'])
{
$connection = Application::getConnection();
$tableName = static::getTableName();
$parentColumnName = static::getParentNodeColumnName();
$childColumnName = static::getNodeColumnName();
/** @noinspection PhpUndefinedClassInspection */
$subQuerySql = Helper::getTemporaryTableSubQuerySql(
"SELECT {$childColumnName} FROM {$tableName} WHERE {$parentColumnName} = {$id}",
$childColumnName
);
$sql = "
DELETE FROM {$tableName}
WHERE {$childColumnName} IN ({$subQuerySql})
";
try
{
$connection->query($sql);
}
catch (SqlException $exception)
{
$result->addException($exception);
}
if($result->isSuccess())
{
$removed = $connection->getAffectedRowsCount();
if(!$removed)
{
$result->addWarning('NO_ROWS_AFFECTED', 'No rows were affected');
}
}
}
else
{
$parentId = static::getClosestParentId($id);
$detachResult = static::detach($id); //detach node with its subtree
$result->adoptErrors($detachResult);
if ($detachResult->isSuccess())
{
$children = static::getChildren($id, ['DIRECT' => true]);
if (!empty($children))
{
foreach ($children as $childId)
{
$subDetachResult = static::detach($childId); //detach each sub-tree
$result->adoptErrors($subDetachResult);
if ($parentId !== $id && $subDetachResult->isSuccess())
{
static::attach($childId, $parentId); //attach each sub-tree to the parent
}
}
}
static::delete($id, ['DELETE_SUBTREE' => true]); //remove node itself
}
}
return $result;
}