<?php

class UpgradeAdmin extends UpgradeNew
{
    public function check_adminFiltersTable()
    {
        return $this->checkTableExists('admin_filters');
    }

    /** Create table for admin_filters storage*/
    public function upgrade_adminFiltersTable()
    {
        sqlQuery("create table admin_filters
            (
            id int auto_increment
                primary key,
            name varchar(50) null,
            description text null,
            type varchar(50) null,
            list text null,
            global enum('Y', 'N') default 'N' null,
            position int null,
            id_admin int null,
            color char(7) default '#AAB2BD' null,
            favorite enum('Y', 'N') default 'N' null,
            CONSTRAINT admin_filters_admin_id_fk
            FOREIGN KEY (id_admin) REFERENCES admins (id) ON DELETE CASCADE ON UPDATE CASCADE
            )
            ;");
        $this->upgradeOK();
    }

    public function check_adminFiltersTableDefault()
    {
        return $this->checkColumnExists('admin_filters', 'default_filter');
    }

    /** Create table for admin_filters column default */
    public function upgrade_adminFiltersTableDefault()
    {
        sqlQuery("ALTER TABLE admin_filters ADD `default_filter` ENUM('Y', 'N') DEFAULT 'N' NULL;");
        $this->upgradeOK();
    }

    public function check_AdminFilterColumns()
    {
        return $this->checkColumnExists('admin_filters', 'columns');
    }

    /** Add columns to admin filters */
    public function upgrade_AdminFilterColumns()
    {
        sqlQuery('ALTER TABLE admin_filters ADD `columns` TEXT DEFAULT NULL;');
        $this->upgradeOK();
    }

    public function check_addAdminName()
    {
        return $this->checkColumnExists('admins', 'name');
    }

    /** Add name field to admins table */
    public function upgrade_addAdminName()
    {
        sqlQuery('ALTER TABLE admins ADD `name` VARCHAR(50) default "" AFTER id');
        $this->upgradeOK();
    }

    public function check_addInventoryApplyDate()
    {
        return findModule(Modules::INVENTORY) && $this->checkColumnExists('inventory', 'date_apply');
    }

    /** Add date_apply to inventory table */
    public function upgrade_addInventoryApplyDate()
    {
        sqlQuery('ALTER TABLE inventory ADD `date_apply` DATETIME default null');
        $this->upgradeOK();
    }
}
