<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Upgrade ciext module to version 3.1.0
 *
 * @author Igor Ratsun
 */
class Ciext_upgrade_3_0_12 extends Abstract_upgrade
{

    protected $db;
    protected $lang;

    public function __construct()
    {
        $this->db = get_instance()->db;
        $this->lang = get_instance()->lang;
    }

    public function up()
    {
        if (!$this->is_table_exist('translate')) {
            $this->update_lan_table()->create_translate_table();
        }

        if (!$this->is_table_exist('translate_value')) {
            $this->create_translate_value_table();
        }

        $this->add_acl_action()->add_menu_item();

        return '3.1.0';
    }

    public function down()
    {
        return '3.0.12';
    }

    protected function update_lan_table()
    {
        $this->db->query('ALTER TABLE lan ENGINE = InnoDB;');
        $this->db->query('ALTER TABLE `lan` ADD UNIQUE(`short_name`);');

        return $this;
    }

    protected function create_translate_table()
    {
        $this->db->query('CREATE TABLE IF NOT EXISTS `translate`'
                . ' ( `id_translate` int(11) NOT NULL AUTO_INCREMENT, `line` text CHARACTER SET utf8 NOT NULL,'
                . 'PRIMARY KEY (`id_translate`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');

        return $this;
    }

    protected function create_translate_value_table()
    {
        $this->db->query('CREATE TABLE IF NOT EXISTS `translate_value`'
                . ' ( `id_translate_value` int(11) NOT NULL AUTO_INCREMENT'
                . ', `id_translate` int(11) NOT NULL'
                . ', `id_lan` int(11) NOT NULL'
                . ', `value` text CHARACTER SET utf8 NOT NULL,'
                . 'PRIMARY KEY (`id_translate_value`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
        $this->db->query('ALTER TABLE `translate_value` ADD INDEX(`id_translate`);');
        $this->db->query('ALTER TABLE `translate_value` ADD INDEX(`id_lan`);');
        $this->db->query('ALTER TABLE `translate_value` ADD FOREIGN KEY (`id_translate`) REFERENCES `translate`(`id_translate`) ON DELETE CASCADE ON UPDATE CASCADE;');
        $this->db->query('ALTER TABLE `translate_value` ADD FOREIGN KEY (`id_lan`) REFERENCES `lan`(`id_lan`) ON DELETE CASCADE ON UPDATE CASCADE;');
        $this->db->query('ALTER TABLE `translate_value` ADD UNIQUE `unique_id_translate_id_lan` (`id_translate`, `id_lan`) COMMENT \'\';');

        return $this;
    }

    protected function add_acl_action()
    {
        $where = [
            'id_acl_resource' => 2,
            'acl_action_name' => 'translate_manager',
        ];
        $check = $this->db->select('id_acl_action')->where($where)->get('acl_action')->row_array();
        if (empty($check)) {
            $this->db->insert('acl_action', array_merge($where, ['acl_action_alias' => $this->lang->line('Translate manager')]));
        }

        return $this;
    }

    protected function add_menu_item()
    {
        $where = [
            'page'         => 'translate/values',
            'acl_resource' => 'system',
            'acl_action'   => 'translate_manager',
        ];
        $check = $this->db->select('id_admin_menu')->where($where)->get('admin_menu')->row_array();
        if (empty($check)) {
            $this->db->insert('admin_menu', array_merge($where, [
                'name'      => $this->lang->line('Translate manager'),
                'parent_id' => 0,
                'active'    => 1,
            ]));
        }

        return $this;
    }

}
