/** * Update maintenances. * * @param array $maintenances * * @return boolean */ public function update(array $maintenances) { $maintenances = zbx_toArray($maintenances); $maintenanceids = zbx_objectValues($maintenances, 'maintenanceid'); // validate maintenance permissions if (self::$userData['type'] == USER_TYPE_ZABBIX_USER) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } $updMaintenances = $this->get(['maintenanceids' => zbx_objectValues($maintenances, 'maintenanceid'), 'editable' => true, 'output' => API_OUTPUT_EXTEND, 'selectGroups' => ['groupid'], 'selectHosts' => ['hostid'], 'selectTimeperiods' => API_OUTPUT_EXTEND, 'preservekeys' => true]); $maintenanceNamesChanged = []; foreach ($maintenances as $maintenance) { if (!isset($updMaintenances[$maintenance['maintenanceid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } if (isset($maintenance['name']) && !zbx_empty($maintenance['name']) && $updMaintenances[$maintenance['maintenanceid']]['name'] !== $maintenance['name']) { if (isset($maintenanceNamesChanged[$maintenance['name']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Maintenance "%1$s" already exists.', $maintenance['name'])); } else { $maintenanceNamesChanged[$maintenance['name']] = $maintenance['name']; } } } // check if maintenance already exists if ($maintenanceNamesChanged) { $dbMaintenances = $this->get(['output' => ['name'], 'filter' => ['name' => $maintenanceNamesChanged], 'nopermissions' => true, 'limit' => 1]); if ($dbMaintenances) { $dbMaintenance = reset($dbMaintenances); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Maintenance "%1$s" already exists.', $dbMaintenance['name'])); } } $hostids = []; $groupids = []; foreach ($maintenances as $maintenance) { // validate maintenance active since if (!validateUnixTime($maintenance['active_since'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('"%s" must be between 1970.01.01 and 2038.01.18.', _('Active since'))); } // validate maintenance active till if (!validateUnixTime($maintenance['active_till'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('"%s" must be between 1970.01.01 and 2038.01.18.', _('Active till'))); } // validate maintenance active interval if ($maintenance['active_since'] > $maintenance['active_till']) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Maintenance "Active since" value cannot be bigger than "Active till".')); } // validate timeperiods if (!array_key_exists('timeperiods', $maintenance) || !is_array($maintenance['timeperiods']) || !$maintenance['timeperiods']) { self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one maintenance period must be created.')); } foreach ($maintenance['timeperiods'] as $timeperiod) { if (!is_array($timeperiod)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one maintenance period must be created.')); } } $hostids = array_merge($hostids, $maintenance['hostids']); $groupids = array_merge($groupids, $maintenance['groupids']); } // validate hosts & groups if (empty($hostids) && empty($groupids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one host or group should be selected.')); } // validate hosts permissions $options = ['hostids' => $hostids, 'editable' => true, 'output' => ['hostid'], 'preservekeys' => true]; $updHosts = API::Host()->get($options); foreach ($hostids as $hostid) { if (!isset($updHosts[$hostid])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.')); } } // validate groups permissions $options = ['groupids' => $groupids, 'editable' => true, 'output' => ['groupid'], 'preservekeys' => true]; $updGroups = API::HostGroup()->get($options); foreach ($groupids as $groupid) { if (!isset($updGroups[$groupid])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } } $this->removeSecondsFromTimes($maintenances); $update = []; foreach ($maintenances as $mnum => $maintenance) { $dbFields = ['maintenanceid' => null]; // validate fields if (!check_db_fields($dbFields, $maintenance)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect parameters for maintenance.')); } $update[$mnum] = ['values' => $maintenance, 'where' => ['maintenanceid' => $maintenance['maintenanceid']]]; // update time periods $this->replaceTimePeriods($updMaintenances[$maintenance['maintenanceid']], $maintenance); } DB::update('maintenances', $update); // some of the hosts and groups bound to maintenance must be deleted, other inserted and others left alone $insertHosts = []; $insertGroups = []; foreach ($maintenances as $maintenance) { // putting apart those host<->maintenance connections that should be inserted, deleted and not changed // $hostDiff['first'] - new hosts, that should be inserted // $hostDiff['second'] - hosts, that should be deleted // $hostDiff['both'] - hosts, that should not be touched $hostDiff = zbx_array_diff(zbx_toObject($maintenance['hostids'], 'hostid'), $updMaintenances[$maintenance['maintenanceid']]['hosts'], 'hostid'); foreach ($hostDiff['first'] as $host) { $insertHosts[] = ['hostid' => $host['hostid'], 'maintenanceid' => $maintenance['maintenanceid']]; } foreach ($hostDiff['second'] as $host) { $deleteHosts = ['hostid' => $host['hostid'], 'maintenanceid' => $maintenance['maintenanceid']]; DB::delete('maintenances_hosts', $deleteHosts); } // now the same with the groups $groupDiff = zbx_array_diff(zbx_toObject($maintenance['groupids'], 'groupid'), $updMaintenances[$maintenance['maintenanceid']]['groups'], 'groupid'); foreach ($groupDiff['first'] as $group) { $insertGroups[] = ['groupid' => $group['groupid'], 'maintenanceid' => $maintenance['maintenanceid']]; } foreach ($groupDiff['second'] as $group) { $deleteGroups = ['groupid' => $group['groupid'], 'maintenanceid' => $maintenance['maintenanceid']]; DB::delete('maintenances_groups', $deleteGroups); } add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_MAINTENANCE, $maintenance['maintenanceid'], array_key_exists('name', $maintenance) ? $maintenance['name'] : $updMaintenances[$maintenance['maintenanceid']]['name'], 'maintenances', $updMaintenances[$maintenance['maintenanceid']], $maintenance); } DB::insert('maintenances_hosts', $insertHosts); DB::insert('maintenances_groups', $insertGroups); return ['maintenanceids' => $maintenanceids]; }
/** * Update map. * * @param array $maps multidimensional array with Hosts data * @param string $maps['sysmapid'] * @param string $maps['name'] * @param array $maps['width'] * @param int $maps['height'] * @param string $maps['backgroundid'] * @param array $maps['label_type'] * @param int $maps['label_location'] * @param int $maps['grid_size'] size of one grid cell. 100 refers to 100x100 and so on. * @param int $maps['grid_show'] does grid need to be shown. Constants: SYSMAP_GRID_SHOW_ON / SYSMAP_GRID_SHOW_OFF * @param int $maps['grid_align'] do elements need to be aligned to the grid. Constants: SYSMAP_GRID_ALIGN_ON / SYSMAP_GRID_ALIGN_OFF * * @return array */ public function update(array $maps) { $maps = zbx_toArray($maps); $sysmapIds = zbx_objectValues($maps, 'sysmapid'); $dbMaps = $this->checkInput($maps, __FUNCTION__); $updateMaps = array(); $urlIdsToDelete = $urlsToUpdate = $urlsToAdd = array(); $selementsToDelete = $selementsToUpdate = $selementsToAdd = array(); $linksToDelete = $linksToUpdate = $linksToAdd = array(); foreach ($maps as $map) { $updateMaps[] = array('values' => $map, 'where' => array('sysmapid' => $map['sysmapid'])); $dbMap = $dbMaps[$map['sysmapid']]; // urls if (isset($map['urls'])) { $urlDiff = zbx_array_diff($map['urls'], $dbMap['urls'], 'name'); foreach ($urlDiff['both'] as $updateUrl) { $urlsToUpdate[] = array('values' => $updateUrl, 'where' => array('name' => $updateUrl['name'], 'sysmapid' => $map['sysmapid'])); } foreach ($urlDiff['first'] as $newUrl) { $newUrl['sysmapid'] = $map['sysmapid']; $urlsToAdd[] = $newUrl; } $urlIdsToDelete = array_merge($urlIdsToDelete, zbx_objectValues($urlDiff['second'], 'sysmapurlid')); } // elements if (isset($map['selements'])) { $selementDiff = zbx_array_diff($map['selements'], $dbMap['selements'], 'selementid'); // we need sysmapid for add operations foreach ($selementDiff['first'] as $newSelement) { $newSelement['sysmapid'] = $map['sysmapid']; $selementsToAdd[] = $newSelement; } $selementsToUpdate = array_merge($selementsToUpdate, $selementDiff['both']); $selementsToDelete = array_merge($selementsToDelete, $selementDiff['second']); } // links if (isset($map['links'])) { $linkDiff = zbx_array_diff($map['links'], $dbMap['links'], 'linkid'); // we need sysmapId for add operations foreach ($linkDiff['first'] as $newLink) { $newLink['sysmapid'] = $map['sysmapid']; $linksToAdd[] = $newLink; } $linksToUpdate = array_merge($linksToUpdate, $linkDiff['both']); $linksToDelete = array_merge($linksToDelete, $linkDiff['second']); } } DB::update('sysmaps', $updateMaps); // urls DB::insert('sysmap_url', $urlsToAdd); DB::update('sysmap_url', $urlsToUpdate); if ($urlIdsToDelete) { DB::delete('sysmap_url', array('sysmapurlid' => $urlIdsToDelete)); } // selements $newSelementIds = array('selementids' => array()); if ($selementsToAdd) { $newSelementIds = $this->createSelements($selementsToAdd); } if ($selementsToUpdate) { $this->updateSelements($selementsToUpdate); } if ($selementsToDelete) { $this->deleteSelements($selementsToDelete); } // links if ($linksToAdd || $linksToUpdate) { $selementsNames = array(); foreach ($newSelementIds['selementids'] as $key => $selementId) { $selementsNames[$selementsToAdd[$key]['selementid']] = $selementId; } foreach ($selementsToUpdate as $selement) { $selementsNames[$selement['selementid']] = $selement['selementid']; } foreach ($linksToAdd as $key => $link) { if (isset($selementsNames[$link['selementid1']])) { $linksToAdd[$key]['selementid1'] = $selementsNames[$link['selementid1']]; } if (isset($selementsNames[$link['selementid2']])) { $linksToAdd[$key]['selementid2'] = $selementsNames[$link['selementid2']]; } } foreach ($linksToUpdate as $key => $link) { if (isset($selementsNames[$link['selementid1']])) { $linksToUpdate[$key]['selementid1'] = $selementsNames[$link['selementid1']]; } if (isset($selementsNames[$link['selementid2']])) { $linksToUpdate[$key]['selementid2'] = $selementsNames[$link['selementid2']]; } } unset($selementsNames); } $newLinkIds = $updateLinkIds = array('linkids' => array()); if ($linksToAdd) { $newLinkIds = $this->createLinks($linksToAdd); } if ($linksToUpdate) { $updateLinkIds = $this->updateLinks($linksToUpdate); } if ($linksToDelete) { $this->deleteLinks($linksToDelete); } // link triggers $linkTriggersToDelete = $linkTriggersToUpdate = $linkTriggersToAdd = array(); foreach ($newLinkIds['linkids'] as $key => $linkId) { if (!isset($linksToAdd[$key]['linktriggers'])) { continue; } foreach ($linksToAdd[$key]['linktriggers'] as $linkTrigger) { $linkTrigger['linkid'] = $linkId; $linkTriggersToAdd[] = $linkTrigger; } } $dbLinks = array(); $linkTriggerResource = DBselect('SELECT slt.* FROM sysmaps_link_triggers slt WHERE ' . dbConditionInt('slt.linkid', $updateLinkIds['linkids'])); while ($dbLinkTrigger = DBfetch($linkTriggerResource)) { zbx_subarray_push($dbLinks, $dbLinkTrigger['linkid'], $dbLinkTrigger); } foreach ($updateLinkIds['linkids'] as $key => $linkId) { if (!isset($linksToUpdate[$key]['linktriggers'])) { continue; } $dbLinkTriggers = isset($dbLinks[$linkId]) ? $dbLinks[$linkId] : array(); $dbLinkTriggersDiff = zbx_array_diff($linksToUpdate[$key]['linktriggers'], $dbLinkTriggers, 'linktriggerid'); foreach ($dbLinkTriggersDiff['first'] as $newLinkTrigger) { $newLinkTrigger['linkid'] = $linkId; $linkTriggersToAdd[] = $newLinkTrigger; } $linkTriggersToUpdate = array_merge($linkTriggersToUpdate, $dbLinkTriggersDiff['both']); $linkTriggersToDelete = array_merge($linkTriggersToDelete, $dbLinkTriggersDiff['second']); } if ($linkTriggersToDelete) { $this->deleteLinkTriggers($linkTriggersToDelete); } if ($linkTriggersToAdd) { $this->createLinkTriggers($linkTriggersToAdd); } if ($linkTriggersToUpdate) { $this->updateLinkTriggers($linkTriggersToUpdate); } return array('sysmapids' => $sysmapIds); }
protected function updateOperations($operations, $actionsDb) { $operationsUpdate = array(); // messages $opmessageCreate = array(); $opmessageUpdate = array(); $opmessageDeleteByOpId = array(); $opmessageGrpCreate = array(); $opmessageUsrCreate = array(); $opmessageGrpDeleteByOpId = array(); $opmessageUsrDeleteByOpId = array(); // commands $opcommandCreate = array(); $opcommandUpdate = array(); $opcommandDeleteByOpId = array(); $opcommandGrpCreate = array(); $opcommandHstCreate = array(); $opcommandGrpDeleteByOpId = array(); $opcommandHstDeleteByOpId = array(); // groups $opgroupCreate = array(); $opgroupDeleteByOpId = array(); // templates $optemplateCreate = array(); $optemplateDeleteByOpId = array(); $opconditionsCreate = array(); foreach ($operations as $operation) { $operationsDb = zbx_toHash($actionsDb[$operation['actionid']]['operations'], 'operationid'); $operationDb = $operationsDb[$operation['operationid']]; $typeChanged = false; if (isset($operation['operationtype']) && $operation['operationtype'] != $operationDb['operationtype']) { $typeChanged = true; switch ($operationDb['operationtype']) { case OPERATION_TYPE_MESSAGE: $opmessageDeleteByOpId[] = $operationDb['operationid']; $opmessageGrpDeleteByOpId[] = $operationDb['operationid']; $opmessageUsrDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_COMMAND: $opcommandDeleteByOpId[] = $operationDb['operationid']; $opcommandHstDeleteByOpId[] = $operationDb['operationid']; $opcommandGrpDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_GROUP_ADD: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_REMOVE) { break; } case OPERATION_TYPE_GROUP_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_ADD) { break; } $opgroupDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_TEMPLATE_ADD: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_REMOVE) { break; } case OPERATION_TYPE_TEMPLATE_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_ADD) { break; } $optemplateDeleteByOpId[] = $operationDb['operationid']; break; } } if (!isset($operation['operationtype'])) { $operation['operationtype'] = $operationDb['operationtype']; } switch ($operation['operationtype']) { case OPERATION_TYPE_MESSAGE: if (!isset($operation['opmessage_grp'])) { $operation['opmessage_grp'] = array(); } else { zbx_array_push($operation['opmessage_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opmessage_usr'])) { $operation['opmessage_usr'] = array(); } else { zbx_array_push($operation['opmessage_usr'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opmessage_usr'])) { $operationDb['opmessage_usr'] = array(); } if (!isset($operationDb['opmessage_grp'])) { $operationDb['opmessage_grp'] = array(); } if ($typeChanged) { $operation['opmessage']['operationid'] = $operation['operationid']; $opmessageCreate[] = $operation['opmessage']; $opmessageGrpCreate = array_merge($opmessageGrpCreate, $operation['opmessage_grp']); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $operation['opmessage_usr']); } else { $opmessageUpdate[] = array('values' => $operation['opmessage'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opmessage_grp'], $operationDb['opmessage_grp'], 'usrgrpid'); $opmessageGrpCreate = array_merge($opmessageGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opmessage_grp', array('usrgrpid' => $omgrp['usrgrpid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opmessage_usr'], $operationDb['opmessage_usr'], 'userid'); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $diff['first']); foreach ($diff['second'] as $omusr) { DB::delete('opmessage_usr', array('userid' => $omusr['userid'], 'operationid' => $operation['operationid'])); } } break; case OPERATION_TYPE_COMMAND: if (!isset($operation['opcommand_grp'])) { $operation['opcommand_grp'] = array(); } else { zbx_array_push($operation['opcommand_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opcommand_hst'])) { $operation['opcommand_hst'] = array(); } else { zbx_array_push($operation['opcommand_hst'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opcommand_grp'])) { $operationDb['opcommand_grp'] = array(); } if (!isset($operationDb['opcommand_hst'])) { $operationDb['opcommand_hst'] = array(); } if ($typeChanged) { $operation['opcommand']['operationid'] = $operation['operationid']; $opcommandCreate[] = $operation['opcommand']; $opcommandGrpCreate = array_merge($opcommandGrpCreate, $operation['opcommand_grp']); $opcommandHstCreate = array_merge($opcommandHstCreate, $operation['opcommand_hst']); } else { // clear and reset fields to default values on type change if ($operation['opcommand']['type'] == ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT) { $operation['opcommand']['command'] = ''; } else { $operation['opcommand']['scriptid'] = null; } if ($operation['opcommand']['type'] != ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT) { $operation['opcommand']['execute_on'] = ZBX_SCRIPT_EXECUTE_ON_AGENT; } if ($operation['opcommand']['type'] != ZBX_SCRIPT_TYPE_SSH && $operation['opcommand']['type'] != ZBX_SCRIPT_TYPE_TELNET) { $operation['opcommand']['port'] = ''; $operation['opcommand']['username'] = ''; $operation['opcommand']['password'] = ''; } if (!isset($operation['opcommand']['authtype'])) { $operation['opcommand']['authtype'] = ITEM_AUTHTYPE_PASSWORD; } if ($operation['opcommand']['authtype'] == ITEM_AUTHTYPE_PASSWORD) { $operation['opcommand']['publickey'] = ''; $operation['opcommand']['privatekey'] = ''; } $opcommandUpdate[] = array('values' => $operation['opcommand'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opcommand_grp'], $operationDb['opcommand_grp'], 'groupid'); $opcommandGrpCreate = array_merge($opcommandGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opcommand_grp', array('groupid' => $omgrp['groupid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opcommand_hst'], $operationDb['opcommand_hst'], 'hostid'); $opcommandHstCreate = array_merge($opcommandHstCreate, $diff['first']); $opCommandHostIds = zbx_objectValues($diff['second'], 'opcommand_hstid'); if ($opCommandHostIds) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opCommandHostIds)); } } break; case OPERATION_TYPE_GROUP_ADD: case OPERATION_TYPE_GROUP_REMOVE: if (!isset($operation['opgroup'])) { $operation['opgroup'] = array(); } else { zbx_array_push($operation['opgroup'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opgroup'])) { $operationDb['opgroup'] = array(); } $diff = zbx_array_diff($operation['opgroup'], $operationDb['opgroup'], 'groupid'); $opgroupCreate = array_merge($opgroupCreate, $diff['first']); foreach ($diff['second'] as $ogrp) { DB::delete('opgroup', array('groupid' => $ogrp['groupid'], 'operationid' => $operation['operationid'])); } break; case OPERATION_TYPE_TEMPLATE_ADD: case OPERATION_TYPE_TEMPLATE_REMOVE: if (!isset($operation['optemplate'])) { $operation['optemplate'] = array(); } else { zbx_array_push($operation['optemplate'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['optemplate'])) { $operationDb['optemplate'] = array(); } $diff = zbx_array_diff($operation['optemplate'], $operationDb['optemplate'], 'templateid'); $optemplateCreate = array_merge($optemplateCreate, $diff['first']); foreach ($diff['second'] as $otpl) { DB::delete('optemplate', array('templateid' => $otpl['templateid'], 'operationid' => $operation['operationid'])); } break; } if (!isset($operation['opconditions'])) { $operation['opconditions'] = array(); } else { zbx_array_push($operation['opconditions'], array('operationid' => $operation['operationid'])); } self::validateOperationConditions($operation['opconditions']); $diff = zbx_array_diff($operation['opconditions'], $operationDb['opconditions'], 'opconditionid'); $opconditionsCreate = array_merge($opconditionsCreate, $diff['first']); $opconditionsidDelete = zbx_objectValues($diff['second'], 'opconditionid'); if (!empty($opconditionsidDelete)) { DB::delete('opconditions', array('opconditionid' => $opconditionsidDelete)); } $operationid = $operation['operationid']; unset($operation['operationid']); if (!empty($operation)) { $operationsUpdate[] = array('values' => $operation, 'where' => array('operationid' => $operationid)); } } DB::update('operations', $operationsUpdate); if (!empty($opmessageDeleteByOpId)) { DB::delete('opmessage', array('operationid' => $opmessageDeleteByOpId)); } if (!empty($opcommandDeleteByOpId)) { DB::delete('opcommand', array('operationid' => $opcommandDeleteByOpId)); } if (!empty($opmessageGrpDeleteByOpId)) { DB::delete('opmessage_grp', array('operationid' => $opmessageGrpDeleteByOpId)); } if (!empty($opmessageUsrDeleteByOpId)) { DB::delete('opmessage_usr', array('operationid' => $opmessageUsrDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('operationid' => $opcommandHstDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('operationid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('opcommand_grpid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opcommandHstDeleteByOpId)); } if (!empty($opgroupDeleteByOpId)) { DB::delete('opgroup', array('operationid' => $opgroupDeleteByOpId)); } if (!empty($optemplateDeleteByOpId)) { DB::delete('optemplate', array('operationid' => $optemplateDeleteByOpId)); } DB::insert('opmessage', $opmessageCreate, false); DB::insert('opcommand', $opcommandCreate, false); DB::insert('opmessage_grp', $opmessageGrpCreate); DB::insert('opmessage_usr', $opmessageUsrCreate); DB::insert('opcommand_grp', $opcommandGrpCreate); DB::insert('opcommand_hst', $opcommandHstCreate); DB::insert('opgroup', $opgroupCreate); DB::insert('optemplate', $optemplateCreate); DB::update('opmessage', $opmessageUpdate); DB::update('opcommand', $opcommandUpdate); DB::insert('opconditions', $opconditionsCreate); }
/** * Update maintenances * * @param array $maintenances * @return boolean */ public function update(array $maintenances) { $maintenances = zbx_toArray($maintenances); $maintenanceids = zbx_objectValues($maintenances, 'maintenanceid'); // validate maintenance permissions if (self::$userData['type'] == USER_TYPE_ZABBIX_USER) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } $hostids = array(); $groupids = array(); $updMaintenances = $this->get(array('maintenanceids' => zbx_objectValues($maintenances, 'maintenanceid'), 'editable' => true, 'output' => API_OUTPUT_EXTEND, 'selectGroups' => API_OUTPUT_REFER, 'selectHosts' => API_OUTPUT_REFER, 'selectTimeperiods' => API_OUTPUT_EXTEND, 'preservekeys' => true)); foreach ($maintenances as $maintenance) { if (!isset($updMaintenances[$maintenance['maintenanceid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } // Checking whether a maintenance with this name already exists. First, getting all maintenances with the same name as this $receivedMaintenances = API::Maintenance()->get(array('filter' => array('name' => $maintenance['name']))); // validate if maintenance name already exists foreach ($receivedMaintenances as $rMaintenance) { if (bccomp($rMaintenance['maintenanceid'], $maintenance['maintenanceid']) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Maintenance "%s" already exists.', $maintenance['name'])); } } // validate maintenance active since if (!validateUnixTime($maintenance['active_since'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('"%s" must be between 1970.01.01 and 2038.01.18.', _('Active since'))); } // validate maintenance active till if (!validateUnixTime($maintenance['active_till'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('"%s" must be between 1970.01.01 and 2038.01.18.', _('Active till'))); } // validate maintenance active interval if ($maintenance['active_since'] > $maintenance['active_till']) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Maintenance "Active since" value cannot be bigger than "Active till".')); } // validate timeperiods if (empty($maintenance['timeperiods'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one maintenance period must be created.')); } $hostids = array_merge($hostids, $maintenance['hostids']); $groupids = array_merge($groupids, $maintenance['groupids']); } // validate hosts & groups if (empty($hostids) && empty($groupids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one host or group should be selected.')); } // validate hosts permissions $options = array('hostids' => $hostids, 'editable' => true, 'output' => array('hostid'), 'preservekeys' => true); $updHosts = API::Host()->get($options); foreach ($hostids as $hostid) { if (!isset($updHosts[$hostid])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.')); } } // validate groups permissions $options = array('groupids' => $groupids, 'editable' => true, 'output' => array('groupid'), 'preservekeys' => true); $updGroups = API::HostGroup()->get($options); foreach ($groupids as $groupid) { if (!isset($updGroups[$groupid])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } } $this->removeSecondsFromTimes($maintenances); $update = array(); foreach ($maintenances as $mnum => $maintenance) { $dbFields = array('maintenanceid' => null); // validate fields if (!check_db_fields($dbFields, $maintenance)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect parameters for maintenance.')); } $update[$mnum] = array('values' => $maintenance, 'where' => array('maintenanceid' => $maintenance['maintenanceid'])); // update time periods $this->replaceTimePeriods($updMaintenances[$maintenance['maintenanceid']], $maintenance); } DB::update('maintenances', $update); // some of the hosts and groups bound to maintenance must be deleted, other inserted and others left alone $insertHosts = array(); $insertGroups = array(); foreach ($maintenances as $maintenance) { // putting apart those host<->maintenance connections that should be inserted, deleted and not changed // $hostDiff['first'] - new hosts, that should be inserted // $hostDiff['second'] - hosts, that should be deleted // $hostDiff['both'] - hosts, that should not be touched $hostDiff = zbx_array_diff(zbx_toObject($maintenance['hostids'], 'hostid'), $updMaintenances[$maintenance['maintenanceid']]['hosts'], 'hostid'); foreach ($hostDiff['first'] as $host) { $insertHosts[] = array('hostid' => $host['hostid'], 'maintenanceid' => $maintenance['maintenanceid']); } foreach ($hostDiff['second'] as $host) { $deleteHosts = array('hostid' => $host['hostid'], 'maintenanceid' => $maintenance['maintenanceid']); DB::delete('maintenances_hosts', $deleteHosts); } // now the same with the groups $groupDiff = zbx_array_diff(zbx_toObject($maintenance['groupids'], 'groupid'), $updMaintenances[$maintenance['maintenanceid']]['groups'], 'groupid'); foreach ($groupDiff['first'] as $group) { $insertGroups[] = array('groupid' => $group['groupid'], 'maintenanceid' => $maintenance['maintenanceid']); } foreach ($groupDiff['second'] as $group) { $deleteGroups = array('groupid' => $group['groupid'], 'maintenanceid' => $maintenance['maintenanceid']); DB::delete('maintenances_groups', $deleteGroups); } } DB::insert('maintenances_hosts', $insertHosts); DB::insert('maintenances_groups', $insertGroups); return array('maintenanceids' => $maintenanceids); }
/** * Update element to sysmap. * * @param array $elements[0,...]['selementid'] * @param array $elements[0,...]['sysmapid'] * @param array $elements[0,...]['elementid'] * @param array $elements[0,...]['elementtype'] * @param array $elements[0,...]['label'] * @param array $elements[0,...]['x'] * @param array $elements[0,...]['y'] * @param array $elements[0,...]['iconid_off'] * @param array $elements[0,...]['iconid_on'] * @param array $elements[0,...]['iconid_disabled'] * @param array $elements[0,...]['url'] * @param array $elements[0,...]['label_location'] */ protected function updateSelements(array $selements) { $selements = zbx_toArray($selements); $selementIds = array(); $dbSelements = $this->checkSelementInput($selements, __FUNCTION__); $update = array(); $urlsToDelete = $urlsToUpdate = $urlsToAdd = array(); foreach ($selements as $selement) { $update[] = array('values' => $selement, 'where' => array('selementid' => $selement['selementid'])); $selementIds[] = $selement['selementid']; if (!isset($selement['urls'])) { continue; } $diffUrls = zbx_array_diff($selement['urls'], $dbSelements[$selement['selementid']]['urls'], 'name'); // add foreach ($diffUrls['first'] as $newUrl) { $newUrl['selementid'] = $selement['selementid']; $urlsToAdd[] = $newUrl; } // update url foreach ($diffUrls['both'] as $updUrl) { $urlsToUpdate[] = array('values' => $updUrl, 'where' => array('selementid' => $selement['selementid'], 'name' => $updUrl['name'])); } // delete url $urlsToDelete = array_merge($urlsToDelete, zbx_objectValues($diffUrls['second'], 'sysmapelementurlid')); } DB::update('sysmaps_elements', $update); if (!empty($urlsToDelete)) { DB::delete('sysmap_element_url', array('sysmapelementurlid' => $urlsToDelete)); } if (!empty($urlsToUpdate)) { DB::update('sysmap_element_url', $urlsToUpdate); } if (!empty($urlsToAdd)) { DB::insert('sysmap_element_url', $urlsToAdd); } return array('selementids' => $selementIds); }
protected function updateOperations($operations, $actionsDb) { $operationsUpdate = array(); // messages $opmessageCreate = array(); $opmessageUpdate = array(); $opmessageDeleteByOpId = array(); $opmessageGrpCreate = array(); $opmessageUsrCreate = array(); $opmessageGrpDeleteByOpId = array(); $opmessageUsrDeleteByOpId = array(); // commands $opcommandCreate = array(); $opcommandUpdate = array(); $opcommandDeleteByOpId = array(); $opcommandGrpCreate = array(); $opcommandHstCreate = array(); $opcommandGrpDeleteByOpId = array(); $opcommandHstDeleteByOpId = array(); // groups $opgroupCreate = array(); $opgroupDeleteByOpId = array(); // templates $optemplateCreate = array(); $optemplateDeleteByOpId = array(); $opconditionsCreate = array(); foreach ($operations as $operation) { $operationDb = $actionsDb[$operation['actionid']]['operations'][$operation['operationid']]; $typeChanged = false; if (isset($operation['operationtype']) && $operation['operationtype'] != $operationDb['operationtype']) { $typeChanged = true; switch ($operationDb['operationtype']) { case OPERATION_TYPE_MESSAGE: $opmessageDeleteByOpId[] = $operationDb['operationid']; $opmessageGrpDeleteByOpId[] = $operationDb['operationid']; $opmessageUsrDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_COMMAND: $opcommandDeleteByOpId[] = $operationDb['operationid']; $opcommandHstDeleteByOpId[] = $operationDb['operationid']; $opcommandGrpDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_GROUP_ADD: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_REMOVE) { break; } case OPERATION_TYPE_GROUP_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_GROUP_ADD) { break; } $opgroupDeleteByOpId[] = $operationDb['operationid']; break; case OPERATION_TYPE_TEMPLATE_ADD: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_REMOVE) { break; } case OPERATION_TYPE_TEMPLATE_REMOVE: if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_ADD) { break; } $optemplateDeleteByOpId[] = $operationDb['operationid']; break; } } if (!isset($operation['operationtype'])) { $operation['operationtype'] = $operationDb['operationtype']; } switch ($operation['operationtype']) { case OPERATION_TYPE_MESSAGE: if (!isset($operation['opmessage_grp'])) { $operation['opmessage_grp'] = array(); } else { zbx_array_push($operation['opmessage_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opmessage_usr'])) { $operation['opmessage_usr'] = array(); } else { zbx_array_push($operation['opmessage_usr'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opmessage_usr'])) { $operationDb['opmessage_usr'] = array(); } if (!isset($operationDb['opmessage_grp'])) { $operationDb['opmessage_grp'] = array(); } if ($typeChanged) { $operation['opmessage']['operationid'] = $operation['operationid']; $opmessageCreate[] = $operation['opmessage']; $opmessageGrpCreate = array_merge($opmessageGrpCreate, $operation['opmessage_grp']); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $operation['opmessage_usr']); } else { $opmessageUpdate[] = array('values' => $operation['opmessage'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opmessage_grp'], $operationDb['opmessage_grp'], 'usrgrpid'); $opmessageGrpCreate = array_merge($opmessageGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opmessage_grp', array('usrgrpid' => $omgrp['usrgrpid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opmessage_usr'], $operationDb['opmessage_usr'], 'userid'); $opmessageUsrCreate = array_merge($opmessageUsrCreate, $diff['first']); foreach ($diff['second'] as $omusr) { DB::delete('opmessage_usr', array('userid' => $omusr['userid'], 'operationid' => $operation['operationid'])); } } break; case OPERATION_TYPE_COMMAND: if (!isset($operation['opcommand_grp'])) { $operation['opcommand_grp'] = array(); } else { zbx_array_push($operation['opcommand_grp'], array('operationid' => $operation['operationid'])); } if (!isset($operation['opcommand_hst'])) { $operation['opcommand_hst'] = array(); } else { zbx_array_push($operation['opcommand_hst'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opcommand_grp'])) { $operationDb['opcommand_grp'] = array(); } if (!isset($operationDb['opcommand_hst'])) { $operationDb['opcommand_hst'] = array(); } if ($typeChanged) { $operation['opcommand']['operationid'] = $operation['operationid']; $opcommandCreate[] = $operation['opcommand']; $opcommandGrpCreate = array_merge($opcommandGrpCreate, $operation['opcommand_grp']); $opcommandHstCreate = array_merge($opcommandHstCreate, $operation['opcommand_hst']); } else { $opcommandUpdate[] = array('values' => $operation['opcommand'], 'where' => array('operationid' => $operation['operationid'])); $diff = zbx_array_diff($operation['opcommand_grp'], $operationDb['opcommand_grp'], 'groupid'); $opcommandGrpCreate = array_merge($opcommandGrpCreate, $diff['first']); foreach ($diff['second'] as $omgrp) { DB::delete('opcommand_grp', array('groupid' => $omgrp['groupid'], 'operationid' => $operation['operationid'])); } $diff = zbx_array_diff($operation['opcommand_hst'], $operationDb['opcommand_hst'], 'hostid'); $opcommandHstCreate = array_merge($opcommandHstCreate, $diff['first']); $opCommandHostIds = zbx_objectValues($diff['second'], 'opcommand_hstid'); if ($opCommandHostIds) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opCommandHostIds)); } } break; case OPERATION_TYPE_GROUP_ADD: case OPERATION_TYPE_GROUP_REMOVE: if (!isset($operation['opgroup'])) { $operation['opgroup'] = array(); } else { zbx_array_push($operation['opgroup'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['opgroup'])) { $operationDb['opgroup'] = array(); } $diff = zbx_array_diff($operation['opgroup'], $operationDb['opgroup'], 'groupid'); $opgroupCreate = array_merge($opgroupCreate, $diff['first']); foreach ($diff['second'] as $ogrp) { DB::delete('opgroup', array('groupid' => $ogrp['groupid'], 'operationid' => $operation['operationid'])); } break; case OPERATION_TYPE_TEMPLATE_ADD: case OPERATION_TYPE_TEMPLATE_REMOVE: if (!isset($operation['optemplate'])) { $operation['optemplate'] = array(); } else { zbx_array_push($operation['optemplate'], array('operationid' => $operation['operationid'])); } if (!isset($operationDb['optemplate'])) { $operationDb['optemplate'] = array(); } $diff = zbx_array_diff($operation['optemplate'], $operationDb['optemplate'], 'templateid'); $optemplateCreate = array_merge($optemplateCreate, $diff['first']); foreach ($diff['second'] as $otpl) { DB::delete('optemplate', array('templateid' => $otpl['templateid'], 'operationid' => $operation['operationid'])); } break; } if (!isset($operation['opconditions'])) { $operation['opconditions'] = array(); } else { zbx_array_push($operation['opconditions'], array('operationid' => $operation['operationid'])); } self::validateOperationConditions($operation['opconditions']); $diff = zbx_array_diff($operation['opconditions'], $operationDb['opconditions'], 'opconditionid'); $opconditionsCreate = array_merge($opconditionsCreate, $diff['first']); $opconditionsidDelete = zbx_objectValues($diff['second'], 'opconditionid'); if (!empty($opconditionsidDelete)) { DB::delete('opconditions', array('opconditionid' => $opconditionsidDelete)); } $operationid = $operation['operationid']; unset($operation['operationid']); if (!empty($operation)) { $operationsUpdate[] = array('values' => $operation, 'where' => array('operationid' => $operationid)); } } DB::update('operations', $operationsUpdate); if (!empty($opmessageDeleteByOpId)) { DB::delete('opmessage', array('operationid' => $opmessageDeleteByOpId)); } if (!empty($opcommandDeleteByOpId)) { DB::delete('opcommand', array('operationid' => $opcommandDeleteByOpId)); } if (!empty($opmessageGrpDeleteByOpId)) { DB::delete('opmessage_grp', array('operationid' => $opmessageGrpDeleteByOpId)); } if (!empty($opmessageUsrDeleteByOpId)) { DB::delete('opmessage_usr', array('operationid' => $opmessageUsrDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('operationid' => $opcommandHstDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('operationid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandGrpDeleteByOpId)) { DB::delete('opcommand_grp', array('opcommand_grpid' => $opcommandGrpDeleteByOpId)); } if (!empty($opcommandHstDeleteByOpId)) { DB::delete('opcommand_hst', array('opcommand_hstid' => $opcommandHstDeleteByOpId)); } if (!empty($opgroupDeleteByOpId)) { DB::delete('opgroup', array('operationid' => $opgroupDeleteByOpId)); } if (!empty($optemplateDeleteByOpId)) { DB::delete('optemplate', array('operationid' => $optemplateDeleteByOpId)); } DB::insert('opmessage', $opmessageCreate, false); DB::insert('opcommand', $opcommandCreate, false); DB::insert('opmessage_grp', $opmessageGrpCreate); DB::insert('opmessage_usr', $opmessageUsrCreate); DB::insert('opcommand_grp', $opcommandGrpCreate); DB::insert('opcommand_hst', $opcommandHstCreate); DB::insert('opgroup', $opgroupCreate); DB::insert('optemplate', $optemplateCreate); DB::update('opmessage', $opmessageUpdate); DB::update('opcommand', $opcommandUpdate); DB::insert('opconditions', $opconditionsCreate); }
/** * Update map. * * @param array $maps multidimensional array with Hosts data * @param string $maps['sysmapid'] * @param string $maps['name'] * @param array $maps['width'] * @param int $maps['height'] * @param string $maps['backgroundid'] * @param array $maps['label_type'] * @param int $maps['label_location'] * @param int $maps['grid_size'] size of one grid cell. 100 refers to 100x100 and so on. * @param int $maps['grid_show'] does grid need to be shown. Constants: SYSMAP_GRID_SHOW_ON / SYSMAP_GRID_SHOW_OFF * @param int $maps['grid_align'] do elements need to be aligned to the grid. Constants: SYSMAP_GRID_ALIGN_ON / SYSMAP_GRID_ALIGN_OFF * * @return array */ public function update(array $maps) { $maps = zbx_toArray($maps); $sysmapids = zbx_objectValues($maps, 'sysmapid'); $db_maps = $this->get(['output' => API_OUTPUT_EXTEND, 'sysmapids' => zbx_objectValues($maps, 'sysmapid'), 'selectLinks' => API_OUTPUT_EXTEND, 'selectSelements' => API_OUTPUT_EXTEND, 'selectUrls' => ['sysmapid', 'sysmapurlid', 'name', 'url'], 'selectUsers' => ['sysmapuserid', 'sysmapid', 'userid', 'permission'], 'selectUserGroups' => ['sysmapusrgrpid', 'sysmapid', 'usrgrpid', 'permission'], 'editable' => true, 'preservekeys' => true]); $this->validateUpdate($maps, $db_maps); $update_maps = []; $url_ids_to_delete = []; $urls_to_update = []; $urls_to_add = []; $selements_to_delete = []; $selements_to_update = []; $selements_to_add = []; $links_to_delete = []; $links_to_update = []; $links_to_add = []; $shared_userids_to_delete = []; $shared_users_to_update = []; $shared_users_to_add = []; $shared_user_groupids_to_delete = []; $shared_user_groups_to_update = []; $shared_user_groups_to_add = []; foreach ($maps as $map) { $update_maps[] = ['values' => $map, 'where' => ['sysmapid' => $map['sysmapid']]]; $db_map = $db_maps[$map['sysmapid']]; // Map user shares. if (array_key_exists('users', $map)) { $user_shares_diff = zbx_array_diff($map['users'], $db_map['users'], 'userid'); foreach ($user_shares_diff['both'] as $update_user_share) { $shared_users_to_update[] = ['values' => $update_user_share, 'where' => ['userid' => $update_user_share['userid'], 'sysmapid' => $map['sysmapid']]]; } foreach ($user_shares_diff['first'] as $new_shared_user) { $new_shared_user['sysmapid'] = $map['sysmapid']; $shared_users_to_add[] = $new_shared_user; } $shared_userids_to_delete = array_merge($shared_userids_to_delete, zbx_objectValues($user_shares_diff['second'], 'sysmapuserid')); } // Map user group shares. if (array_key_exists('userGroups', $map)) { $user_group_shares_diff = zbx_array_diff($map['userGroups'], $db_map['userGroups'], 'usrgrpid'); foreach ($user_group_shares_diff['both'] as $update_user_share) { $shared_user_groups_to_update[] = ['values' => $update_user_share, 'where' => ['usrgrpid' => $update_user_share['usrgrpid'], 'sysmapid' => $map['sysmapid']]]; } foreach ($user_group_shares_diff['first'] as $new_shared_user_group) { $new_shared_user_group['sysmapid'] = $map['sysmapid']; $shared_user_groups_to_add[] = $new_shared_user_group; } $shared_user_groupids_to_delete = array_merge($shared_user_groupids_to_delete, zbx_objectValues($user_group_shares_diff['second'], 'sysmapusrgrpid')); } // Urls. if (array_key_exists('urls', $map)) { $url_diff = zbx_array_diff($map['urls'], $db_map['urls'], 'name'); foreach ($url_diff['both'] as $updateUrl) { $urls_to_update[] = ['values' => $updateUrl, 'where' => ['name' => $updateUrl['name'], 'sysmapid' => $map['sysmapid']]]; } foreach ($url_diff['first'] as $new_url) { $new_url['sysmapid'] = $map['sysmapid']; $urls_to_add[] = $new_url; } $url_ids_to_delete = array_merge($url_ids_to_delete, zbx_objectValues($url_diff['second'], 'sysmapurlid')); } // Map elements. if (array_key_exists('selements', $map)) { $selement_diff = zbx_array_diff($map['selements'], $db_map['selements'], 'selementid'); // We need sysmapid for add operations. foreach ($selement_diff['first'] as $new_selement) { $new_selement['sysmapid'] = $map['sysmapid']; $selements_to_add[] = $new_selement; } $selements_to_update = array_merge($selements_to_update, $selement_diff['both']); $selements_to_delete = array_merge($selements_to_delete, $selement_diff['second']); } // Links. if (array_key_exists('links', $map)) { $link_diff = zbx_array_diff($map['links'], $db_map['links'], 'linkid'); // We need sysmapId for add operations. foreach ($link_diff['first'] as $newLink) { $newLink['sysmapid'] = $map['sysmapid']; $links_to_add[] = $newLink; } $links_to_update = array_merge($links_to_update, $link_diff['both']); $links_to_delete = array_merge($links_to_delete, $link_diff['second']); } } DB::update('sysmaps', $update_maps); // User shares. DB::insert('sysmap_user', $shared_users_to_add); DB::update('sysmap_user', $shared_users_to_update); if ($shared_userids_to_delete) { DB::delete('sysmap_user', ['sysmapuserid' => $shared_userids_to_delete]); } // User group shares. DB::insert('sysmap_usrgrp', $shared_user_groups_to_add); DB::update('sysmap_usrgrp', $shared_user_groups_to_update); if ($shared_user_groupids_to_delete) { DB::delete('sysmap_usrgrp', ['sysmapusrgrpid' => $shared_user_groupids_to_delete]); } // Urls. DB::insert('sysmap_url', $urls_to_add); DB::update('sysmap_url', $urls_to_update); if ($url_ids_to_delete) { DB::delete('sysmap_url', ['sysmapurlid' => $url_ids_to_delete]); } // Selements. $new_selementids = ['selementids' => []]; if ($selements_to_add) { $new_selementids = $this->createSelements($selements_to_add); } if ($selements_to_update) { $this->updateSelements($selements_to_update); } if ($selements_to_delete) { $this->deleteSelements($selements_to_delete); } // Links. if ($links_to_add || $links_to_update) { $selements_names = []; foreach ($new_selementids['selementids'] as $key => $selementId) { $selements_names[$selements_to_add[$key]['selementid']] = $selementId; } foreach ($selements_to_update as $selement) { $selements_names[$selement['selementid']] = $selement['selementid']; } foreach ($links_to_add as $key => $link) { if (array_key_exists($link['selementid1'], $selements_names)) { $links_to_add[$key]['selementid1'] = $selements_names[$link['selementid1']]; } if (array_key_exists($link['selementid2'], $selements_names)) { $links_to_add[$key]['selementid2'] = $selements_names[$link['selementid2']]; } } foreach ($links_to_update as $key => $link) { if (array_key_exists($link['selementid1'], $selements_names)) { $links_to_update[$key]['selementid1'] = $selements_names[$link['selementid1']]; } if (array_key_exists($link['selementid2'], $selements_names)) { $links_to_update[$key]['selementid2'] = $selements_names[$link['selementid2']]; } } unset($selements_names); } $new_linkids = ['linkids' => []]; $update_linkids = ['linkids' => []]; if ($links_to_add) { $new_linkids = $this->createLinks($links_to_add); } if ($links_to_update) { $update_linkids = $this->updateLinks($links_to_update); } if ($links_to_delete) { $this->deleteLinks($links_to_delete); } // Link triggers. $link_triggers_to_delete = []; $link_triggers_to_update = []; $link_triggers_to_add = []; foreach ($new_linkids['linkids'] as $key => $linkid) { if (!array_key_exists('linktriggers', $links_to_add[$key])) { continue; } foreach ($links_to_add[$key]['linktriggers'] as $link_trigger) { $link_trigger['linkid'] = $linkid; $link_triggers_to_add[] = $link_trigger; } } $db_links = []; $link_trigger_resource = DBselect('SELECT slt.* FROM sysmaps_link_triggers slt WHERE ' . dbConditionInt('slt.linkid', $update_linkids['linkids'])); while ($db_link_trigger = DBfetch($link_trigger_resource)) { zbx_subarray_push($db_links, $db_link_trigger['linkid'], $db_link_trigger); } foreach ($update_linkids['linkids'] as $key => $linkid) { if (!array_key_exists('linktriggers', $links_to_update[$key])) { continue; } $db_link_triggers = array_key_exists($linkid, $db_links) ? $db_links[$linkid] : []; $db_link_triggers_diff = zbx_array_diff($links_to_update[$key]['linktriggers'], $db_link_triggers, 'linktriggerid'); foreach ($db_link_triggers_diff['first'] as $new_link_trigger) { $new_link_trigger['linkid'] = $linkid; $link_triggers_to_add[] = $new_link_trigger; } $link_triggers_to_update = array_merge($link_triggers_to_update, $db_link_triggers_diff['both']); $link_triggers_to_delete = array_merge($link_triggers_to_delete, $db_link_triggers_diff['second']); } if ($link_triggers_to_delete) { $this->deleteLinkTriggers($link_triggers_to_delete); } if ($link_triggers_to_add) { $this->createLinkTriggers($link_triggers_to_add); } if ($link_triggers_to_update) { $this->updateLinkTriggers($link_triggers_to_update); } return ['sysmapids' => $sysmapids]; }
function update_slideshow($data) { $user_data = CWebUser::$data; // Validate slides. if (empty($data['slides'])) { error(_('Slide show must contain slides.')); return false; } // validate screens. $screenids = zbx_objectValues($data['slides'], 'screenid'); $screens = API::Screen()->get(['output' => ['screenid'], 'screenids' => $screenids, 'preservekeys' => true]); foreach ($screenids as $screenid) { if (!array_key_exists($screenid, $screens)) { error(_('Incorrect screen provided for slide show.')); return false; } } // Validate slide name. $db_slideshow = DBfetch(DBselect('SELECT s.slideshowid' . ' FROM slideshows s' . ' WHERE s.name=' . zbx_dbstr($data['name']) . ' AND s.slideshowid<>' . zbx_dbstr($data['slideshowid']))); if ($db_slideshow) { error(_s('Slide show "%1$s" already exists.', $data['name'])); return false; } // Validate slide show owner. if (array_key_exists('userid', $data)) { if ($data['userid'] === null || $data['userid'] === '') { error(_('Slide show owner cannot be empty.')); return false; } elseif ($data['userid'] != $user_data['userid'] && $user_data['type'] != USER_TYPE_SUPER_ADMIN && $user_data['type'] != USER_TYPE_ZABBIX_ADMIN) { error(_('Only administrators can set slide show owner.')); return false; } } $to_update = $data; unset($to_update['slideshowid'], $to_update['slides'], $to_update['users'], $to_update['userGroups']); DB::update('slideshows', ['values' => $to_update, 'where' => ['slideshowid' => $data['slideshowid']]]); // Read-only sharing validation. foreach ($data['users'] as $user) { if ($data['private'] == PUBLIC_SHARING && $user['permission'] == PERM_READ) { error(_s('Slide show "%1$s" is public and read-only sharing is disallowed.', $data['name'])); return false; } } foreach ($data['userGroups'] as $user_group) { if ($data['private'] == PUBLIC_SHARING && $user_group['permission'] == PERM_READ) { error(_s('Slide show "%1$s" is public and read-only sharing is disallowed.', $data['name'])); return false; } } $shared_userids_to_delete = []; $shared_users_to_update = []; $shared_users_to_add = []; $shared_user_groupids_to_delete = []; $shared_user_groups_to_update = []; $shared_user_groups_to_add = []; // Slide show user shares. $db_slideshow['users'] = DBfetchArray(DBselect('SELECT s.userid,s.permission,s.slideshowuserid' . ' FROM slideshow_user s' . ' WHERE s.slideshowid=' . zbx_dbstr(getRequest('slideshowid')))); $userids = []; foreach ($db_slideshow['users'] as $user) { $userids[] = $user['userid']; } $allowed_users = API::User()->get(['output' => ['userid'], 'userids' => $userids, 'preservekeys' => true]); foreach ($db_slideshow['users'] as $key => $user) { if (!array_key_exists($user['userid'], $allowed_users)) { unset($db_slideshow['users'][$key]); } } $user_shares_diff = zbx_array_diff($data['users'], $db_slideshow['users'], 'userid'); foreach ($user_shares_diff['both'] as $update_user_share) { $shared_users_to_update[] = ['values' => $update_user_share, 'where' => ['userid' => $update_user_share['userid'], 'slideshowid' => $data['slideshowid']]]; } foreach ($user_shares_diff['first'] as $new_shared_user) { $new_shared_user['slideshowid'] = $data['slideshowid']; $shared_users_to_add[] = $new_shared_user; } $shared_userids_to_delete = zbx_objectValues($user_shares_diff['second'], 'slideshowuserid'); // Slide show user group shares. $db_slideshow['userGroups'] = DBfetchArray(DBselect('SELECT s.usrgrpid,s.permission,s.slideshowusrgrpid' . ' FROM slideshow_usrgrp s' . ' WHERE s.slideshowid=' . zbx_dbstr(getRequest('slideshowid')))); $usrgrpids = []; foreach ($db_slideshow['userGroups'] as $user_group) { $usrgrpids[] = $user_group['usrgrpid']; } $allowed_user_groups = API::UserGroup()->get(['output' => ['usrgrpid'], 'usrgrpids' => $usrgrpids, 'preservekeys' => true]); foreach ($db_slideshow['userGroups'] as $key => $user_group) { if (!array_key_exists($user_group['usrgrpid'], $allowed_user_groups)) { unset($db_slideshow['userGroups'][$key]); } } $user_group_shares_diff = zbx_array_diff($data['userGroups'], $db_slideshow['userGroups'], 'usrgrpid'); foreach ($user_group_shares_diff['both'] as $update_user_share) { $shared_user_groups_to_update[] = ['values' => $update_user_share, 'where' => ['usrgrpid' => $update_user_share['usrgrpid'], 'slideshowid' => $data['slideshowid']]]; } foreach ($user_group_shares_diff['first'] as $new_shared_user_group) { $new_shared_user_group['slideshowid'] = $data['slideshowid']; $shared_user_groups_to_add[] = $new_shared_user_group; } $shared_user_groupids_to_delete = zbx_objectValues($user_group_shares_diff['second'], 'slideshowusrgrpid'); // User shares. DB::insert('slideshow_user', $shared_users_to_add); DB::update('slideshow_user', $shared_users_to_update); if ($shared_userids_to_delete) { DB::delete('slideshow_user', ['slideshowuserid' => $shared_userids_to_delete]); } // User group shares. DB::insert('slideshow_usrgrp', $shared_user_groups_to_add); DB::update('slideshow_usrgrp', $shared_user_groups_to_update); if ($shared_user_groupids_to_delete) { DB::delete('slideshow_usrgrp', ['slideshowusrgrpid' => $shared_user_groupids_to_delete]); } // get slides $db_slides = DBfetchArrayAssoc(DBselect('SELECT s.* FROM slides s WHERE s.slideshowid=' . zbx_dbstr($data['slideshowid'])), 'slideid'); $slidesToDel = zbx_objectValues($db_slides, 'slideid'); $slidesToDel = zbx_toHash($slidesToDel); $step = 0; foreach ($data['slides'] as $slide) { $slide['delay'] = $slide['delay'] ? $slide['delay'] : 0; if (isset($db_slides[$slide['slideid']])) { // update slide if ($db_slides[$slide['slideid']]['delay'] != $slide['delay'] || $db_slides[$slide['slideid']]['step'] != $step) { $result = DBexecute('UPDATE slides SET step=' . zbx_dbstr($step) . ', delay=' . zbx_dbstr($slide['delay']) . ' WHERE slideid=' . zbx_dbstr($slide['slideid'])); } else { $result = true; } unset($slidesToDel[$slide['slideid']]); } else { $slideid = get_dbid('slides', 'slideid'); $result = DBexecute('INSERT INTO slides (slideid,slideshowid,screenid,step,delay)' . ' VALUES (' . zbx_dbstr($slideid) . ',' . zbx_dbstr($data['slideshowid']) . ',' . zbx_dbstr($slide['screenid']) . ',' . zbx_dbstr($step) . ',' . zbx_dbstr($slide['delay']) . ')'); } $step++; if (!$result) { return false; } } // delete unnecessary slides if (!empty($slidesToDel)) { DBexecute('DELETE FROM slides WHERE slideid IN(' . implode(',', $slidesToDel) . ')'); } return true; }
/** * Saves screens and screen data. * * @param array $screens * @param array $db_screens array of existing screens with screen IDs as keys */ protected function updateReal(array $screens, array $db_screens) { $update_screens = []; foreach ($screens as $screen) { $screenid = $screen['screenid']; unset($screen['screenid'], $screen['screenitems'], $screen['users'], $screen['userGroups']); if ($screen) { $update_screens[] = ['values' => $screen, 'where' => ['screenid' => $screenid]]; } } DB::update('screens', $update_screens); $shared_userids_to_delete = []; $shared_users_to_update = []; $shared_users_to_add = []; $shared_user_groupids_to_delete = []; $shared_user_groups_to_update = []; $shared_user_groups_to_add = []; foreach ($screens as $screen) { $db_screen = $db_screens[$screen['screenid']]; // Screen user shares. if (array_key_exists('users', $screen)) { $user_shares_diff = zbx_array_diff($screen['users'], $db_screen['users'], 'userid'); foreach ($user_shares_diff['both'] as $update_user_share) { $shared_users_to_update[] = ['values' => $update_user_share, 'where' => ['userid' => $update_user_share['userid'], 'screenid' => $screen['screenid']]]; } foreach ($user_shares_diff['first'] as $new_shared_user) { $new_shared_user['screenid'] = $screen['screenid']; $shared_users_to_add[] = $new_shared_user; } $shared_userids_to_delete = array_merge($shared_userids_to_delete, zbx_objectValues($user_shares_diff['second'], 'screenuserid')); } // Screen user group shares. if (array_key_exists('userGroups', $screen)) { $user_group_shares_diff = zbx_array_diff($screen['userGroups'], $db_screen['userGroups'], 'usrgrpid'); foreach ($user_group_shares_diff['both'] as $update_user_share) { $shared_user_groups_to_update[] = ['values' => $update_user_share, 'where' => ['usrgrpid' => $update_user_share['usrgrpid'], 'screenid' => $screen['screenid']]]; } foreach ($user_group_shares_diff['first'] as $new_shared_user_group) { $new_shared_user_group['screenid'] = $screen['screenid']; $shared_user_groups_to_add[] = $new_shared_user_group; } $shared_user_groupids_to_delete = array_merge($shared_user_groupids_to_delete, zbx_objectValues($user_group_shares_diff['second'], 'screenusrgrpid')); } // Replace screen items. if (array_key_exists('screenitems', $screen)) { $this->replaceItems($screen['screenid'], $screen['screenitems']); } } // User shares. DB::insert('screen_user', $shared_users_to_add); DB::update('screen_user', $shared_users_to_update); if ($shared_userids_to_delete) { DB::delete('screen_user', ['screenuserid' => $shared_userids_to_delete]); } // User group shares. DB::insert('screen_usrgrp', $shared_user_groups_to_add); DB::update('screen_usrgrp', $shared_user_groups_to_update); if ($shared_user_groupids_to_delete) { DB::delete('screen_usrgrp', ['screenusrgrpid' => $shared_user_groupids_to_delete]); } }