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('System.Web.UI.ActiveControls.TActivePanel');
24 Prado::using('Application.Web.Portlets.Portlets');
26 class JobRunConfiguration extends Portlets {
28 const USE_CACHE = true;
30 const DEFAULT_JOB_PRIORITY = 10;
32 public $jobToVerify = array('C', 'O', 'd');
34 public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
36 public function configure($jobname) {
37 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', 'name', $jobname), null, true, self::USE_CACHE)->output;
39 $this->JobName->Text = $jobname;
40 $this->Estimation->Text = '';
42 $levels = $this->Application->getModule('misc')->getJobLevels();
43 $jobAttr = $this->getPage()->JobConfiguration->getJobAttr($jobshow);
44 $levelsFlip = array_flip($levels);
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']];
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';
55 $this->Level->dataBind();
57 $this->AccurateLine->Display = 'Dynamic';
58 $this->EstimateLine->Display = 'Dynamic';
60 $verifyValues = array();
62 foreach($this->verifyOptions as $value => $text) {
63 $verifyValues[$value] = Prado::localize($text);
66 $this->JobToVerifyOptions->dataSource = $verifyValues;
67 $this->JobToVerifyOptions->dataBind();
69 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), null, true, self::USE_CACHE)->output;
71 $jobsAllDirs = array();
72 foreach($jobTasks as $director => $tasks) {
73 $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
76 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
77 $this->JobToVerifyJobName->dataBind();
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;
87 $clientsList[$client->clientid] = $client->name;
89 $this->Client->dataSource = $clientsList;
90 $this->Client->SelectedValue = $selectedClientId;
91 $this->Client->dataBind();
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);
99 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
100 $this->FileSet->SelectedValue = $selectedFileset;
101 $this->FileSet->dataBind();
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;
107 foreach($pools as $pool) {
108 if ($pool->name === $selectedPool) {
109 $selectedPoolId = $pool->poolid;
111 $poolList[$pool->poolid] = $pool->name;
113 $this->Pool->dataSource = $poolList;
114 $this->Pool->SelectedValue = $selectedPoolId;
115 $this->Pool->dataBind();
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;
125 $storagesList[$storage->storageid] = $storage->name;
127 $this->Storage->dataSource = $storagesList;
128 $this->Storage->SelectedValue = $selectedStorageId;
129 $this->Storage->dataBind();
131 $this->Priority->Text = array_key_exists('priority', $jobAttr) ? $jobAttr['priority'] : self::DEFAULT_JOB_PRIORITY;
134 public function run_job($sender, $param) {
135 if($this->PriorityValidator->IsValid === false) {
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;
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;
156 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
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);
163 $this->Estimation->Text = implode(PHP_EOL, $result);
167 public function estimate($sender, $param) {
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);
178 public function priorityValidator($sender, $param) {
179 $isValid = preg_match('/^[0-9]+$/', $this->Priority->Text) === 1 && $this->Priority->Text > 0;
180 $param->setIsValid($isValid);
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;
190 $param->setIsValid($isValid);
194 private function getVerifyVals() {
195 $verifyOpt = array_keys($this->verifyOptions);
196 $verifyVals = array_combine($verifyOpt, $verifyOpt);