<?php

class UpgradeRestrictions extends UpgradeNew
{
    public function check_restrictionsTables()
    {
        return $this->checkTableExists('restrictions');
    }

    /**  Create restrictions tables**/
    public function upgrade_restrictionsTables()
    {
        sqlQuery('
         CREATE TABLE restrictions
         (
           id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
           name TEXT DEFAULT NULL,
           domain INT NOT NULL,
           object_value TEXT NOT NULL
         );

         CREATE TABLE restrictions_domains
         (
           id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
           id_restriction INT DEFAULT NULL ,
           id_user INT(11) UNSIGNED DEFAULT NULL,
           country CHAR(2) DEFAULT NULL
         );

         ALTER TABLE restrictions_domains ADD CONSTRAINT `restrictions_domains_id_restrictions` FOREIGN KEY (id_restriction) REFERENCES restrictions(id) ON DELETE CASCADE ON UPDATE CASCADE;
         ALTER TABLE restrictions_domains ADD CONSTRAINT `restrictions_domains_id_user` FOREIGN KEY (id_user) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE;
      ');
    }

    public function check_restrictionsDomainLogin()
    {
        return $this->checkColumnExists('restrictions_domains', 'login');
    }

    /**  Add login column to restrictions **/
    public function upgrade_restrictionsDomainLogin()
    {
        sqlQuery('
         ALTER TABLE restrictions_domains ADD COLUMN login SET(\'logIn\', \'logOut\') DEFAULT NULL;
      ');
    }

    public function check_restrictionsDomainPL()
    {
        return findModule(Modules::PRICE_LEVELS) && $this->checkColumnExists('restrictions_domains', 'id_price_level');
    }

    /**  Add id_price_level column to restrictions **/
    public function upgrade_restrictionsDomainPL()
    {
        sqlQuery('
         ALTER TABLE restrictions_domains ADD COLUMN id_price_level INT UNSIGNED DEFAULT NULL;
      ');
    }

    public function check_idUserGroupColumn()
    {
        return $this->checkColumnExists('restrictions_domains', 'id_users_group');
    }

    /** Add id_users_group into restrictions_domains */
    public function upgrade_idUserGroupColumn()
    {
        sqlQuery('ALTER TABLE restrictions_domains ADD COLUMN id_users_group INT(11) DEFAULT NULL ');
        sqlQuery('ALTER TABLE restrictions_domains ADD CONSTRAINT FK_id_users_group FOREIGN KEY (id_users_group) REFERENCES users_groups(id) ON DELETE CASCADE ON UPDATE CASCADE');

        $this->upgradeOK();
    }

    public function check_domainInvertColumn()
    {
        return $this->checkColumnExists('restrictions', 'domain_invert');
    }

    /** Add domain_invert column into restrictions */
    public function upgrade_domainInvertColumn()
    {
        sqlQuery("ALTER TABLE restrictions ADD COLUMN domain_invert ENUM('Y', 'N') NOT NULL DEFAULT 'N' AFTER domain");

        $this->upgradeOK();
    }

    public function check_restrictionTypeColumn()
    {
        return $this->checkColumnExists('restrictions', 'type');
    }

    /** Restrictions - add type column into restrictions table */
    public function upgrade_restrictionTypeColumn()
    {
        sqlQuery("alter table restrictions add type ENUM ('H', 'S') default 'H' not null comment '(H)ide, (S)how';");

        $this->upgradeOK();
    }
}
