3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2015 Marcin Haba
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.Portlets.Portlets');
26 class JobRunConfiguration extends Portlets {
28 const DEFAULT_JOB_PRIORITY = 10;
30 public $jobToVerify = array('C', 'O', 'd');
32 public $verifyOptions = array('jobname' => 'Verify by Job Name', 'jobid' => 'Verify by JobId');
34 public function configure($jobname) {
35 $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', 'name', rawurlencode($jobname)), true)->output;
37 $this->JobName->Text = $jobname;
38 $this->Estimation->Text = '';
40 $levels = $this->Application->getModule('misc')->getJobLevels();
41 $jobAttr = $this->getPage()->JobConfiguration->getJobAttr($jobshow);
42 $levelsFlip = array_flip($levels);
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']];
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';
53 $this->Level->dataBind();
55 $this->AccurateLine->Display = 'Dynamic';
56 $this->EstimateLine->Display = 'Dynamic';
58 $verifyValues = array();
60 foreach($this->verifyOptions as $value => $text) {
61 $verifyValues[$value] = Prado::localize($text);
64 $this->JobToVerifyOptions->dataSource = $verifyValues;
65 $this->JobToVerifyOptions->dataBind();
67 $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), true)->output;
69 $jobsAllDirs = array();
70 foreach($jobTasks as $director => $tasks) {
71 $jobsAllDirs = array_merge($jobsAllDirs, $tasks);
74 $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs);
75 $this->JobToVerifyJobName->dataBind();
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;
85 $clientsList[$client->clientid] = $client->name;
87 $this->Client->dataSource = $clientsList;
88 $this->Client->SelectedValue = $selectedClientId;
89 $this->Client->dataBind();
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);
97 $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList);
98 $this->FileSet->SelectedValue = $selectedFileset;
99 $this->FileSet->dataBind();
101 $pools = $this->Application->getModule('api')->get(array('pools'), true)->output;
102 $selectedPool = $this->getPage()->JobConfiguration->getResourceName('pool', $jobshow);
103 $selectedPoolId = null;
105 foreach($pools as $pool) {
106 if ($pool->name === $selectedPool) {
107 $selectedPoolId = $pool->poolid;
109 $poolList[$pool->poolid] = $pool->name;
111 $this->Pool->dataSource = $poolList;
112 $this->Pool->SelectedValue = $selectedPoolId;
113 $this->Pool->dataBind();
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;
123 $storagesList[$storage->storageid] = $storage->name;
125 $this->Storage->dataSource = $storagesList;
126 $this->Storage->SelectedValue = $selectedStorageId;
127 $this->Storage->dataBind();
129 $this->Priority->Text = array_key_exists('priority', $jobAttr) ? $jobAttr['priority'] : self::DEFAULT_JOB_PRIORITY;
132 public function run_job($sender, $param) {
133 if($this->PriorityValidator->IsValid === false) {
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;
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;
154 $result = $this->Application->getModule('api')->create(array('jobs', 'run'), $params)->output;
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);
161 $this->Estimation->Text = implode(PHP_EOL, $result);
165 public function estimate($sender, $param) {
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);
176 public function priorityValidator($sender, $param) {
177 $isValid = preg_match('/^[0-9]+$/', $this->Priority->Text) === 1 && $this->Priority->Text > 0;
178 $param->setIsValid($isValid);
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;
188 $param->setIsValid($isValid);
192 private function getVerifyVals() {
193 $verifyOpt = array_keys($this->verifyOptions);
194 $verifyVals = array_combine($verifyOpt, $verifyOpt);