]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/JobManager.php
f08a27001b6016596f09d6184aa0a9f0a177634b
[bacula/bacula] / gui / baculum / protected / Class / JobManager.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 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 class JobManager extends TModule {
24
25         public function getJobs($limit, $allowedJobs = array()) {
26                 $criteria = new TActiveRecordCriteria;
27                 $order = 'JobId';
28                 $cfg = $this->Application->getModule('configuration');
29                 $appCfg = $cfg->getApplicationConfig();
30                 if($cfg->isPostgreSQLType($appCfg['db']['type'])) {
31                     $order = strtolower($order);
32                 }
33                 $criteria->OrdersBy[$order] = 'desc';
34                 if(is_int($limit) && $limit > 0) {
35                         $criteria->Limit = $limit;
36                 }
37
38                 if (count($allowedJobs) > 0) {
39                         $where = array();
40                         $names = array();
41                         for ($i = 0; $i < count($allowedJobs); $i++) {
42                                 $where[] = "name = :name$i";
43                                 $names[":name$i"] = $allowedJobs[$i];
44                         }
45                         $criteria->Condition = implode(' OR ', $where);
46                         foreach($names as $name => $jobname) {
47                                 $criteria->Parameters[$name] = $jobname;
48                         }
49                 }
50                 return JobRecord::finder()->findAll($criteria);
51         }
52
53         public function getJobById($id) {
54                 return JobRecord::finder()->findByjobid($id);
55         }
56
57         public function getJobByName($name) {
58                 return JobRecord::finder()->findByname($name);
59         }
60
61         public function deleteJobById($id) {
62                 return JobRecord::finder()->deleteByjobid($id);
63         }
64
65         public function getRecentJobids($jobname, $clientid) {
66                 $sql = "name='$jobname' AND clientid='$clientid' AND jobstatus IN ('T', 'W') AND level IN ('F', 'I', 'D')";
67                 $finder = JobRecord::finder();
68                 $criteria = new TActiveRecordCriteria;
69                 $order = 'EndTime';
70                 $cfg = $this->Application->getModule('configuration');
71                 $appCfg = $cfg->getApplicationConfig();
72                 if($cfg->isPostgreSQLType($appCfg['db']['type'])) {
73                     $order = strtolower($order);
74                 }
75                 $criteria->OrdersBy[$order] = 'desc';
76                 $criteria->Condition = $sql;
77                 $jobs = $finder->findAll($criteria);
78
79                 $jobids = array();
80                 $waitForFull = false;
81                 if(!is_null($jobs)) {
82                         foreach($jobs as $job) {
83                                 if($job->level == 'F') {
84                                         $jobids[] = $job->jobid;
85                                         break;
86                                 } elseif($job->level == 'D' && $waitForFull === false) {
87                                         $jobids[] = $job->jobid;
88                                         $waitForFull = true;
89                                 } elseif($job->level == 'I' && $waitForFull === false) {
90                                         $jobids[] = $job->jobid;
91                                 }
92                         }
93                 }
94                 return $jobids;
95         }
96
97         public function getJobTotals($allowedJobs = array()) {
98                 $jobtotals = array('bytes' => 0, 'files' => 0);
99                 $connection = JobRecord::finder()->getDbConnection();
100                 $connection->setActive(true);
101
102                 $where = '';
103                 if (count($allowedJobs) > 0) {
104                         $where = " WHERE name='" . implode("' OR name='", $allowedJobs) . "'";
105                 }
106
107                 $sql = "SELECT sum(JobFiles) AS files, sum(JobBytes) AS bytes FROM Job $where";
108                 $pdo = $connection->getPdoInstance();
109                 $result = $pdo->query($sql);
110                 $ret = $result->fetch();
111                 $jobtotals['bytes'] = $ret['bytes'];
112                 $jobtotals['files'] = $ret['files'];
113                 $pdo = null;
114                 return $jobtotals;
115         }
116
117         /**
118          * Get jobs stored on given volume.
119          *
120          * @param string $mediaid volume identifier
121          * @param array $allowed_jobs jobs allowed to show
122          * @return array jobs stored on volume
123          */
124         public function getJobsOnVolume($mediaid, $allowed_jobs = array()) {
125                 $jobs_criteria = '';
126                 if (count($allowed_jobs) > 0) {
127                         $jobs_sql = implode("', '", $allowed_jobs);
128                         $jobs_criteria = " AND Job.Name IN ('" . $jobs_sql . "')";
129                 }
130                 $sql = "SELECT DISTINCT Job.* FROM Job, JobMedia WHERE JobMedia.MediaId='$mediaid' AND JobMedia.JobId=Job.JobId $jobs_criteria";
131                 return JobRecord::finder()->findAllBySql($sql);
132         }
133
134 }
135 ?>