3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2017 Kern Sibbald
8 * The main author of Baculum is Marcin Haba.
9 * The original author of Bacula is Kern Sibbald, with contributions
10 * from many others, a complete list can be found in the file AUTHORS.
12 * You may use this file and others of this release according to the
13 * license defined in the LICENSE file, which includes the Affero General
14 * Public License, v3.0 ("AGPLv3") and some additional permissions and
15 * terms pursuant to its AGPLv3 Section 7.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
23 Prado::using('Application.API.Class.APIModule');
24 Prado::using('Application.API.Class.VolumeRecord');
25 Prado::using('Application.API.Class.Database');
27 class VolumeManager extends APIModule {
29 public function getVolumes($limit, $with_pools = false) {
30 $criteria = new TActiveRecordCriteria;
31 $orderPool = 'PoolId';
32 $orderVolume = 'VolumeName';
33 $db_params = $this->getModule('api_config')->getConfig('db');
34 if($db_params['type'] === Database::PGSQL_TYPE) {
35 $orderPool = strtolower($orderPool);
36 $orderVolume = strtolower($orderVolume);
38 $criteria->OrdersBy[$orderPool] = 'asc';
39 $criteria->OrdersBy[$orderVolume] = 'asc';
40 if(is_int($limit) && $limit > 0) {
41 $criteria->Limit = $limit;
43 $volumes = VolumeRecord::finder()->findAll($criteria);
44 $this->setExtraVariables($volumes, $with_pools);
48 public function getVolumesByPoolId($poolid) {
49 return VolumeRecord::finder()->findBypoolid($poolid);
52 public function getVolumeByName($volumeName) {
53 return VolumeRecord::finder()->findByvolumename($volumeName);
56 public function getVolumeById($volumeId) {
57 return VolumeRecord::finder()->findBymediaid($volumeId);
60 private function setExtraVariables(&$volumes, $with_pools) {
61 $pools = $this->Application->getModule('pool')->getPools(false);
62 foreach($volumes as $volume) {
63 $volstatus = strtolower($volume->volstatus);
64 $volume->whenexpire = ($volstatus == 'full' || $volstatus == 'used') ? date( 'Y-m-d H:i:s', (strtotime($volume->lastwritten) + $volume->volretention)) : 'no date';
65 if ($with_pools === true) {
66 foreach($pools as $pool) {
67 if($volume->poolid == $pool->poolid) {
68 $volume->pool = $pool;
76 * Get volumes for specific jobid and fileid.
78 * @param integer $jobid job identifier
79 * @param integer $fileid file identifier
80 * @return array volumes list
82 public function getVolumesForJob($jobid, $fileid) {
83 $connection = VolumeRecord::finder()->getDbConnection();
84 $connection->setActive(true);
85 $sql = sprintf('SELECT first_index, last_index, VolumeName AS volname, InChanger AS inchanger FROM (
86 SELECT VolumeName, InChanger, MIN(FirstIndex) as first_index, MAX(LastIndex) as last_index
87 FROM JobMedia JOIN Media ON (JobMedia.MediaId = Media.MediaId)
88 WHERE JobId = %d GROUP BY VolumeName, InChanger
90 WHERE FileIndex >= first_index
91 AND FileIndex <= last_index
92 AND File.FileId = %d', $jobid, $fileid);
93 $pdo = $connection->getPdoInstance();
94 $result = $pdo->query($sql);
95 $ret = $result->fetchAll();
99 for ($i = 0; $i < count($ret); $i++) {
101 'first_index' => $ret[$i]['first_index'],
102 'last_index' => $ret[$i]['last_index'],
103 'volume' => $ret[$i]['volname'],
104 'inchanger' => $ret[$i]['inchanger']