/**
 * Удаляет все коментарии пользователя.
 * @param int $UserId
 * @return void
 */
function DeleteAllUserComments($UserId)
{
    $UserId = SafeEnv($UserId, 11, int);
    $where = "`user_id`='{$UserId}'";
    $ctables = System::database()->Select('comments');
    foreach ($ctables as $table) {
        $comms = System::database()->Select(SafeEnv($table['table'], 255, str), $where);
        $comments = array();
        $objects = array();
        //Отсортировываем id комментарий по объектам
        foreach ($comms as $com) {
            $comments[$com['object_id']] = SafeEnv($com['id'], 11, int);
            $objects[] = SafeEnv($com['object_id'], 11, int);
        }
        //теперь нужно обойти все объекты уменьшая счетчик
        foreach ($objects as $obj) {
            $id_coll = SafeEnv($table['id_coll'], 255, str);
            CalcCounter($table['objects_table'], "`{$id_coll}`='{$obj}'", $table['counter_coll'], count($comments[$obj]) * -1);
        }
        System::database()->Delete(SafeEnv($table['table'], 255, str), $where);
    }
}
function AdminCommentsDelete()
{
    if (!isset($_POST['delcomments'])) {
        GO(ADMIN_FILE . '?exe=comments');
    }
    if (isset($_GET['page'])) {
        $page = SafeEnv($_GET['page'], 10, int);
    } else {
        $page = 1;
    }
    $com_tables = System::database()->Select('comments');
    foreach ($com_tables as $table) {
        $comments_tables[$table['table']] = $table;
    }
    $posts_tables = array();
    $del_posts = explode('#', $_POST['delcomments']);
    foreach ($del_posts as $post) {
        if ($post != '') {
            $a = explode('--', $post);
            $posts_tables[$a[1]][] = array($a[0], $a[2]);
        }
    }
    // Удаляем комментарии для каждой таблицы отдельно
    foreach ($posts_tables as $post_table => $posts_id) {
        $where = '';
        foreach ($posts_id as $p) {
            $post_id = SafeEnv($p[0], 11, int);
            $obj_id = SafeEnv($p[1], 11, int);
            $where .= "`id`='{$post_id}' or ";
            $t = $comments_tables[$post_table];
            CalcCounter($t['objects_table'], "`{$t['id_coll']}`='{$obj_id}'", $t['counter_coll'], -1);
        }
        $where = substr($where, 0, strlen($where) - 4);
        System::database()->Delete($post_table, $where);
        Audit('Комментарии: Удаление комментариев (table: ' . $post_table . ', where: ' . $where . ')');
    }
    GO(ADMIN_FILE . '?exe=comments&page=' . $page);
}