<?php

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

class Product_install extends InstallController
{

    public function __construct()
    {
        parent::__construct();

        // Set acl module name
        $this->module_config['module_acl_name'] = 'product';

        // Set title
        $this->module_config['module_title'] = $this->lang->line('Products dictionaries');

        // Set module image
        $this->module_config['module_image'] = '';

        // Set actions
        $this->module_config['module_actions'] = array(
            // 'acl_action' => 'description'
            'view_product' => $this->lang->line('View'),
            'edit_product' => $this->lang->line('Edit'),
        );

        // Set menu items
        $this->module_config['module_menu_items'] = array(
//            array('method in admin controller', 'title in menu', 'path to the image. empty - default image', 'acl_action')
            array('manufacturer', $this->lang->line('Manufacturers'), '', 'edit_product'),
            array('attributes', $this->lang->line('Attributes'), '', 'edit_product'),
            array('groups', $this->lang->line('Product templates'), '', 'edit_product'),
            array('icons', $this->lang->line('Product icons'), '', 'edit_product'),
//            array('products', $this->lang->line('Products'), '', 'edit_product'),
        );
    }

    public function install($options = [])
    {
        // Create photogallery tables
        $this->load->module('alphagallery/alphagallery_admin');
        $config['main_table'] = 'product_photo'; // Main photo table
        $config['foreign_key'] = 'id_product'; // foreign table primery key
        $this->alphagallery_admin->create_tables($config);

        // Video
        $this->load->library('rtgrid_video/rtgrid_video_api', array('table' => 'product_video', 'parent_field' => 'id_product'));
        $this->rtgrid_video_api->install();

        // Create image folders
        mkdir(trim($this->config->item('product_photo_path'), '/'), 0755, true);
        mkdir(trim($this->config->item('product_icon_photo_path'), '/'), 0755, true);

        return parent::install(array(
                    'shemaFile' => dirname(__DIR__) . '/data/install.sql',
        ));
    }

    public function uninstall($options = [])
    {
        if ($this->input->post('remove_tables')) {
            $this->db->query('DELETE FROM `admin_menu` WHERE `page` = \'Products\';');

            // Drop photogallery tables
            $this->load->module('alphagallery/alphagallery_admin');
            $config['main_table'] = 'product_photo'; // Main photo table
            $this->alphagallery_admin->drop_tables($config);

            // Remove photos and folders
            self::delTree(trim($this->config->item('product_photo_path'), '/'));
            self::delTree(trim($this->config->item('product_icon_photo_path'), '/'));
        }

        return parent::uninstall(array(
                    'dropTables' => array(
                        'product_related_linker',
                        'product_icon_linker',
                        'product_attr_value',
                        'product_group_linker',
                        'product_icon',
                        'product_translate',
                        'product',
                        'product_group',
                        'product_attr_translate',
                        'product_attr',
                        'product_manufacturer_translate',
                        'product_manufacturer',
                        'product_video',
                    ),
        ));
    }

    public static function delTree($dir)
    {
        $files = array_diff(scandir($dir), array('.', '..'));
        foreach ($files as $file) {
            (is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
        }
        return rmdir($dir);
    }

}
