Skip to content

Commit

Permalink
Setup, update and check the database from the admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Trystan committed Oct 24, 2012
1 parent 67dc535 commit 3b1c56e
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Modules/auto
Modules/energyform
Modules/energydata
Modules/energygroup
Modules/raspberrypi
47 changes: 23 additions & 24 deletions Modules/admin/admin_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

function admin_controller()
{
require "Models/feed_model.php";
global $session, $action,$format;
//require "Modules/feed/feed_model.php";
global $session, $route;

$format = $route['format'];
$action = $route['action'];

$output['content'] = "";
$output['message'] = "";
Expand All @@ -31,31 +34,27 @@ function admin_controller()
// http://yoursite/emoncms/admin/users
//---------------------------------------------------------------------------------------------------------

if ($action == '' && $session['write'] && $session['admin'])
if ($action == 'view' && $session['write'] && $session['admin'])
{
$output['content'] = view("admin/admin_main_view.php", array());
}

if ($action == 'users' && $session['write'] && $session['admin'])
{
$userlist = get_user_list();
$total_memuse = 0;
for ($i=0;$i<count($userlist);$i++) {
$user = $userlist[$i];
$stats = get_statistics($user['userid']);
$user['uphits'] = $stats['uphits'];
$user['dnhits'] = $stats['dnhits'];
$user['memuse'] = $stats['memory'];
$total_memuse += $user['memuse'];
$userlist[$i] = $user;
}

usort($userlist, 'user_sort'); // sort by highest memory user first

$output['content'] = view("admin/admin_view.php", array('userlist'=>$userlist,'total_memuse'=>$total_memuse));
//$userlist = get_user_list();
//$output['content'] = view("admin/admin_view.php", array('userlist'=>$userlist));
}

if ($action == 'db' && $session['write'] && $session['admin'])
{
$out = db_schema_setup(load_db_schema());
$output['content'] = view("admin/admin_db_view.php", array('out'=>$out));
}



return $output;
}


function user_sort($x, $y)
{
return $y['memuse'] - $x['memuse'];
}

?>
?>
23 changes: 23 additions & 0 deletions Modules/admin/admin_db_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h2>Database setup, update and status check</h2>
<p>This page display's the output of the database setup and update process which checks the database requirements of each module installed and enter's any new table's or fields if required.</p>
<p>If all the item statuses below show ok that means your database is setup correctly.</p>
<br>

<table class="catlist" >
<tr><th>Schema item</th><th>Name</th><th>Status</th></tr>
<?php $i=0; foreach ($out as $line) { $i++; ?>

<?php if ($line[0]=='Table') { ?>

<tr class="d<?php echo ($i & 1); ?>" ><th><?php echo $line[0]; ?></th><th><?php echo $line[1]; ?></th><th><?php echo $line[2]; ?></th></tr>

<?php } ?>

<?php if ($line[0]=='field') { ?>

<tr class="d<?php echo ($i & 1); ?>" ><td><i><?php echo $line[0]; ?></i></td><td><i><?php echo $line[1]; ?></i></td><td><i><?php echo $line[2]; ?></i></td></tr>

<?php } ?>

<?php } ?>
</table>
16 changes: 16 additions & 0 deletions Modules/admin/admin_main_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php global $path; ?>
<h2>Admin</h2>

<table class="table table-striped ">
<tr>
<td>
<h3>Update database</h3>
<p>Run this after updating emoncms, after installing a new module or to check emoncms database status.</p>
</td>
<td>
<br>
<a href="<?php echo $path; ?>admin/db" class="btn btn-info"><?php echo _('Update & check'); ?></a>
</td>
</tr>
</table>

1 change: 1 addition & 0 deletions Theme/basic/menu_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ function custom_sort($a,$b) {
if (isset($session[$item['session']]) && $session[$item['session']]==1)
echo "<li><a href=".$path.$item['path']." >"._($item['name'])."</a></li>";
}

?>
</ul>
1 change: 1 addition & 0 deletions core.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function get_application_path()
function emon_session_start()
{
session_start();
if (isset($_SESSION['admin'])) $session['admin'] = $_SESSION['admin']; else $session['admin'] = 0;
if (isset($_SESSION['read'])) $session['read'] = $_SESSION['read']; else $session['read'] = 0;
if (isset($_SESSION['write'])) $session['write'] = $_SESSION['write']; else $session['write'] = 0;
if (isset($_SESSION['userid'])) $session['userid'] = $_SESSION['userid']; else $session['userid'] = 0;
Expand Down
12 changes: 6 additions & 6 deletions db.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function db_schema_setup($schema)
{
if (table_exists($table))
{
$out[] = array("TABLE ".$table,"ok");
$out[] = array('Table',$table,"ok");
//-----------------------------------------------------
// Check table fields from schema
//-----------------------------------------------------
Expand All @@ -139,12 +139,12 @@ function db_schema_setup($schema)

if (field_exists($table, $field))
{
$out[] = array($field,"ok");
$out[] = array('field',$field,"ok");
}
else
{
$query = "ALTER TABLE `$table` ADD `$field` $type";
$out[] = array($field,"added");
$out[] = array('field',$field,"added");
db_query($query);
}

Expand All @@ -158,7 +158,7 @@ function db_schema_setup($schema)
if ($array['Extra']!=$extra && $extra=="auto_increment") { $out .= "Extra: $extra"; $query .= " auto_increment"; }
if ($array['Key']!=$key && $key=="PRI") { $out .= "Key: $key, "; $query .= " primary key"; }

if ($query) $query = "ALTER TABLE $table MODIFY $field $type".$query;
if ($query) $query = "ALTER TABLE $table MODIFY `$field` $type".$query;
if ($query) db_query($query);

next($schema[$table]);
Expand All @@ -179,7 +179,7 @@ function db_schema_setup($schema)
if (isset($schema[$table][$field]['default'])) $default = $schema[$table][$field]['default']; else $default = null;
if (isset($schema[$table][$field]['Extra'])) $extra = $schema[$table][$field]['Extra']; else $extra = null;

$query .= $field;
$query .= '`'.$field.'`';
$query .= " $type";
if ($default) $query .= " Default '$default'";
if ($null=="NO") $query .= " not null";
Expand All @@ -193,7 +193,7 @@ function db_schema_setup($schema)
}
}
$query .= ")";
$out[] = array("TABLE ".$table,"created");
$out[] = array('Table',$table,"created");

if ($query) db_query($query);
}
Expand Down
2 changes: 0 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
case 4: db_schema_setup(load_db_schema()); break;
}

//db_schema_setup(load_db_schema());

// Session control
require("Modules/user/user_model.php");
if (get('apikey'))
Expand Down

0 comments on commit 3b1c56e

Please sign in to comment.