public function testDuplicateUnknownId() { $this->setExpectedException('\\Centreon\\Internal\\Exception', $this->errMsg); Service::duplicate(9999); $this->setExpectedException('\\Centreon\\Internal\\Exception', $this->errMsg); Service::duplicate(9999, 2); }
/** * Used for duplicate a host * * @param int $sourceObjectId The source host id * @param int $duplicateEntries The number entries * @return array List of new host id */ public static function duplicate($sourceObjectId, $duplicateEntries = 1) { $db = Di::getDefault()->get(static::$databaseName); $sourceParams = static::getParameters($sourceObjectId, '*'); if (false === $sourceParams) { throw new \Exception(static::OBJ_NOT_EXIST); } unset($sourceParams['host_id']); $originalName = $sourceParams['host_name']; $explodeOriginalName = explode('_', $originalName); $j = 0; if (($count = count($explodeOriginalName)) > 1 && is_numeric($explodeOriginalName[$count - 1])) { $originalName = join('_', array_slice($explodeOriginalName, 0, -1)); $j = $explodeOriginalName[$count - 1]; } $listDuplicateId = array(); for ($i = 0; $i < $duplicateEntries; $i++) { /* Search the unique name for duplicate host */ do { $j++; $unique = self::isUnique($originalName . '_' . $j); } while (false === $unique); $sourceParams['host_name'] = $originalName . '_' . $j; /* Insert the duplicate host */ $lastId = static::insert($sourceParams); if (false === is_numeric($lastId)) { throw new \Exception("The value is not numeric"); } $listDuplicateId[] = $lastId; /* Insert relation */ /* Duplicate service */ /* Get service for the source host */ $listSvc = HostServiceRelation::getTargetIdFromSourceId('service_service_id', 'host_host_id', $sourceObjectId); foreach ($listSvc as $svcId) { /* Duplicate service */ $newSvcId = Service::duplicate($svcId, 1, true); if (count($newSvcId) > 0) { /* Attach the new service to the new host */ HostServiceRelation::insert($lastId, $newSvcId[0]); } } $db->beginTransaction(); /* Duplicate macros */ $queryDupMacros = "INSERT INTO cfg_customvariables_hosts (host_macro_name, host_macro_value, is_password, host_host_id)\n SELECT host_macro_name, host_macro_value, is_password, " . $lastId . " FROM cfg_customvariables_hosts\n WHERE host_host_id = :sourceObjectId"; $stmt = $db->prepare($queryDupMacros); $stmt->bindParam(':sourceObjectId', $sourceObjectId); $stmt->execute(); /* Host template */ $queryDupTemplate = "INSERT INTO cfg_hosts_templates_relations (host_host_id, host_tpl_id, `order`)\n SELECT " . $lastId . ", host_tpl_id, `order` FROM cfg_hosts_templates_relations\n WHERE host_host_id = :sourceObjectId"; $stmt = $db->prepare($queryDupTemplate); $stmt->bindParam(':sourceObjectId', $sourceObjectId); $stmt->execute(); /* Host global tags */ $queryDupTag = "INSERT INTO cfg_tags_hosts (tag_id, resource_id)\n SELECT th.tag_id, " . $lastId . " FROM cfg_tags_hosts th, cfg_tags t\n WHERE t.user_id IS NULL AND t.tag_id = th.tag_id AND th.resource_id = :sourceObjectId"; $stmt = $db->prepare($queryDupTag); $stmt->bindParam(':sourceObjectId', $sourceObjectId); $stmt->execute(); $db->commit(); } }