<?php
run_query('ALTER TABLE ' . $config['table_prefix'] . 'calc ADD `num` TINYINT(1) NOT NULL DEFAULT 0 AFTER table_id');
$table_id = 0;
$result = sql_select_field($config['table_prefix'] . 'calc', 'id, table_id', 'arc = 0 AND table_id != 0 AND hidden = 0 ORDER BY table_id, id');
while ($calc = sql_fetch_assoc($result)) {
    $skip = 0;
    $result2 = sql_select_field($config['table_prefix'] . 'calc_cond', 'type', 'calc_id = ', $calc['id']);
    while ($cond = sql_fetch_assoc($result2)) {
        if (($cond['type'] != EVT_LINE_SAVE) && ($cond['type'] != EVT_FIELD_SHOW) && ($cond['type'] != EVT_FIELD_CHANGE) &&
            ($cond['type'] != EVT_TABLE_IMPORT) && ($cond['type'] != EVT_LINE_DROP) && ($cond['type'] != EVT_LINE_RESTORE) && ($cond['type'] != EVT_SCHEDULE)
        ) {
            $skip = 1;
            break;
        }
    }
    if ($skip) continue;

    if ($calc['table_id'] != $table_id) {
        $table_id = $calc['table_id'];
        $num = 1;
    }
    sql_update($config['table_prefix'] . 'calc', ['num' => $num++], 'id = ', $calc['id']);
}