abstract class CHostBase extends CApiService {
public const ACCESS_RULES = [
'get' => ['min_user_type' => USER_TYPE_ZABBIX_USER],
'create' => ['min_user_type' => USER_TYPE_ZABBIX_ADMIN],
'update' => ['min_user_type' => USER_TYPE_ZABBIX_ADMIN],
'delete' => ['min_user_type' => USER_TYPE_ZABBIX_ADMIN]
protected $tableName = 'hosts';
protected $tableAlias = 'h';
protected function checkTemplates(array &$hosts, ?array &$db_hosts = null, ?string $path = null,
?array $template_indexes = null, ?string $path_clear = null, ?array $template_clear_indexes = null): void {
$id_field_name = $this instanceof CTemplate ? 'templateid' : 'hostid';
$ins_template_indexes = [];
$clear_template_indexes = [];
foreach ($hosts as $i1 => &$host) {
if (array_key_exists('templates', $host) && array_key_exists('templates_clear', $host)) {
foreach ($host['templates_clear'] as $i2_clear => $template_clear) {
foreach ($host['templates'] as $i2 => $template) {
if (bccomp($template['templateid'], $template_clear['templateid']) != 0) {
$path_clear = '/'.($i1 + 1).'/templates_clear/'.($i2_clear + 1);
$path = '/'.($i1 + 1).'/templates/'.($i2 + 1);
$path_clear .= '/'.($template_clear_indexes[$template['templateid']] + 1);
$path .= '/'.($template_indexes[$template['templateid']] + 1);
$error = _s('cannot be specified the value of parameter "%1$s"', $path.'/templateid');
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Invalid parameter "%1$s": %2$s.', $path_clear.'/templateid', $error)
if (array_key_exists('templates', $host)) {
$db_templates = $db_hosts !== null
? array_column($db_hosts[$host[$id_field_name]]['templates'], null, 'templateid')
foreach ($host['templates'] as $i2 => $template) {
if (array_key_exists($template['templateid'], $db_templates)) {
if ($db_templates[$template['templateid']]['link_type'] != TEMPLATE_LINK_MANUAL) {
unset($host['templates'][$i2]);
$db_hosttemplateid = $db_templates[$template['templateid']]['hosttemplateid'];
unset($db_hosts[$host[$id_field_name]]['templates'][$db_hosttemplateid]);
unset($db_templates[$template['templateid']]);
$ins_template_indexes[$template['templateid']][$i1] = $i2;
$templateids_clear = array_key_exists('templates_clear', $host)
? array_column($host['templates_clear'], 'templateid')
foreach ($db_templates as $db_template) {
if ($db_template['link_type'] != TEMPLATE_LINK_MANUAL
&& !in_array($db_template['templateid'], $templateids_clear)) {
unset($db_hosts[$host[$id_field_name]]['templates'][$db_template['hosttemplateid']]);