]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/BaculaConfigResources.php
baculum: Add removing single resource
[bacula/bacula] / gui / baculum / protected / Web / Portlets / BaculaConfigResources.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.TActiveLinkButton');
24 Prado::using('System.Web.UI.ActiveControls.TActiveRepeater');
25 Prado::using('Application.Web.Portlets.ResourceListTemplate');
26
27 class BaculaConfigResources extends ResourceListTemplate {
28
29         const CHILD_CONTROL = 'BaculaConfigDirectives';
30
31         public $resource_names = array();
32
33         private function getConfigData($host, $component_type) {
34                 $params = array('config', $component_type);
35                 $result = $this->Application->getModule('api')->get($params, $host, false);
36                 $config = array();
37                 if (is_object($result) && $result->error === 0 && is_array($result->output)) {
38                         $config = $result->output;
39                 }
40                 return $config;
41         }
42
43         public function loadConfig() {
44                 $host = $this->getHost();
45                 $component_type = $this->getComponentType();
46                 $component_name = $this->getComponentName();
47                 $resources = array();
48                 $config = $this->getConfigData($host, $component_type);
49                 for ($i = 0; $i < count($config); $i++) {
50                         $resource_type = $this->getConfigResourceType($config[$i]);
51                         $resource_name = property_exists($config[$i]->{$resource_type}, 'Name') ? $config[$i]->{$resource_type}->Name : '';
52                         $resource = array(
53                                 'host' => $host,
54                                 'component_type' => $component_type,
55                                 'component_name' => $component_name,
56                                 'resource_type' => $resource_type,
57                                 'resource_name' => $resource_name
58                         );
59                         array_push($resources, $resource);
60                         if (!array_key_exists($resource_type, $this->resource_names)) {
61                                 $this->resource_names[$resource_type] = array();
62                         }
63                         array_push($this->resource_names[$resource_type], $resource_name);
64                 }
65
66                 $this->RepeaterResources->DataSource = $resources;
67                 $this->RepeaterResources->dataBind();
68         }
69
70         public function createResourceListElement($sender, $param) {
71                 $control = $this->getChildControl($param->Item, self::CHILD_CONTROL);
72                 if (is_object($control)) {
73                         $control->setHost($param->Item->DataItem['host']);
74                         $control->setComponentType($param->Item->DataItem['component_type']);
75                         $control->setComponentName($param->Item->DataItem['component_name']);
76                         $control->setResourceType($param->Item->DataItem['resource_type']);
77                         $control->setResourceName($param->Item->DataItem['resource_name']);
78                         $control->setResourceNames($this->resource_names);
79                 }
80                 $param->Item->RemoveResource->setCommandParameter($param->Item->DataItem);
81         }
82
83         public function getDirectives($sender, $param) {
84                 $control = $this->getChildControl($sender->getParent(), self::CHILD_CONTROL);
85                 if (is_object($control)) {
86                         $control->raiseEvent('OnDirectiveListLoad', $this, null);
87                 }
88         }
89
90         /**
91          * Remove resource callback method.
92          *
93          * @return object $sender sender instance
94          * @return mixed $param additional parameters
95          * @return none
96          */
97         public function removeResource($sender, $param) {
98                 if (!$this->getPage()->IsCallback) {
99                         // removing resource available only by callback
100                         return;
101                 }
102                 $host_params = $param->getCommandParameter();
103                 if (!is_array($host_params) || count($host_params) === 0) {
104                         return;
105                 }
106                 $host = $this->getHost();
107                 $config = $this->getConfigData($host, $host_params['component_type']);
108                 $deps = $this->getModule('data_deps')->checkDependencies(
109                         $host_params['component_type'],
110                         $host_params['resource_type'],
111                         $host_params['resource_name'],
112                         $config
113                 );
114                 if (count($deps) === 0) {
115                         // NO DEPENDENCY. Ready to remove.
116                         $this->removeResourceFromConfig(
117                                 $config,
118                                 $host_params['resource_type'],
119                                 $host_params['resource_name']
120                         );
121                         $result = $this->getModule('api')->set(
122                                 array('config', $host_params['component_type']),
123                                 array('config' => json_encode($config)),
124                                 $host,
125                                 false
126                         );
127                         if ($result->error === 0) {
128                                 $this->showRemovedResourceInfo(
129                                         $host_params['resource_type'],
130                                         $host_params['resource_name']
131                                 );
132                         } else {
133                                 $this->showRemovedResourceError($result->output);
134                         }
135                 } else {
136                         // DEPENDENCIES EXIST. List them on the interface.
137                         $this->showDependenciesError(
138                                 $deps,
139                                 $host_params['resource_type'],
140                                 $host_params['resource_name']
141                         );
142                 }
143         }
144
145         /**
146          * Show removed resource information.
147          *
148          * @param string $resource_type removed resource type
149          * @param string $resource_name removed resource name
150          * @return none
151          */
152         private function showRemovedResourceInfo($resource_type, $resource_name) {
153                 $msg = Prado::localize('Resource %s "%s" removed successfully.');
154                 $msg = sprintf(
155                         $msg,
156                         $resource_type,
157                         $resource_name
158                 );
159                 $this->RemoveResourceOk->Text = $msg;
160                 $this->getPage()->getCallbackClient()->hide($this->RemoveResourceError);
161                 $this->getPage()->getCallbackClient()->show($this->RemoveResourceOk);
162         }
163
164         /**
165          * Show removed resource error message.
166          *
167          * @param string $error_message error message
168          * @return none
169          */
170         private function showRemovedResourceError($error_message) {
171                 $this->RemoveResourceError->Text = $error_message;
172                 $this->getPage()->getCallbackClient()->hide($this->RemoveResourceOk);
173                 $this->getPage()->getCallbackClient()->show($this->RemoveResourceError);
174         }
175
176         /**
177          * Show dependencies error message.
178          *
179          * @param array $deps list dependencies for the removing resource
180          * @param string $resource_type resource type of the removing resource
181          * @param string $resource_name resource name of the removing resource
182          * @return none
183          */
184         private function showDependenciesError($deps, $resource_type, $resource_name) {
185                 $emsg = Prado::localize('Resource %s "%s" is used in the following resources:');
186                 $emsg = sprintf($emsg, $resource_type, $resource_name);
187                 $emsg_deps = Prado::localize('Component: %s, Resource: %s "%s", Directive: %s');
188                 $dependencies = array();
189                 for ($i = 0; $i < count($deps); $i++) {
190                         $dependencies[] = sprintf(
191                                 $emsg_deps,
192                                 $deps[$i]['component_type'],
193                                 $deps[$i]['resource_type'],
194                                 $deps[$i]['resource_name'],
195                                 $deps[$i]['directive_name']
196                         );
197                 }
198                 $emsg_sum = Prado::localize('Please unassign resource %s "%s" from these resources and try again.');
199                 $emsg_sum = sprintf($emsg_sum, $resource_type, $resource_name);
200                 $error = array($emsg, implode('<br />', $dependencies),  $emsg_sum);
201                 $error_message = implode('<br /><br />', $error);
202                 $this->showRemovedResourceError($error_message);
203         }
204
205         /**
206          * Remove resource from config.
207          * Note, passing config by reference.
208          *
209          * @param array $config entire config
210          * @param string $resource_type resource type to remove
211          * @param string $resource_name resource name to remove
212          * @return none
213          */
214         private function removeResourceFromConfig(&$config, $resource_type, $resource_name) {
215                 for ($i = 0; $i < count($config); $i++) {
216                         foreach ($config[$i] as $rtype => $resource) {
217                                 if (!property_exists($resource, 'Name')) {
218                                         continue;
219                                 }
220                                 if ($rtype === $resource_type && $resource->Name === $resource_name) {
221                                         // remove resource
222                                         array_splice($config, $i, 1);
223                                         break;
224                                 }
225                         }
226                 }
227         }
228 }
229 ?>