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.TActiveLinkButton');
24 Prado::using('System.Web.UI.ActiveControls.TActiveRepeater');
25 Prado::using('Application.Web.Portlets.ResourceListTemplate');
27 class BaculaConfigResources extends ResourceListTemplate {
29 const CHILD_CONTROL = 'BaculaConfigDirectives';
31 public $resource_names = array();
33 private function getConfigData($host, $component_type) {
34 $params = array('config', $component_type);
35 $result = $this->Application->getModule('api')->get($params, $host, false);
37 if (is_object($result) && $result->error === 0 && is_array($result->output)) {
38 $config = $result->output;
43 public function loadConfig() {
44 $host = $this->getHost();
45 $component_type = $this->getComponentType();
46 $component_name = $this->getComponentName();
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 : '';
54 'component_type' => $component_type,
55 'component_name' => $component_name,
56 'resource_type' => $resource_type,
57 'resource_name' => $resource_name
59 array_push($resources, $resource);
60 if (!array_key_exists($resource_type, $this->resource_names)) {
61 $this->resource_names[$resource_type] = array();
63 array_push($this->resource_names[$resource_type], $resource_name);
66 $this->RepeaterResources->DataSource = $resources;
67 $this->RepeaterResources->dataBind();
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);
80 $param->Item->RemoveResource->setCommandParameter($param->Item->DataItem);
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);
91 * Remove resource callback method.
93 * @return object $sender sender instance
94 * @return mixed $param additional parameters
97 public function removeResource($sender, $param) {
98 if (!$this->getPage()->IsCallback) {
99 // removing resource available only by callback
102 $host_params = $param->getCommandParameter();
103 if (!is_array($host_params) || count($host_params) === 0) {
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'],
114 if (count($deps) === 0) {
115 // NO DEPENDENCY. Ready to remove.
116 $this->removeResourceFromConfig(
118 $host_params['resource_type'],
119 $host_params['resource_name']
121 $result = $this->getModule('api')->set(
122 array('config', $host_params['component_type']),
123 array('config' => json_encode($config)),
127 if ($result->error === 0) {
128 $this->showRemovedResourceInfo(
129 $host_params['resource_type'],
130 $host_params['resource_name']
133 $this->showRemovedResourceError($result->output);
136 // DEPENDENCIES EXIST. List them on the interface.
137 $this->showDependenciesError(
139 $host_params['resource_type'],
140 $host_params['resource_name']
146 * Show removed resource information.
148 * @param string $resource_type removed resource type
149 * @param string $resource_name removed resource name
152 private function showRemovedResourceInfo($resource_type, $resource_name) {
153 $msg = Prado::localize('Resource %s "%s" removed successfully.');
159 $this->RemoveResourceOk->Text = $msg;
160 $this->getPage()->getCallbackClient()->hide($this->RemoveResourceError);
161 $this->getPage()->getCallbackClient()->show($this->RemoveResourceOk);
165 * Show removed resource error message.
167 * @param string $error_message error message
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);
177 * Show dependencies error message.
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
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(
192 $deps[$i]['component_type'],
193 $deps[$i]['resource_type'],
194 $deps[$i]['resource_name'],
195 $deps[$i]['directive_name']
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);
206 * Remove resource from config.
207 * Note, passing config by reference.
209 * @param array $config entire config
210 * @param string $resource_type resource type to remove
211 * @param string $resource_name resource name to remove
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')) {
220 if ($rtype === $resource_type && $resource->Name === $resource_name) {
222 array_splice($config, $i, 1);