Skip to content

Commit

Permalink
#2 add backups option and database logging, adjust menu bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Dart committed Mar 8, 2020
1 parent c16b821 commit 573c1d2
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 43 deletions.
32 changes: 32 additions & 0 deletions application/controllers/Backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Backup extends MY_Controller{


function __construct(){
parent::__construct();
$this->load->model('logging_model','logging');
}

function index(){
// Load the DB utility class
$this->load->dbutil();
$dbs = $this->dbutil->list_databases();
// Backup your entire database and assign it to a variable
$backup = $this->dbutil->backup();
$filename = sprintf('backup-%s.sql.gz',date('Y-m-d-H-i-s'));
$path = sprintf('/tmp/');
$temp_file = $path . $filename;
// Load the file helper and write the file to your server
$this->load->helper('file');
write_file($temp_file, $backup);
$this->logging->log('backup','success');
$this->session->set_flashdata('notice','Move the file backup file from your downloads folder to your backups folder.');

// Load the download helper and send the file to your desktop
$this->load->helper('download');
force_download($filename, $backup);
redirect('home');
}


}
5 changes: 3 additions & 2 deletions application/controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Home extends MY_Controller
function __construct()
{
parent::__construct();

}


Expand Down Expand Up @@ -47,6 +46,8 @@ function send($user,$subject, $text){
$errors = "An email has been sent to your account with instructions for resetting your password.";

}




}
}
23 changes: 23 additions & 0 deletions application/controllers/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class Update extends My_Controller {


function __construct() {
parent::__construct();
$this->load->model('Update_model', 'update');
}

function index() {
/*$updates = [
[
'id' => 1,
'query' => 'CREATE TABLE `logging` CHANGE `id` `id` varchar(128) NOT NULL;',
'description' => 'CI upgrade from 3.1.1 to 3.1.2',
],
];
$this->update->run_updates($updates);*/
redirect('home');
}

}
66 changes: 66 additions & 0 deletions application/models/Logging_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Logging_Model extends MY_Model {

var $event_type;

var $event;

var $timestamp;

function __construct() {
parent::__construct();
$this->install_table();
}

function install_table() {
$this->load->dbforge();
$fields = [
'id' => [
'type' => 'INT',
'constraint' => 3,
'unsigned' => TRUE,
'auto_increment' => TRUE,
],
'event_type' => [
'type' => 'VARCHAR',
'constraint' => 255,
],
'event' => [
'type' => 'text',
'unsigned' => TRUE,
],
'timestamp' => [
'type' => 'timestamp',
],
];
$attr = ['engine' => 'InnoDB'];
$this->dbforge->add_field($fields);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('log', TRUE, $attr);
}

function get_latest($event_type) {
$this->db->where("event_type", $event_type);
$this->db->order_by("timestamp", "DESC");
$this->db->limit(1);
$this->db->select("UNIX_TIMESTAMP(`timestamp`) as `unix_time`", FALSE);
$this->db->select("log.*");
$this->db->from("log");
$result = $this->db->get()->row();

return $result;
}


function log($event_type, $event) {
$tables = $this->db->list_tables();
var_dump($tables);
/* $this->event_type = $event_type;
$this->event = $event;
$this->timestamp = date("Y-m-d H:i:s");
$this->db->insert("log", $this);*/

}

}
59 changes: 59 additions & 0 deletions application/models/Update_model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php


class Update_model extends My_Model {


function __construct() {
parent::__construct();
$this->install_table();
}

function install_table() {
$this->load->dbforge();
$fields = [
'id' => [
'type' => 'INT',
'constraint' => 9,
'unsigned' => TRUE,
],
'description' => [
'type' => 'VARCHAR',
'constraint' => 255,
],
'rec_modified' => [
'type' => 'timestamp',
],
];
$attr = ['engine' => 'InnoDB'];
$this->dbforge->add_field($fields);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('updates', TRUE, $attr);
}

/**
* @param array $updates
*/
function run_updates(array $updates) {
foreach ($updates as $update) {
$update = (object) $update;
if ($this->update_exists($update->id) === 0) {
$result = $this->db->query($update->query);
if ($result) {
$this->db->insert('updates', [
'id' => $update->id,
'description' => $update->description,
]);
}
}

}
}

function update_exists($id) {
$this->db->from('updates');
$this->db->where('id', $id);
return $this->db->get()->num_rows();
}

}
33 changes: 14 additions & 19 deletions application/views/menu/list.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed');
$userID = $this->session->userdata("userID");
echo "<div class='button-bar'><span class='button new menu_item_add'>Add Menu Item</span></div>";
$current_menu = "";
foreach($menus as $menu){

if($current_menu != $menu->kMenu){
$current_menu = $menu->kMenu;

echo "<h3>Menu: $menu->name</h3>";
} //endif
echo "<div class='button-bar'><ul class='menu'>";
$button = create_button_object($menu,$userID);
call_user_func('form_dropdown',eval("\$button = \"$button\";"));
echo "<li>$button</li>";
echo "<li><span class='button edit menu_item_edit' id='mie_$menu->kItem'>Edit</span></li>";
echo "</ul></div>";


} //end foreach
$userID = $this->session->userdata("userID");?>
<div class='button-bar'><span class='button new menu_item_add'>Add Menu Item</span></div>
<?php $current_menu = "";?>
<?php foreach($menus as $menu):?>

<?php if($current_menu != $menu->kMenu):?>
<?php $current_menu = $menu->kMenu; ?>
<h3>Menu: <?php print $menu->name;?></h3>
<?php endif; ?>
<div class='button-bar'><ul class='menu'>
<?php $button = create_button_object($menu,$userID);?>
<li><?php print $button;?></li>
<li><a class="button edit" href="<?php print base_url('/menu/edit_item/' . $menu->kItem);?>" id='mie_$menu->kItem'>Edit</a></li>
</ul></div>
<?php endforeach;
18 changes: 9 additions & 9 deletions application/views/page/navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
$nav_buttons['transactions'] = [
"text" => "Transactions",
"href" => site_url('transaction'),
'class' => 'btn btn-sm btn-secondary'
'class' => 'btn btn-sm btn-secondary',
];
$user_buttons['chart_of_accounts'] = [
"item" => "account",
Expand All @@ -53,21 +53,21 @@
'href' => site_url('bank'),
'class' => 'btn btn-sm btn-secondary',
];
$user_buttons['backup'] = [
'item' => 'backup',
'text' => 'Backup',
'title' => 'Download a backup of the site database',
'href' => site_url('backup'),
'class' => 'btn btn-sm btn-warning',
];
$user_buttons["user_list"] = [
"item" => "user",
"text" => "User List",
"href" => site_url("user/show_all"),
"class" => "btn btn-sm btn-secondary show-user-list",
];
}

$user_buttons["preferences"] = [
"item" => "preference",
"text" => "Preferences",
"title" => "Change Your Settings",
"href" => site_url("preference/view/" . $this->session->userdata("userID")),
"class" => "btn btn-sm btn-secondary",
];
}

$user_buttons["log_out"] = [
"item" => "user",
Expand Down
13 changes: 0 additions & 13 deletions js/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,6 @@
});


$(document).on("click", ".menu_item_edit", function () {
myId = this.id.split("_")[1];
myUrl = base_url + "index.php/menu/edit_item/" + myId;
$.ajax({
type: "GET",
url: myUrl,
success: function (data) {
showPopup("Edit Menu Item", data, "auto");
}
});
});


$(document).on("click", ".menu_item_add", function () {
myUrl = base_url + "index.php/menu/create_item/";
$.ajax({
Expand Down

0 comments on commit 573c1d2

Please sign in to comment.