Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Revision creation button + Multi Database support #46

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion DBV.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class DBV_Exception extends Exception

class DBV
{

protected $_action = "index";
protected $_adapter;
protected $_log = array();
Expand Down Expand Up @@ -133,6 +132,28 @@ public function schemaAction()
$this->_json($return);
}
}

public function switchDatabaseAction()
{
$databaseID = isset($_GET['newdb']) ? $_GET['newdb'] : false;
if ($databaseID !== false)
{
$lines = @file(DBV_ROOT_PATH . DS . "config_list.php");
if ($lines)
{
$needlesize = @strlen('$current_id');
foreach ($lines as &$line)
if (!@strncmp($line, '$current_id', $needlesize))
{
$line = '$current_id = ' . $databaseID . ";" . PHP_EOL;
break ;
}
@file_put_contents(DBV_ROOT_PATH . DS . "config_list.php", @implode("", $lines));
header('Location: index.php');
}
}
$this->indexAction();
}

public function revisionsAction()
{
Expand Down Expand Up @@ -176,6 +197,24 @@ public function revisionsAction()
}
}

public function addRevisionFolderAction()
{
$revision = $this->_getCurrentRevision();
while (++$revision)
{
$dir = DBV_REVISIONS_PATH . DS . $revision;
if (!@file_exists($dir)) {
if (!@mkdir($dir))
$this->_json(array('ok' => false, 'message' => __("Cannot create folder for revision #{revision}!", array('revision' => "<strong>$revision</strong>"))));
$dir .= '/comments.sql';
if (!@file_put_contents($dir, ' '))
$this->_json(array('ok' => false, 'message' => __("Cannot create sql file for revision #{revision}!", array('revision' => "<strong>$revision</strong>"))));
break ;
}
}

$this->_json(array('ok' => true, 'rev' => $revision, 'message' => __("Revision <strong>#{revision}</strong> successfully added!", array('revision' => "$revision"))));
}

