Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

module generated by module-generator is now automatically backup data…

…base when uninstalled
  • Loading branch information...
commit e72a479ac98a322ae6319ae4c1660b7954506bfa 1 parent 4fe4f04
@goFrendiAsgard authored
View
3  modules/module_generator/assets/script/script_set.js
@@ -162,6 +162,7 @@ function submit(){
if(response.success){
$("#error_message").hide();
$("#error_message").html('');
+ alert('The module has been generated successfully');
}else{
var html = "";
html += "<ul>";
@@ -172,11 +173,13 @@ function submit(){
html += "</ul>";
$("#error_message").html(html);
$("#error_message").show();
+ alert('Some error ocured when trying to generate module');
}
},
error: function(response){
$("#error_message").html('AJAX request failed');
$("#error_message").show();
+ alert('Some error ocured when trying to generate module');
console.log(response);
},
complete: function(){
View
31 modules/module_generator/controllers/module_generator.php
@@ -69,7 +69,7 @@ public function generate(){
if($overwrite && $this->is_exists($directory) && !$this->is_writable($directory)){
$errors[] = "Directory '/modules/$directory' is not writable";
}else if(!$overwrite && $this->is_exists($directory)){
- $errors[] = "Directory '/modules/$directory' already exists";
+ $errors[] = "Directory '/modules/$directory' already exists, activate 'Force overwrite' option to overwrite";
}
}
if(!$structures){
@@ -98,6 +98,7 @@ public function generate(){
$result = array(
"table_name" => $table_name,
"fields" => $this->generator_model->list_fields($table_name),
+ "ai_field" => $this->generator_model->auto_increment_field($table_name),
"navigation_name" => underscore($directory.'_'.$navigation_caption),
"navigation_caption" => $navigation_caption,
"function_name" => $function_name,
@@ -150,10 +151,15 @@ private function make_module($module){
$this->make_folder($directory.'/controllers');
$this->make_folder($directory.'/controllers/data');
$this->make_folder($directory.'/assets');
- $this->make_folder($directory.'/assets/db/');
- $this->make_folder($directory.'/assets/images/');
- $this->make_folder($directory.'/assets/scripts/');
- $this->make_folder($directory.'/assets/styles/');
+ $this->make_folder($directory.'/assets/db');
+ $this->make_folder($directory.'/assets/images');
+ $this->make_folder($directory.'/assets/scripts');
+ $this->make_folder($directory.'/assets/styles');
+
+ $this->make_htaccess_deny($directory.'/models');
+ $this->make_htaccess_deny($directory.'/views');
+ $this->make_htaccess_deny($directory.'/controllers');
+ $this->make_htaccess_deny($directory.'/assets/db');
// get all unique table_name & controller name
$unique_table_names = array();
@@ -198,7 +204,7 @@ private function make_module($module){
$remove_navigation_template = $this->read_file($current_module_path."/res/install_partial/remove_navigation.php.txt");
$navigation_parent_name = $directory.'_index';
$add_navigation = "";
- $remove_navigation = "";
+ $remove_navigation = "";
foreach($navigations as $navigation){
$search = array(
'@navigation_name',
@@ -216,12 +222,18 @@ private function make_module($module){
$remove_navigation = $this->replace($remove_navigation_template, $search, $replace).
PHP_EOL.$remove_navigation;
}
+ $quoted_unique_table_names = array();
+ foreach($unique_table_names as $table_name){
+ $quoted_unique_table_names[] = "'".$table_name."'";
+ }
+ $unique_table_list = implode(', ', $quoted_unique_table_names);
$search = array(
'@navigation_parent_name',
'@add_navigations',
'@remove_navigations',
'@namespace',
'@directory',
+ '@table_list',
);
$replace = array(
$navigation_parent_name,
@@ -229,6 +241,7 @@ private function make_module($module){
$remove_navigation,
$namespace,
$directory,
+ $unique_table_list,
);
$this->copy_file($current_module_path.'/res/install.php.txt',
$directory.'/controllers/install.php', $search, $replace);
@@ -256,9 +269,11 @@ private function make_module($module){
$table_name = $property['table_name'];
$navigation_name = $property['navigation_name'];
$fields = $property['fields'];
+ $ai_field = $property['ai_field'];
$function_name = $property['function_name'];
$quoted_fields = array();
foreach($fields as $field){
+ if($field == $ai_field) continue;
// quoted_fields
$quoted_fields[] = "'".$field."'";
// display_as
@@ -312,6 +327,10 @@ private function make_file($fileName, $content){
chmod(BASEPATH.'../modules/'.$fileName, 0777);
}
+ private function make_htaccess_deny($folderName){
+ $this->make_file($folderName.'/.htaccess', 'Deny From All');
+ }
+
private function read_file($fileName){
return file_get_contents(BASEPATH.'../modules/'.$fileName);
}
View
24 modules/module_generator/models/generator_model.php
@@ -13,14 +13,14 @@ public function __construct(){
// get tables & fields list
public function list_tables(){
$list_tables = $this->db->list_tables();
- $result = array();
+ $return = array();
foreach($list_tables as $table_name){
- $result[] = array(
+ $return[] = array(
"table_name" => $table_name,
"fields" => $this->list_fields($table_name),
);
}
- return $result;
+ return $return;
}
// get fields of certain table
@@ -28,14 +28,28 @@ public function list_fields($table_name){
$return = $this->db->list_fields($table_name);
return $return;
}
-
+
+ // get auto increment field
+ public function auto_increment_field($table_name){
+ $table_name = addslashes($table_name);
+ $sql = "SHOW COLUMNS
+ FROM `$table_name`
+ WHERE Extra LIKE '%auto_increment%'";
+ $query = $this->db->query($sql);
+ if($query->num_rows()>0){
+ $row = $query->row();
+ return $row->Field;
+ }else{
+ return '';
+ }
+ }
// get create tables syntax
public function get_create_tables_syntax($table_names){
$result_create = '';
$result_insert = '';
for($i=0; $i<count($table_names); $i++){
- $table = $table_names[$i];
+ $table = addslashes($table_names[$i]);
// get create syntax
$query = $this->db->query("SHOW CREATE TABLE `$table`");
View
59 modules/module_generator/res/install.php.txt
@@ -9,12 +9,13 @@ class Install extends CMS_Module_Installer {
protected $NAME = '@namespace';
//this should be what happen when user install this module
- protected function do_install(){
+ protected function do_install(){
$this->remove_all();
$this->build_all();
}
//this should be what happen when user uninstall this module
protected function do_uninstall(){
+ $this->backup_database(array(@table_list));
$this->remove_all();
}
@@ -27,13 +28,10 @@ class Install extends CMS_Module_Installer {
// remove parent of all navigations
$this->remove_navigation("@navigation_parent_name");
- $sql_array = explode('/*split*/',
- file_get_contents(BASEPATH.'../modules/'.
- $module_path.'/assets/db/uninstall.sql')
- );
- foreach($sql_array as $sql){
- $this->db->query($sql);
- }
+ // import uninstall.sql
+ $this->import_sql(BASEPATH.'../modules/'.$module_path.
+ '/assets/db/uninstall.sql');
+
}
private function build_all(){
@@ -45,15 +43,54 @@ class Install extends CMS_Module_Installer {
// add navigations
@add_navigations
-
+
+ // import install.sql
+ $this->import_sql(BASEPATH.'../modules/'.$module_path.
+ '/assets/db/install.sql');
+ }
+
+ private function import_sql($file_name){
$sql_array = explode('/*split*/',
- file_get_contents(BASEPATH.'../modules/'.
- $module_path.'/assets/db/install.sql')
+ file_get_contents($file_name)
);
foreach($sql_array as $sql){
$this->db->query($sql);
}
}
+
+ private function backup_database($table_names, $limit = 100){
+ $module_path = $this->cms_module_path();
+
+ $this->load->dbutil();
+ $sql = '';
+
+ // create DROP TABLE syntax
+ for($i=count($table_names)-1; $i>=0; $i--){
+ $table_name = $table_names[$i];
+ $sql .= 'DROP TABLE IF EXISTS `'.$table_name.'`; '.PHP_EOL;
+ }
+ if($sql !='')$sql.= PHP_EOL;
+
+ // create CREATE TABLE and INSERT syntax
+ $prefs = array(
+ 'tables' => $table_names,
+ 'ignore' => array(),
+ 'format' => 'txt',
+ 'filename' => 'mybackup.sql',
+ 'add_drop' => FALSE,
+ 'add_insert' => TRUE,
+ 'newline' => PHP_EOL
+ );
+ $sql.= $this->dbutil->backup($prefs);
+
+ //write file
+ $file_name = 'backup_'.date('Y-m-d_G:i:s').'.sql';
+ file_put_contents(
+ BASEPATH.'../modules/'.$module_path.'/assets/db/'.$file_name,
+ $sql
+ );
+
+ }
}
?>
View
4 readme.md
@@ -262,9 +262,11 @@ Roadmap
* (done, tested) flexigrid should also be responsive
* (done, tested) use "slow slidetoggle" in help and bootstrap theme
* (done, tested) use bootstrap for installation
-* (proposed) use database for help module
+* (done, tested) use database for help module
* (done, tested) upload new module feature
* (done, tested) upload new theme feature
* (done, tested) use 'title' for blog url
* (cancelled) change default controller programmatically and use it on $this->view
* (done, tested) add how to change default_controller instructions
+* (done, tested) module generated by module generator should backup all needed database everytime uninstalled
+* (proposed) master-detail by using grocery-crud
Please sign in to comment.
Something went wrong with that request. Please try again.