/**
  * Get Screen data
  *
  * @param array $options
  * @param array $options['nodeids'] Node IDs
  * @param boolean $options['with_items'] only with items
  * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins
  * @param int $options['count'] count Hosts, returned column name is rowscount
  * @param string $options['pattern'] search hosts by pattern in host names
  * @param int $options['limit'] limit selection
  * @param string $options['order'] deprecated parameter (for now)
  * @return array|boolean Host data as array or false if error
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('screenid', 'name');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     $sqlParts = array('select' => array('screens' => 's.screenid,s.templateid'), 'from' => array('screens' => 'screens s'), 'where' => array('template' => 's.templateid IS NOT NULL'), 'order' => array(), 'group' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'screenids' => null, 'screenitemids' => null, 'templateids' => null, 'hostids' => null, 'editable' => null, 'noInheritance' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectScreenItems' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['screens']);
         $dbTable = DB::getSchema('screens');
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 's.' . $field;
             }
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     if (!is_null($options['editable']) || is_null($options['hostids']) && is_null($options['templateids'])) {
         $options['noInheritance'] = 1;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         // TODO: think how we could combine templateids && hostids options
         if (!is_null($options['templateids'])) {
             unset($options['hostids']);
             $options['templateids'] = API::Template()->get(array('templateids' => $options['templateids'], 'editable' => $options['editable'], 'preservekeys' => true));
             $options['templateids'] = array_keys($options['templateids']);
         } elseif (!is_null($options['hostids'])) {
             $options['templateids'] = API::Host()->get(array('hostids' => $options['hostids'], 'editable' => $options['editable'], 'preservekeys' => true));
             $options['templateids'] = array_keys($options['templateids']);
         } else {
             // TODO: get screen
             $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
             $userGroups = getUserGroupsByUserId($userid);
             $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE s.templateid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
         }
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // screenids
     if (!is_null($options['screenids'])) {
         zbx_value2array($options['screenids']);
         $sqlParts['where'][] = dbConditionInt('s.screenid', $options['screenids']);
     }
     // screenitemids
     if (!is_null($options['screenitemids'])) {
         zbx_value2array($options['screenitemids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['screenitemid'] = 'si.screenitemid';
         }
         $sqlParts['from']['screens_items'] = 'screens_items si';
         $sqlParts['where']['ssi'] = 'si.screenid=s.screenid';
         $sqlParts['where'][] = dbConditionInt('si.screenitemid', $options['screenitemids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (isset($options['hostids']) && !is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         // collecting template chain
         $templatesChain = array();
         $linkedTemplateids = $options['hostids'];
         $childTemplateids = $options['hostids'];
         while (is_null($options['noInheritance']) && !empty($childTemplateids)) {
             $sql = 'SELECT ht.*' . ' FROM hosts_templates ht' . ' WHERE ' . dbConditionInt('hostid', $childTemplateids);
             $dbTemplates = DBselect($sql);
             $childTemplateids = array();
             while ($link = DBfetch($dbTemplates)) {
                 $childTemplateids[$link['templateid']] = $link['templateid'];
                 $linkedTemplateids[$link['templateid']] = $link['templateid'];
                 createParentToChildRelation($templatesChain, $link, 'templateid', 'hostid');
             }
         }
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['templateid'] = 's.templateid';
         }
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['templateid'] = 's.templateid';
         }
         $sqlParts['where']['templateid'] = dbConditionInt('s.templateid', $linkedTemplateids);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('screens s', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('screens s', $options, $sqlParts);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['screens'] = 's.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('count(DISTINCT s.screenid) as rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // sorting
     zbx_db_sorting($sqlParts, $options, $sortColumns, 's');
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $screenids = array();
     $sqlParts['select'] = array_unique($sqlParts['select']);
     $sqlParts['from'] = array_unique($sqlParts['from']);
     $sqlParts['where'] = array_unique($sqlParts['where']);
     $sqlParts['group'] = array_unique($sqlParts['group']);
     $sqlParts['order'] = array_unique($sqlParts['order']);
     $sqlSelect = '';
     $sqlFrom = '';
     $sqlWhere = '';
     $sqlGroup = '';
     $sqlOrder = '';
     if (!empty($sqlParts['select'])) {
         $sqlSelect .= implode(',', $sqlParts['select']);
     }
     if (!empty($sqlParts['from'])) {
         $sqlFrom .= implode(',', $sqlParts['from']);
     }
     if (!empty($sqlParts['where'])) {
         $sqlWhere .= ' AND ' . implode(' AND ', $sqlParts['where']);
     }
     if (!empty($sqlParts['group'])) {
         $sqlGroup .= ' GROUP BY ' . implode(',', $sqlParts['group']);
     }
     if (!empty($sqlParts['order'])) {
         $sqlOrder .= ' ORDER BY ' . implode(',', $sqlParts['order']);
     }
     $sqlLimit = $sqlParts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sqlParts) . ' ' . $sqlSelect . ' FROM ' . $sqlFrom . ' WHERE ' . DBin_node('s.screenid', $nodeids) . $sqlWhere . $sqlGroup . $sqlOrder;
     $res = DBselect($sql, $sqlLimit);
     while ($screen = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $screen;
             } else {
                 $result = $screen['rowscount'];
             }
         } else {
             $screenids[$screen['screenid']] = $screen['screenid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$screen['screenid']] = array('screenid' => $screen['screenid'], 'templateid' => $screen['templateid']);
             } else {
                 if (!isset($result[$screen['screenid']])) {
                     $result[$screen['screenid']] = array();
                 }
                 if (!is_null($options['selectScreenItems']) && !isset($result[$screen['screenid']]['screenitems'])) {
                     $result[$screen['screenid']]['screenitems'] = array();
                 }
                 if (isset($screen['screenitemid']) && is_null($options['selectScreenItems'])) {
                     if (!isset($result[$screen['screenid']]['screenitems'])) {
                         $result[$screen['screenid']]['screenitems'] = array();
                     }
                     $result[$screen['screenid']]['screenitems'][] = array('screenitemid' => $screen['screenitemid']);
                     unset($screen['screenitemid']);
                 }
                 $result[$screen['screenid']] += $screen;
             }
         }
     }
     // hashing
     $options['hostids'] = zbx_toHash($options['hostids']);
     // adding screenitems
     if (!is_null($options['selectScreenItems']) && str_in_array($options['selectScreenItems'], $subselectsAllowedOutputs)) {
         $screensItems = array();
         $dbSitems = DBselect('SELECT si.* FROM screens_items si WHERE ' . dbConditionInt('si.screenid', $screenids));
         while ($sitem = DBfetch($dbSitems)) {
             // sorting
             $screensItems[$sitem['screenitemid']] = $sitem;
             switch ($sitem['resourcetype']) {
                 case SCREEN_RESOURCE_GRAPH:
                     $graphids[$sitem['resourceid']] = $sitem['resourceid'];
                     break;
                 case SCREEN_RESOURCE_SIMPLE_GRAPH:
                 case SCREEN_RESOURCE_PLAIN_TEXT:
                     $itemids[$sitem['resourceid']] = $sitem['resourceid'];
                     break;
             }
         }
         foreach ($screensItems as $sitem) {
             if (!isset($result[$sitem['screenid']]['screenitems'])) {
                 $result[$sitem['screenid']]['screenitems'] = array();
             }
             $result[$sitem['screenid']]['screenitems'][] = $sitem;
         }
     }
     // creating linkage of template -> real objects
     if (!is_null($options['selectScreenItems']) && !is_null($options['hostids'])) {
         // prepare graphs
         if (!empty($graphids)) {
             $tplGraphs = API::Graph()->get(array('output' => array('graphid', 'name'), 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
             $dbGraphs = API::Graph()->get(array('output' => array('graphid', 'name'), 'hostids' => $options['hostids'], 'filter' => array('name' => zbx_objectValues($tplGraphs, 'name')), 'nopermissions' => true, 'preservekeys' => true));
             $realGraphs = array();
             foreach ($dbGraphs as $graph) {
                 $host = reset($graph['hosts']);
                 unset($graph['hosts']);
                 if (!isset($realGraphs[$host['hostid']])) {
                     $realGraphs[$host['hostid']] = array();
                 }
                 $realGraphs[$host['hostid']][$graph['name']] = $graph;
             }
         }
         // prepare items
         if (!empty($itemids)) {
             $tplItems = API::Item()->get(array('output' => array('itemid', 'key_'), 'itemids' => $itemids, 'nopermissions' => true, 'preservekeys' => true));
             $dbItems = API::Item()->get(array('output' => array('itemid', 'key_'), 'hostids' => $options['hostids'], 'filter' => array('key_' => zbx_objectValues($tplItems, 'key_')), 'nopermissions' => true, 'preservekeys' => true));
             $realItems = array();
             foreach ($dbItems as $item) {
                 unset($item['hosts']);
                 if (!isset($realItems[$item['hostid']])) {
                     $realItems[$item['hostid']] = array();
                 }
                 $realItems[$item['hostid']][$item['key_']] = $item;
             }
         }
     }
     if (is_null($options['countOutput']) || !is_null($options['countOutput']) && !is_null($options['groupCount'])) {
         // creating copies of templated screens (inheritance)
         // screenNum is needed due to we can't refer to screenid/hostid/templateid as they will repeat
         $screenNum = 0;
         $vrtResult = array();
         foreach ($result as $screen) {
             if (is_null($options['hostids']) || isset($options['hostids'][$screen['templateid']])) {
                 $screenNum++;
                 $vrtResult[$screenNum] = $screen;
                 $vrtResult[$screenNum]['hostid'] = $screen['templateid'];
             }
             if (!isset($templatesChain[$screen['templateid']])) {
                 continue;
             }
             foreach ($templatesChain[$screen['templateid']] as $hostid) {
                 if (!isset($options['hostids'][$hostid])) {
                     continue;
                 }
                 $screenNum++;
                 $vrtResult[$screenNum] = $screen;
                 $vrtResult[$screenNum]['hostid'] = $hostid;
                 if (!isset($vrtResult[$screenNum]['screenitems'])) {
                     continue;
                 }
                 foreach ($vrtResult[$screenNum]['screenitems'] as &$screenitem) {
                     switch ($screenitem['resourcetype']) {
                         case SCREEN_RESOURCE_GRAPH:
                             $graphName = $tplGraphs[$screenitem['resourceid']]['name'];
                             $screenitem['real_resourceid'] = $realGraphs[$hostid][$graphName]['graphid'];
                             break;
                         case SCREEN_RESOURCE_SIMPLE_GRAPH:
                         case SCREEN_RESOURCE_PLAIN_TEXT:
                             $itemKey = $tplItems[$screenitem['resourceid']]['key_'];
                             $screenitem['real_resourceid'] = $realItems[$hostid][$itemKey]['itemid'];
                             break;
                     }
                 }
                 unset($screenitem);
             }
         }
         $result = array_values($vrtResult);
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     } elseif (!is_null($options['noInheritance'])) {
         $result = zbx_toHash($result, 'screenid');
     }
     return $result;
 }
Beispiel #2
0
 /**
  * Get screen data.
  *
  * @param array  $options
  * @param array  $options['nodeids']		Node IDs
  * @param bool   $options['with_items']		only with items
  * @param bool   $options['editable']		only with read-write permission. Ignored for SuperAdmins
  * @param int    $options['count']			count Hosts, returned column name is rowscount
  * @param string $options['pattern']		search hosts by pattern in host names
  * @param int    $options['limit']			limit selection
  * @param string $options['order']			deprecated parameter (for now)
  *
  * @return array
  */
 public function get($options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     $sqlParts = array('select' => array('screens' => 's.screenid,s.templateid'), 'from' => array('screens' => 'screens s'), 'where' => array('template' => 's.templateid IS NOT NULL'), 'order' => array(), 'group' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'screenids' => null, 'screenitemids' => null, 'templateids' => null, 'hostids' => null, 'editable' => null, 'noInheritance' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'output' => API_OUTPUT_REFER, 'selectScreenItems' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (!is_null($options['editable']) || is_null($options['hostids']) && is_null($options['templateids'])) {
         $options['noInheritance'] = 1;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         // TODO: think how we could combine templateids && hostids options
         if (!is_null($options['templateids'])) {
             unset($options['hostids']);
             $options['templateids'] = API::Template()->get(array('templateids' => $options['templateids'], 'editable' => $options['editable'], 'preservekeys' => true));
             $options['templateids'] = array_keys($options['templateids']);
         } elseif (!is_null($options['hostids'])) {
             $options['templateids'] = API::Host()->get(array('hostids' => $options['hostids'], 'editable' => $options['editable'], 'preservekeys' => true));
             $options['templateids'] = array_keys($options['templateids']);
         } else {
             // TODO: get screen
             $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
             $userGroups = getUserGroupsByUserId($userid);
             $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE s.templateid=hgg.hostid' . ' GROUP BY hgg.hostid' . ' HAVING MIN(r.permission)>' . PERM_DENY . ' AND MAX(r.permission)>=' . $permission . ')';
         }
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // screenids
     if (!is_null($options['screenids'])) {
         zbx_value2array($options['screenids']);
         $sqlParts['where'][] = dbConditionInt('s.screenid', $options['screenids']);
     }
     // screenitemids
     if (!is_null($options['screenitemids'])) {
         zbx_value2array($options['screenitemids']);
         if ($options['output'] != API_OUTPUT_EXTEND) {
             $sqlParts['select']['screenitemid'] = 'si.screenitemid';
         }
         $sqlParts['from']['screens_items'] = 'screens_items si';
         $sqlParts['where']['ssi'] = 'si.screenid=s.screenid';
         $sqlParts['where'][] = dbConditionInt('si.screenitemid', $options['screenitemids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (isset($options['hostids']) && !is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     $templatesChain = array();
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         // collecting template chain
         $linkedTemplateids = $options['hostids'];
         $childTemplateids = $options['hostids'];
         while (is_null($options['noInheritance']) && !empty($childTemplateids)) {
             $sql = 'SELECT ht.*' . ' FROM hosts_templates ht' . ' WHERE ' . dbConditionInt('hostid', $childTemplateids);
             $dbTemplates = DBselect($sql);
             $childTemplateids = array();
             while ($link = DBfetch($dbTemplates)) {
                 $childTemplateids[$link['templateid']] = $link['templateid'];
                 $linkedTemplateids[$link['templateid']] = $link['templateid'];
                 createParentToChildRelation($templatesChain, $link, 'templateid', 'hostid');
             }
         }
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['templateid'] = 's.templateid';
         }
         $sqlParts['where']['templateid'] = dbConditionInt('s.templateid', $linkedTemplateids);
     }
     // filter
     if (is_array($options['filter'])) {
         $this->dbFilter('screens s', $options, $sqlParts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('screens s', $options, $sqlParts);
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sqlParts['limit'] = $options['limit'];
     }
     $sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     $res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
     while ($screen = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $screen;
             } else {
                 $result = $screen['rowscount'];
             }
         } else {
             if (!isset($result[$screen['screenid']])) {
                 $result[$screen['screenid']] = array();
             }
             if (isset($screen['screenitemid']) && is_null($options['selectScreenItems'])) {
                 if (!isset($result[$screen['screenid']]['screenitems'])) {
                     $result[$screen['screenid']]['screenitems'] = array();
                 }
                 $result[$screen['screenid']]['screenitems'][] = array('screenitemid' => $screen['screenitemid']);
                 unset($screen['screenitemid']);
             }
             $result[$screen['screenid']] += $screen;
         }
     }
     if ($options['countOutput'] !== null && $options['groupCount'] === null) {
         return $result;
     }
     $screenIds = array_keys($result);
     // adding screenitems
     if ($options['selectScreenItems'] !== null && $options['selectScreenItems'] != API_OUTPUT_COUNT) {
         $screenItems = API::getApi()->select('screens_items', array('output' => $this->outputExtend('screens_items', array('screenid', 'screenitemid', 'resourcetype', 'resourceid'), $options['selectScreenItems']), 'filter' => array('screenid' => $screenIds), 'preservekeys' => true, 'nodeids' => get_current_nodeid(true)));
         $relationMap = $this->createRelationMap($screenItems, 'screenid', 'screenitemid');
         foreach ($screenItems as $screenItem) {
             switch ($screenItem['resourcetype']) {
                 case SCREEN_RESOURCE_GRAPH:
                     $graphids[$screenItem['resourceid']] = $screenItem['resourceid'];
                     break;
                 case SCREEN_RESOURCE_SIMPLE_GRAPH:
                 case SCREEN_RESOURCE_PLAIN_TEXT:
                     $itemids[$screenItem['resourceid']] = $screenItem['resourceid'];
                     break;
             }
         }
         $screenItems = $this->unsetExtraFields($screenItems, array('screenid', 'screenitemid', 'resourceid', 'resourcetype'), $options['selectScreenItems']);
         $result = $relationMap->mapMany($result, $screenItems, 'screenitems');
     }
     // creating linkage of template -> real objects
     if (!is_null($options['selectScreenItems']) && !is_null($options['hostids'])) {
         // prepare graphs
         if (!empty($graphids)) {
             $tplGraphs = API::Graph()->get(array('output' => array('graphid', 'name'), 'graphids' => $graphids, 'nopermissions' => true, 'preservekeys' => true));
             $dbGraphs = API::Graph()->get(array('output' => array('graphid', 'name'), 'selectHosts' => array('hostid'), 'hostids' => $options['hostids'], 'filter' => array('name' => zbx_objectValues($tplGraphs, 'name')), 'nopermissions' => true, 'preservekeys' => true));
             $realGraphs = array();
             foreach ($dbGraphs as $graph) {
                 $host = reset($graph['hosts']);
                 unset($graph['hosts']);
                 if (!isset($realGraphs[$host['hostid']])) {
                     $realGraphs[$host['hostid']] = array();
                 }
                 $realGraphs[$host['hostid']][$graph['name']] = $graph;
             }
         }
         // prepare items
         if (!empty($itemids)) {
             $tplItems = API::Item()->get(array('output' => array('itemid', 'key_', 'hostid'), 'itemids' => $itemids, 'nopermissions' => true, 'preservekeys' => true));
             $dbItems = API::Item()->get(array('output' => array('itemid', 'key_', 'hostid'), 'hostids' => $options['hostids'], 'filter' => array('key_' => zbx_objectValues($tplItems, 'key_')), 'nopermissions' => true, 'preservekeys' => true));
             $realItems = array();
             foreach ($dbItems as $item) {
                 unset($item['hosts']);
                 if (!isset($realItems[$item['hostid']])) {
                     $realItems[$item['hostid']] = array();
                 }
                 $realItems[$item['hostid']][$item['key_']] = $item;
             }
         }
     }
     // hashing
     $options['hostids'] = zbx_toHash($options['hostids']);
     if (is_null($options['countOutput']) || !is_null($options['countOutput']) && !is_null($options['groupCount'])) {
         // creating copies of templated screens (inheritance)
         // screenNum is needed due to we can't refer to screenid/hostid/templateid as they will repeat
         $screenNum = 0;
         $vrtResult = array();
         foreach ($result as $screen) {
             if (is_null($options['hostids']) || isset($options['hostids'][$screen['templateid']])) {
                 $screenNum++;
                 $vrtResult[$screenNum] = $screen;
                 $vrtResult[$screenNum]['hostid'] = $screen['templateid'];
             }
             if (!isset($templatesChain[$screen['templateid']])) {
                 continue;
             }
             foreach ($templatesChain[$screen['templateid']] as $hostid) {
                 if (!isset($options['hostids'][$hostid])) {
                     continue;
                 }
                 $screenNum++;
                 $vrtResult[$screenNum] = $screen;
                 $vrtResult[$screenNum]['hostid'] = $hostid;
                 if (!isset($vrtResult[$screenNum]['screenitems'])) {
                     continue;
                 }
                 foreach ($vrtResult[$screenNum]['screenitems'] as &$screenitem) {
                     switch ($screenitem['resourcetype']) {
                         case SCREEN_RESOURCE_GRAPH:
                             $graphName = $tplGraphs[$screenitem['resourceid']]['name'];
                             $screenitem['real_resourceid'] = $realGraphs[$hostid][$graphName]['graphid'];
                             break;
                         case SCREEN_RESOURCE_SIMPLE_GRAPH:
                         case SCREEN_RESOURCE_PLAIN_TEXT:
                             $itemKey = $tplItems[$screenitem['resourceid']]['key_'];
                             $screenitem['real_resourceid'] = $realItems[$hostid][$itemKey]['itemid'];
                             break;
                     }
                 }
                 unset($screenitem);
             }
         }
         $result = array_values($vrtResult);
     }
     if (!is_null($options['countOutput'])) {
         return $result;
     }
     if ($result) {
         $result = $this->unsetExtraFields($result, array('templateid'), $options['output']);
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     } elseif (!is_null($options['noInheritance'])) {
         $result = zbx_toHash($result, 'screenid');
     }
     return $result;
 }