Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Media: Native director connectivity
Browse files Browse the repository at this point in the history
Replaces database connection with native director connection.
  • Loading branch information
fbergkemper committed Aug 31, 2015
1 parent 8283ca3 commit e413a89
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 392 deletions.
2 changes: 1 addition & 1 deletion module/Media/config/module.config.php
Expand Up @@ -43,7 +43,7 @@
'route' => '/media[/][:action][/:id][order_by/:order_by][/:order][/][limit/:limit]',
'constraints' => array(
'action' => '(?!\blimit\b)(?!\border_by\b)[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
'order_by' => '[a-zA-Z][a-zA-Z0-9_-]*',
'order' => 'ASC|DESC',
'limit' => '[0-9]+',
Expand Down
50 changes: 29 additions & 21 deletions module/Media/src/Media/Controller/MediaController.php
Expand Up @@ -27,30 +27,31 @@

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;

class MediaController extends AbstractActionController
{

protected $mediaTable;

public function indexAction()
{
if($_SESSION['bareos']['authenticated'] == true && $this->SessionTimeoutPlugin()->timeout()) {
$order_by = $this->params()->fromRoute('order_by') ? $this->params()->fromRoute('order_by') : 'MediaId';
$order = $this->params()->fromRoute('order') ? $this->params()->fromRoute('order') : 'DESC';

$volumes = $this->getVolumes();
$page = (int) $this->params()->fromQuery('page');
$limit = $this->params()->fromRoute('limit') ? $this->params()->fromRoute('limit') : '25';
$paginator = $this->getMediaTable()->fetchAll(true, $order_by, $order);
$paginator->setCurrentPageNumber( (int) $this->params()->fromQuery('page', 1) );

$paginator = new Paginator(new ArrayAdapter($volumes));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($limit);

return new ViewModel(
array(
'paginator' => $paginator,
'order_by' => $order_by,
'order' => $order,
'limit' => $limit,
'limit' => $limit,
)
);

}
else {
return $this->redirect()->toRoute('auth', array('action' => 'login'));
Expand All @@ -60,27 +61,34 @@ public function indexAction()
public function detailsAction()
{
if($_SESSION['bareos']['authenticated'] == true && $this->SessionTimeoutPlugin()->timeout()) {
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('media');
}

$volumename = $this->params()->fromRoute('id');
$volume = $this->getVolume($volumename);

return new ViewModel(array(
'media' => $this->getMediaTable()->getMedia($id),
'media' => $volume,
));

}
else {
return $this->redirect()->toRoute('auth', array('action' => 'login'));
return $this->redirect()->toRoute('auth', array('action' => 'login'));
}
}

public function getMediaTable()
private function getVolumes()
{
if(!$this->mediaTable) {
$sm = $this->getServiceLocator();
$this->mediaTable = $sm->get('Media\Model\MediaTable');
}
return $this->mediaTable;
$director = $this->getServiceLocator()->get('director');
$result = $director->send_command("llist volumes all", 2, null);
$pools = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
return $pools['result']['volumes'];
}

private function getVolume($volume)
{
$director = $this->getServiceLocator()->get('director');
$result = $director->send_command('llist volume="'.$volume.'"', 2, null);
$pools = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
return $pools['result']['volume'];
}

}
Expand Down
98 changes: 3 additions & 95 deletions module/Media/src/Media/Model/Media.php
Expand Up @@ -3,9 +3,9 @@
/**
*
* bareos-webui - Bareos Web-Frontend
*
*
* @link https://github.com/bareos/bareos-webui for the canonical source repository
* @copyright Copyright (c) 2013-2014 Bareos GmbH & Co. KG (http://www.bareos.org/)
* @copyright Copyright (c) 2013-2015 Bareos GmbH & Co. KG (http://www.bareos.org/)
* @license GNU Affero General Public License (http://www.gnu.org/licenses/)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -25,99 +25,7 @@

namespace Media\Model;

class Media
class Media
{

public $mediaid;
public $volumename;
public $slot;
public $poolid;
public $mediatype;
public $mediatypeid;
public $labeltype;
public $firstwritten;
public $lastwritten;
public $labeldate;
public $voljobs;
public $volfiles;
public $volblocks;
public $volmounts;
public $volbytes;
public $volerrors;
public $volwrites;
public $volcapacitybytes;
public $volstatus;
public $enabled;
public $recycle;
public $actiononpurge;
public $volretention;
public $voluseduration;
public $maxvoljobs;
public $maxvolfiles;
public $maxvolbytes;
public $inchanger;
public $storageid;
public $deviceid;
public $mediaaddressing;
public $volreadtime;
public $volwritetime;
public $endfile;
public $endblock;
public $locationid;
public $recyclecount;
public $initialwrite;
public $scratchpoolid;
public $recyclepoolid;
public $encryptionkey;
public $comment;

public function exchangeArray($data)
{
$data = array_change_key_case($data, CASE_LOWER);

$this->mediaid = (!empty($data['mediaid'])) ? $data['mediaid'] : null;
$this->volumename = (!empty($data['volumename'])) ? $data['volumename'] : null;
$this->slot = (!empty($data['slot'])) ? $data['slot'] : null;
$this->poolid = (!empty($data['poolid'])) ? $data['poolid'] : null;
$this->mediatype = (!empty($data['mediatype'])) ? $data['mediatype'] : null;
$this->mediatypeid = (!empty($data['mediatypeid'])) ? $data['mediatypeid'] : null;
$this->labeltype = (!empty($data['labeltype'])) ? $data['labeltype'] : null;
$this->firstwritten = (!empty($data['firstwritten'])) ? $data['firstwritten'] : null;
$this->lastwritten = (!empty($data['lastwritten'])) ? $data['lastwritten'] : null;
$this->labeldate = (!empty($data['labeldate'])) ? $data['labeldate'] : null;
$this->voljobs = (!empty($data['voljobs'])) ? $data['voljobs'] : null;
$this->volfiles = (!empty($data['volfiles'])) ? $data['volfiles'] : null;
$this->volblocks = (!empty($data['volblocks'])) ? $data['volblocks'] : null;
$this->volmounts = (!empty($data['volmounts'])) ? $data['volmounts'] : null;
$this->volbytes = (!empty($data['volbytes'])) ? $data['volbytes'] : null;
$this->volerrors = (!empty($data['volerrors'])) ? $data['volerrors'] : null;
$this->volwrites = (!empty($data['volwrites'])) ? $data['volwrites'] : null;
$this->volcapacitybytes = (!empty($data['volcapacitybytes'])) ? $data['volcapacitybytes'] : null;
$this->volstatus = (!empty($data['volstatus'])) ? $data['volstatus'] : null;
$this->enabled = (!empty($data['enabled'])) ? $data['enabled'] : null;
$this->recycle = (!empty($data['recycle'])) ? $data['recycle'] : null;
$this->actiononpurge = (!empty($data['actiononpurge'])) ? $data['actiononpurge'] : null;
$this->volretention = (!empty($data['volretention'])) ? $data['volretention'] : null;
$this->voluseduration = (!empty($data['voluseduration'])) ? $data['voluseduration'] : null;
$this->maxvoljobs = (!empty($data['maxvoljobs'])) ? $data['maxvoljobs'] : null;
$this->maxvolfiles = (!empty($data['maxvolfiles'])) ? $data['maxvolfiles'] : null;
$this->maxvolbytes = (!empty($data['maxvolbytes'])) ? $data['maxvolbytes'] : null;
$this->inchanger = (!empty($data['inchanger'])) ? $data['inchanger'] : null;
$this->storageid = (!empty($data['storageid'])) ? $data['storageid'] : null;
$this->deviceid = (!empty($data['deviceid'])) ? $data['deviceid'] : null;
$this->mediaaddressing = (!empty($data['mediaaddressing'])) ? $data['mediaaddressing'] : null;
$this->volreadtime = (!empty($data['volreadtime'])) ? $data['volreadtime'] : null;
$this->volwritetime = (!empty($data['volwritetime'])) ? $data['volwritetime'] : null;
$this->endfile = (!empty($data['endfile'])) ? $data['endfile'] : null;
$this->endblock = (!empty($data['endblock'])) ? $data['endblock'] : null;
$this->locationid = (!empty($data['locationid'])) ? $data['locationid'] : null;
$this->recyclecount = (!empty($data['recyclecount'])) ? $data['recyclecount'] : null;
$this->initialwrite = (!empty($data['initialwrite'])) ? $data['initialwrite'] : null;
$this->scratchpoolid = (!empty($data['scratchpoolid'])) ? $data['scratchpoolid'] : null;
$this->recyclepoolid = (!empty($data['recyclepoolid'])) ? $data['recyclepoolid'] : null;
$this->encryptionkey = (!empty($data['encryptionkey'])) ? $data['encryptionkey'] : null;
$this->comment = (!empty($data['comment'])) ? $data['comment'] : null;
}

}

114 changes: 2 additions & 112 deletions module/Media/src/Media/Model/MediaTable.php
Expand Up @@ -5,7 +5,7 @@
* bareos-webui - Bareos Web-Frontend
*
* @link https://github.com/bareos/bareos-webui for the canonical source repository
* @copyright Copyright (c) 2013-2014 Bareos GmbH & Co. KG (http://www.bareos.org/)
* @copyright Copyright (c) 2013-2015 Bareos GmbH & Co. KG (http://www.bareos.org/)
* @license GNU Affero General Public License (http://www.gnu.org/licenses/)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -25,117 +25,7 @@

namespace Media\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Bareos\Db\Sql\BareosSqlCompatHelper;

class MediaTable implements ServiceLocatorAwareInterface
class MediaTable
{

protected $tableGateway;
protected $serviceLocator;

public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}

public function getServiceLocator() {
return $this->serviceLocator;
}

public function getDbDriverConfig() {
$config = $this->getServiceLocator()->get('Config');
return $config['db']['adapters'][$_SESSION['bareos']['director']]['driver'];
}

public function fetchAll($paginated=false, $order_by=null, $order=null)
{
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select($bsqlch->strdbcompat("Media"));

if($order_by != null && $order != null) {
$select->order($bsqlch->strdbcompat($order_by) . " " . $order);
}
else {
$select->order($bsqlch->strdbcompat("MediaId") . " ASC");
}

if($paginated) {
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Media());
$paginatorAdapter = new DbSelect(
$select,
$this->tableGateway->getAdapter(),
$resultSetPrototype
);
$paginator = new Paginator($paginatorAdapter);
return $paginator;
}
else {
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
}

public function getMedia($id)
{
$mediaid = (int) $id;

$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from($bsqlch->strdbcompat("Media"));
$select->where($bsqlch->strdbcompat("MediaId") . " = " . $mediaid);

$resultSet = $this->tableGateway->selectWith($select);
$row = $resultSet->current();

return $row;
}

public function getMediaNum()
{
$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from($bsqlch->strdbcompat("Media"));

$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Media());
$rowset = new DbSelect(
$select,
$this->tableGateway->getAdapter(),
$resultSetPrototype
);
$num = $rowset->count();

return $num;
}

public function getPoolVolumes($id)
{
$poolid = (int) $id;

$bsqlch = new BareosSqlCompatHelper($this->getDbDriverConfig());
$select = new Select();
$select->from($bsqlch->strdbcompat("Media"));
$select->where($bsqlch->strdbcompat("PoolId") . " = " . $poolid);
$select->order($bsqlch->strdbcompat("MediaId") . " ASC");

$resultSet = $this->tableGateway->selectWith($select);

return $resultSet;

}

}

0 comments on commit e413a89

Please sign in to comment.