]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Fix renaming config resources
authorMarcin Haba <marcin.haba@bacula.pl>
Thu, 5 Nov 2020 03:53:12 +0000 (04:53 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 5 Nov 2020 03:53:12 +0000 (04:53 +0100)
gui/baculum/protected/Web/Pages/JobHistoryView.page
gui/baculum/protected/Web/Pages/JobHistoryView.php
gui/baculum/protected/Web/Pages/JobView.page
gui/baculum/protected/Web/Pages/JobView.php
gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php
gui/baculum/protected/Web/Portlets/DirectiveRenderer.php
gui/baculum/protected/Web/Portlets/DirectiveTemplate.php
gui/baculum/protected/Web/Portlets/DirectiveTextBox.php

index 47445932786d3931ae3031fd7f3cddaf92b0e575..cee5aae0692fe89f3ba496a8effd79fed58ef5f7 100644 (file)
                <button id="btn_job_actions" type="button" class="w3-bar-item w3-button tab_btn w3-grey" onclick="W3Tabs.open(this.id, 'job_actions'); job_callback_func(); oRunningJobStatus.init_refresh();"><%[ Actions ]%></button>
                <com:TActiveLinkButton
                        CssClass="w3-bar-item w3-button tab_btn"
-                       Attributes.onclick="W3Tabs.open(this.id, 'job_config'); clear_node('#fileset_config div.directive_field'); clear_node('#schedule_config div.directive_field');"
+                       Attributes.onclick="W3Tabs.open(this.id, 'job_config');"
                        Text="<%[ Configure job ]%>"
                        Visible="<%=!empty($_SESSION['dir'])%>"
                        OnClick="loadJobConfig"
                />
                <com:TActiveLinkButton
                        CssClass="w3-bar-item w3-button tab_btn"
-                       Attributes.onclick="W3Tabs.open(this.id, 'fileset_config'); clear_node('#job_config div.directive_field'); clear_node('#schedule_config  div.directive_field');"
+                       Attributes.onclick="W3Tabs.open(this.id, 'fileset_config');"
                        Text="<%[ Configure fileset ]%>"
                        Visible="<%=!empty($_SESSION['dir'])%>"
                        OnClick="loadFileSetConfig"
                />
                <com:TActiveLinkButton
                        CssClass="w3-bar-item w3-button tab_btn"
-                       Attributes.onclick="W3Tabs.open(this.id, 'schedule_config'); clear_node('#job_config div.directive_field'); clear_node('#fileset_config div.directive_field');"
+                       Attributes.onclick="W3Tabs.open(this.id, 'schedule_config');"
                        Text="<%[ Configure schedule ]%>"
                        Visible="<%=!empty($_SESSION['dir'])%>"
                        OnClick="loadScheduleConfig"
@@ -497,6 +497,8 @@ $(function() {
                        ComponentType="dir"
                        ResourceType="Job"
                        ShowCancelButton="false"
+                       OnSave="reloadJobInfo"
+                       DisableRename="true"
                />
        </div>
        <div class="w3-container tab_item" id="fileset_config" style="display: none">
@@ -506,6 +508,7 @@ $(function() {
                        ResourceType="Fileset"
                        ShowRemoveButton="false"
                        ShowCancelButton="false"
+                       OnSave="reloadJobInfo"
                />
        </div>
        <div class="w3-container tab_item" id="schedule_config" style="display: none">
@@ -515,6 +518,7 @@ $(function() {
                        ResourceType="Schedule"
                        ShowRemoveButton="false"
                        ShowCancelButton="false"
+                       OnSave="reloadJobInfo"
                />
        </div>
        <div id="job_delete_confirm" class="w3-modal" style="display: none">
index 0d9f9eefab37bfadd2477d7d849f8dcfe6f37522..b289859bb90adc1b39df0d3a12e4724b45352d49 100644 (file)
@@ -90,7 +90,6 @@ class JobHistoryView extends BaculumWebPage {
 
        public function onInit($param) {
                parent::onInit($param);
-               $this->JobConfig->attachEventHandler('OnSave', [$this, 'reloadJobInfo']);
                $job_name = $this->getJobName();
                $this->RunJobModal->setJobId($this->getJobId());
                $this->RunJobModal->setJobName($job_name);
@@ -312,10 +311,11 @@ class JobHistoryView extends BaculumWebPage {
        /**
         * Reload job information.
         *
+        * @param BaculaConfigDirectives $sender sender object
         * @param mixed $param save event parameter
         * @return none
         */
-       public function reloadJobInfo($param) {
+       public function reloadJobInfo($sender, $param) {
                $job_name = $this->getJobName();
                $this->setJobInfo($job_name);
        }
@@ -381,6 +381,8 @@ class JobHistoryView extends BaculumWebPage {
                        $this->JobConfig->setResourceName($this->getJobName());
                        $this->JobConfig->setLoadValues(true);
                        $this->JobConfig->raiseEvent('OnDirectiveListLoad', $this, null);
+                       $this->FileSetConfig->unloadDirectives();
+                       $this->ScheduleConfig->unloadDirectives();
                }
        }
 
@@ -392,6 +394,8 @@ class JobHistoryView extends BaculumWebPage {
                                $this->FileSetConfig->setResourceName($job_info['fileset']['name']);
                                $this->FileSetConfig->setLoadValues(true);
                                $this->FileSetConfig->raiseEvent('OnDirectiveListLoad', $this, null);
+                               $this->JobConfig->unloadDirectives();
+                               $this->ScheduleConfig->unloadDirectives();
                        }
                }
        }
@@ -404,6 +408,10 @@ class JobHistoryView extends BaculumWebPage {
                                $this->ScheduleConfig->setResourceName($job_info['schedule']['name']);
                                $this->ScheduleConfig->setLoadValues(true);
                                $this->ScheduleConfig->raiseEvent('OnDirectiveListLoad', $this, null);
+                               $this->JobConfig->unloadDirectives();
+                               $this->FileSetConfig->unloadDirectives();
+                       } else {
+                               $this->ScheduleConfig->unloadDirectives();
                        }
                }
        }
index 8499094f179b26d8704874df1b1ed923d0da372e..8b517223f5ec0d61ffe2c44e4d48c7755c671061 100644 (file)
                <button id="btn_job_actions" type="button" class="w3-bar-item w3-button tab_btn w3-grey" onclick="W3Tabs.open(this.id, 'job_actions');"><%[ Actions ]%></button>
                <com:TActiveLinkButton
                        CssClass="w3-bar-item w3-button tab_btn"
-                       Attributes.onclick="W3Tabs.open(this.id, 'job_config'); clear_node('#fileset_config div.directive_field'); clear_node('#schedule_config div.directive_field');"
+                       Attributes.onclick="W3Tabs.open(this.id, 'job_config')"
                        Text="<%[ Configure job ]%>"
                        Visible="<%=!empty($_SESSION['dir'])%>"
                        OnClick="loadJobConfig"
                />
                <com:TActiveLinkButton
                        CssClass="w3-bar-item w3-button tab_btn"
-                       Attributes.onclick="W3Tabs.open(this.id, 'fileset_config'); clear_node('#job_config div.directive_field'); clear_node('#schedule_config  div.directive_field');"
+                       Attributes.onclick="W3Tabs.open(this.id, 'fileset_config')"
                        Text="<%[ Configure fileset ]%>"
                        Visible="<%=!empty($_SESSION['dir'])%>"
                        OnClick="loadFileSetConfig"
                />
                <com:TActiveLinkButton
                        CssClass="w3-bar-item w3-button tab_btn"
-                       Attributes.onclick="W3Tabs.open(this.id, 'schedule_config'); clear_node('#job_config div.directive_field'); clear_node('#fileset_config div.directive_field');"
+                       Attributes.onclick="W3Tabs.open(this.id, 'schedule_config')"
                        Text="<%[ Configure schedule ]%>"
                        Visible="<%=!empty($_SESSION['dir'])%>"
                        OnClick="loadScheduleConfig"
@@ -36,7 +36,7 @@
                        CssClass="w3-bar-item w3-button tab_btn"
                        Text="<%[ Job schedules ]%>"
                        OnCallback="loadSchedules"
-                       Attributes.onclick="W3Tabs.open(this.id, 'job_schedules'); clear_node('#job_config div.directive_field'); clear_node('#fileset_config div.directive_field'); clear_node('#schedule_config  div.directive_field');"
+                       Attributes.onclick="W3Tabs.open(this.id, 'job_schedules');"
                />
        </div>
        <div class="w3-container tab_item" id="job_actions">
@@ -360,6 +360,8 @@ var oJobGraphs = {
                        ComponentType="dir"
                        ResourceType="Job"
                        ShowCancelButton="false"
+                       OnSave="reloadJobInfo"
+                       DisableRename="true"
                />
        </div>
        <div class="w3-container tab_item" id="fileset_config" style="display: none">
@@ -369,6 +371,7 @@ var oJobGraphs = {
                        ResourceType="Fileset"
                        ShowRemoveButton="false"
                        ShowCancelButton="false"
+                       OnSave="reloadJobInfo"
                />
        </div>
        <div class="w3-container tab_item" id="schedule_config" style="display: none">
@@ -378,6 +381,7 @@ var oJobGraphs = {
                        ResourceType="Schedule"
                        ShowRemoveButton="false"
                        ShowCancelButton="false"
+                       OnSave="reloadJobInfo"
                />
        </div>
        <div class="w3-container tab_item" id="job_history" style="display: none">
index 470d386f54089face26b63f668a3fa462e6f8243..98a98a3c13b47ed4b278be0ee89868dd3ff22ef8 100644 (file)
@@ -39,7 +39,6 @@ class JobView extends BaculumWebPage {
 
        public function onInit($param) {
                parent::onInit($param);
-               $this->JobConfig->attachEventHandler('OnSave', [$this, 'reloadJobInfo']);
                if ($this->IsCallBack || $this->IsPostBack) {
                        return;
                }
@@ -102,10 +101,11 @@ class JobView extends BaculumWebPage {
        /**
         * Reload job information.
         *
+        * @param BaculaConfigDirectives $sender sender object
         * @param mixed $param save event parameter
         * @return none
         */
-       public function reloadJobInfo($param) {
+       public function reloadJobInfo($sender, $param) {
                if ($this->Request->contains('job')) {
                        $this->setJobInfo($this->Request['job']);
                }
@@ -121,6 +121,8 @@ class JobView extends BaculumWebPage {
                        $this->JobConfig->setResourceName($this->getJobName());
                        $this->JobConfig->setLoadValues(true);
                        $this->JobConfig->raiseEvent('OnDirectiveListLoad', $this, null);
+                       $this->FileSetConfig->unloadDirectives();
+                       $this->ScheduleConfig->unloadDirectives();
                }
        }
 
@@ -132,6 +134,8 @@ class JobView extends BaculumWebPage {
                                $this->FileSetConfig->setResourceName($job_info['fileset']['name']);
                                $this->FileSetConfig->setLoadValues(true);
                                $this->FileSetConfig->raiseEvent('OnDirectiveListLoad', $this, null);
+                               $this->JobConfig->unloadDirectives();
+                               $this->ScheduleConfig->unloadDirectives();
                        }
                }
        }
@@ -144,6 +148,8 @@ class JobView extends BaculumWebPage {
                                $this->ScheduleConfig->setResourceName($job_info['schedule']['name']);
                                $this->ScheduleConfig->setLoadValues(true);
                                $this->ScheduleConfig->raiseEvent('OnDirectiveListLoad', $this, null);
+                               $this->JobConfig->unloadDirectives();
+                               $this->FileSetConfig->unloadDirectives();
                        } else {
                                $this->ScheduleConfig->unloadDirectives();
                        }
index 39e118f3ee9fd9cf8d6d8ee36071ea6bf299077a..a34dcc28d11667938689c6453b27f037b00abe73 100644 (file)
@@ -53,6 +53,7 @@ class BaculaConfigDirectives extends DirectiveListTemplate {
        const SHOW_ALL_DIRECTIVES = 'ShowAllDirectives';
        const SHOW_BOTTOM_BUTTONS = 'ShowBottomButtons';
        const SAVE_DIRECTIVE_ACTION_OK = 'SaveDirectiveActionOk';
+       const DISABLE_RENAME = 'DisableRename';
 
        private $show_all_directives = false;
 
@@ -149,7 +150,6 @@ class BaculaConfigDirectives extends DirectiveListTemplate {
                                $predefined = true;
                        }
                }
-
                $data_desc = $this->Application->getModule('data_desc');
                $resource_desc = $data_desc->getDescription($component_type, $resource_type);
                foreach ($resource_desc as $directive_name => $directive_desc) {
@@ -285,6 +285,7 @@ class BaculaConfigDirectives extends DirectiveListTemplate {
                                }
                                $directive_name = $controls[$j]->getDirectiveName();
                                $directive_value = $controls[$j]->getDirectiveValue();
+
                                $default_value = null;
                                if (key_exists($directive_name, $resource_desc)) {
                                        $default_value = $resource_desc[$directive_name]->DefaultValue;
@@ -355,10 +356,19 @@ class BaculaConfigDirectives extends DirectiveListTemplate {
                        }
                }
                $load_values = $this->getLoadValues();
+               $res_name_dir = key_exists('Name', $directives) ? $directives['Name'] : '';
+               $resource_name = $this->getResourceName();
                if ($load_values === true) {
-                       $resource_name = $this->getResourceName();
+                       if ($resource_name !== $res_name_dir) {
+                               // RENAME RESOURCE
+                               if ($this->renameResource($res_name_dir)) {
+                                       // set new resource name
+                                       $this->setResourceName($res_name_dir);
+                                       $resource_name = $res_name_dir;
+                               }
+                       }
                } else {
-                       $resource_name = array_key_exists('Name', $directives) ? $directives['Name'] : '';
+                       $resource_name = $res_name_dir;
                }
 
                $params = array(
@@ -547,6 +557,84 @@ class BaculaConfigDirectives extends DirectiveListTemplate {
                }
        }
 
+       /**
+        * Rename resource.
+        * This rename method takes into account resource dependencies
+        * and updates them as well.
+        *
+        * @param string $new_resource_name new resource name to set
+        * @return boolean true on success, false on failure
+        */
+       public function renameResource($resource_name_new) {
+               $success = true;
+               $component_type = $this->getComponentType();
+               if (empty($_SESSION[$component_type])) {
+                       return false;
+               }
+               $host = null;
+               $resource_type = $this->getResourceType();
+               $resource_name = $this->getResourceName();
+               $config = $this->getConfigData($host, array($component_type));
+               $deps = $this->getModule('data_deps')->checkDependencies(
+                       $component_type,
+                       $resource_type,
+                       $resource_name,
+                       $config
+               );
+               $this->renameResourceInConfig(
+                       $config,
+                       $deps,
+                       $resource_type,
+                       $resource_name,
+                       $resource_name_new,
+               );
+               $result = $this->getModule('api')->set(
+                       array('config', $component_type),
+                       array('config' => json_encode($config)),
+                       $host,
+                       false
+               );
+               if ($result->error != 0) {
+                       $success = false;
+                       $emsg = 'Error while renaming resource: ' . $result->output;
+                       $this->Application->getModule('logging')->log(
+                               __FUNCTION__,
+                               $emsg,
+                               Logging::CATEGORY_APPLICATION,
+                               __FILE__,
+                               __LINE__
+                       );
+               }
+               return $success;
+       }
+
+       /**
+        * Rename resource in config.
+        * Note, passing config by reference.
+        *
+        * @param array $config entire config
+        * @param string $resource_type resource type to rename
+        * @param string $resource_name resource name to rename
+        * @param string $resource_name_new new resource name to set
+        * @return none
+        */
+       private function renameResourceInConfig(&$config, $deps, $resource_type, $resource_name, $resource_name_new) {
+               for ($i = 0; $i < count($config); $i++) {
+                       foreach ($config[$i] as $rtype => $resource) {
+                               for ($j = 0; $j < count($deps); $j++) {
+                                       if ($rtype === $deps[$j]['resource_type'] && $resource->Name === $deps[$j]['resource_name']) {
+                                               // Change resource name in dependent resources
+                                               $config[$i]->{$rtype}->{$deps[$j]['directive_name']} = $resource_name_new;
+                                       }
+                               }
+                               if ($rtype === $resource_type && $resource->Name === $resource_name) {
+                                       // Change resource name
+                                       $config[$i]->{$rtype}->Name = $resource_name_new;
+                               }
+                       }
+               }
+       }
+
        /**
         * Set if remove button should be available.
         *
@@ -607,5 +695,24 @@ class BaculaConfigDirectives extends DirectiveListTemplate {
        public function onSave($param) {
                $this->raiseEvent('OnSave', $this, $param);
        }
+
+       /**
+        * Set if name field should be disabled.
+        *
+        * @return none;
+        */
+       public function setDisableRename($rename) {
+               $rename = TPropertyValue::ensureBoolean($rename);
+               $this->setViewState(self::DISABLE_RENAME, $rename);
+       }
+
+       /**
+        * Get if name field should be disabled.
+        *
+        * @return bool true if field is disabled, otherwise false
+        */
+       public function getDisableRename() {
+               return $this->getViewState(self::DISABLE_RENAME, false);
+       }
 }
 ?>
index b84f9f75663a1ea3040f6ed01c215f2f61d12710..27e378da1b0554a001d04c948e35d5b6e383ed95 100644 (file)
@@ -115,6 +115,9 @@ class DirectiveRenderer extends TItemDataRenderer {
                        $control->setGroupName($data['group_name']);
                        $control->setParentName($data['parent_name']);
                        $control->setResourceNames($this->SourceTemplateControl->getResourceNames());
+                       if ($data['directive_name'] === 'Name') {
+                               $control->setDisabled($this->SourceTemplateControl->getDisableRename());
+                       }
                } elseif (in_array($type, $this->directive_list_types)) {
                        $control->setHost($data['host']);
                        $control->setComponentType($data['component_type']);
index f439dae270f8a027c86351a2491ca6d944f5bc39..58fb8d0af146d479949a2c5272c049c1fa854433 100644 (file)
@@ -53,6 +53,7 @@ class DirectiveTemplate extends DirectiveControlTemplate implements IDirectiveFi
        const IS_DIRECTIVE_CREATED = 'IsDirectiveCreated';
        const VALIDATION_GROUP = 'ValidationGroup';
        const CSSCLASS = 'CssClass';
+       const DISABLED = 'Disabled';
 
        public $display_directive;
 
@@ -277,5 +278,14 @@ class DirectiveTemplate extends DirectiveControlTemplate implements IDirectiveFi
        public function setCssClass($cssclass) {
                $this->setViewState(self::CSSCLASS, $cssclass);
        }
+
+       public function getDisabled() {
+               return $this->getViewState(self::DISABLED);
+       }
+
+       public function setDisabled($disabled) {
+               $disabled = TPropertyValue::ensureBoolean($disabled);
+               $this->setViewState(self::DISABLED, $disabled);
+       }
 }
 ?>
index 3017647306f3657af7876c1653a0c511e2f46105..d28fccb29b15ca203a28e085e0a1a216c10aaaa5 100644 (file)
@@ -53,6 +53,9 @@ class DirectiveTextBox extends DirectiveTemplate {
                        }
                }
                $this->Directive->setText($directive_value);
+               if ($this->getDisabled()) {
+                       $this->Directive->setReadOnly(true);
+               }
                $validate = $this->getRequired();
                $this->DirectiveValidator->setVisible($validate);
                $cssclass = $this->getCssClass();