diff --git a/pandora_console/include/javascript/pandora_dashboards.js b/pandora_console/include/javascript/pandora_dashboards.js index 5b36832b29..0d76c1a618 100644 --- a/pandora_console/include/javascript/pandora_dashboards.js +++ b/pandora_console/include/javascript/pandora_dashboards.js @@ -269,6 +269,10 @@ function initialiceLayout(data) { $("#configure-widget-" + id).click(function() { getSizeModalConfiguration(id, widgetId); }); + + $("#copy-widget-" + id).click(function() { + duplicateWidget(id, widgetId); + }); }, error: function(error) { console.error(error); @@ -299,6 +303,31 @@ function initialiceLayout(data) { return false; } + function duplicateWidget(original_cellId, original_widgetId) { + let duplicate_cellId = insertCellLayoutForDuplicate(); + + $.ajax({ + method: "post", + url: data.url, + data: { + page: data.page, + method: "duplicateWidget", + dashboardId: data.dashboardId, + widgetId: original_widgetId, + cellId: original_cellId, + duplicateCellId: duplicate_cellId + }, + dataType: "json", + success: function(success) { + console.log(success); + }, + error: function(error) { + console.log(error); + return []; + } + }); + } + function saveLayout() { var items = $(".grid-stack > .grid-stack-item:visible") .map(function(i, el) { @@ -394,6 +423,37 @@ function initialiceLayout(data) { }); } + function insertCellLayoutForDuplicate() { + let duplicateCellId = 0; + $.ajax({ + async: false, + method: "post", + url: data.url, + data: { + page: data.page, + method: "insertCellLayout", + dashboardId: data.dashboardId, + auth_class: data.auth.class, + auth_hash: data.auth.hash, + id_user: data.auth.user + }, + dataType: "json", + success: function(data) { + // By default x and y = 0 + // width and height = 4 + // position auto = true. + if (data.cellId !== 0) { + addCell(data.cellId, 0, 0, 4, 4, true, 0, 2000, 0, 2000, 0, true); + duplicateCellId = data.cellId; + } + }, + error: function(error) { + console.error(error); + } + }); + return duplicateCellId; + } + function configurationWidget(cellId, widgetId, size) { load_modal({ target: $("#modal-config-widget"), @@ -722,6 +782,10 @@ function initialiceLayout(data) { getSizeModalConfiguration(cellId, widgetId); }); + $("#copy-widget-" + cellId).click(function() { + duplicateWidget(cellId, widgetId); + }); + saveLayout(); } }); diff --git a/pandora_console/include/lib/Dashboard/Manager.php b/pandora_console/include/lib/Dashboard/Manager.php index 9d2c0d4356..40b6d2d1a0 100644 --- a/pandora_console/include/lib/Dashboard/Manager.php +++ b/pandora_console/include/lib/Dashboard/Manager.php @@ -160,6 +160,13 @@ class Manager implements PublicLogin */ private $publicLink; + /** + * Duplicate Id Cell. + * + * @var integer + */ + private $duplicateCellId; + /** * Allowed methods to be called using AJAX request. * @@ -181,6 +188,7 @@ class Manager implements PublicLogin 'formSlides', 'callWidgetMethod', 'getSizeModalConfiguration', + 'duplicateWidget', ]; @@ -283,6 +291,7 @@ class Manager implements PublicLogin $this->widgetId = (int) $extradata['widgetId']; } else { $this->cellId = (int) \get_parameter('cellId', []); + $this->duplicateCellId = (int) \get_parameter('duplicateCellId', []); $this->offset = (int) \get_parameter('offset', 0); $this->dashboardId = (int) \get_parameter('dashboardId', 0); @@ -597,6 +606,42 @@ class Manager implements PublicLogin } + /** + * Duplicate widget. + * + * @return integer + */ + public function duplicateWidget():int + { + $original_widget = []; + + $original_cellId = $this->cellId; + foreach ($this->cells as $cells) { + if ($cells['id'] == $original_cellId) { + $original_widget['id_widget'] = $cells['id_widget']; + $original_widget['options'] = $cells['options']; + break; + } + } + + $options = json_decode($original_widget['options'], true); + $options['title'] = __('Copy of %s', $options['title']); + $options_json = json_encode($options); + + $values = [ + 'options' => $options_json, + 'id_widget' => $original_widget['id_widget'], + ]; + $res = \db_process_sql_update( + 'twidget_dashboard', + $values, + ['id' => $this->duplicateCellId] + ); + return $res; + + } + + /** * Copy Dashboard and asociate widgets. * diff --git a/pandora_console/views/dashboard/cell.php b/pandora_console/views/dashboard/cell.php index ae8545e2ce..919ce9e07e 100644 --- a/pandora_console/views/dashboard/cell.php +++ b/pandora_console/views/dashboard/cell.php @@ -47,6 +47,18 @@ $output .= '
'; if ($manageDashboards !== 0 || $writeDashboards !== 0) { if ((int) $cellData['id_widget'] !== 0) { + $output .= ''; + $output .= html_print_image( + 'images/copy.svg', + true, + [ + 'width' => '16px', + 'title' => __('Copy widget'), + 'class' => 'invert_filter', + ] + ); + $output .= ' '; + $output .= ''; $output .= html_print_image( 'images/configuration@svg.svg',