Skip to content

Commit

Permalink
manage saved jobs (add, edit, kick from templates)
Browse files Browse the repository at this point in the history
  • Loading branch information
pentium10 committed Feb 16, 2014
1 parent 9bc5a4b commit 1ea2759
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 32 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
- You can add/kick/delete jobs in every tube
- You can select multiple tubes by regExp and clear them
- Ability to Pause tubes
- Saved jobs (store sample jobs as a template, kick/edit them, very useful for development)
- Customizable UI (code highlighter, choose columns, edit auto refresh seconds, pause tube seconds)

Change log on [Releases](https://github.com/ptrofimov/beanstalk_console/releases).

**Installation**

### Use composer (*recommended*)
Expand Down
138 changes: 134 additions & 4 deletions lib/include.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function __autoload($class) {
require_once str_replace('_', '/', $class) . '.php';
}

session_start();
require_once 'BeanstalkInterface.class.php';
require_once dirname(__FILE__) . '/../config.php';
require_once dirname(__FILE__) . '/../src/Storage.php';
Expand Down Expand Up @@ -253,7 +254,7 @@ protected function _main() {

if (!isset($_GET['server'])) {
// execute methods without a server
if (isset($_GET['action']) && in_array($_GET['action'], array('serversRemove'))) {
if (isset($_GET['action']) && in_array($_GET['action'], array('serversRemove', 'manageSamples', 'deleteSample', 'editSample', 'newSample'))) {
$funcName = "_action" . ucfirst($this->_globalVar['action']);
if (method_exists($this, $funcName)) {
$this->$funcName();
Expand Down Expand Up @@ -446,7 +447,132 @@ protected function _actionLoadSample() {
$this->interface->addJob($this->_globalVar['tube'], $job['data']);
}
}
header(sprintf('Location: index.php?server=%s&tube=%s', $this->_globalVar['server'], $this->_globalVar['tube']));
if (isset($_GET['redirect'])) {
$_SESSION['info'] = 'Job placed on tube';
header(sprintf('Location: %s', $_GET['redirect']));
} else {
header(sprintf('Location: index.php?server=%s&tube=%s', $this->_globalVar['server'], $this->_globalVar['tube']));
}
exit();
}

protected function _actionManageSamples() {
$this->_tplVars['_tplMain'] = 'main';
$this->_tplVars['_tplPage'] = 'sampleJobsManage';
}

protected function _actionEditSample() {
$this->_tplVars['_tplMain'] = 'main';
$this->_tplVars['_tplPage'] = 'sampleJobsEdit';
$key = $_GET['key'];
if (!empty($key)) {
$storage = new Storage($this->_globalVar['config']['storage']);
$job = $storage->load($key);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['jobdata']) && isset($_POST['name']) && isset($_POST['tubes'])) {
$oldjob = $job;
$storage->delete($key);
$job['name'] = $_POST['name'];
$job['tubes'] = $_POST['tubes'];
$job['data'] = htmlspecialchars_decode($_POST['jobdata']);
if ($storage->saveJob($job)) {
header('Location: index.php?action=manageSamples');
} else {
$storage->saveJob($oldjob);
$this->_tplVars['error'] = $storage->getError();
}
} else {
$job['name'] = @$_POST['name'];
$job['data'] = @$_POST['jobdata'];
$job['tubes'] = @$_POST['tubes'];
$this->_tplVars['error'] = 'Required fields are not set';
}
}
if ($job) {
$this->_tplVars['job'] = $job;
} else {
$this->_errors[] = 'Cannot locate job';
return;
}
} else {
$this->_errors[] = 'The requested key is invalid';
return;
}
$serverTubes = array();
if (is_array($this->servers)) {
foreach ($this->servers as $server) {
try {
$interface = new BeanstalkInterface($server);
$tubes = $interface->getTubes();
if (is_array($tubes)) {
$serverTubes[$server] = $tubes;
}
} catch (Exception $e) {

}
}
}
if (empty($serverTubes)) {
$this->_errors[] = 'No tubes were found, please connect a server.';
return;
}
$this->_tplVars['serverTubes'] = $serverTubes;
}

protected function _actionNewSample() {
$this->_tplVars['_tplMain'] = 'main';
$this->_tplVars['_tplPage'] = 'sampleJobsEdit';
$this->_tplVars['isNewRecord'] = true;
$storage = new Storage($this->_globalVar['config']['storage']);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['jobdata']) && isset($_POST['name']) && isset($_POST['tubes'])) {
$job['name'] = $_POST['name'];
$job['tubes'] = $_POST['tubes'];
$job['data'] = htmlspecialchars_decode($_POST['jobdata']);
if ($storage->saveJob($job)) {
header('Location: index.php?action=manageSamples');
} else {
$this->_tplVars['error'] = $storage->getError();
}
} else {
$job['name'] = @$_POST['name'];
$job['data'] = @$_POST['jobdata'];
$job['tubes'] = @$_POST['tubes'];
$this->_tplVars['error'] = 'Required fields are not set';
}
}

