protected function addRelatedObjects(array $options, array $result) { $result = parent::addRelatedObjects($options, $result); $hostids = array_keys($result); // adding inventories if ($options['selectInventory'] !== null) { $relationMap = $this->createRelationMap($result, 'hostid', 'hostid'); $inventory = API::getApiService()->select('host_inventory', ['output' => $options['selectInventory'], 'filter' => ['hostid' => $hostids]]); $result = $relationMap->mapOne($result, zbx_toHash($inventory, 'hostid'), 'inventory'); } // adding hostinterfaces if ($options['selectInterfaces'] !== null) { if ($options['selectInterfaces'] != API_OUTPUT_COUNT) { $interfaces = API::HostInterface()->get(['output' => $this->outputExtend($options['selectInterfaces'], ['hostid', 'interfaceid']), 'hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true]); // we need to order interfaces for proper linkage and viewing order_result($interfaces, 'interfaceid', ZBX_SORT_UP); $relationMap = $this->createRelationMap($interfaces, 'hostid', 'interfaceid'); $interfaces = $this->unsetExtraFields($interfaces, ['hostid', 'interfaceid'], $options['selectInterfaces']); $result = $relationMap->mapMany($result, $interfaces, 'interfaces', $options['limitSelects']); } else { $interfaces = API::HostInterface()->get(['hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true]); $interfaces = zbx_toHash($interfaces, 'hostid'); foreach ($result as $hostid => $host) { $result[$hostid]['interfaces'] = isset($interfaces[$hostid]) ? $interfaces[$hostid]['rowscount'] : 0; } } } // adding screens if ($options['selectScreens'] !== null) { if ($options['selectScreens'] != API_OUTPUT_COUNT) { $screens = API::TemplateScreen()->get(['output' => $this->outputExtend($options['selectScreens'], ['hostid']), 'hostids' => $hostids, 'nopermissions' => true]); if (!is_null($options['limitSelects'])) { order_result($screens, 'name'); } // inherited screens do not have a unique screenid, so we're building a map using array keys $relationMap = new CRelationMap(); foreach ($screens as $key => $screen) { $relationMap->addRelation($screen['hostid'], $key); } $screens = $this->unsetExtraFields($screens, ['hostid'], $options['selectScreens']); $result = $relationMap->mapMany($result, $screens, 'screens', $options['limitSelects']); } else { $screens = API::TemplateScreen()->get(['hostids' => $hostids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true]); $screens = zbx_toHash($screens, 'hostid'); foreach ($result as $hostid => $host) { $result[$hostid]['screens'] = isset($screens[$hostid]) ? $screens[$hostid]['rowscount'] : 0; } } } // adding discovery rule if ($options['selectDiscoveryRule'] !== null && $options['selectDiscoveryRule'] != API_OUTPUT_COUNT) { // discovered items $discoveryRules = DBFetchArray(DBselect('SELECT hd.hostid,hd2.parent_itemid' . ' FROM host_discovery hd,host_discovery hd2' . ' WHERE ' . dbConditionInt('hd.hostid', $hostids) . ' AND hd.parent_hostid=hd2.hostid')); $relationMap = $this->createRelationMap($discoveryRules, 'hostid', 'parent_itemid'); $discoveryRules = API::DiscoveryRule()->get(['output' => $options['selectDiscoveryRule'], 'itemids' => $relationMap->getRelatedIds(), 'preservekeys' => true]); $result = $relationMap->mapOne($result, $discoveryRules, 'discoveryRule'); } // adding host discovery if ($options['selectHostDiscovery'] !== null) { $hostDiscoveries = API::getApiService()->select('host_discovery', ['output' => $this->outputExtend($options['selectHostDiscovery'], ['hostid']), 'filter' => ['hostid' => $hostids], 'preservekeys' => true]); $relationMap = $this->createRelationMap($hostDiscoveries, 'hostid', 'hostid'); $hostDiscoveries = $this->unsetExtraFields($hostDiscoveries, ['hostid'], $options['selectHostDiscovery']); $result = $relationMap->mapOne($result, $hostDiscoveries, 'hostDiscovery'); } return $result; }
/** * Additionally allows to unlink templates from hosts and other templates. * * Checks write permissions for templates. * * Additional supported $data parameters are: * - hostids - an array of host or template IDs to unlink the given templates from * * @param array $data * * @return array */ public function massRemove(array $data) { $templateids = zbx_toArray($data['templateids']); // check permissions if (!$this->isWritable($templateids)) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.')); } if (isset($data['hostids'])) { API::Template()->unlink($templateids, zbx_toArray($data['hostids'])); } $data['hostids'] = array(); return parent::massRemove($data); }
protected function applyQueryNodeOptions($tableName, $tableAlias, array $options, array $sqlParts) { // only apply the node option if no specific ids are given if ($options['hostids'] === null && $options['proxyids'] === null && $options['templateids'] === null && $options['interfaceids'] === null && $options['itemids'] === null && $options['triggerids'] === null && $options['maintenanceids'] === null && $options['graphids'] === null && $options['applicationids'] === null && $options['dhostids'] === null && $options['dserviceids'] === null && $options['httptestids'] === null && $options['groupids'] === null) { $sqlParts = parent::applyQueryNodeOptions($tableName, $tableAlias, $options, $sqlParts); } return $sqlParts; }
protected function addRelatedObjects(array $options, array $result) { $result = parent::addRelatedObjects($options, $result); $templateids = array_keys($result); // Adding Templates if ($options['selectTemplates'] !== null) { if ($options['selectTemplates'] != API_OUTPUT_COUNT) { $relationMap = $this->createRelationMap($result, 'templateid', 'hostid', 'hosts_templates'); $templates = API::Template()->get(array('output' => $options['selectTemplates'], 'templateids' => $relationMap->getRelatedIds(), 'preservekeys' => true)); if (!is_null($options['limitSelects'])) { order_result($templates, 'host'); } $result = $relationMap->mapMany($result, $templates, 'templates', $options['limitSelects']); } else { $templates = API::Template()->get(array('parentTemplateids' => $templateids, 'countOutput' => true, 'groupCount' => true)); $templates = zbx_toHash($templates, 'templateid'); foreach ($result as $templateid => $template) { if (isset($templates[$templateid])) { $result[$templateid]['templates'] = $templates[$templateid]['rowscount']; } else { $result[$templateid]['templates'] = 0; } } } } // Adding Hosts if ($options['selectHosts'] !== null) { if ($options['selectHosts'] != API_OUTPUT_COUNT) { $relationMap = $this->createRelationMap($result, 'templateid', 'hostid', 'hosts_templates'); $hosts = API::Host()->get(array('output' => $options['selectHosts'], 'hostids' => $relationMap->getRelatedIds(), 'preservekeys' => true)); if (!is_null($options['limitSelects'])) { order_result($hosts, 'host'); } $result = $relationMap->mapMany($result, $hosts, 'hosts', $options['limitSelects']); } else { $hosts = API::Host()->get(array('templateids' => $templateids, 'countOutput' => true, 'groupCount' => true)); $hosts = zbx_toHash($hosts, 'templateid'); foreach ($result as $templateid => $template) { if (isset($hosts[$templateid])) { $result[$templateid]['hosts'] = $hosts[$templateid]['rowscount']; } else { $result[$templateid]['hosts'] = 0; } } } } // Adding screens if ($options['selectScreens'] !== null) { if ($options['selectScreens'] != API_OUTPUT_COUNT) { $screens = API::TemplateScreen()->get(array('output' => $this->outputExtend($options['selectScreens'], array('templateid')), 'templateids' => $templateids, 'nopermissions' => true)); if (!is_null($options['limitSelects'])) { order_result($screens, 'name'); } // preservekeys is not supported by templatescreen.get, so we're building a map using array keys $relationMap = new CRelationMap(); foreach ($screens as $key => $screen) { $relationMap->addRelation($screen['templateid'], $key); } $screens = $this->unsetExtraFields($screens, array('templateid'), $options['selectScreens']); $result = $relationMap->mapMany($result, $screens, 'screens', $options['limitSelects']); } else { $screens = API::TemplateScreen()->get(array('templateids' => $templateids, 'nopermissions' => true, 'countOutput' => true, 'groupCount' => true)); $screens = zbx_toHash($screens, 'templateid'); foreach ($result as $templateid => $template) { if (isset($screens[$templateid])) { $result[$templateid]['screens'] = $screens[$templateid]['rowscount']; } else { $result[$templateid]['screens'] = 0; } } } } return $result; }