Skip to content

Instantly share code, notes, and snippets.

@francisrod01
Forked from dbpolito/gist:504389298511a4a679bc
Last active August 29, 2015 14:01
Show Gist options
  • Save francisrod01/b692ff9af73b78af59e0 to your computer and use it in GitHub Desktop.
Save francisrod01/b692ff9af73b78af59e0 to your computer and use it in GitHub Desktop.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Constructor_lib {
function __construct() {
$this->CI =& get_instance();
}
function create_controller_files($tables = NULL, $create = FALSE) {
if(!$tables) $tables = $this->CI->db->list_tables();
foreach($tables AS $table) {
$path = APPPATH;
$filename = 'admin.php';
$columns = $this->CI->db->list_fields($table);
if($create) {
if(is_dir($path.'modules/')) {
if(is_dir($path.'modules/'.$table)) {
$path = $path.'modules/'.$table.'/';
} else {
if(mkdir($path.'modules/'.$table, 0755)) {
$path = $path.'modules/'.$table.'/';
}
}
}
if(is_dir($path.'controllers')) {
$path = $path.'controllers/';
} else {
if(mkdir($path.'controllers', 0755)) {
$path = $path.'controllers/';
}
}
} else {
$path = $path.'modules/'.$table.'/';
}
$output = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\n";
$output .= "class Admin extends MY_Controller {\n\n";
$output .= "\tfunction __construct()\n";
$output .= "\t{\n";
$output .= "\t\tparent::__construct();\n\n";
$output .= "\t\t\$this->load->model('".$table."_model', '$table');\n";
$output .= "\t}\n\n";
$output .= "\t// LIST\n";
$output .= "\tfunction index()\n";
$output .= "\t{\n";
$output .= "\t\t\$this->data['" . $table . "s'] = \$this->" . $table . "->get(TRUE);\n\n";
$output .= "\t\t\$this->template\n";
$output .= "\t\t\t\t->set_css('jquery/table')\n";
$output .= "\t\t\t\t->set_js('jquery/table')\n";
$output .= "\t\t\t\t->title(lang('list_$table'))\n";
$output .= "\t\t\t\t->build('admin/index', \$this->data);\n";
$output .= "\t}\n\n";
$output .= "\t// CREATE/EDIT FORM\n";
$output .= "\tfunction form(\$id = NULL)\n";
$output .= "\t{\n";
$output .= "\t\t\$title = is_null(\$id) ? lang('add_$table') : lang('edit_$table');\n\n";
$output .= "\t\t// VALIDATE FORM\n";
$output .= "\t\tif (!\$this->" . $table . "->validate())\n";
$output .= "\t\t{\n";
$output .= "\t\t\t\$this->data['info_type'] = 'error';\n";
$output .= "\t\t\t\$this->data['info_message'] = (validation_errors() ? lang('error_required_fields') : \$this->data['info_message']);\n\n";
$output .= "\t\t\t// PREPARE EDIT FORM\n";
$output .= "\t\t\tif(\$_SERVER['REQUEST_METHOD'] != 'POST' && \$id)\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t\t\$params['where'] = array('$table.id' => \$id);\n";
$output .= "\t\t\t\t\$this->" . $table . "->prep_validation(\$params);\n";
$output .= "\t\t\t}\n\n";
$output .= "\t\t\t\$this->template\n";
$output .= "\t\t\t\t\t->set('$table', (array)\$this->$table)\n";
$output .= "\t\t\t\t\t->title(\$title)\n";
$output .= "\t\t\t\t\t->build('admin/form', \$this->data);\n";
$output .= "\t\t}\n";
$output .= "\t\telse\n";
$output .= "\t\t{\n";
$output .= "\t\t\t// SUCCESS\n";
$output .= "\t\t\t\$this->" . $table . "->save(\$id);\n\n";
$output .= "\t\t\tif(!is_null(\$id))\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t\talert('success', '" . ucwords($table) . " ' . \$id . ' Updated');\n";
$output .= "\t\t\t\tlog_message('info', '" . ucwords($table) . " ' . \$id . ' Updated');\n";
$output .= "\t\t\t}\n";
$output .= "\t\t\t\n";
$output .= "\t\t\telse\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t\talert('success', '" . ucwords($table) . " Created');\n";
$output .= "\t\t\t\tlog_message('info', '" . ucwords($table) . " Created');\n";
$output .= "\t\t\t}\n\n";
$output .= "\t\t\tredirect('admin/$table');\n";
$output .= "\t\t}\n";
$output .= "\t}\n\n";
$output .= "\t// DELETE\n";
$output .= "\tfunction delete(\$id = NULL)\n";
$output .= "\t{\n";
$output .= "\t\t\$id = (int)\$id;\n";
$output .= "\t\tif(\$id > 0)\n";
$output .= "\t\t{\n";
$output .= "\t\t\t\$this->" . $table . "->delete(array('$table.id' => \$id));\n";
$output .= "\t\t\tlog_message('info', '" . ucwords($table) . " '.\$id.' Deleted');\n";
$output .= "\t\t\tif(\$this->is_ajax())\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t\techo '" . ucwords($table) . " Deleted';\n";
$output .= "\t\t\t\texit;\n";
$output .= "\t\t\t}\n";
$output .= "\t\t\telse\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t\talert('info', '" . ucwords($table) . " Deleted');\n";
$output .= "\t\t\t}\n";
$output .= "\t\t}\n";
$output .= "\t\telse\n";
$output .= "\t\t{\n";
$output .= "\t\t\tif(\$this->is_ajax())\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t\techo '" . ucwords($table) . " Not Found';\n";
$output .= "\t\t\t\texit;\n";
$output .= "\t\t\t}\n";
$output .= "\t\t\telse\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t\talert('error', '" . ucwords($table) . " Not Found');\n";
$output .= "\t\t\t}\n";
$output .= "\t\t}\n\n";
$output .= "\t\tredirect('admin/" . $table ."');\n";
$output .= "\t}\n";
$output .= "}\n\n";
$output .= "/* End of file $filename */\n";
$output .= "/* Location: ./$path$filename */";
if($create != FALSE) {
file_put_contents($path.$filename, $output);
echo 'Created '.$path.$filename.'<br />';
} else {
echo $filename;
}
debug_code($output);
}
}
function create_model_files($tables = NULL, $create = FALSE) {
if(!$tables) $tables = $this->CI->db->list_tables();
foreach($tables AS $table) {
$path = APPPATH;
$filename = $table.'_model.php';
$columns = $this->CI->db->list_fields($table);
if($create) {
if(is_dir($path.'modules/')) {
if(is_dir($path.'modules/'.$table)) {
$path = $path.'modules/'.$table.'/';
} else {
if(mkdir($path.'modules/'.$table, 0755)) {
$path = $path.'modules/'.$table.'/';
}
}
}
if(is_dir($path.'models')) {
$path = $path.'models/';
} else {
if(mkdir($path.'models', 0755)) {
$path = $path.'models/';
}
}
} else {
$path = $path.'modules/'.$table.'/';
}
$output = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\n";
$output .= "class " . ucwords($table) . "_model extends MY_Model {\n\n";
$output .= "\tfunction __construct()\n";
$output .= "\t{\n";
$output .= "\t\tparent::__construct();\n\n";
$output .= "\t\t\$this->table_name = '$table';\n";
$output .= "\t\t\$this->select_fields = 'SQL_CALC_FOUND_ROWS $table.*';\n";
$output .= "\t\t\$this->primary_key = array('$table.id');\n";
$output .= "\t}\n\n";
$output .= "\t// VALIDATE POST FORM\n";
$output .= "\tfunction validate()\n";
$output .= "\t{\n";
foreach($columns as $column) {
if($column != "id") {
$output .= "\t\t\$this->form_validation->set_rules('$column', lang('$column'), 'required');\n";
}
}
$output .= "\n";
$output .= "\t\treturn parent::validate();\n";
$output .= "\t}\n\n";
$output .= "\t// SAVE DATA\n";
$output .= "\tfunction save(\$id = NULL)\n";
$output .= "\t{\n";
$output .= "\t\t\$db_array = parent::db_array();\n\n";
$output .= "\t\t// NEW RECORD\n";
$output .= "\t\tif(is_null(\$id))\n";
$output .= "\t\t\t{\n";
$output .= "\t\t\t// DO SOMETHING\n";
$output .= "\t\t}\n\n";
$output .= "\t\treturn parent::save(\$db_array, \$id);\n";
$output .= "\t}\n";
$output .= "}\n\n";
$output .= "/* End of file $filename */\n";
$output .= "/* Location: ./$path$filename */";
if($create != FALSE) {
file_put_contents($path.$filename, $output);
echo 'Created '.$path.$filename.'<br />';
} else {
echo $filename;
}
debug_code($output);
}
}
function create_view_files($tables = NULL, $create = FALSE) {
if(!$tables) $tables = $this->CI->db->list_tables();
foreach($tables AS $table) {
$path = APPPATH;
if($create) {
if(is_dir($path.'modules/')) {
if(is_dir($path.'modules/'.$table)) {
$path = $path.'modules/'.$table.'/';
} else {
if(mkdir($path.'modules/'.$table, 0755)) {
$path = $path.'modules/'.$table.'/';
}
}
}
if(is_dir($path.'views')) {
$path = $path.'views/';
} else {
if(mkdir($path.'views', 0755)) {
$path = $path.'views/';
}
}
if(is_dir($path.'admin')) {
$path = $path.'admin/';
} else {
if(mkdir($path.'admin', 0755)) {
$path = $path.'admin/';
}
}
} else {
$path = $path.'modules/'.$table.'/';
}
$columns = $this->CI->db->list_fields($table);
$filename = 'index.php';
$output = "\t\t<a class=\"ui-button ui-widget ui-state-default ui-corner-all submit\" href=\"{site_url('admin/$table/form')}\">{lang('add_$table')}</a>\n";
$output .= "\t\t<table class=\"table\">\n";
$output .= "\t\t\t<thead>\n";
$output .= "\t\t\t\t<tr>\n";
$i = 0;
foreach($columns as $column) {
if($i++ <= 5 && !strpos($column, '_id')) {
$output .= "\t\t\t\t\t<th" . ($column == 'id' ? ' style="width: 50px;"' : '') . ">{lang(\"$column\")}</th>\n";
}
}
$output .= "\t\t\t\t\t<th class=\"no-sort\" style=\"width: 60px;\">{lang(\"action\")}</th>\n";
$output .= "\t\t\t\t</tr>\n";
$output .= "\t\t\t</thead>\n";
$output .= "\t\t\t<tfoot>\n";
$output .= "\t\t\t\t<tr>\n";
$i = 0;
foreach($columns as $column) {
if($i++ <= 5 && !strpos($column, '_id')) {
$output .= "\t\t\t\t\t<th>{lang(\"$column\")}</th>\n";
}
}
$output .= "\t\t\t\t\t<th>{lang(\"action\")}</th>\n";
$output .= "\t\t\t\t</tr>\n";
$output .= "\t\t\t</tfoot>\n";
$output .= "\t\t\t<tbody>\n";
$output .= "\t\t\t\t{foreach \$" . $table . "s $table}\n";
$output .= "\t\t\t\t<tr>\n";
$i = 0;
foreach($columns as $column) {
if($i++ <= 5 && !strpos($column, '_id')) {
$output .= "\t\t\t\t\t<td>{\$$table.$column}</td>\n";
}
}
$output .= "\t\t\t\t\t<td class=\"icons\">\n";
$output .= "\t\t\t\t\t\t<ul>\n";
$output .= "\t\t\t\t\t\t\t<li class=\"ui-state-default ui-corner-all\">\n";
$output .= "\t\t\t\t\t\t\t\t<a href=\"{site_url('admin/$table/form/\$$table.id')}\" class=\"tooltip\" title=\"{lang('edit_record')}\"><span class=\"ui-icon ui-icon-pencil\"></span></a>\n";
$output .= "\t\t\t\t\t\t\t</li>\n";
$output .= "\t\t\t\t\t\t\t<li class=\"ui-state-default ui-corner-all\">\n";
$output .= "\t\t\t\t\t\t\t\t<a href=\"{site_url('admin/$table/delete/\$$table.id')}\" class=\"tooltip\" title=\"{lang('delete_record')}\"><span class=\"ui-icon ui-icon-trash\"></span></a>\n";
$output .= "\t\t\t\t\t\t\t</li>\n";
$output .= "\t\t\t\t\t\t</ul>\n";
$output .= "\t\t\t\t\t</td>\n";
$output .= "\t\t\t\t</tr>\n";
$output .= "\t\t\t\t{/foreach}\n";
$output .= "\t\t\t</tbody>\n";
$output .= "\t\t</table>\n";
$output .= "\t\t<script type=\"text/javascript\">\n";
$output .= "\t\t$(document).ready(function() {\n";
$output .= "\t\t\t$('.table').dataTable({\n";
$output .= "\t\t\t\t\"bJQueryUI\": true,\n";
$output .= "\t\t\t\t\"sPaginationType\": \"full_numbers\",\n";
$output .= "\t\t\t\t\"aoColumnDefs\": [\n";
$output .= "\t\t\t\t\t{ \"aTargets\": ['no-sort'], 'bSortable': false}\n";
$output .= "\t\t\t\t]\n";
$output .= "\t\t\t});\n";
$output .= "\t\t});\n";
$output .= "\t\t</script>\n";
if($create != FALSE) {
file_put_contents($path.$filename, $output);
echo 'Created '.$path.$filename.'<br />';
} else {
echo $filename;
}
debug_code($output);
$filename = 'form.php';
$output = "\t\t<form class=\"form\" action=\"{current_url()}\" method=\"post\">\n";
$output .= "\t\t\t<fieldset>\n";
foreach($columns as $column) {
if($column != "id") {
$output .= "\t\t\t\t<div>\n";
$output .= "\t\t\t\t\t<label for=\"$column\">{lang('$column')}</label>\n";
$output .= "\t\t\t\t\t<input type=\"text\" id=\"$column\" name=\"$column\" class=\"required\" value=\"{\$$table.$column}\" />\n";
$output .= "\t\t\t\t\t{form_error('$column')}\n";
$output .= "\t\t\t\t</div>\n";
}
}
$output .= "\t\t\t\t<div>\n";
$output .= "\t\t\t\t\t<button class=\"ui-button ui-widget ui-state-default ui-corner-all submit\" type=\"submit\">{lang('submit')}</button>\n";
$output .= "\t\t\t\t</div>\n";
$output .= "\t\t\t</fieldset>\n";
$output .= "\t\t</form>\n";
$output .= "\t\t<script type=\"text/javascript\">\n";
$output .= "\t\t$(document).ready(function() {\n";
$output .= "\t\t\t$('.form').validate();\n";
$output .= "\t\t});\n";
$output .= "\t\t</script>\n";
if($create != FALSE) {
file_put_contents($path.$filename, $output);
echo 'Created '.$path.$filename.'<br />';
} else {
echo $filename;
}
debug_code($output);
echo '<hr />';
}
}
}
/* End of file Factory.php */
/* Location: ./application/libraries/Factory.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment