Updated device-groups functions to support returning all data or just group id

This commit is contained in:
laf
2015-12-13 17:20:34 +00:00
parent d0306ae8ae
commit d5b6e0e8da
2 changed files with 15 additions and 23 deletions

View File

@@ -1024,7 +1024,7 @@ function get_device_groups() {
// use hostname as device_id if it's all digits // use hostname as device_id if it's all digits
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (is_numeric($device_id)) { if (is_numeric($device_id)) {
$groups = GetFullGroupsFromDevice($device_id); $groups = GetGroupsFromDevice($device_id,1);
} }
else { else {
$groups = GetDeviceGroups(); $groups = GetDeviceGroups();

View File

@@ -31,7 +31,7 @@
* @param string $search What to searchid for * @param string $search What to searchid for
* @return string * @return string
*/ */
function GenGroupSQL($pattern, $search='') { function GenGroupSQL($pattern, $search='',$extra=0) {
$pattern = RunGroupMacros($pattern); $pattern = RunGroupMacros($pattern);
if ($pattern === false) { if ($pattern === false) {
return false; return false;
@@ -66,7 +66,11 @@ function GenGroupSQL($pattern, $search='') {
$search .= ' &&'; $search .= ' &&';
} }
$sql = 'SELECT DISTINCT('.str_replace('(', '', $tables[0]).'.device_id),`devices`.* FROM '.implode(',', $tables).' WHERE '.$search.' ('.str_replace(array('%', '@', '!~', '~'), array('', '.*', 'NOT REGEXP', 'REGEXP'), $pattern).')'; $sql_extra = '';
if ($extra === 1) {
$sql_extra = ",`devices`.*";
}
$sql = 'SELECT DISTINCT('.str_replace('(', '', $tables[0]).'.device_id)'.$sql_extra.' FROM '.implode(',', $tables).' WHERE '.$search.' ('.str_replace(array('%', '@', '!~', '~'), array('', '.*', 'NOT REGEXP', 'REGEXP'), $pattern).')';
return $sql; return $sql;
}//end GenGroupSQL() }//end GenGroupSQL()
@@ -104,28 +108,16 @@ function GetDeviceGroups() {
* @param integer $device Device-ID * @param integer $device Device-ID
* @return array * @return array
*/ */
function GetGroupsFromDevice($device) { function GetGroupsFromDevice($device,$extra=0) {
$ret = array(); $ret = array();
foreach (GetDeviceGroups() as $group) { foreach (GetDeviceGroups() as $group) {
if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?').' LIMIT 1', array($device)) == $device) { if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?',$extra).' LIMIT 1', array($device)) == $device) {
$ret[] = $group['id']; if ($extra === 0) {
} $ret[] = $group['id'];
} }
else {
return $ret; $ret[] = $group;
}
}//end GetGroupsFromDevice()
/**
* Get all groups of Device
* @param integer $device Device-ID
* @return array
*/
function GetFullGroupsFromDevice($device) {
$ret = array();
foreach (GetDeviceGroups() as $group) {
if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?').' LIMIT 1', array($device)) == $device) {
$ret[] = $group;
} }
} }