<?php

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

class chunk_m_table extends CI_Model implements chunk_interface
{

    public function get_admin_html()
    {
        return '<div class="control-group"><div class="controls">'
                . form_checkbox('create_m_table_file', '1', $this->input->post('create_m_table_file')) . ' ' . $this->lang->line('Create tmodel for table')
                . ' ' . form_input('tmodel_table', $this->input->post('tmodel_table'), 'placeholder="' . $this->lang->line('Enter table name') . '"')
                . '</div></div>';
    }

    public function run()
    {
        // Create install controller
        if ($this->input->post('create_m_table_file')) {
            $moduleName = $this->input->post('moduleName');
            $table = $this->input->post('tmodel_table');
            $folder = $this->skeleton_model_admin->getModuleFolder() . '/models';
            $file = $folder . '/' . $table . '_tmodel.php';
            
            if (!$this->skeleton_model_admin->createFolder($folder) || file_exists($file) || empty($table)) {

                return FALSE;
            }
            
            $source = str_replace(array('MODULE', 'TABLE'), array($moduleName, $table), file_get_contents($this->skeleton_model_admin->getSrcFolder() . '/models/tmodel.php'));

            return $this->skeleton_model_admin->createFile($file, $source);
        }
    }

}
