From 6781e513c857d5893ae2e6698afff094a3ec69e6 Mon Sep 17 00:00:00 2001
From: fbsanchez <fborja.sanchez@artica.es>
Date: Wed, 6 Oct 2021 19:52:46 +0200
Subject: [PATCH] NCM WIP

---
 pandora_console/extras/mr/50.sql              |  1 -
 .../pandoradb_migrate_6.0_to_7.0.mysql.sql    | 21 +++++++++----------
 pandora_console/include/lib/Entity.php        | 14 +++++++++++++
 pandora_console/include/styles/ncm.css        | 10 +++++++++
 pandora_console/pandoradb.sql                 | 21 +++++++++----------
 5 files changed, 44 insertions(+), 23 deletions(-)
 create mode 100644 pandora_console/include/styles/ncm.css

diff --git a/pandora_console/extras/mr/50.sql b/pandora_console/extras/mr/50.sql
index a3b5c41ba6..69f951efd5 100644
--- a/pandora_console/extras/mr/50.sql
+++ b/pandora_console/extras/mr/50.sql
@@ -37,7 +37,6 @@ CREATE TABLE IF NOT EXISTS `tncm_template_scripts` (
     `id` serial,
     `id_template` bigint(20) unsigned NOT NULL,
     `id_script` bigint(20) unsigned NOT NULL,
-    `order` int unsigned not null default 0,
     PRIMARY KEY (`id`),
     FOREIGN KEY (`id_template`) REFERENCES `tncm_template`(`id`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`id_script`) REFERENCES `tncm_script`(`id`) ON UPDATE CASCADE ON DELETE CASCADE
diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
index 492e72996b..1b0d17c604 100644
--- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
+++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
@@ -4120,7 +4120,6 @@ CREATE TABLE IF NOT EXISTS `tncm_template_scripts` (
     `id` serial,
     `id_template` bigint(20) unsigned NOT NULL,
     `id_script` bigint(20) unsigned NOT NULL,
-    `order` int unsigned not null default 0,
     PRIMARY KEY (`id`),
     FOREIGN KEY (`id_template`) REFERENCES `tncm_template`(`id`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`id_script`) REFERENCES `tncm_script`(`id`) ON UPDATE CASCADE ON DELETE CASCADE
@@ -4131,13 +4130,13 @@ CREATE TABLE IF NOT EXISTS `tncm_template_scripts` (
 -- ----------------------------------------------------------------------
 CREATE TABLE IF NOT EXISTS `tncm_agent` (
     `id_agent` int(10) unsigned NOT NULL,
-	`vendor` text,
+    `vendor` text,
     `model` text,
     `protocol` int unsigned not null default 0,
     `cred_key` varchar(100),
-	`status` int(4) NOT NULL default 5,
-	`updated_at` bigint(20) NOT NULL default 0,
-	`config_backup_id` bigint(20) UNSIGNED DEFAULT NULL,
+    `status` int(4) NOT NULL default 5,
+    `updated_at` bigint(20) NOT NULL default 0,
+    `config_backup_id` bigint(20) UNSIGNED DEFAULT NULL,
     PRIMARY KEY (`id_agent`),
     FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`cred_key`) REFERENCES `tcredential_store`(`identifier`) ON UPDATE CASCADE ON DELETE SET NULL
@@ -4149,9 +4148,9 @@ CREATE TABLE IF NOT EXISTS `tncm_agent` (
 CREATE TABLE IF NOT EXISTS `tncm_agent_templates` (
     `id_agent` int(10) unsigned NOT NULL,
     `id_template` bigint(20) unsigned NOT NULL,
-	`status` int(4) NOT NULL default 5,
-	`updated_at` bigint(20) NOT NULL default 0,
-	
+    `status` int(4) NOT NULL default 5,
+    `updated_at` bigint(20) NOT NULL default 0,
+    `execute` int(2) UNSIGNED NOT NULL default 0,
     PRIMARY KEY (`id_agent`, `id_template`),
     FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`id_template`) REFERENCES `tncm_template`(`id`) ON UPDATE CASCADE ON DELETE CASCADE
@@ -4163,9 +4162,9 @@ CREATE TABLE IF NOT EXISTS `tncm_agent_templates` (
 CREATE TABLE IF NOT EXISTS `tncm_agent_data` (
     `id_agent` int(10) unsigned NOT NULL,
     `id_template` bigint(20) unsigned NOT NULL,
-	`data` LONGBLOB,
-	`status` int(4) NOT NULL default 5,
-	`updated_at` bigint(20) NOT NULL default 0,
+    `data` LONGBLOB,
+    `status` int(4) NOT NULL default 5,
+    `updated_at` bigint(20) NOT NULL default 0,
     PRIMARY KEY (`id_agent`, `id_template`),
     FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`id_template`) REFERENCES `tncm_template`(`id`) ON UPDATE CASCADE ON DELETE CASCADE
diff --git a/pandora_console/include/lib/Entity.php b/pandora_console/include/lib/Entity.php
index c9a8d4448d..59306db0ca 100644
--- a/pandora_console/include/lib/Entity.php
+++ b/pandora_console/include/lib/Entity.php
@@ -36,6 +36,13 @@ namespace PandoraFMS;
 abstract class Entity
 {
 
+    /**
+     * Load from DB or new one.
+     *
+     * @var boolean
+     */
+    protected $existsInDB;
+
     /**
      * Entity fields (from table).
      *
@@ -88,6 +95,7 @@ abstract class Entity
             $obj->{$k}($v);
         }
 
+        $obj->existsInDB = true;
         return $obj;
     }
 
@@ -137,6 +145,9 @@ abstract class Entity
             foreach ($data as $k => $v) {
                 $this->fields[$k] = $v;
             }
+
+            // Mark as existing object.
+            $this->existsInDB = true;
         } else {
             // Empty one.
             $data = \db_get_all_rows_sql(
@@ -149,6 +160,9 @@ abstract class Entity
             foreach ($data as $row) {
                 $this->fields[$row['Field']] = null;
             }
+
+            // Mark as virtual object.
+            $this->existsInDB = false;
         }
 
         if (\enterprise_installed() === true
diff --git a/pandora_console/include/styles/ncm.css b/pandora_console/include/styles/ncm.css
new file mode 100644
index 0000000000..332d891431
--- /dev/null
+++ b/pandora_console/include/styles/ncm.css
@@ -0,0 +1,10 @@
+span.select2.select2-container.select2-container--default {
+  max-width: 175px !important;
+  width: 175px !important;
+}
+
+.edit_discovery_input b {
+  display: flex;
+  flex-direction: row;
+  align-items: center;
+}
diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql
index 28e985d642..a8e53bd245 100644
--- a/pandora_console/pandoradb.sql
+++ b/pandora_console/pandoradb.sql
@@ -4056,7 +4056,6 @@ CREATE TABLE IF NOT EXISTS `tncm_template_scripts` (
     `id` serial,
     `id_template` bigint(20) unsigned NOT NULL,
     `id_script` bigint(20) unsigned NOT NULL,
-    `order` int unsigned not null default 0,
     PRIMARY KEY (`id`),
     FOREIGN KEY (`id_template`) REFERENCES `tncm_template`(`id`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`id_script`) REFERENCES `tncm_script`(`id`) ON UPDATE CASCADE ON DELETE CASCADE
@@ -4067,13 +4066,13 @@ CREATE TABLE IF NOT EXISTS `tncm_template_scripts` (
 -- ----------------------------------------------------------------------
 CREATE TABLE IF NOT EXISTS `tncm_agent` (
     `id_agent` int(10) unsigned NOT NULL,
-	`vendor` text,
+    `vendor` text,
     `model` text,
     `protocol` int unsigned not null default 0,
     `cred_key` varchar(100),
-	`status` int(4) NOT NULL default 5,
-	`udpated_at` bigint(20) NOT NULL default 0,
-	`config_backup_id` bigint(20) UNSIGNED DEFAULT NULL,
+    `status` int(4) NOT NULL default 5,
+    `updated_at` bigint(20) NOT NULL default 0,
+    `config_backup_id` bigint(20) UNSIGNED DEFAULT NULL,
     PRIMARY KEY (`id_agent`),
     FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`cred_key`) REFERENCES `tcredential_store`(`identifier`) ON UPDATE CASCADE ON DELETE SET NULL
@@ -4085,9 +4084,9 @@ CREATE TABLE IF NOT EXISTS `tncm_agent` (
 CREATE TABLE IF NOT EXISTS `tncm_agent_templates` (
     `id_agent` int(10) unsigned NOT NULL,
     `id_template` bigint(20) unsigned NOT NULL,
-	`status` int(4) NOT NULL default 5,
-	`udpated_at` bigint(20) NOT NULL default 0,
-	`execute` int(2) UNSIGNED NOT NULL default 0,
+    `status` int(4) NOT NULL default 5,
+    `updated_at` bigint(20) NOT NULL default 0,
+    `execute` int(2) UNSIGNED NOT NULL default 0,
     PRIMARY KEY (`id_agent`, `id_template`),
     FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`id_template`) REFERENCES `tncm_template`(`id`) ON UPDATE CASCADE ON DELETE CASCADE
@@ -4099,9 +4098,9 @@ CREATE TABLE IF NOT EXISTS `tncm_agent_templates` (
 CREATE TABLE IF NOT EXISTS `tncm_agent_data` (
     `id_agent` int(10) unsigned NOT NULL,
     `id_template` bigint(20) unsigned NOT NULL,
-	`data` LONGBLOB,
-	`status` int(4) NOT NULL default 5,
-	`udpated_at` bigint(20) NOT NULL default 0,
+    `data` LONGBLOB,
+    `status` int(4) NOT NULL default 5,
+    `updated_at` bigint(20) NOT NULL default 0,
     PRIMARY KEY (`id_agent`, `id_template`),
     FOREIGN KEY (`id_agent`) REFERENCES `tagente`(`id_agente`) ON UPDATE CASCADE ON DELETE CASCADE,
     FOREIGN KEY (`id_template`) REFERENCES `tncm_template`(`id`) ON UPDATE CASCADE ON DELETE CASCADE