]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add new size directive control
authorMarcin Haba <marcin.haba@bacula.pl>
Wed, 24 Oct 2018 06:04:04 +0000 (08:04 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Wed, 24 Oct 2018 06:04:04 +0000 (08:04 +0200)
It also fixes a bug with saving size directives reported by Catin King.

gui/baculum/protected/Web/Data/data_desc.json
gui/baculum/protected/Web/JavaScript/misc.js
gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php
gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.tpl
gui/baculum/protected/Web/Portlets/DirectiveSize.php [new file with mode: 0644]
gui/baculum/protected/Web/Portlets/DirectiveSize.tpl [new file with mode: 0644]
gui/baculum/protected/Web/Portlets/DirectiveTimePeriod.tpl

index afbc5276b911c43cc6fe70118bb3a62a84d00629..6ee4ff38f5d9f38217181ac190a7ba1e8def97c5 100644 (file)
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "ReRunFailedLevels": {
                                "Required": false,
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "CatalogFiles": {
                                "Required": false,
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MigrationLowBytes": {
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "NextPool": {
                                "Required": false,
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "ReRunFailedLevels": {
                                "Required": false,
                                "Required": false,
                                "ValueType": "size32",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MaximumBlockSize": {
                                "Required": false,
                                "ValueType": "maxblocksize",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "PaddingSize": {
                                "Required": false,
                                "ValueType": "size32",
                                "DefaultValue": 4096,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "FileAlignment": {
                                "Required": false,
                                "ValueType": "size32",
                                "DefaultValue": 4096,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MaximumVolumeSize": {
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MaximumFileSize": {
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 1000000000,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "VolumeCapacity": {
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MinimumFeeSpace": {
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 5000000,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MaximumConcurrentJobs": {
                                "Required": false,
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MaximumJobSpoolSize": {
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "DriveIndex": {
                                "Required": false,
                                "Required": false,
                                "ValueType": "size64",
                                "DefaultValue": 0,
-                               "FieldType": "TextBox"
+                               "FieldType": "Size"
                        },
                        "MountPoint": {
                                "Required": false,
index fdcfd57c70dc3425dbc6d5e97d2bc0392989993b..85ac758552bbb3252f45f686fc928573d4bbe8ce 100644 (file)
@@ -1,16 +1,28 @@
 var Units = {
-       formats: [
-               {format: 'second', value: 1},
-               {format: 'minute', value: 60},
-               {format: 'hour', value: 60},
-               {format: 'day', value: 24}
-       ],
-       units: ['K', 'M', 'G', 'T', 'P'],
+       units: {
+               size: [
+                       {short: '',  long: 'byte', value: 1},
+                       {short: 'K', long: 'kilobyte', value: 1000},
+                       {short: 'M', long: 'megabyte', value: 1000},
+                       {short: 'G', long: 'gigabyte', value: 1000},
+                       {short: 'T', long: 'terabyte', value: 1000},
+                       {short: 'P', long: 'petabyte', value: 1000}
+               ],
+               time: [
+                       {long: 'second', value: 1},
+                       {long: 'minute', value: 60},
+                       {long: 'hour', value: 60},
+                       {long: 'day', value: 24}
+               ]
+       },
        get_decimal_size: function(size) {
                var dec_size;
                var size_unit = 'B';
-               var units = Units.units.slice(0);
-
+               var units = [];
+               for (var u in Units.units.size) {
+                       units.push(Units.units.size[u].short);
+               }
+               units.shift(); // remove "byte" unit
                if (size === null) {
                        size = 0;
                }
@@ -35,15 +47,32 @@ var Units = {
                }
                return dec_size;
        },
+       format_size: function(size_bytes, format) {
+               var reminder;
+               var f = this.units.size[0].long;
+               for (var i = 0; i < this.units.size.length; i++) {
+                       if (this.units.size[i].long != format && size_bytes) {
+                               reminder = size_bytes % this.units.size[i].value
+                               if (reminder === 0) {
+                                       size_bytes /= this.units.size[i].value;
+                                       f = this.units.size[i].long;
+                                       continue;
+                               }
+                               break;
+                       }
+               }
+               var ret = {value: size_bytes, format: f};
+               return ret;
+       },
        format_time_period: function(time_seconds, format) {
                var reminder;
-               var f;
-               for (var i = 0; i < this.formats.length; i++) {
-                       if (this.formats[i].format != format) {
-                               reminder = time_seconds % this.formats[i].value;
+               var f = this.units.time[0].long;
+               for (var i = 0; i < this.units.time.length; i++) {
+                       if (this.units.time[i].long != format && time_seconds) {
+                               reminder = time_seconds % this.units.time[i].value;
                                if (reminder === 0) {
-                                       time_seconds /= this.formats[i].value;
-                                       f = this.formats[i].format;
+                                       time_seconds /= this.units.time[i].value;
+                                       f = this.units.time[i].long;
                                        continue;
                                }
                                break;
index 5eeacb22ba77533a84daf813264df4f9c9575ee1..3a89fdb641f7ad9c2cac2cfd7c9c1002163d6516 100644 (file)
@@ -30,6 +30,7 @@ Prado::using('Application.Web.Portlets.DirectiveBoolean');
 Prado::using('Application.Web.Portlets.DirectiveComboBox');
 Prado::using('Application.Web.Portlets.DirectiveInteger');
 Prado::using('Application.Web.Portlets.DirectiveListBox');
+Prado::using('Application.Web.Portlets.DirectiveSize');
 Prado::using('Application.Web.Portlets.DirectiveText');
 Prado::using('Application.Web.Portlets.DirectiveTimePeriod');
 Prado::using('Application.Web.Portlets.DirectiveRunscript');
@@ -52,6 +53,7 @@ class BaculaConfigDirectives extends DirectiveListTemplate {
                'DirectiveInteger',
                'DirectiveListBox',
                'DirectiveText',
+               'DirectiveSize',
                'DirectiveTimePeriod'
        );
 
index 38fdb2f6481ae1f1b76fb2574a4fbceb1b66151d..1751c15642b3a779327a3f623c3d7372e70c7764 100644 (file)
                                                <com:Application.Web.Portlets.DirectiveTimePeriod />
                                        </prop:TrueTemplate>
                                </com:Application.Web.Portlets.BConditional>
+                               <com:Application.Web.Portlets.BConditional BCondition="<%#($this->Data['field_type'] === 'Size')%>">
+                                       <prop:TrueTemplate>
+                                               <com:Application.Web.Portlets.DirectiveSize />
+                                       </prop:TrueTemplate>
+                               </com:Application.Web.Portlets.BConditional>
                                <com:Application.Web.Portlets.BConditional BCondition="<%#($this->Data['field_type'] === 'ComboBox')%>">
                                        <prop:TrueTemplate>
                                                <com:Application.Web.Portlets.DirectiveComboBox />
diff --git a/gui/baculum/protected/Web/Portlets/DirectiveSize.php b/gui/baculum/protected/Web/Portlets/DirectiveSize.php
new file mode 100644 (file)
index 0000000..6605335
--- /dev/null
@@ -0,0 +1,133 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2018 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+Prado::using('System.Web.UI.ActiveControls.TActiveLabel');
+Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
+Prado::using('System.Web.UI.ActiveControls.TActiveDropDownList');
+Prado::using('Application.Web.Portlets.DirectiveTemplate');
+
+class DirectiveSize extends DirectiveTemplate {
+
+       const SIZE_FORMAT = 'SizeFormat';
+       const DEFAULT_SIZE_FORMAT = 'byte';
+
+       private $size_formats = array(
+               array('format' => 'byte', 'value' => 1, 'label' => 'Bytes'),
+               array('format' => 'kilobyte', 'value' => 1000, 'label' => 'Kilobytes'),
+               array('format' => 'megabyte', 'value' => 1000, 'label' => 'Megabytes'),
+               array('format' => 'gigabyte', 'value' => 1000, 'label' => 'Gigabytes'),
+               array('format' => 'terabyte', 'value' => 1000, 'label' => 'Terabytes')
+       );
+
+       public function getValue() {
+               $value = $this->Directive->Text;
+               if (is_numeric($value)) {
+                       settype($value, 'integer');
+                       $size_format = $this->SizeFormat->SelectedValue;
+                       $value = $this->getValueBytes($value, $size_format);
+               } else {
+                       $value = null;
+               }
+               return $value;
+       }
+
+       public function getSizeFormat() {
+               return $this->getViewState(self::SIZE_FORMAT, self::DEFAULT_SIZE_FORMAT);
+       }
+
+       public function setSizeFormat($format) {
+               $this->setViewState(self::SIZE_FORMAT, $format);
+       }
+
+       public function getSizeFormats() {
+               $size_formats = array();
+               for ($i = 0; $i < count($this->size_formats); $i++) {
+                       $format = array(
+                               'label' => Prado::localize($this->size_formats[$i]['label']),
+                               'format' => $this->size_formats[$i]['format']
+                       );
+                       array_push($size_formats, $format);
+               }
+               return $size_formats;
+       }
+
+       public function createDirective() {
+               $size_format = $this->getSizeFormat();
+               $directive_value = $this->getDirectiveValue();
+               $default_value = $this->getDefaultValue();
+               if ($this->getInConfig() === false) {
+                       if ($default_value !== 0) {
+                               $directive_value = $default_value;
+                       } else {
+                               $directive_value = 0;
+                       }
+               }
+               $formatted_value = $this->formatSize($directive_value, $size_format);
+               $this->Directive->Text = $formatted_value['value'];
+               $this->SizeFormat->DataSource = $this->size_formats;
+               $this->SizeFormat->SelectedValue = $formatted_value['format'];
+               $this->SizeFormat->dataBind();
+               $this->Label->Text = $this->getLabel();
+               $validate = $this->getRequired();
+               $this->DirectiveValidator->setVisible($validate);
+       }
+
+       /**
+        * Convert original size in bytes into given size format.
+        *
+        * Note, if there is not possible to convert size value into given format
+        * then there will be returned value converted by using as close format as possible.
+        * Example:
+        *  size_value: 121000
+        *  given format: byte
+        *  returned value: 121
+        *  returned format: kilobyte
+        */
+       private function formatSize($size_bytes, $format) {
+               $value = $size_bytes;
+               for ($i = 0; $i < count($this->size_formats); $i++) {
+                       if ($this->size_formats[$i]['format'] != $format) {
+                               $remainder = $value % $this->size_formats[$i]['value'];
+                               if ($remainder === 0) {
+                                       $value /= $this->size_formats[$i]['value'];
+                                       $format = $this->size_formats[$i]['format'];
+                                       continue;
+                               }
+                               break;
+                       }
+                       break;
+               }
+               $result = array('value' => $value, 'format' => $format);
+               return $result;
+       }
+
+       private function getValueBytes($value, $size_format) {
+               for ($i = 0; $i < count($this->size_formats); $i++) {
+                       $value *= $this->size_formats[$i]['value'];
+                       if ($this->size_formats[$i]['format'] === $size_format) {
+                               break;
+                       }
+               }
+               return $value;
+       }
+}
+?>
diff --git a/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl b/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl
new file mode 100644 (file)
index 0000000..4ff363e
--- /dev/null
@@ -0,0 +1,32 @@
+<div class="w3-row w3-margin-bottom<%=!$this->display_directive ? ' hide' : '';%>">
+       <div class="w3-col w3-quarter"><com:TActiveLabel ID="Label" ActiveControl.EnableUpdate="false" Visible="<%=$this->display_directive%>" />:</div>
+       <div class="w3-col w3-threequarter directive_value">
+               <com:TActiveTextBox ID="Directive"
+                       OnTextChanged="saveValue"
+                       CssClass="w3-input w3-border w3-third"
+                       Visible="<%=$this->display_directive%>"
+                       ActiveControl.EnableUpdate="false"
+               />
+               <com:TActiveDropDownList
+                       ID="SizeFormat"
+                       CssClass="w3-select w3-border w3-quarter"
+                       DataTextField="label"
+                       DataValueField="format"
+                       Visible="<%=$this->display_directive%>"
+                       ActiveControl.EnableUpdate="false"
+                       AutoPostBack="false"
+                       OnSelectedIndexChanged="saveValue"
+               /> <%=$this->getRequired() ? '&nbsp;<i class="fa fa-asterisk w3-text-red" style="line-height: 40px"></i>' : ''%>
+               <i class="fa fa-undo reset_btn" onclick="var fsize = Units.format_size(parseInt('<%=$this->getDefaultValue()%>', 10), 'byte'); document.getElementById('<%=$this->Directive->ClientID%>').value = fsize.value; document.getElementById('<%=$this->SizeFormat->ClientID%>').value = fsize.format;" alt="<%[ Reset to default value ]%>" title="<%[ Reset to default value ]%>"></i>
+               <i class="fa fa-trash-alt remove_btn" onclick="document.getElementById('<%=$this->Directive->ClientID%>').value = '';" alt="<%[ Remove directive ]%>" title="<%[ Remove directive ]%>"></i>
+               <com:TRequiredFieldValidator
+                       ID="DirectiveValidator"
+                       ValidationGroup="Directive"
+                       Display="Dynamic"
+                       ControlToValidate="Directive"
+                       FocusOnError="true"
+                       Text="Field required."
+                       Enabled="<%=$this->getRequired() && $this->getShow()%>"
+               />
+       </div>
+</div>
index 251fc2642d8a3c492b30c5cd3f45846ab9bc3ecf..135244c049567e80227b78f6f654d6710dd975f1 100644 (file)
@@ -14,6 +14,8 @@
                        DataValueField="format"
                        Visible="<%=$this->display_directive%>"
                        ActiveControl.EnableUpdate="false"
+                       AutoPostBack="false"
+                       OnSelectedIndexChanged="saveValue"
                /> <%=$this->getRequired() ? '&nbsp;<i class="fa fa-asterisk w3-text-red" style="line-height: 40px"></i>' : ''%>
                <i class="fa fa-undo reset_btn" onclick="var ftime = Units.format_time_period(parseInt('<%=$this->getDefaultValue()%>', 10), 'second'); document.getElementById('<%=$this->Directive->ClientID%>').value = ftime.value; document.getElementById('<%=$this->TimeFormat->ClientID%>').value = ftime.format;" alt="<%[ Reset to default value ]%>" title="<%[ Reset to default value ]%>"></i>
                <i class="fa fa-trash-alt remove_btn" onclick="document.getElementById('<%=$this->Directive->ClientID%>').value = '';" alt="<%[ Remove directive ]%>" title="<%[ Remove directive ]%>"></i>