<?php

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

class skeleton_model_admin extends CI_Model {

    public function createModule($mainFolder) {
        $moduleName = $this->input->post('moduleName');
        if (!$this->createFolders($mainFolder) || !$this->createConfig($mainFolder, $moduleName) || !$this->createControllers($mainFolder, $moduleName) || !$this->createSQL($mainFolder) || !$this->createLanguage($mainFolder, $moduleName) || !$this->createModels($mainFolder, $moduleName) || !$this->createViews($mainFolder, $moduleName)) {
            @unlink($mainFolder);

            return FALSE;
        }

        return TRUE;
    }

    protected function createFolders($mf) {
        $folders = array(
            '',
            '/config',
            '/controllers',
            '/data',
            '/language',
            '/models',
            '/views'
        );

        foreach ($this->config->item('allowed_language') as $lang) {
            $folders[] = '/language/' . $lang;
        }

        $oldmask = umask(0);
        foreach ($folders as $folder) {
            if (!mkdir($mf . $folder, 0775, TRUE)) {

                return FALSE;
            }
        }
        umask($oldmask);

        return TRUE;
    }

    protected function createConfig($mf, $moduleName) {
        $config = array(
            'config'
        );

        return $this->_createFiles($mf, 'config', $moduleName, $config, FALSE);
    }

    protected function createControllers($mf, $moduleName) {
        $controllers = array(
            'admin',
            'install',
            'site'
        );

        return $this->_createFiles($mf, 'controllers', $moduleName, $controllers);
    }

    protected function createSQL($mf) {
        $file = $mf . '/data/install.sql';
        if (file_put_contents($file, '') === FALSE) {

            return FALSE;
        }
        chmod($file, 0775);

        return TRUE;
    }

    protected function createLanguage($mf, $moduleName) {
        $items = array(
            'admin_lang',
            'install_lang'
        );

        foreach ($this->config->item('allowed_language') as $lang) {
            if (!$this->_createFiles($mf, 'language/' . $lang, $moduleName, $items, FALSE)) {

                return FALSE;
            }
        }

        return TRUE;
    }

    protected function createModels($mf, $moduleName) {
        $models = array(
            'model_admin',
            'model_site'
        );

        return $this->_createFiles($mf, 'models', $moduleName, $models);
    }

    protected function createViews($mf, $moduleName) {
        $views = array(
            'template'
        );

        return $this->_createFiles($mf, 'views', $moduleName, $views, FALSE);
    }

    protected function _createFiles($mainFolder, $folder, $moduleName, $files, $filePrefix = TRUE) {
        $src_folder = dirname(__DIR__) . '/data/files/';
        foreach ($files as $item) {
            $file_data = file_get_contents($src_folder . $folder . '/' . $item . '.html');
            $data = '<?php ' . $this->parser->parse_string($file_data, array('moduleName' => $moduleName), TRUE);
            $file = $mainFolder . '/' . $folder . '/' . (($filePrefix) ? $moduleName . '_' : '') . $item . '.php';
            if (!file_put_contents($file, $data)) {

                return FALSE;
            }
            chmod($file, 0775);
        }

        return TRUE;
    }

}
