]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/JobConfiguration.php
baculum: Add link to go back from job configuration window
[bacula/bacula] / gui / baculum / protected / Web / Portlets / JobConfiguration.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 Prado::using('System.Web.UI.ActiveControls.TActiveHiddenField');
24 Prado::using('System.Web.UI.ActiveControls.TActivePanel');
25 Prado::using('Application.Web.Portlets.Portlets');
26
27 class JobConfiguration extends Portlets {
28
29         const USE_CACHE = true;
30
31         const DEFAULT_JOB_PRIORITY = 10;
32
33         const RESOURCE_SHOW_PATTERN = '/^\s+--> %resource: name=(.+?(?=\s\S+\=.+)|.+$)/i';
34         const JOB_SHOW_PATTERN = '/^Job:\sname=(?P<jobname>.+)\sJobType=\d+\slevel=(?P<level>\w+)\sPriority=(?P<priority>\d+)/i';
35
36         public $jobToVerify = array('C', 'O', 'd');
37
38         public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
39
40         public function configure($jobid, $params = array()) {
41                 $jobdata = $this->Application->getModule('api')->get(array('jobs', $jobid))->output;
42                 $this->JobName->Text = $jobdata->job;
43                 $this->JobID->Text = $jobdata->jobid;
44                 $joblog = $this->Application->getModule('api')->get(array('joblog', $jobdata->jobid))->output;
45                 $running_job_states = $this->Application->getModule('misc')->getRunningJobStates();
46                 if (in_array($jobdata->jobstatus, $running_job_states)) {
47                         $this->Estimation->CssClass = 'textbox-auto wheel-loader';
48                 } else {
49                         $this->Estimation->CssClass = 'textbox-auto';
50                 }
51                 $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");
52
53                 $this->Level->dataSource = $this->Application->getModule('misc')->getJobLevels();
54                 $this->Level->SelectedValue = $jobdata->level;
55                 $this->Level->dataBind();
56
57                 $isVerifyOption = in_array($jobdata->level, $this->jobToVerify);
58                 $this->JobToVerifyOptionsLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
59                 $this->JobToVerifyJobNameLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
60                 $this->JobToVerifyJobIdLine->Display = 'None';
61                 $this->AccurateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
62                 $this->EstimateLine->Display = ($isVerifyOption === true) ? 'None' : 'Dynamic';
63
64                 $verifyValues = array();
65
66                 foreach($this->verifyOptions as $value => $text) {
67                         $verifyValues[$value] = Prado::localize($text);
68                 }
69
70                 $this->JobToVerifyOptions->dataSource = $verifyValues;
71                 $this->JobToVerifyOptions->dataBind();
72
73                 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), null, true, self::USE_CACHE)->output;
74                 $jobsAllDirs = array();
75                 foreach($jobTasks as $director => $tasks) {
76                         $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
77                 }
78
79                 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
80                 $this->JobToVerifyJobName->dataBind();
81
82                 $clients = $this->Application->getModule('api')->get(array('clients'), null, true, self::USE_CACHE)->output;
83                 $clientsList = array();
84                 foreach($clients as $client) {
85                         $clientsList[$client->clientid] = $client->name;
86                 }
87                 $this->Client->dataSource = $clientsList;
88                 $this->Client->SelectedValue = $jobdata->clientid;
89                 $this->Client->dataBind();
90
91                 $filesetsAll = $this->Application->getModule('api')->get(array('filesets'), null, true, self::USE_CACHE)->output;
92                 $filesetsList = array();
93                 foreach($filesetsAll as $director => $filesets) {
94                         $filesetsList = array_merge($filesets, $filesetsList);
95                 }
96                 $selectedFileset = '';
97                 if($jobdata->filesetid != 0) {
98                         $selectedFileset = $this->Application->getModule('api')->get(array('filesets', $jobdata->filesetid), null, true, self::USE_CACHE)->output->fileset;
99                 }
100                 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
101                 $this->FileSet->SelectedValue = array_key_exists('fileset', $params) ? $params['fileset'] : $selectedFileset;
102                 $this->FileSet->dataBind();
103
104                 $pools = $this->Application->getModule('api')->get(array('pools'), null, true, self::USE_CACHE)->output;
105                 $poolList = array();
106                 foreach($pools as $pool) {
107                         $poolList[$pool->poolid] = $pool->name;
108                 }
109                 $this->Pool->dataSource = $poolList;
110                 $this->Pool->SelectedValue = array_key_exists('poolid', $params) ? $params['poolid'] : $jobdata->poolid;
111                 $this->Pool->dataBind();
112
113                 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid), null, true, self::USE_CACHE)->output;
114                 $storageShow = $this->getResourceName('storage', $jobshow);
115                 $storagesList = array();
116                 $selectedStorageId = null;
117                 $storages = $this->Application->getModule('api')->get(array('storages'), null, true, self::USE_CACHE)->output;
118                 foreach($storages as $storage) {
119                         if ($storage->name == $storageShow) {
120                                 $selectedStorageId = $storage->storageid;
121                         }
122                         $storagesList[$storage->storageid] = $storage->name;
123                 }
124                 $this->Storage->dataSource = $storagesList;
125                 if (!is_null($selectedStorageId)) {
126                         $this->Storage->SelectedValue = $selectedStorageId;
127                 }
128                 $this->Storage->dataBind();
129
130                 $isJobRunning = in_array($jobdata->jobstatus, $running_job_states);
131
132                 $this->Priority->Text = ($jobdata->priorjobid == 0) ? self::DEFAULT_JOB_PRIORITY : $jobdata->priorjobid;
133                 $this->DeleteButton->Visible = true;
134                 $this->CancelButton->Visible = $isJobRunning;
135                 $this->RefreshStart->Value = $isJobRunning;
136                 $this->Run->Display = 'Dynamic';
137                 $this->EstimateLine->Display = 'Dynamic';
138                 $this->Status->Visible = true;
139                 if (key_exists('prev_window', $params)) {
140                         $this->GoBack->Value = $params['prev_window'];
141                         $this->getPage()->getCallbackClient()->show('job_go_back');
142                 } else {
143
144                         $this->getPage()->getCallbackClient()->hide('job_go_back');
145                 }
146         }
147
148         public function status($sender, $param) {
149                 $jobdata = $this->Application->getModule('api')->get(array('jobs', $this->JobID->Text))->output;
150                 $running_job_states = $this->Application->getModule('misc')->getRunningJobStates();
151                 if (in_array($jobdata->jobstatus, $running_job_states)) {
152                         $this->RefreshStart->Value = true;
153                 } else {
154                         $this->RefreshStart->Value = false;
155                         $this->CancelButton->Visible = false;
156                         $this->Estimation->CssClass = 'textbox-auto';
157                 }
158
159                 $joblog = $this->Application->getModule('api')->get(array('joblog', $this->JobID->Text))->output;
160                 $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");
161         }
162
163         public function delete($sender, $param) {
164                 $this->Application->getModule('api')->remove(array('jobs', $this->JobID->Text));
165                 $this->Status->Visible = false;
166                 $this->Run->Display = 'None';
167                 $this->DeleteButton->Visible = false;
168                 $this->EstimateLine->Display = 'None';
169                 $this->Estimation->CssClass = 'textbox-auto';
170         }
171
172         public function cancel($sender, $param) {
173                 $this->Application->getModule('api')->set(array('jobs', 'cancel', $this->JobID->Text), array('a' => 'b'));
174                 $this->CancelButton->Visible = false;
175                 $this->status(null, null);
176                 $this->Estimation->CssClass = 'textbox-auto';
177         }
178
179         public function run_again($sender, $param) {
180                 if($this->PriorityValidator->IsValid === false) {
181                         return false;
182                 }
183
184                 $params = array();
185                 if (is_null($sender) && is_numeric($param)) {
186                         $jobdata = $this->Application->getModule('api')->get(array('jobs', $param))->output;
187                         $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid))->output;
188                         $params['id'] = $jobdata->jobid;
189                         $params['level'] = $jobdata->level;
190                         $params['fileset'] = $this->getResourceName('fileset', $jobshow);
191                         $params['clientid'] = $jobdata->clientid;
192                         $storage = $this->getResourceName('storage', $jobshow);
193                         if (!is_null($storage)) {
194                                 $params['storageid'] = $this->getStorageByName($storage)->storageid;
195                         } else {
196                                 $autochanger = $this->getResourceName('autochanger', $jobshow);
197                                 if (!is_null($autochanger)) {
198                                         $params['storageid'] = $this->getStorageByName($autochanger)->storageid;
199                                 }
200                         }
201                         $pool = $this->getResourceName('pool', $jobshow);
202                         if (!is_null($pool)) {
203                                 $params['poolid'] = $this->getPoolByName($pool)->poolid;
204                         }
205                 } else {
206                         $params['id'] = $this->JobID->Text;
207                         $params['level'] = $this->Level->SelectedValue;
208                         $params['fileset'] = $this->FileSet->SelectedValue;
209                         $params['clientid'] = $this->Client->SelectedValue;
210                         $params['storageid'] = $this->Storage->SelectedValue;
211                         $params['poolid'] = $this->Pool->SelectedValue;
212                         $params['priority'] = $this->Priority->Text;
213
214                         if (in_array($this->Level->SelectedItem->Value, $this->jobToVerify)) {
215                                 $verifyVals = $this->getVerifyVals();
216                                 if ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobname']) {
217                                         $params['verifyjob'] = $this->JobToVerifyJobName->SelectedValue;
218                                 } elseif ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
219                                         $params['jobid'] = $this->JobToVerifyJobId->Text;
220                                 }
221                         }
222                 }
223                 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
224                 $startedJobId = $this->Application->getModule('misc')->findJobIdStartedJob($result);
225                 if (is_numeric($startedJobId)) {
226                         $params['jobid'] = $startedJobId;
227                         $this->configure($startedJobId, $params);
228                 } else {
229                         $this->Estimation->Text = implode(PHP_EOL, $result);
230                 }
231         }
232
233         public function estimate($sender, $param) {
234                 $params = array();
235                 $params['id'] = $this->JobID->Text;
236                 $params['level'] = $this->Level->SelectedValue;
237                 $params['fileset'] = $this->FileSet->SelectedValue;
238                 $params['clientid'] = $this->Client->SelectedValue;
239                 $params['accurate'] = (integer)$this->Accurate->Checked;
240                 $result = $this->Application->getModule('api')->create(array('jobs', 'estimate'), $params)->output;
241                 $this->Estimation->Text = implode(PHP_EOL, $result);
242         }
243
244         public function priorityValidator($sender, $param) {
245                 $isValid = preg_match('/^[0-9]+$/',$this->Priority->Text) === 1 && $this->Priority->Text > 0;
246                 $param->setIsValid($isValid);
247         }
248
249         public function jobIdToVerifyValidator($sender, $param) {
250                 $verifyVals = $this->getVerifyVals();
251                 if(in_array($this->Level->SelectedValue, $this->jobToVerify) && $this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
252                         $isValid = preg_match('/^[0-9]+$/',$this->JobToVerifyJobId->Text) === 1 && $this->JobToVerifyJobId->Text > 0;
253                 } else {
254                         $isValid = true;
255                 }
256                 $param->setIsValid($isValid);
257                 return $isValid;
258         }
259
260         private function getVerifyVals() {
261                 $verifyOpt = array_keys($this->verifyOptions);
262                 $verifyVals = array_combine($verifyOpt, $verifyOpt);
263                 return $verifyVals;
264         }
265
266         public function getResourceName($resource, $jobshow) {
267                 $resource_name = null;
268                 $pattern = str_replace('%resource', $resource, self::RESOURCE_SHOW_PATTERN);
269                 for ($i = 0; $i < count($jobshow); $i++) {
270                         if (preg_match($pattern, $jobshow[$i], $match) === 1) {
271                                 $resource_name = $match[1];
272                                 break;
273                         }
274                 }
275                 return $resource_name;
276         }
277
278         public function getJobAttr($jobshow) {
279                 $attr = array();
280                 for ($i = 0; $i < count($jobshow); $i++) {
281                         if (preg_match(self::JOB_SHOW_PATTERN, $jobshow[$i], $match) === 1) {
282                                 $attr['jobname'] = $match['jobname'];
283                                 $attr['level'] = $match['level'];
284                                 $attr['priority'] = $match['priority'];
285                                 break;
286                         }
287                 }
288                 return $attr;
289         }
290
291         private function getPoolByName($name) {
292                 $pool = null;
293                 $pools = $this->Application->getModule('api')->get(array('pools'), null, true, self::USE_CACHE)->output;
294                 for ($i = 0; $i < count($pools); $i++) {
295                         if ($pools[$i]->name == $name) {
296                                 $pool = $pools[$i];
297                                 break;
298                         }
299                 }
300                 return $pool;
301         }
302
303         private function getStorageByName($name) {
304                 $storage = null;
305                 $storages = $this->Application->getModule('api')->get(array('storages'), null, true, self::USE_CACHE)->output;
306                 for ($i = 0; $i < count($storages); $i++) {
307                         if ($storages[$i]->name == $name) {
308                                 $storage = $storages[$i];
309                                 break;
310                         }
311                 }
312                 return $storage;
313         }
314 }
315 ?>