]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/API/Class/VolumeManager.php
baculum: Fix SQL grouping error in restore wizard reported by Rasmus Linden
[bacula/bacula] / gui / baculum / protected / API / Class / VolumeManager.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 Kern Sibbald
7  *
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.
11  *
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.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('Application.API.Class.APIModule');
24 Prado::using('Application.API.Class.VolumeRecord');
25 Prado::using('Application.API.Class.Database');
26
27 class VolumeManager extends APIModule {
28
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);
37                 }
38                 $criteria->OrdersBy[$orderPool] = 'asc';
39                 $criteria->OrdersBy[$orderVolume] = 'asc';
40                 if(is_int($limit) && $limit > 0) {
41                         $criteria->Limit = $limit;
42                 }
43                 $volumes = VolumeRecord::finder()->findAll($criteria);
44                 $this->setExtraVariables($volumes, $with_pools);
45                 return $volumes;
46         }
47
48         public function getVolumesByPoolId($poolid) {
49                 return VolumeRecord::finder()->findBypoolid($poolid);
50         }
51
52         public function getVolumeByName($volumeName) {
53                 return VolumeRecord::finder()->findByvolumename($volumeName);
54         }
55
56         public function getVolumeById($volumeId) {
57                 return VolumeRecord::finder()->findBymediaid($volumeId);
58         }
59
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;
69                                         }
70                                 }
71                         }
72                 }
73         }
74
75         /**
76          * Get volumes for specific jobid and fileid.
77          *
78          * @param integer $jobid job identifier
79          * @param integer $fileid file identifier
80          * @return array volumes list
81          */
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
89                 ) AS gv, File
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();
96                 $pdo = null;
97                 $volumes = array();
98                 if (is_array($ret)) {
99                         for ($i = 0; $i < count($ret); $i++) {
100                                 $volumes[] = array(
101                                         'first_index' => $ret[$i]['first_index'],
102                                         'last_index' => $ret[$i]['last_index'],
103                                         'volume' => $ret[$i]['volname'],
104                                         'inchanger' => $ret[$i]['inchanger']
105                                 );
106                         }
107                 }
108                 return $volumes;
109         }
110 }
111 ?>