<?php

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

class skeleton_admin extends MX_Controller {

    public function __construct() {
        $this->load->model('skeleton/skeleton_model_admin');
    }

    public function index() {
        $this->load->library('ciext/admin_lib', 'ciext/ciext');
        
        // Acceess check
        if (!$this->rt_user->isAllowed('resmng', 'resmng_view')) {
            $this->admin_lib->show_denied();
        }
        
        // Unset left menu
        $this->admin_lib->content['user_left_menu'] = '';
        // Set top menu active
        $this->admin_lib->content['active_menu'] = 2;

        $this->ciext->load_bootstrap();
        $this->load->helper('form');
        $lang = $this->lang->load('skeleton/admin', '', TRUE);

        $content = array(
            'message' => '',
            'form_open' => form_open('', array('class' => 'form-horizontal')),
            'moduleFolderValue' => $this->input->post('moduleFolder'),
            'moduleNameValue' => $this->input->post('moduleName'),
            'form_close' => form_close()
        );

        $content['moduleFolderValue'] = ($content['moduleFolderValue']) ? : APPPATH . 'modules/';

        if ($this->input->post('send')) {
            $moduleFolder = rtrim($content['moduleFolderValue'], '/') . '/' . $this->input->post('moduleName');
            $messaga = $lang['Other error'];
            if (!$this->input->post('moduleName')) {
                $messaga = $lang['Empty module name'];
            } else
            if (file_exists($moduleFolder)) {
                $messaga = $lang['Module already exists'];
            } else
            if ($this->skeleton_model_admin->createModule($moduleFolder)) {
                $messaga = $lang['Created'];
            }

            $content['message'] = sprintf('<div class="alert alert-%s"><button type="button" class="close" data-dismiss="alert">&times;</button>%s</div>', (($messaga == $lang['Created']) ? 'success' : 'error'), sprintf($messaga, $this->input->post('moduleName')));
        }

        return array(
            'title' => $lang['title'],
            'content' => $this->parser->parse('skeleton/template.php', array_merge($content, $lang), TRUE)
        );
    }

}