public function saveRevisionFileAction()
{
Expand Down Expand Up @@ -274,6 +313,8 @@ protected function _getAction()

protected function _view($view)
{
global $conf_list;
global $current_id;
$file = DBV_ROOT_PATH . DS . 'templates' . DS . "$view.php";
if (file_exists($file)) {
include($file);
Expand Down
2 changes: 1 addition & 1 deletion config.php.sample → config/config.php.sample
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ define('DBV_PASSWORD', 'dbv');
define('DB_ADAPTER', 'MySQL');

define('DS', DIRECTORY_SEPARATOR);
define('DBV_ROOT_PATH', dirname(__FILE__));
define('DBV_ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR . "..");

/**
* Only edit this lines if you want to place your schema files in custom locations
Expand Down
16 changes: 16 additions & 0 deletions config_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$conf_list = array();

if ($handle = opendir('config')) {
while (false !== ($entry = readdir($handle))) {
if (array_pop(explode('.', $entry)) == 'php')
array_push($conf_list, $entry);
}
closedir($handle);
}

$current_id = 2;
if (!isset($conf_list[$current_id]))
$current_id = 0;
$current = $conf_list[$current_id];
6 changes: 5 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config_list.php';
if ($current == '')
throw new Exception('Make sure $current in "' . dirname(__FILE__) . DIRECTORY_SEPARATOR
. 'config_list.php" is set to the path of your configuration file');
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "config/" . $current;
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib/functions.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'DBV.php';

Expand Down
6 changes: 6 additions & 0 deletions public/scripts/bootstrap.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions public/scripts/jquery.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions templates/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,32 @@
<script type="text/javascript" src="public/scripts/codemirror/mode/php.js"></script>

<script type="text/javascript" src="public/scripts/dbv.js"></script>

<script type="text/javascript" src="public/scripts/jquery.js"></script>
<script type="text/javascript" src="public/scripts/bootstrap.js"></script>
<script>
//Use $j instead of $ when you want jQuery stuff. Avoid conflict with prototype.js
$j = jQuery.noConflict();
</script>
</head>
<body>
<div class="navbar navbar-static-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a href="index.php" class="brand">dbv<span>.php</span></a>
<ul class="nav pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Select database
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<?php
foreach ($conf_list as $i => $conf)
echo '<li '. ($i == $current_id ? 'class="disabled"' : "") .'><a href="index.php?a=switchDatabase&newdb='.$i.'">'.preg_replace("/\.[^.]*$/", "", $conf) .'</a></li>';
?>
</ul>
</li>
<li><a href="http://dbv.vizuina.com"><?php echo __('Check for updates'); ?></a></li>
</ul>
</div>
Expand Down
84 changes: 76 additions & 8 deletions templates/revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<th><?php echo __('Revision ID'); ?></th>
</tr>
</thead>
<tbody>
<tbody id="revision_body">
<?php foreach ($this->revisions as $revision) { ?>
<?php
$ran = $this->revision >= $revision;
Expand Down Expand Up @@ -55,7 +55,8 @@
<?php } ?>
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Run selected revisions" />
<button id="add_revision" class="span3 btn btn-success">New revision</button>
<input type="submit" class="span3 btn btn-primary" value="Run selected revisions" />
</form>
<?php } else { ?>
<div class="alert alert-info nomargin">
Expand All @@ -69,8 +70,7 @@
return;
}

var textareas = form.select('textarea');
textareas.each(function (textarea) {
function init_textarea(textarea) {
textarea['data-editor'] = CodeMirror.fromTextArea(textarea, {
mode: "text/x-mysql",
tabMode: "indent",
Expand All @@ -79,9 +79,13 @@
lineNumbers: true,
theme: 'default'
});
});
}

$$('.revision-handle').invoke('observe', 'click', function (event) {
var textareas = form.select('textarea');
textareas.each(init_textarea);

function revision_handle(event) {

var element = event.findElement('.revision-handle');
var container = element.up('td').down('.revision-files');
if (container) {
Expand All @@ -97,9 +101,10 @@
});
}
}
});
}
$$('.revision-handle').invoke('observe', 'click', revision_handle);

$$('button[data-role="editor-save"]').invoke('observe', 'click', function (event) {
function editor_save(event) {
var self = this;

var editor = this.up('.heading').next('textarea')['data-editor'];
Expand Down Expand Up @@ -127,6 +132,69 @@
render_messages('success', container, response.message);
}
});
}

$$('button[data-role="editor-save"]').invoke('observe', 'click', editor_save);

$$("#add_revision").invoke('observe', 'click', function(event) {
event.stop();


var self = this;

this.disable();

clear_messages('revisions');

new Ajax.Request('index.php?a=addRevisionFolder', {
parameters: {

},
onSuccess: function (transport) {

self.enable();

var response = transport.responseText.evalJSON();

if (response.ok != true) {
render_messages('error', 'revisions', response.message, '<?php echo __('The following errors occured:'); ?>');
}
else {
render_messages('success', 'revisions', response.message, '<?php echo __('The following actions completed successfuly:'); ?>');

var rev = parseInt(response.rev);
if (!isNaN(rev)) {
var tbody = document.getElementById('revision_body');
var tr = tbody.insertRow(0);

tr.setAttribute('data-revision', rev);

var td = document.createElement('td');
td.className = "center";
td.innerHTML = '<input type="checkbox" name="revisions[]" value="'+rev+'" style="margin-top: 7px" />';
var td2 = document.createElement('td');
td2.innerHTML = '<h3 class="nomargin"><a href="javascript:" class="revision-handle">'+rev+'</a></h3>'
+'<div class="revision-files" style="display: none;"><div id="revision-file-'+rev+'-1">'
+'<div class="log"></div>'
+'<div class="alert alert-info heading">'
+'<button data-role="editor-save" data-revision="'+rev+'" data-file="comments.sql" type="button" class="btn btn-mini btn-info pull-right" style="margin-top: 1px;">Save file</button>'
+'<strong class="alert-heading">comments.sql</strong>'
+'</div>'
+'<textarea data-role="editor" name="revision_files['+rev+'][comments.sql]" rows="1" style="display:none;"> </textarea>'
+'</div></div>';
tr.appendChild(td);
tr.appendChild(td2);
$$('.revision-handle').invoke('observe', 'click', revision_handle);
$$('button[data-role="editor-save"]').invoke('observe', 'click', editor_save);
textareas = form.select('textarea');
init_textarea(textareas[0]);
}
}


Effect.ScrollTo('log', {duration: 0.2});
}
})
});

form.on('submit', function (event) {
Expand Down