$serverTubes = array();
if (is_array($this->servers)) {
foreach ($this->servers as $server) {
try {
$interface = new BeanstalkInterface($server);
$tubes = $interface->getTubes();
if (is_array($tubes)) {
$serverTubes[$server] = $tubes;
}
} catch (Exception $e) {

}
}
}
if (empty($serverTubes)) {
$this->_errors[] = 'No tubes were found, please connect a server.';
return;
}
$this->_tplVars['serverTubes'] = $serverTubes;
}

protected function _actionDeleteSample() {
$key = $_GET['key'];
if (!empty($key)) {
$storage = new Storage($this->_globalVar['config']['storage']);
$job = $storage->load($key);
if ($job) {
$storage->delete($key);
}
}
header('Location: index.php?action=manageSamples');
exit();
}

Expand All @@ -463,9 +589,13 @@ private function _storeSampleJob($post, $jobData) {
}
}

public function getSampleJobs($tube) {
public function getSampleJobs($tube = null) {
$storage = new Storage($this->_globalVar['config']['storage']);
return $storage->getJobsForTube($tube);
if ($tube) {
return $storage->getJobsForTube($tube);
} else {
return $storage->getJobs();
}
}

}
6 changes: 5 additions & 1 deletion lib/tpl/currentTube.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@
<a href="?server=<?php echo $server ?>&tube=<?php echo $tube ?>&action=loadSample&key=<?php echo $key; ?>"><?php echo htmlspecialchars($name); ?></a>
</li>
<?php }
?>
<li class="divider"></li>
<li><a href="?action=manageSamples">Manage samples</a></li>
<?php
} else {
?>
<li>
There are no sample jobs
<a href="#">There are no sample jobs</a>
</li>
<?php } ?>
</ul>
Expand Down
40 changes: 15 additions & 25 deletions lib/tpl/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,38 +87,24 @@
</ul>
</li>
<?php endif ?>
<!--<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something <li><a href="#">Something <li><a href="#">Something <li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li class="nav-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>-->
</ul>
<!--<form class="navbar-search pull-left" action="">
<input type="text" class="search-query span2" placeholder="Search">
</form>-->
<ul class="nav pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Toolbox <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#filter" role="button" data-toggle="modal">Filter columns</a></li>
<?php if ($server && !$tube) { ?>
<?php if (!isset($_tplPage)) { ?>
<li><a href="#filter" role="button" data-toggle="modal">Filter columns</a></li>
<?php
}
if ($server && !$tube) {
?>
<li><a href="#clear-tubes" role="button" data-toggle="modal">Clear multiple tubes</a></li>
<?php } ?>
<li><a href="index.php?action=manageSamples" role="button">Manage samples</a></li>
<li class="divider"></li>
<li><a href="#settings" role="button" data-toggle="modal">Edit settings</a></li>
</ul>
</li>


<!--<li><a href="#">Link</a></li>
<li class="divider-vertical"></li>-->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Links <b class="caret"></b></a>
<ul class="dropdown-menu">
Expand All @@ -142,7 +128,9 @@
<p class="alert alert-error"><span class="label label-important">Error</span> <?php echo $item ?></p>
<?php endforeach; ?>
<?php else: ?>
<?php if (!$server): ?>
<?php if (isset($_tplPage)): ?>
<?php include(dirname(__FILE__) . '/' . $_tplPage . '.php') ?>
<?php elseif (!$server): ?>
<?php include(dirname(__FILE__) . '/serversList.php') ?>
<?php elseif (!$tube): ?>
<div id="idAllTubes">
Expand All @@ -155,10 +143,12 @@
<br><br><a href="./?server=<?php echo $server ?>"> << back </a>
<?php else: ?>
<?php require_once '../lib/tpl/currentTube.php'; ?>
<?php require_once '../lib/tpl/modalAddJob.php'; ?>
<?php require_once '../lib/tpl/modalAddSample.php'; ?>
<?php require_once '../lib/tpl/modalAddJob.php'; ?>
<?php require_once '../lib/tpl/modalAddSample.php'; ?>
<?php endif; ?>
<?php require_once '../lib/tpl/modalFilterColumns.php'; ?>
<?php if (!isset($_tplPage)) { ?>
<?php require_once '../lib/tpl/modalFilterColumns.php'; ?>
<?php } ?>
<?php require_once '../lib/tpl/modalSettings.php'; ?>
<?php endif; ?>
</div>
Expand Down
4 changes: 2 additions & 2 deletions lib/tpl/modalAddSample.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
</div>
<input type="hidden" name="addsamplestate" id="addsamplestate">
<div class="control-group">
<label class="control-label" for="addsamplename" title="You can highlight text inside the job, then hit the Add button, it will be automatically populated here."><b>Name*</b> <i>(highlighted text is auto populated)</i></label>
<label class="control-label" for="addsamplename" title="You can highlight text inside the job, then hit the Add button, it will be automatically populated here."><b>Name *</b> <i>(highlighted text is auto populated)</i></label>
<div class="controls">
<input class="input-xlarge focused" id="addsamplename" name="addsamplename" type="text" value="" autocomplete="off">
</div>
</div>
</fieldset>
<div>
<label class="control-label" for="focusedInput"><b>Available on tubes*</label>
<label class="control-label" for="focusedInput"><b>Available on tubes *</label>
<?php
foreach ($tubes as $t):
$checked = '';
Expand Down
Loading

0 comments on commit 1ea2759

Please sign in to comment.