diff --git a/www/class/centreonCustomView.class.php b/www/class/centreonCustomView.class.php index 7a20e358bbe..3be4868dea1 100644 --- a/www/class/centreonCustomView.class.php +++ b/www/class/centreonCustomView.class.php @@ -594,7 +594,8 @@ public function syncCustomView($customViewId, $userId = null) public function loadCustomView($params) { $isLocked = 1; - $query = 'SELECT custom_view_id, locked ' . + $update = false; + $query = 'SELECT custom_view_id, locked, user_id ' . 'FROM custom_view_user_relation ' . 'WHERE custom_view_id = :viewLoad ' . 'AND ' . @@ -616,14 +617,25 @@ public function loadCustomView($params) if ($row['locked'] == "0") { $isLocked = $row['locked']; } + if (!is_null($row['user_id']) && $row['user_id'] > 0){ + $update = true; + } } - $query = 'INSERT INTO custom_view_user_relation (custom_view_id,user_id,is_owner,locked,is_share) ' . - 'VALUES (:viewLoad, :userId, 0, :isLocked, 1)'; - $stmt = $this->db->prepare($query); - $stmt->bindParam(':viewLoad', $params['viewLoad'], PDO::PARAM_INT); - $stmt->bindParam(':userId', $this->userId, PDO::PARAM_INT); - $stmt->bindParam(':isLocked', $isLocked, PDO::PARAM_INT); + if ($update) { + $query = 'UPDATE custom_view_user_relation SET is_consumed=1 WHERE ' . + ' custom_view_id = :viewLoad AND user_id = :userId'; + $stmt = $this->db->prepare($query); + $stmt->bindParam(':viewLoad', $params['viewLoad'], PDO::PARAM_INT); + $stmt->bindParam(':userId', $this->userId, PDO::PARAM_INT); + } else { + $query = 'INSERT INTO custom_view_user_relation (custom_view_id,user_id,is_owner,locked,is_share) ' . + 'VALUES (:viewLoad, :userId, 0, :isLocked, 1)'; + $stmt = $this->db->prepare($query); + $stmt->bindParam(':viewLoad', $params['viewLoad'], PDO::PARAM_INT); + $stmt->bindParam(':userId', $this->userId, PDO::PARAM_INT); + $stmt->bindParam(':isLocked', $isLocked, PDO::PARAM_INT); + } $dbResult = $stmt->execute(); if (!$dbResult) { throw new \Exception("An error occured"); diff --git a/www/install/createTables.sql b/www/install/createTables.sql index b9cdf068f13..47839136ec8 100644 --- a/www/install/createTables.sql +++ b/www/install/createTables.sql @@ -965,7 +965,8 @@ CREATE TABLE `custom_view_user_relation` ( `is_owner` tinyint(6) DEFAULT '0', `is_share` tinyint(6) DEFAULT '0', `is_consumed` int(1) NOT NULL DEFAULT 1, - UNIQUE KEY `view_user_unique_index` (`custom_view_id`,`user_id`,`usergroup_id`), + UNIQUE KEY `view_user_unique_index` (`custom_view_id`,`user_id`), + UNIQUE KEY `view_usergroup_unique_index` (`custom_view_id`,`usergroup_id`), KEY `fk_custom_views_user_id` (`user_id`), KEY `fk_custom_views_usergroup_id` (`usergroup_id`), CONSTRAINT `fk_custom_views_user_id` FOREIGN KEY (`user_id`) REFERENCES `contact` (`contact_id`) ON DELETE CASCADE, diff --git a/www/install/sql/centreon/Update-DB-2.8.10_to_2.9.0.sql b/www/install/sql/centreon/Update-DB-2.8.10_to_2.9.0.sql index 30c44cebc9f..fde40c2de02 100644 --- a/www/install/sql/centreon/Update-DB-2.8.10_to_2.9.0.sql +++ b/www/install/sql/centreon/Update-DB-2.8.10_to_2.9.0.sql @@ -13,4 +13,27 @@ WHERE `fieldname` = 'negociation'; UPDATE `cfg_centreonbroker_info` SET `config_key` = 'negotiation' -WHERE `config_key` = 'negociation'; \ No newline at end of file +WHERE `config_key` = 'negociation'; + +-- Delete duplicate entries in custom_view_user_relation +ALTER TABLE `custom_view_user_relation` + DROP FOREIGN KEY `fk_custom_views_usergroup_id`, + DROP FOREIGN KEY `fk_custom_views_user_id`, + DROP FOREIGN KEY `fk_custom_view_user_id`, + DROP INDEX `view_user_unique_index`; +ALTER IGNORE TABLE `custom_view_user_relation` + ADD UNIQUE INDEX `view_user_unique_index` (`custom_view_id`, `user_id`), + ADD UNIQUE INDEX `view_usergroup_unique_index` (`custom_view_id`, `usergroup_id`); +ALTER TABLE `custom_view_user_relation` + ADD CONSTRAINT `fk_custom_views_usergroup_id` + FOREIGN KEY (`usergroup_id`) + REFERENCES `centreon`.`contactgroup` (`cg_id`) + ON DELETE CASCADE, + ADD CONSTRAINT `fk_custom_views_user_id` + FOREIGN KEY (`user_id`) + REFERENCES `centreon`.`contact` (`contact_id`) + ON DELETE CASCADE, + ADD CONSTRAINT `fk_custom_view_user_id` + FOREIGN KEY (`custom_view_id`) + REFERENCES `centreon`.`custom_views` (`custom_view_id`) + ON DELETE CASCADE; \ No newline at end of file