]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Portlets/JobConfiguration.php
baculum: Update copyright dates
[bacula/bacula] / gui / baculum / protected / Portlets / JobConfiguration.php
1 <?php
2 /**
3  * Bacula® - The Network Backup Solution
4  * Baculum - Bacula web interface
5  *
6  * Copyright (C) 2013-2015 Marcin Haba
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The main author of Bacula is Kern Sibbald, with contributions from many
10  * 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  * Bacula® is a registered trademark of Kern Sibbald.
18  */
19
20 Prado::using('System.Web.UI.ActiveControls.TActivePanel');
21 Prado::using('Application.Portlets.Portlets');
22
23 class JobConfiguration extends Portlets {
24
25         const DEFAULT_JOB_PRIORITY = 10;
26
27
28         public $jobToVerify = array('C', 'O', 'd');
29
30         public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
31
32         public function configure($jobId) {
33                 $jobdata = $this->Application->getModule('api')->get(array('jobs', $jobId))->output;
34                 $this->JobName->Text = $jobdata->job;
35                 $this->JobID->Text = $jobdata->jobid;
36                 $joblog = $this->Application->getModule('api')->get(array('joblog', $jobdata->jobid))->output;
37                 $this->Estimation->Text = is_array($joblog) ? implode(PHP_EOL, $joblog) : Prado::localize("Output for selected job is not available yet or you do not have enabled logging job logs to catalog database. For watching job log there is need to add to the job Messages resource next directive: console = all, !skipped, !saved");
38
39                 $this->Level->dataSource = $this->Application->getModule('misc')->getJobLevels();
40                 $this->Level->SelectedValue = $jobdata->level;
41                 $this->Level->dataBind();
42
43                 $isVerifyOption = in_array($jobdata->level, $this->jobToVerify);
44                 $this->JobToVerifyOptionsLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
45                 $this->JobToVerifyJobNameLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
46                 $this->JobToVerifyJobIdLine->Display = 'None';
47                 $this->AccurateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
48                 $this->EstimateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
49
50                 $verifyValues = array();
51
52                 foreach($this->verifyOptions as $value => $text) {
53                         $verifyValues[$value] = Prado::localize($text);
54                 }
55
56                 $this->JobToVerifyOptions->dataSource = $verifyValues;
57                 $this->JobToVerifyOptions->dataBind();
58
59                 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'))->output;
60
61                 $jobsAllDirs = array();
62                 foreach($jobTasks as $director => $tasks) {
63                         $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
64                 }
65
66                 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
67                 $this->JobToVerifyJobName->dataBind();
68
69                 $clients = $this->Application->getModule('api')->get(array('clients'))->output;
70                 $clientsList = array();
71                 foreach($clients as $client) {
72                         $clientsList[$client->clientid] = $client->name;
73                 }
74                 $this->Client->dataSource = $clientsList;
75                 $this->Client->SelectedValue = $jobdata->clientid;
76                 $this->Client->dataBind();
77
78                 $filesetsAll = $this->Application->getModule('api')->get(array('filesets'))->output;
79                 $filesetsList = array();
80                 foreach($filesetsAll as $director => $filesets) {
81                         $filesetsList = array_merge($filesets, $filesetsList);
82                 }
83                 if($jobdata->filesetid != 0) {
84                         $selectedFileset = $this->Application->getModule('api')->get(array('filesets', $jobdata->filesetid))->output;
85                 }
86                 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
87                 $this->FileSet->SelectedValue = @$selectedFileset->fileset;
88                 $this->FileSet->dataBind();
89
90                 $pools = $this->Application->getModule('api')->get(array('pools'))->output;
91                 $poolList = array();
92                 foreach($pools as $pool) {
93                         $poolList[$pool->poolid] = $pool->name;
94                 }
95                 $this->Pool->dataSource = $poolList;
96                 $this->Pool->SelectedValue = $jobdata->poolid;
97                 $this->Pool->dataBind();
98
99                 $storages = $this->Application->getModule('api')->get(array('storages'))->output;
100                 $storagesList = array();
101                 foreach($storages as $storage) {
102                         $storagesList[$storage->storageid] = $storage->name;
103                 }
104                 $this->Storage->dataSource = $storagesList;
105                 $this->Storage->dataBind();
106
107                 $runningJobStates = $this->Application->getModule('misc')->getRunningJobStates();
108
109                 $this->Priority->Text = ($jobdata->priorjobid == 0) ? self::DEFAULT_JOB_PRIORITY : $jobdata->priorjobid;
110                 $this->DeleteButton->Visible = true;
111                 $this->CancelButton->Visible = in_array($jobdata->jobstatus, $runningJobStates);
112         }
113
114         public function status($sender, $param) {
115                 $joblog = $this->Application->getModule('api')->get(array('joblog', $this->JobID->Text))->output;
116                 $this->Estimation->Text = is_array($joblog) ? implode(PHP_EOL, $joblog) : Prado::localize("Output for selected job is not available yet or you do not have enabled logging job logs to catalog database. For watching job log there is need to add to the job Messages resource next directive: console = all, !skipped, !saved");
117         }
118
119         public function delete($sender, $param) {
120                 $this->Application->getModule('api')->remove(array('jobs', $this->JobID->Text));
121                 $this->DeleteButton->Visible = false;
122         }
123
124         public function cancel($sender, $param) {
125                 $this->Application->getModule('api')->set(array('jobs', 'cancel', $this->JobID->Text), array('a' => 'b'));
126                 $this->CancelButton->Visible = false;
127         }
128
129         public function run_again($sender, $param) {
130                 if($this->PriorityValidator->IsValid === false) {
131                         return false;
132                 }
133                 $params = array();
134                 $params['id'] = $this->JobID->Text;
135                 $params['level'] = $this->Level->SelectedValue;
136                 $params['fileset'] = $this->FileSet->SelectedValue;
137                 $params['clientid'] = $this->Client->SelectedValue;
138                 $params['storageid'] = $this->Storage->SelectedValue;
139                 $params['poolid'] = $this->Pool->SelectedValue;
140                 $params['priority'] = $this->Priority->Text;
141
142                 if (in_array($this->Level->SelectedItem->Value, $this->jobToVerify)) {
143                         $verifyVals = $this->getVerifyVals();
144                         if ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobname']) {
145                                 $params['verifyjob'] = $this->JobToVerifyJobName->SelectedValue;
146                         } elseif ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
147                                 $params['jobid'] = $this->JobToVerifyJobId->Text;
148                         }
149                 }
150
151                 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
152                 $this->Estimation->Text = implode(PHP_EOL, $result);
153         }
154
155         public function estimate($sender, $param) {
156                 $params = array();
157                 $params['id'] = $this->JobID->Text;
158                 $params['level'] = $this->Level->SelectedValue;
159                 $params['fileset'] = $this->FileSet->SelectedValue;
160                 $params['clientid'] = $this->Client->SelectedValue;
161                 $params['accurate'] = (integer)$this->Accurate->Checked;
162                 $result = $this->Application->getModule('api')->create(array('jobs', 'estimate'), $params)->output;
163                 $this->Estimation->Text = implode(PHP_EOL, $result);
164         }
165
166         public function priorityValidator($sender, $param) {
167                 $isValid = preg_match('/^[0-9]+$/',$this->Priority->Text) === 1 && $this->Priority->Text > 0;
168                 $param->setIsValid($isValid);
169         }
170
171         public function jobIdToVerifyValidator($sender, $param) {
172                 $verifyVals = $this->getVerifyVals();
173                 if(in_array($this->Level->SelectedValue, $this->jobToVerify) && $this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
174                         $isValid = preg_match('/^[0-9]+$/',$this->JobToVerifyJobId->Text) === 1 && $this->JobToVerifyJobId->Text > 0;
175                 } else {
176                         $isValid = true;
177                 }
178                 $param->setIsValid($isValid);
179                 return $isValid;
180         }
181
182         private function getVerifyVals() {
183                 $verifyOpt = array_keys($this->verifyOptions);
184                 $verifyVals = array_combine($verifyOpt, $verifyOpt);
185                 return $verifyVals;
186         }
187 }
188 ?>