Рекурсивное удаление каталога

17 Сентября 2019 15:57 (Редактировано: 23 Июня 2020 01:34)

<?php
function clearCatalog($root_id = false) {
    if ($root_id === false) {
        return;
    }

    $ids_arr = $this->getChildIds($root_id);
    $in = str_repeat('?,', count($ids_arr) - 1) . '?';
    $table_site_content = "modx_site_content";
    $table_tvs = "modx_site_tmplvar_contentvalues";
    $st = $modx->prepare("
        DELETE FROM $table_site_content, $table_tvs USING $table_site_content

        LEFT JOIN $table_tvs
        ON $table_tvs.contentid = $table_site_content.id

        WHERE $table_site_content.id IN ($in)
    ");
    $st->execute($ids_arr);
}

function getChildIds($root) {
    $st = $modx->prepare("
        SELECT id
        FROM "modx_site_content
        WHERE parent = :parent
    ");
    $st->execute(array('parent' => $root));
    $result_ids = array();
    if ($st) {
        while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
            $result_ids[] = $row['id'];
            $result_ids = array_merge($result_ids, $this->getChildIds($row['id']));
        }
    }
    return $result_ids;
}