break;
     case 'year':
         $period_start = mktime(0, 0, 0, 1, 1, date('Y'));
         break;
     case 24:
     case 24 * 7:
     case 24 * 30:
     case 24 * DAY_IN_YEAR:
         $period_start = $period_end - $period * 3600;
         break;
 }
 // fetch services
 $services = API::Service()->get(array('output' => array('name', 'serviceid', 'showsla', 'goodsla', 'algorithm'), 'selectParent' => array('serviceid'), 'selectDependencies' => array('servicedownid', 'soft', 'linkid'), 'selectTrigger' => array('description', 'triggerid', 'expression'), 'preservekeys' => true, 'sortfield' => 'sortorder', 'sortorder' => ZBX_SORT_UP));
 // expand trigger descriptions
 $triggers = zbx_objectValues($services, 'trigger');
 $triggers = CTriggerHelper::batchExpandDescription($triggers);
 foreach ($services as &$service) {
     if ($service['trigger']) {
         $service['trigger'] = $triggers[$service['trigger']['triggerid']];
     }
 }
 unset($service);
 // fetch sla
 $slaData = API::Service()->getSla(array('intervals' => array(array('from' => $period_start, 'to' => $period_end))));
 // expand problem trigger descriptions
 foreach ($slaData as &$serviceSla) {
     foreach ($serviceSla['problems'] as &$problemTrigger) {
         $problemTrigger['description'] = $triggers[$problemTrigger['triggerid']]['description'];
     }
     unset($problemTrigger);
 }
 /**
  * Get Triggers data
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  *
  * @return array|int item data as array or false if error
  */
 public function get(array $options = array())
 {
     $result = array();
     $userType = self::$userData['type'];
     $userid = self::$userData['userid'];
     // allowed columns for sorting
     $sortColumns = array('triggerid', 'description', 'status', 'priority', 'lastchange', 'hostname');
     // allowed output options for [ select_* ] params
     $subselectsAllowedOutputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     $fieldsToUnset = array();
     $sqlParts = array('select' => array('triggers' => 't.triggerid'), 'from' => array('t' => 'triggers t'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $defOptions = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'triggerids' => null, 'itemids' => null, 'applicationids' => null, 'functions' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'active' => null, 'maintenance' => null, 'withUnacknowledgedEvents' => null, 'withAcknowledgedEvents' => null, 'withLastEventUnacknowledged' => null, 'skipDependent' => null, 'nopermissions' => null, 'editable' => null, 'lastChangeSince' => null, 'lastChangeTill' => null, 'group' => null, 'host' => null, 'only_true' => null, 'min_severity' => null, 'filter' => null, 'search' => null, 'searchByAny' => null, 'startSearch' => null, 'excludeSearch' => null, 'searchWildcardsEnabled' => null, 'expandData' => null, 'expandDescription' => null, 'expandExpression' => null, 'output' => API_OUTPUT_REFER, 'selectGroups' => null, 'selectHosts' => null, 'selectItems' => null, 'selectFunctions' => null, 'selectDependencies' => null, 'selectDiscoveryRule' => null, 'selectLastEvent' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($defOptions, $options);
     if (is_array($options['output'])) {
         unset($sqlParts['select']['triggers']);
         $dbTable = DB::getSchema('triggers');
         $sqlParts['select']['triggerid'] = ' t.triggerid';
         foreach ($options['output'] as $field) {
             if (isset($dbTable['fields'][$field])) {
                 $sqlParts['select'][$field] = 't.' . $field;
             }
         }
         if (!is_null($options['expandDescription'])) {
             if (!str_in_array('description', $options['output'])) {
                 $options['expandDescription'] = null;
             } else {
                 if (!str_in_array('expression', $options['output'])) {
                     $sqlParts['select']['expression'] = ' t.expression';
                     $fieldsToUnset[] = 'expression';
                 }
             }
         }
         // ignore the "expandExpression" parameter if the expression is not requested
         if ($options['expandExpression'] !== null && !str_in_array('expression', $options['output'])) {
             $options['expandExpression'] = null;
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $userGroups = getUserGroupsByUserId($userid);
         $sqlParts['where'][] = 'EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts_groups hgg' . ' JOIN rights r' . ' ON r.id=hgg.groupid' . ' AND ' . dbConditionInt('r.groupid', $userGroups) . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=hgg.hostid' . ' GROUP BY f.triggerid' . ' HAVING MIN(r.permission)>=' . $permission . ')';
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         sort($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['groupid'] = 'hg.groupid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['groupid'] = dbConditionInt('hg.groupid', $options['groupids']);
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!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']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['hostid'] = 'i.hostid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['i'] = 'i.hostid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sqlParts['where']['triggerid'] = dbConditionInt('t.triggerid', $options['triggerids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['itemid'] = 'f.itemid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where']['itemid'] = dbConditionInt('f.itemid', $options['itemids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         if (!is_null($options['groupCount'])) {
             $sqlParts['group']['f'] = 'f.itemid';
         }
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['applicationid'] = 'a.applicationid';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['applications'] = 'applications a';
         $sqlParts['where']['a'] = dbConditionInt('a.applicationid', $options['applicationids']);
         $sqlParts['where']['ia'] = 'i.hostid=a.hostid';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // functions
     if (!is_null($options['functions'])) {
         zbx_value2array($options['functions']);
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where'][] = dbConditionString('f.function', $options['functions']);
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sqlParts['where']['monitored'] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts h' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND (' . 'i.status<>' . ITEM_STATUS_ACTIVE . ' OR h.status<>' . HOST_STATUS_MONITORED . ')' . ')';
         $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // active
     if (!is_null($options['active'])) {
         $sqlParts['where']['active'] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts h' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND h.status<>' . HOST_STATUS_MONITORED . ')';
         $sqlParts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // maintenance
     if (!is_null($options['maintenance'])) {
         $sqlParts['where'][] = ($options['maintenance'] == 0 ? 'NOT ' : '') . 'EXISTS (' . 'SELECT NULL' . ' FROM functions f,items i,hosts h' . ' WHERE t.triggerid=f.triggerid' . ' AND f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND h.maintenance_status=' . HOST_MAINTENANCE_STATUS_ON . ')';
         $sqlParts['where'][] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // lastChangeSince
     if (!is_null($options['lastChangeSince'])) {
         $sqlParts['where']['lastchangesince'] = 't.lastchange>' . zbx_dbstr($options['lastChangeSince']);
     }
     // lastChangeTill
     if (!is_null($options['lastChangeTill'])) {
         $sqlParts['where']['lastchangetill'] = 't.lastchange<' . zbx_dbstr($options['lastChangeTill']);
     }
     // withUnacknowledgedEvents
     if (!is_null($options['withUnacknowledgedEvents'])) {
         $sqlParts['where']['unack'] = 'EXISTS (' . 'SELECT NULL' . ' FROM events e' . ' WHERE t.triggerid=e.objectid' . ' AND e.object=' . EVENT_OBJECT_TRIGGER . ' AND e.value_changed=' . TRIGGER_VALUE_CHANGED_YES . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=' . EVENT_NOT_ACKNOWLEDGED . ')';
     }
     // withAcknowledgedEvents
     if (!is_null($options['withAcknowledgedEvents'])) {
         $sqlParts['where']['ack'] = 'NOT EXISTS (' . 'SELECT NULL' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=' . EVENT_OBJECT_TRIGGER . ' AND e.value_changed=' . TRIGGER_VALUE_CHANGED_YES . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=' . EVENT_NOT_ACKNOWLEDGED . ')';
     }
     // templated
     if (!is_null($options['templated'])) {
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sqlParts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sqlParts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sqlParts['where'][] = 't.templateid IS NOT NULL';
         } else {
             $sqlParts['where'][] = 't.templateid IS NULL';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('triggers t', $options, $sqlParts);
     }
     // filter
     if (is_null($options['filter'])) {
         $options['filter'] = array();
     }
     if (is_array($options['filter'])) {
         if (!array_key_exists('flags', $options['filter'])) {
             $options['filter']['flags'] = array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED);
         }
         $this->dbFilter('triggers t', $options, $sqlParts);
         if (isset($options['filter']['host']) && !is_null($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sqlParts['from']['functions'] = 'functions f';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
             $sqlParts['from']['hosts'] = 'hosts h';
             $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
             $sqlParts['where']['host'] = dbConditionString('h.host', $options['filter']['host']);
         }
         if (isset($options['filter']['hostid']) && !is_null($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sqlParts['from']['functions'] = 'functions f';
             $sqlParts['from']['items'] = 'items i';
             $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
             $sqlParts['where']['hostid'] = dbConditionInt('i.hostid', $options['filter']['hostid']);
         }
     }
     // group
     if (!is_null($options['group'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['name'] = 'g.name';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
         $sqlParts['from']['groups'] = 'groups g';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sqlParts['where']['ghg'] = 'g.groupid = hg.groupid';
         $sqlParts['where']['group'] = ' g.name=' . zbx_dbstr($options['group']);
     }
     // host
     if (!is_null($options['host'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sqlParts['select']['host'] = 'h.host';
         }
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['i'] = dbConditionInt('i.hostid', $options['hostids']);
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
         $sqlParts['where']['host'] = ' h.host=' . zbx_dbstr($options['host']);
     }
     // only_true
     if (!is_null($options['only_true'])) {
         $config = select_config();
         $sqlParts['where']['ot'] = '((t.value=' . TRIGGER_VALUE_TRUE . ')' . ' OR ' . '((t.value=' . TRIGGER_VALUE_FALSE . ') AND (t.lastchange>' . (time() - $config['ok_period']) . ')))';
     }
     // min_severity
     if (!is_null($options['min_severity'])) {
         $sqlParts['where'][] = 't.priority>=' . zbx_dbstr($options['min_severity']);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sqlParts['select']['triggers'] = 't.*';
     }
     // expandData
     if (!is_null($options['expandData'])) {
         $sqlParts['select']['hostname'] = 'h.name AS hostname';
         $sqlParts['select']['host'] = 'h.host';
         $sqlParts['select']['hostid'] = 'h.hostid';
         $sqlParts['from']['functions'] = 'functions f';
         $sqlParts['from']['items'] = 'items i';
         $sqlParts['from']['hosts'] = 'hosts h';
         $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sqlParts['where']['fi'] = 'f.itemid=i.itemid';
         $sqlParts['where']['hi'] = 'h.hostid=i.hostid';
     }
     // count or grouped counts via direct SQL count
     if (!is_null($options['countOutput']) && !$this->requiresPostSqlFiltering($options)) {
         $options['sortfield'] = '';
         $sqlParts['select'] = array('COUNT(DISTINCT t.triggerid) as rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sqlParts['group'] as $key => $fields) {
                 $sqlParts['select'][$key] = $fields;
             }
         }
     }
     // sorting
     if (!zbx_empty($options['sortfield'])) {
         if (!is_array($options['sortfield'])) {
             $options['sortfield'] = array($options['sortfield']);
         }
         foreach ($options['sortfield'] as $i => $sortfield) {
             // validate sortfield
             if (!str_in_array($sortfield, $sortColumns)) {
                 throw new APIException(ZBX_API_ERROR_INTERNAL, _s('Sorting by field "%s" not allowed.', $sortfield));
             }
             // add sort field to order
             $sortorder = '';
             if (is_array($options['sortorder'])) {
                 if (!empty($options['sortorder'][$i])) {
                     $sortorder = $options['sortorder'][$i] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : '';
                 }
             } else {
                 $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : '';
             }
             // we will be using lastchange for ordering in any case
             if (!str_in_array('t.lastchange', $sqlParts['select']) && !str_in_array('t.*', $sqlParts['select'])) {
                 $sqlParts['select']['lastchange'] = 't.lastchange';
             }
             switch ($sortfield) {
                 case 'hostname':
                     // the only way to sort by host name is to get it like this:
                     // triggers -> functions -> items -> hosts
                     $sqlParts['select']['hostname'] = 'h.name';
                     $sqlParts['from']['functions'] = 'functions f';
                     $sqlParts['from']['items'] = 'items i';
                     $sqlParts['from']['hosts'] = 'hosts h';
                     $sqlParts['where'][] = 't.triggerid = f.triggerid';
                     $sqlParts['where'][] = 'f.itemid = i.itemid';
                     $sqlParts['where'][] = 'i.hostid = h.hostid';
                     $sqlParts['order'][] = 'h.name ' . $sortorder;
                     break;
                 case 'lastchange':
                     $sqlParts['order'][] = $sortfield . ' ' . $sortorder;
                     break;
                 default:
                     // if lastchange is not used for ordering, it should be the second order criteria
                     $sqlParts['order'][] = 't.' . $sortfield . ' ' . $sortorder;
                     break;
             }
             // add sort field to select if distinct is used
             if (count($sqlParts['from']) > 1) {
                 if (!str_in_array('t.' . $sortfield, $sqlParts['select']) && !str_in_array('t.*', $sqlParts['select'])) {
                     $sqlParts['select'][$sortfield] = 't.' . $sortfield;
                 }
             }
         }
         if (!empty($sqlParts['order'])) {
             $sqlParts['order'][] = 't.lastchange DESC';
         }
     }
     // limit
     if (!zbx_ctype_digit($options['limit']) || !$options['limit']) {
         $options['limit'] = null;
     }
     $sqlParts = $this->applyQueryNodeOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
     // return count or grouped counts via direct SQL count
     if (!is_null($options['countOutput']) && !$this->requiresPostSqlFiltering($options)) {
         $dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $options['limit']);
         while ($trigger = DBfetch($dbRes)) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $trigger;
             } else {
                 $result = $trigger['rowscount'];
             }
         }
         return $result;
     }
     $triggers = zbx_toHash($this->customFetch($this->createSelectQueryFromParts($sqlParts), $options), 'triggerid');
     // return count for post SQL filtered result sets
     if (!is_null($options['countOutput'])) {
         return count($triggers);
     }
     $triggerids = array_keys($triggers);
     sort($triggerids);
     // format result array
     foreach ($triggers as $trigger) {
         if ($options['output'] == API_OUTPUT_SHORTEN) {
             $result[$trigger['triggerid']] = array('triggerid' => $trigger['triggerid']);
         } else {
             if (!isset($result[$trigger['triggerid']])) {
                 $result[$trigger['triggerid']] = array();
             }
             if (!is_null($options['selectHosts']) && !isset($result[$trigger['triggerid']]['hosts'])) {
                 $result[$trigger['triggerid']]['hosts'] = array();
             }
             if (!is_null($options['selectItems']) && !isset($result[$trigger['triggerid']]['items'])) {
                 $result[$trigger['triggerid']]['items'] = array();
             }
             if (!is_null($options['selectFunctions']) && !isset($result[$trigger['triggerid']]['functions'])) {
                 $result[$trigger['triggerid']]['functions'] = array();
             }
             if (!is_null($options['selectDependencies']) && !isset($result[$trigger['triggerid']]['dependencies'])) {
                 $result[$trigger['triggerid']]['dependencies'] = array();
             }
             if (!is_null($options['selectDiscoveryRule']) && !isset($result[$trigger['triggerid']]['discoveryRule'])) {
                 $result[$trigger['triggerid']]['discoveryRule'] = array();
             }
             $result[$trigger['triggerid']] += $trigger;
         }
     }
     /*
      * Adding objects
      */
     // adding last event
     if (!is_null($options['selectLastEvent']) && str_in_array($options['selectLastEvent'], $subselectsAllowedOutputs)) {
         $select = $options['selectLastEvent'] == API_OUTPUT_REFER ? 'e.eventid, e.objectid' : 'e.*';
         $lastEvents = DBfetchArrayAssoc(DBselect('SELECT ' . $select . ' FROM events e JOIN (' . 'SELECT  max(eventid) as lasteventid FROM events e' . ' WHERE ' . dbConditionInt('e.objectid', $triggerids) . ' AND ' . DBin_node('e.objectid') . ' AND e.object=' . EVENT_SOURCE_TRIGGERS . ' AND e.value_changed=' . TRIGGER_VALUE_CHANGED_YES . ' GROUP BY e.objectid' . ') ee ON e.eventid=ee.lasteventid'), 'objectid');
         foreach ($result as $triggerId => $trigger) {
             $result[$triggerId]['lastEvent'] = isset($lastEvents[$triggerId]) ? $lastEvents[$triggerId] : array();
         }
     }
     // adding trigger dependencies
     if (!is_null($options['selectDependencies']) && str_in_array($options['selectDependencies'], $subselectsAllowedOutputs)) {
         $deps = array();
         $depids = array();
         $dbDeps = DBselect('SELECT td.triggerid_up,td.triggerid_down' . ' FROM trigger_depends td' . ' WHERE ' . dbConditionInt('td.triggerid_down', $triggerids));
         while ($dbDep = DBfetch($dbDeps)) {
             if (!isset($deps[$dbDep['triggerid_down']])) {
                 $deps[$dbDep['triggerid_down']] = array();
             }
             $deps[$dbDep['triggerid_down']][$dbDep['triggerid_up']] = $dbDep['triggerid_up'];
             $depids[] = $dbDep['triggerid_up'];
         }
         $objParams = array('triggerids' => $depids, 'output' => $options['selectDependencies'], 'expandData' => true, 'preservekeys' => true);
         $allowed = $this->get($objParams);
         // allowed triggerids
         foreach ($deps as $triggerid => $deptriggers) {
             foreach ($deptriggers as $deptriggerid) {
                 if (isset($allowed[$deptriggerid])) {
                     $result[$triggerid]['dependencies'][] = $allowed[$deptriggerid];
                 }
             }
         }
     }
     // adding groups
     if ($options['groupids'] !== null && $options['selectGroups'] === null) {
         $options['selectGroups'] = API_OUTPUT_REFER;
     }
     if (!is_null($options['selectGroups']) && str_in_array($options['selectGroups'], $subselectsAllowedOutputs)) {
         $objParams = array('nodeids' => $options['nodeids'], 'output' => $options['selectGroups'], 'triggerids' => $triggerids, 'preservekeys' => true);
         $groups = API::HostGroup()->get($objParams);
         foreach ($groups as $groupid => $group) {
             $gtriggers = $group['triggers'];
             unset($group['triggers']);
             foreach ($gtriggers as $trigger) {
                 $result[$trigger['triggerid']]['groups'][] = $group;
             }
         }
     }
     // adding hosts
     if ($options['hostids'] !== null && $options['selectHosts'] === null) {
         $options['selectHosts'] = API_OUTPUT_REFER;
     }
     if (!is_null($options['selectHosts'])) {
         $objParams = array('nodeids' => $options['nodeids'], 'triggerids' => $triggerids, 'templated_hosts' => true, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectHosts'];
             $hosts = API::Host()->get($objParams);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'host');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['triggers']);
                 $count = array();
                 foreach ($host['triggers'] as $trigger) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$trigger['triggerid']])) {
                             $count[$trigger['triggerid']] = 0;
                         }
                         $count[$trigger['triggerid']]++;
                         if ($count[$trigger['triggerid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$trigger['triggerid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectHosts']) {
                 $objParams['countOutput'] = 1;
                 $objParams['groupCount'] = 1;
                 $hosts = API::Host()->get($objParams);
                 $hosts = zbx_toHash($hosts, 'hostid');
                 foreach ($result as $triggerid => $trigger) {
                     if (isset($hosts[$triggerid])) {
                         $result[$triggerid]['hosts'] = $hosts[$triggerid]['rowscount'];
                     } else {
                         $result[$triggerid]['hosts'] = 0;
                     }
                 }
             }
         }
     }
     // adding functions
     if (!is_null($options['selectFunctions']) && str_in_array($options['selectFunctions'], $subselectsAllowedOutputs)) {
         if ($options['selectFunctions'] == API_OUTPUT_EXTEND) {
             $sqlSelect = 'f.*';
         } else {
             $sqlSelect = 'f.functionid,f.triggerid';
         }
         $res = DBselect('SELECT ' . $sqlSelect . ' FROM functions f' . ' WHERE ' . dbConditionInt('f.triggerid', $triggerids));
         while ($function = DBfetch($res)) {
             $triggerid = $function['triggerid'];
             unset($function['triggerid']);
             $result[$triggerid]['functions'][] = $function;
         }
     }
     // adding items
     if ($options['itemids'] !== null && $options['selectItems'] === null) {
         $options['selectItems'] = API_OUTPUT_REFER;
     }
     if (!is_null($options['selectItems']) && (is_array($options['selectItems']) || str_in_array($options['selectItems'], $subselectsAllowedOutputs))) {
         $objParams = array('nodeids' => $options['nodeids'], 'output' => $options['selectItems'], 'triggerids' => $triggerids, 'webitems' => true, 'nopermissions' => true, 'preservekeys' => true);
         $items = API::Item()->get($objParams);
         foreach ($items as $item) {
             $itriggers = $item['triggers'];
             unset($item['triggers']);
             foreach ($itriggers as $trigger) {
                 $result[$trigger['triggerid']]['items'][] = $item;
             }
         }
     }
     // adding discoveryrule
     if (!is_null($options['selectDiscoveryRule'])) {
         $ruleids = $ruleMap = array();
         $dbRules = DBselect('SELECT id.parent_itemid,td.triggerid' . ' FROM trigger_discovery td,item_discovery id,functions f' . ' WHERE ' . dbConditionInt('td.triggerid', $triggerids) . ' AND td.parent_triggerid=f.triggerid' . ' AND f.itemid=id.itemid');
         while ($rule = DBfetch($dbRules)) {
             $ruleids[$rule['parent_itemid']] = $rule['parent_itemid'];
             $ruleMap[$rule['triggerid']] = $rule['parent_itemid'];
         }
         $objParams = array('nodeids' => $options['nodeids'], 'itemids' => $ruleids, 'nopermissions' => true, 'preservekeys' => true);
         if (is_array($options['selectDiscoveryRule']) || str_in_array($options['selectDiscoveryRule'], $subselectsAllowedOutputs)) {
             $objParams['output'] = $options['selectDiscoveryRule'];
             $discoveryRules = API::DiscoveryRule()->get($objParams);
             foreach ($result as $triggerid => $trigger) {
                 if (isset($ruleMap[$triggerid]) && isset($discoveryRules[$ruleMap[$triggerid]])) {
                     $result[$triggerid]['discoveryRule'] = $discoveryRules[$ruleMap[$triggerid]];
                 }
             }
         }
     }
     // expandDescription
     if (!is_null($options['expandDescription']) && $result && array_key_exists('description', reset($result))) {
         $result = CTriggerHelper::batchExpandDescription($result);
     }
     // expand expression
     if ($options['expandExpression'] !== null) {
         foreach ($result as &$trigger) {
             if ($trigger['expression']) {
                 $trigger['expression'] = explode_exp($trigger['expression'], false, true);
             }
         }
         unset($trigger);
     }
     if (!empty($fieldsToUnset)) {
         foreach ($result as $tnum => $trigger) {
             foreach ($fieldsToUnset as $fieldToUnset) {
                 unset($result[$tnum][$fieldToUnset]);
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }