3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 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.JobRecord');
25 Prado::using('Application.API.Class.Database');
27 class JobManager extends APIModule {
29 public function getJobs($limit, $allowedJobs = array()) {
30 $criteria = new TActiveRecordCriteria;
32 $db_params = $this->getModule('api_config')->getConfig('db');
33 if ($db_params['type'] === Database::PGSQL_TYPE) {
34 $order = strtolower($order);
36 $criteria->OrdersBy[$order] = 'desc';
37 if(is_int($limit) && $limit > 0) {
38 $criteria->Limit = $limit;
41 if (count($allowedJobs) > 0) {
44 for ($i = 0; $i < count($allowedJobs); $i++) {
45 $where[] = "name = :name$i";
46 $names[":name$i"] = $allowedJobs[$i];
48 $criteria->Condition = implode(' OR ', $where);
49 foreach($names as $name => $jobname) {
50 $criteria->Parameters[$name] = $jobname;
53 return JobRecord::finder()->findAll($criteria);
56 public function getJobById($id) {
57 return JobRecord::finder()->findByjobid($id);
60 public function getJobByName($name) {
61 return JobRecord::finder()->findByname($name);
64 public function deleteJobById($id) {
65 return JobRecord::finder()->deleteByjobid($id);
68 public function getRecentJobids($jobname, $clientid, $filesetid) {
69 $sql = "name='$jobname' AND clientid='$clientid' AND filesetid='$filesetid' AND jobstatus IN ('T', 'W') AND level IN ('F', 'I', 'D')";
70 $finder = JobRecord::finder();
71 $criteria = new TActiveRecordCriteria;
73 $db_params = $this->getModule('api_config')->getConfig('db');
74 if ($db_params['type'] === Database::PGSQL_TYPE) {
75 $order = strtolower($order);
77 $criteria->OrdersBy[$order] = 'desc';
78 $criteria->Condition = $sql;
79 $jobs = $finder->findAll($criteria);
84 foreach($jobs as $job) {
85 if($job->level == 'F') {
86 $jobids[] = $job->jobid;
88 } elseif($job->level == 'D' && $waitForFull === false) {
89 $jobids[] = $job->jobid;
91 } elseif($job->level == 'I' && $waitForFull === false) {
92 $jobids[] = $job->jobid;
99 public function getJobTotals($allowedJobs = array()) {
100 $jobtotals = array('bytes' => 0, 'files' => 0);
101 $connection = JobRecord::finder()->getDbConnection();
102 $connection->setActive(true);
105 if (count($allowedJobs) > 0) {
106 $where = " WHERE name='" . implode("' OR name='", $allowedJobs) . "'";
109 $sql = "SELECT sum(JobFiles) AS files, sum(JobBytes) AS bytes FROM Job $where";
110 $pdo = $connection->getPdoInstance();
111 $result = $pdo->query($sql);
112 $ret = $result->fetch();
113 $jobtotals['bytes'] = $ret['bytes'];
114 $jobtotals['files'] = $ret['files'];
120 * Get jobs stored on given volume.
122 * @param string $mediaid volume identifier
123 * @param array $allowed_jobs jobs allowed to show
124 * @return array jobs stored on volume
126 public function getJobsOnVolume($mediaid, $allowed_jobs = array()) {
128 if (count($allowed_jobs) > 0) {
129 $jobs_sql = implode("', '", $allowed_jobs);
130 $jobs_criteria = " AND Job.Name IN ('" . $jobs_sql . "')";
132 $sql = "SELECT DISTINCT Job.* FROM Job, JobMedia WHERE JobMedia.MediaId='$mediaid' AND JobMedia.JobId=Job.JobId $jobs_criteria";
133 return JobRecord::finder()->findAllBySql($sql);
137 * Get jobs for given client.
139 * @param string $clientid client identifier
140 * @param array $allowed_jobs jobs allowed to show
141 * @return array jobs for specific client
143 public function getJobsForClient($clientid, $allowed_jobs = array()) {
145 if (count($allowed_jobs) > 0) {
146 $jobs_sql = implode("', '", $allowed_jobs);
147 $jobs_criteria = " AND Job.Name IN ('" . $jobs_sql . "')";
149 $sql = "SELECT DISTINCT Job.* FROM Client, Job WHERE Client.ClientId='$clientid' AND Client.ClientId=Job.ClientId $jobs_criteria";
150 return JobRecord::finder()->findAllBySql($sql);