<?php
// Выбираем все таблицы, смотрим есть ли у них фильтры
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields` ADD `view_tb` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `field_id`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields` ADD `view_add` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `view_tb`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields` ADD `view_edit` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `view_add`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields` ADD `view` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `view_edit`");

run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields_par` ADD `view_tb` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `field_id`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields_par` ADD `view_add` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `view_tb`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields_par` ADD `view_edit` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `view_add`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields_par` ADD `view` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `view_edit`");

// Настройка расширенных прав в таблице
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_tables` ADD `ext_acc` TINYINT( 1 ) NOT NULL DEFAULT '0'");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_tables_par` ADD `ext_acc` TINYINT( 1 ) NOT NULL DEFAULT '0'");


// Переносим права read_tb в view_tb
run_query("UPDATE " . $config["table_prefix"] . "acc_fields SET `view_tb`=`read_tb`");

// Переносим права read в view
run_query("UPDATE " . $config["table_prefix"] . "acc_fields SET `view`=`read`");

// Переносим права write в view_edit
run_query("UPDATE " . $config["table_prefix"] . "acc_fields SET `view_add`=`write`, `view_edit`=`write`");

// Склеиваем права read_tb и read в read
run_query("UPDATE " . $config["table_prefix"] . "acc_fields SET `read`=1 where `read_tb`=1");

// Переносим атрибут hidden
$result = run_query("SELECT * FROM " . $config["table_prefix"] . "fields WHERE `hidden`=1");
while ($one_field = mysqli_fetch_assoc($result)) {
    $field_id = $one_field['id'];
    run_query("UPDATE " . $config["table_prefix"] . "acc_fields SET `view_tb`='0', `view_add`='0', `view_edit`='0', `view`='0' WHERE field_id='$field_id'");
}

// Проставляем настройки расширенных прав в таблицах
$result = run_query("SELECT * FROM " . $config["table_prefix"] . "fields WHERE (`type_field`='7' or `type_field`='11') and (`type_value` like '%|1')");
while ($one_field = mysqli_fetch_assoc($result)) {
    $table_id = $one_field['table_id'];
    $s = "UPDATE " . $config["table_prefix"] . "acc_tables SET `ext_acc`='1' WHERE table_id='$table_id'";
    run_query($s);
    // ставим разрешения по данными полям для просмотра в таблице, просмотре, редактировании
    $result2 = run_query("SELECT * FROM " . $config["table_prefix"] . "acc_fields_ex WHERE user_fid='" . $one_field['id'] . "'");
    while ($ex_right = mysqli_fetch_assoc($result2)) {
        if ($ex_right['read_tb']) {
            $s = "UPDATE " . $config["table_prefix"] . "acc_fields SET `view_tb`='1' WHERE field_id='" . $ex_right['field_id'] . "'";
            run_query($s);
        }
        if ($ex_right['read']) {
            $s = "UPDATE " . $config["table_prefix"] . "acc_fields SET `view`='1' WHERE field_id='" . $ex_right['field_id'] . "'";
            run_query($s);
        }
        if ($ex_right['write']) {
            $s = "UPDATE " . $config["table_prefix"] . "acc_fields SET `view_add`='1', `view_edit`='1' WHERE field_id='" . $ex_right['field_id'] . "'";
            run_query($s);
        }
    }

};
// Склеиваем права read_tb и read в read
run_query("UPDATE " . $config["table_prefix"] . "acc_fields_ex SET `read`=1 where `read_tb`=1");

run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields` DROP `read_tb`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields_par` DROP `read_tb`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "acc_fields_ex` DROP `read_tb`");
run_query("ALTER TABLE `" . $config["table_prefix"] . "fields` DROP `hidden`");


?>