]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/DirectiveMessages.php
baculum: Add link to go back from job configuration window
[bacula/bacula] / gui / baculum / protected / Web / Portlets / DirectiveMessages.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 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('Application.Web.Portlets.DirectiveListTemplate');
24 Prado::using('Application.Web.Portlets.BConditional');
25 Prado::using('Application.Web.Portlets.DirectiveText');
26
27 class DirectiveMessages extends DirectiveListTemplate {
28
29         private $directive_types = array(
30                 'DirectiveText'
31         );
32
33         public $destination_simple = array(
34                 'Console',
35                 'Stdout',
36                 'Stderr',
37                 'Syslog',
38                 'Catalog'
39         );
40
41         public $destination_address = array(
42                 'Director',
43                 'File',
44                 'Append',
45                 'Mail',
46                 'MailOnError',
47                 'MailOnSuccess',
48                 'Operator'
49         );
50
51         private $messages_types = array(
52                 'All',
53                 'Debug',
54                 'Info',
55                 'Warning',
56                 'Error',
57                 'Fatal',
58                 'Terminate',
59                 'Saved',
60                 'Skipped',
61                 'Mount',
62                 'Restored',
63                 'Security',
64                 'Alert',
65                 'Volmgmt'
66         );
67
68         public function loadConfig($sender, $param) {
69                 $load_values = $this->getLoadValues();
70                 $destinations = (array)$this->getData();
71                 if (array_key_exists('Type', $destinations)) {
72                         $destinations = array($destinations);
73                 }
74                 $directives = array();
75                 for ($i = 0; $i < count($destinations); $i++) {
76                         $is_address_type = in_array($destinations[$i]['Type'], $this->destination_address);
77                         $directive_value = null;
78                         if ($is_address_type && array_key_exists('Where', $destinations[$i])) {
79                                 $directive_value = implode(',', $destinations[$i]['Where']);
80                         }
81                         $this->setDirectiveName($destinations[$i]['Type']);
82                         $directives[$i] = array(
83                                 'host' => $this->getHost(),
84                                 'component_type' => $this->getComponentType(),
85                                 'component_name' => $this->getComponentName(),
86                                 'resource_type' => $this->getResourceType(),
87                                 'resource_name' => $this->getResourceName(),
88                                 'directive_name' => $destinations[$i]['Type'],
89                                 'directive_value' => $directive_value,
90                                 'default_value' => false,
91                                 'required' => false,
92                                 'label' => $destinations[$i]['Type'],
93                                 'field_type' => 'TextBox',
94                                 'in_config' => true,
95                                 'show' => true,
96                                 'parent_name' => __CLASS__,
97                                 'is_address_type' => $is_address_type,
98                                 'messages_types' => array()
99                         );
100                         $value_all = $value_not = null;
101                         for ($j = 0; $j < count($this->messages_types); $j++) {
102                                 $value_all = in_array('!' . $this->messages_types[$j], $destinations[$i]['MsgTypes']);
103                                 $value_not = in_array($this->messages_types[$j], $destinations[$i]['MsgTypes']);
104                                 $directives[$i]['messages_types'][] = array(
105                                         'host' => $this->getHost(),
106                                         'component_type' => $this->getComponentType(),
107                                         'component_name' => $this->getComponentName(),
108                                         'resource_type' => $this->getResourceType(),
109                                         'resource_name' => $this->getResourceName(),
110                                         'directive_name' => $this->messages_types[$j],
111                                         'directive_value' => ($value_all || $value_not),
112                                         'default_value' => false,
113                                         'required' => false,
114                                         'label' => $this->messages_types[$j],
115                                         'field_type' => 'Messages',
116                                         'data' => $destinations[$i]['Type'],
117                                         'in_config' => true,
118                                         'show' => true,
119                                         'parent_name' => __CLASS__
120                                 );
121                         }
122                 }
123                 $this->RepeaterMessages->dataSource = $directives;
124                 $this->RepeaterMessages->dataBind();
125         }
126
127         public function getDirectiveValue() {
128                 $values = array();
129                 $controls = $this->RepeaterMessages->getControls();
130                 for ($i = 0; $i < $controls->count(); $i++) {
131                         $directive_values = array();
132                         $where_control = $controls->itemAt($i)->findControlsByType('DirectiveText');
133                         if (count($where_control) === 1 && $where_control[0]->getShow() === true) {
134                                 $directive_values = array($where_control[0]->getDirectiveValue());
135                         }
136                         $types_control = $controls->itemAt($i)->Types;
137                         $types = $types_control->getDirectiveValues();
138                         $directive_name = $types_control->getDirectiveName();
139                         if (count($types) > 0 && count($directive_values) > 0) {
140                                 array_push($directive_values, '=');
141                         }
142                         array_push($directive_values, implode(', ', $types));
143                         $directive_values = array_filter($directive_values);
144                         if (count($directive_values) === 0) {
145                                 continue;
146                         }
147                         $values[$directive_name] = implode(' ', $directive_values);
148                 }
149                 $ret = null;
150                 if (count($values) > 1) {
151                         // multiple messages values
152                         $ret = $values;
153                 } else {
154                         // single messages value
155                         $ret = implode('', array_values($values));
156                 }
157                 return $ret;
158         }
159
160         public function getDirectiveData() {
161                 $values = array();
162                 $controls = $this->RepeaterMessages->getControls();
163                 for ($i = 0; $i < $controls->count(); $i++) {
164                         $directive_values = array();
165                         $where_control = $controls->itemAt($i)->findControlsByType('DirectiveText');
166                         if (count($where_control) === 1 && $where_control[0]->getShow() === true) {
167                                 $directive_values['Where'] = array($where_control[0]->getDirectiveValue());
168                         }
169                         $types_control = $controls->itemAt($i)->Types;
170                         $directive_values['MsgTypes'] = $types_control->getDirectiveValues();
171                         $directive_values['Type'] = $types_control->getDirectiveName();
172                         array_push($values, $directive_values);
173                 }
174                 return $values;
175         }
176
177         public function createDirectiveListElement($sender, $param) {
178                 for ($i = 0; $i < count($this->directive_types); $i++) {
179                         $control = $this->getChildControl($param->Item, $this->directive_types[$i]);
180                         if (is_object($control)) {
181                                 $control->setHost($param->Item->DataItem['host']);
182                                 $control->setComponentType($param->Item->DataItem['component_type']);
183                                 $control->setComponentName($param->Item->DataItem['component_name']);
184                                 $control->setResourceType($param->Item->DataItem['resource_type']);
185                                 $control->setResourceName($param->Item->DataItem['resource_name']);
186                                 $control->setDirectiveName($param->Item->DataItem['directive_name']);
187                                 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
188                                 $control->setDefaultValue($param->Item->DataItem['default_value']);
189                                 $control->setRequired($param->Item->DataItem['required']);
190                                 $control->setLabel($param->Item->DataItem['label']);
191                                 $control->setInConfig($param->Item->DataItem['in_config']);
192                                 $control->setShow($param->Item->DataItem['is_address_type']);
193                                 $control->setParentName($param->Item->DataItem['parent_name']);
194                                 break;
195                         }
196                 }
197                 $param->Item->Types->setData($param->Item->DataItem['messages_types']);
198                 $param->Item->Types->setDirectiveName($param->Item->DataItem['directive_name']);
199         }
200
201         public function loadMessageTypes($sender, $param) {
202                 $param->Item->Types->loadConfig();
203         }
204
205         public function newMessagesDirective($sender, $param) {
206                 $data = $this->getDirectiveData();
207                 $msg_type = $sender->getID();
208                 array_push($data, array('Type' => $msg_type, 'MsgTypes' => array()));
209                 $data = (object)$data;
210                 $this->setData($data);
211                 $this->loadConfig(null, null);
212         }
213 }