<?php

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

class skeleton_model_admin extends CI_Model
{

    const WRITE_PERM = 0775;

    protected $srcFolder = '';
    protected $moduleFolder = '';

    public function __construct()
    {
        $this->srcFolder = dirname(__DIR__) . '/data';
    }

    public function createModule($mainFolder)
    {
        $this->moduleFolder = FCPATH . $mainFolder;

        // Create main folder
        if (!$this->createFolder($this->moduleFolder)) {

            return FALSE;
        }
        // Create config file
        if ($this->input->post('create_config_file')) {
            $confFolder = $this->moduleFolder . '/config';
            if (!$this->createFolder($confFolder)) {

                return FALSE;
            }
            if (!$this->createConfigFile($confFolder)) {

                return FALSE;
            }
        }
        // Create language file
        if ($this->input->post('create_lang_file')) {
            $langFolder = $this->moduleFolder . '/language';
            if (!$this->createFolder($langFolder)) {

                return FALSE;
            }
            if (!$this->createLangFiles($langFolder)) {

                return FALSE;
            }
        }
        // Create dummy SQL file
        if ($this->input->post('create_sql_file')) {
            $sqlFolder = $this->moduleFolder . '/data';
            if (!$this->createFolder($sqlFolder)) {

                return FALSE;
            }
            if (!$this->createSqlFile($sqlFolder)) {

                return FALSE;
            }
        }

        // Run chunk
        foreach ($this->config->item('chunk') as $v) {
            foreach ($v as $model) {
                $name = str_replace('/', '_', $model);
                if ($this->{$name} instanceof chunk_interface) {
                    $this->{$name}->run();
                }
            }
        }

        return TRUE;
    }

    public function getSrcFolder()
    {
        return $this->srcFolder;
    }

    public function getModuleFolder()
    {
        return $this->moduleFolder;
    }

    public function createFolder($folder)
    {
        if (file_exists($folder)) {

            return TRUE;
        }
        
        $error_message = sprintf($this->lang->line('Can\'t create folder `%s`'), $folder);

        try {
            $oldmask = umask(0);
            $create = mkdir($folder, self::WRITE_PERM, TRUE);
            umask($oldmask);
            if (!$create) {
                throw new Exception($error_message);
            }
        } catch (Exception $ex) {
            show_error($error_message);
        }

        return file_exists($folder);
    }

    public function createFile($filename, $data)
    {
        try {
            $rs = file_put_contents($filename, $data);
            chmod($filename, self::WRITE_PERM);
        } catch (Exception $ex) {
            show_error(sprintf($this->lang->line('Can\'t create file `%s`'), $filename));
        }

        return $rs;
    }

    protected function createConfigFile($folder)
    {
        return $this->createFile($folder . '/config.php', file_get_contents($this->srcFolder . '/config/config.php'));
    }

    protected function createLangFiles($langFolder)
    {
        foreach (array('english.mo', 'english.po') as $v) {
            if (!$this->createFile($langFolder . '/' . $v, file_get_contents($this->srcFolder . '/language/' . $v))) {

                return FALSE;
            }
        }

        return TRUE;
    }

    protected function createSqlFile($sqlFolder)
    {
        return $this->createFile($sqlFolder . '/install.sql', file_get_contents($this->srcFolder . '/data/install.sql'));
    }

}
