<?php

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

class skeleton_admin extends MX_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('ciext/admin_lib', 'ciext/ciext');
        $this->ciext->load_bootstrap();

        // Acceess check
        if (!$this->rt_user->isAllowed('skeleton', 'edit_skeleton')) {
            $this->admin_lib->show_denied();
        }

        $this->load->config('skeleton/config');

        $this->load->helper(array('form', 'skeleton/chunk_interface'));

        $this->load->library('gettext_language');
        $this->gettext_language->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');

        // Set top menu active
        $this->admin_lib->content['active_menu'] = 2;

        $this->load->model('skeleton/skeleton_model_admin');
    }

    public function index()
    {
        $chunk = array();
        foreach ($this->config->item('chunk') as $k => $v) {
            foreach ($v as $model) {
                $name = str_replace('/', '_', $model);
                $this->load->model($model, $name);
                $chunk[$k][] = $this->{$name}->get_admin_html();
            }
        }

        $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(),
            'chunk' => $chunk
        );

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

        if ($this->input->post('send')) {
            $moduleFolder = rtrim($content['moduleFolderValue'], '/') . '/' . $this->input->post('moduleName');
            $message = $this->lang->line('Module creation fail');
            if (!$this->input->post('moduleName')) {
                $message = $this->lang->line('Module name can\'t be empty');
            } else
            if ($this->skeleton_model_admin->createModule($moduleFolder)) {
                $this->session->set_userdata('last_created_module', $this->input->post('moduleName'));
                $message = $this->lang->line('Module %s created');
            }

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

        return array(
            'title' => $this->lang->line('New module creation'),
            'content' => $this->parser->parse('skeleton/template.php', $content, TRUE)
        );
    }

    public function crud()
    {
        $this->load->model('skeleton/skeleton_crud_generator');

        if ($this->input->post('module_folder')) {
            $this->session->set_userdata('last_created_module', $this->input->post('module_folder'));
            $generate = $this->skeleton_crud_generator->generate();
            if ($generate) {

                return array(
                    'title' => $this->lang->line('CRUD generator result'),
                    'content' => $this->parser->parse('skeleton/crud/crud_result.php', $generate, TRUE),
                );
            } else {
                $error = TRUE;
            }
        }

        $content = array(
            'module_folder' => $this->session->userdata('last_created_module'),
            'error' => (empty($error)) ? FALSE : TRUE,
        );

        // Load callbacks
        $callbacks = $this->skeleton_crud_generator->load_callbacks();
        foreach ($this->config->item('crud_callback_chunk') as $group => $citems) {
            foreach ($citems as $v) {
                $class = str_replace('skeleton/', '', $v);
                $callback = $callbacks[$class];
                $content['callbacks'][$group][$class] = $callback->get_name();
            }
        }
        
        // Load templates
        $this->load->config('skeleton/crud_field_templates');
        $fTemplates = $this->config->item('crud_field_templates');
        foreach ($fTemplates as $k => $v) {
            $content['aSource'][] = array(
                'label' => $k,
                'value' => $v['field'],
                'data' => $v,
            );
        }

        $crud_items = ($this->input->post('item')) ? $this->input->post('item') : array(
            array(
                'field' => '',
                'display_as' => '',
                'callback' => 0,
                'not_null' => FALSE,
                'in_column' => FALSE,
        ));
        $content['crud_items'] = $crud_items;

        return array(
            'title' => $this->lang->line('CRUD generator'),
            'content' => $this->parser->parse('skeleton/crud/crud.php', $content, TRUE),
        );
    }

}
