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

@@ -31,7 +31,7 @@
* @param string $search What to searchid for
* @return string
*/
function GenGroupSQL($pattern, $search='') {
function GenGroupSQL($pattern, $search='',$extra=0) {
$pattern = RunGroupMacros($pattern);
if ($pattern === false) {
return false;
@@ -66,7 +66,11 @@ function GenGroupSQL($pattern, $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;
}//end GenGroupSQL()
@@ -104,28 +108,16 @@ function GetDeviceGroups() {
* @param integer $device Device-ID
* @return array
*/
function GetGroupsFromDevice($device) {
function GetGroupsFromDevice($device,$extra=0) {
$ret = array();
foreach (GetDeviceGroups() as $group) {
if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?').' LIMIT 1', array($device)) == $device) {
$ret[] = $group['id'];
}
}
return $ret;
}//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;
if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?',$extra).' LIMIT 1', array($device)) == $device) {
if ($extra === 0) {
$ret[] = $group['id'];
}
else {
$ret[] = $group;
}
}
}