<?php

namespace Ciext\Migration;

abstract class AbstractUpgrade
{

    /**
     * @return string New version of module
     */
    abstract public function up();

    /**
     * @return string New version of module
     */
    abstract public function down();

    /**
     * Check for table existing
     * @param string $table
     * @return boolean
     */
    public function is_table_exist($table)
    {
        $ci     = & get_instance();
        $result = $ci->db->query('SELECT * FROM information_schema.tables'
                        . ' WHERE TABLE_SCHEMA = \'' . $ci->db->database . '\''
                        . ' AND TABLE_NAME = \'' . $table . '\''
                        . ' LIMIT 1')->row_array();

        return !empty($result);
    }

    /**
     * Check for existing column in table
     * @param string $table
     * @param string $column
     * @return boolean
     */
    public function is_column_exist($table, $column)
    {
        if (!$this->is_table_exist($table)) {

            return false;
        }
        $ci     = & get_instance();
        $result = $ci->db->query('SELECT * FROM information_schema.COLUMNS'
                        . ' WHERE TABLE_SCHEMA = \'' . $ci->db->database . '\''
                        . ' AND TABLE_NAME = \'' . $table . '\''
                        . ' AND COLUMN_NAME = \'' . $column . '\'')->row_array();

        return !empty($result);
    }

    /**
     * Add new page template to resmng
     * @param string $name template name
     * @param int $parent_id parent resource id
     * @param array $content ['id_lan' => 'some html', 'id_lan2' => 'some html2']
     * @return boolean|array false or data of page template
     */
    public function add_page_template($name = '', $parent_id = 1, $content = [])
    {
        if (empty($name) || empty($parent_id)) {

            return false;
        }

        if (!$this->is_page_template_exist($name)) {
            $ci                   = & get_instance();
            $ci->load->model('resmng/resmng_model');
            $_POST['id_resource'] = $ci->resmng_model->create($name, 2, $parent_id);
            if (!empty($content)) {
                $_POST['content'] = $content;
                $ci->resmng_model->set_content();
            }
        }

        return $this->fetch_page_template_by_name($name);
    }

    /**
     * Check for existing page template
     * @param type $name
     * @return boolean
     */
    public function is_page_template_exist($name = '')
    {
        $check = $this->fetch_page_template_by_name($name);

        return !empty($check);
    }

    /**
     * Add new item to menu if not exists
     * @param string $name
     * @param string $page
     * @param int $parent_id
     * @param string $acl_resource
     * @param string $acl_action
     * @param string $image
     * @param int $view_order
     * @return boolean
     */
    public function add_admin_menu_item($name, $page, $parent_id, $acl_resource, $acl_action, $image = '', $view_order = 100)
    {
        $table  = 'admin_menu';
        $where  = [
            'page'         => $page,
            'parent_id'    => $parent_id,
            'acl_resource' => $acl_resource,
            'acl_action'   => $acl_action,
        ];
        $db     = & get_instance()->db;
        $exists = $db->select('id_admin_menu')->where($where)->get($table)->row_array();
        if (empty($exists)) {
            $db->insert($table, array_merge($where, [
                'name'       => $name,
                'image'      => $image,
                'view_order' => $view_order,
                'active'     => true,
            ]));
        }

        return true;
    }

    protected function fetch_page_template_by_name($name = '')
    {
        $ci  = & get_instance();
        $res = $ci->db->where('name', $name)
                        ->get('resource_type_page_template')->row_array();

        return $res;
    }

}
