]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Portlets/JobRunConfiguration.php
baculum: Assign Baculum copyright to Kern Sibbald
[bacula/bacula] / gui / baculum / protected / Portlets / JobRunConfiguration.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.TActivePanel');
24 Prado::using('Application.Portlets.Portlets');
25
26 class JobRunConfiguration extends Portlets {
27
28         const DEFAULT_JOB_PRIORITY = 10;
29
30         public $jobToVerify = array('C', 'O', 'd');
31
32         public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
33
34         public function configure($jobname) {
35                 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', 'name', rawurlencode($jobname)), true)->output;
36
37                 $this->JobName->Text = $jobname;
38                 $this->Estimation->Text = '';
39
40                 $levels = $this->Application->getModule('misc')->getJobLevels();
41                 $jobAttr = $this->getPage()->JobConfiguration->getJobAttr($jobshow);
42                 $levelsFlip = array_flip($levels);
43
44                 $this->Level->dataSource = $levels;
45                 if (array_key_exists('level', $jobAttr) && array_key_exists($jobAttr['level'], $levelsFlip)) {
46                         $this->Level->SelectedValue = $levelsFlip[$jobAttr['level']];
47
48                         $isVerifyOption = in_array($levelsFlip[$jobAttr['level']], $this->getPage()->JobConfiguration->jobToVerify);
49                         $this->JobToVerifyOptionsLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
50                         $this->JobToVerifyJobNameLine->Display = ($isVerifyOption === true) ? 'Dynamic' : 'None';
51                         $this->JobToVerifyJobIdLine->Display = 'None';
52                 }
53                 $this->Level->dataBind();
54
55                 $this->AccurateLine->Display = 'Dynamic';
56                 $this->EstimateLine->Display = 'Dynamic';
57
58                 $verifyValues = array();
59
60                 foreach($this->verifyOptions as $value => $text) {
61                         $verifyValues[$value] = Prado::localize($text);
62                 }
63
64                 $this->JobToVerifyOptions->dataSource = $verifyValues;
65                 $this->JobToVerifyOptions->dataBind();
66
67                 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), true)->output;
68
69                 $jobsAllDirs = array();
70                 foreach($jobTasks as $director => $tasks) {
71                         $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
72                 }
73
74                 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
75                 $this->JobToVerifyJobName->dataBind();
76
77                 $clients = $this->Application->getModule('api')->get(array('clients'), true)->output;
78                 $selectedClient = $this->getPage()->JobConfiguration->getResourceName('client', $jobshow);
79                 $selectedClientId = null;
80                 $clientsList = array();
81                 foreach($clients as $client) {
82                         if ($client->name === $selectedClient) {
83                                 $selectedClientId = $client->clientid;
84                         }
85                         $clientsList[$client->clientid] = $client->name;
86                 }
87                 $this->Client->dataSource = $clientsList;
88                 $this->Client->SelectedValue = $selectedClientId;
89                 $this->Client->dataBind();
90
91                 $filesetsAll = $this->Application->getModule('api')->get(array('filesets'), true)->output;
92                 $selectedFileset = $this->getPage()->JobConfiguration->getResourceName('fileset', $jobshow);
93                 $filesetsList = array();
94                 foreach($filesetsAll as $director => $filesets) {
95                         $filesetsList = array_merge($filesets, $filesetsList);
96                 }
97                 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
98                 $this->FileSet->SelectedValue = $selectedFileset;
99                 $this->FileSet->dataBind();
100
101                 $pools = $this->Application->getModule('api')->get(array('pools'), true)->output;
102                 $selectedPool = $this->getPage()->JobConfiguration->getResourceName('pool', $jobshow);
103                 $selectedPoolId = null;
104                 $poolList = array();
105                 foreach($pools as $pool) {
106                         if ($pool->name === $selectedPool) {
107                                 $selectedPoolId = $pool->poolid;
108                         }
109                         $poolList[$pool->poolid] = $pool->name;
110                 }
111                 $this->Pool->dataSource = $poolList;
112                 $this->Pool->SelectedValue = $selectedPoolId;
113                 $this->Pool->dataBind();
114
115                 $storages = $this->Application->getModule('api')->get(array('storages'), true)->output;
116                 $selectedStorage = $this->getPage()->JobConfiguration->getResourceName('storage', $jobshow);
117                 $selectedStorageId = null;
118                 $storagesList = array();
119                 foreach($storages as $storage) {
120                         if ($storage->name === $selectedStorage) {
121                                 $selectedStorageId = $storage->storageid;
122                         }
123                         $storagesList[$storage->storageid] = $storage->name;
124                 }
125                 $this->Storage->dataSource = $storagesList;
126                 $this->Storage->SelectedValue = $selectedStorageId;
127                 $this->Storage->dataBind();
128
129                 $this->Priority->Text = array_key_exists('priority', $jobAttr) ? $jobAttr['priority'] : self::DEFAULT_JOB_PRIORITY;
130         }
131
132         public function run_job($sender, $param) {
133                 if($this->PriorityValidator->IsValid === false) {
134                         return false;
135                 }
136                 $params = array();
137                 $params['name'] = $this->JobName->Text;
138                 $params['level'] = $this->Level->SelectedValue;
139                 $params['fileset'] = $this->FileSet->SelectedValue;
140                 $params['clientid'] = $this->Client->SelectedValue;
141                 $params['storageid'] = $this->Storage->SelectedValue;
142                 $params['poolid'] = $this->Pool->SelectedValue;
143                 $params['priority'] = $this->Priority->Text;
144
145                 if (in_array($this->Level->SelectedItem->Value, $this->jobToVerify)) {
146                         $verifyVals = $this->getVerifyVals();
147                         if ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobname']) {
148                                 $params['verifyjob'] = $this->JobToVerifyJobName->SelectedValue;
149                         } elseif ($this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
150                                 $params['jobid'] = $this->JobToVerifyJobId->Text;
151                         }
152                 }
153
154                 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
155
156                 $startedJobId = $this->Application->getModule('misc')->findJobIdStartedJob($result);
157                 if ($this->GoToStartedJob->Checked === true && is_numeric($startedJobId)) {
158                         $params['jobid'] = $startedJobId;
159                         $this->getPage()->JobConfiguration->configure($startedJobId, $params);
160                 } else {
161                         $this->Estimation->Text = implode(PHP_EOL, $result);
162                 }
163         }
164
165         public function estimate($sender, $param) {
166                 $params = array();
167                 $params['name'] = $this->JobName->Text;
168                 $params['level'] = $this->Level->SelectedValue;
169                 $params['fileset'] = $this->FileSet->SelectedValue;
170                 $params['clientid'] = $this->Client->SelectedValue;
171                 $params['accurate'] = (integer)$this->Accurate->Checked;
172                 $result = $this->Application->getModule('api')->create(array('jobs', 'estimate'), $params)->output;
173                 $this->Estimation->Text = implode(PHP_EOL, $result);
174         }
175
176         public function priorityValidator($sender, $param) {
177                 $isValid = preg_match('/^[0-9]+$/', $this->Priority->Text) === 1 && $this->Priority->Text > 0;
178                 $param->setIsValid($isValid);
179         }
180
181         public function jobIdToVerifyValidator($sender, $param) {
182                 $verifyVals = $this->getVerifyVals();
183                 if(in_array($this->Level->SelectedValue, $this->jobToVerify) && $this->JobToVerifyOptions->SelectedItem->Value == $verifyVals['jobid']) {
184                         $isValid = preg_match('/^[0-9]+$/',$this->JobToVerifyJobId->Text) === 1 && $this->JobToVerifyJobId->Text > 0;
185                 } else {
186                         $isValid = true;
187                 }
188                 $param->setIsValid($isValid);
189                 return $isValid;
190         }
191
192         private function getVerifyVals() {
193                 $verifyOpt = array_keys($this->verifyOptions);
194                 $verifyVals = array_combine($verifyOpt, $verifyOpt);
195                 return $verifyVals;
196         }
197 }
198 ?>