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