diff options
author | Jannik Schönartz | 2017-12-14 13:03:44 +0100 |
---|---|---|
committer | Jannik Schönartz | 2017-12-14 13:03:44 +0100 |
commit | 5d5c2f27bee5d4fbd3747555efbf2ac9f337805b (patch) | |
tree | c65898e1b3d6f0f46366a280bbbaf4c6ccbc477c /modules-available/locations/inc/location.inc.php | |
parent | [usb-lock-off] Design changes to fit the design_guidelines. TODO: lang_discar... (diff) | |
parent | [sysconfig] Update translations (diff) | |
download | slx-admin-5d5c2f27bee5d4fbd3747555efbf2ac9f337805b.tar.gz slx-admin-5d5c2f27bee5d4fbd3747555efbf2ac9f337805b.tar.xz slx-admin-5d5c2f27bee5d4fbd3747555efbf2ac9f337805b.zip |
Merge branch 'master' into usb-lock-off
Diffstat (limited to 'modules-available/locations/inc/location.inc.php')
-rw-r--r-- | modules-available/locations/inc/location.inc.php | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/modules-available/locations/inc/location.inc.php b/modules-available/locations/inc/location.inc.php index 1dae4deb..0576e660 100644 --- a/modules-available/locations/inc/location.inc.php +++ b/modules-available/locations/inc/location.inc.php @@ -37,6 +37,11 @@ class Location return Database::queryFirst("SELECT * FROM location WHERE locationid = :locationId", compact('locationId')); } + /** + * Get name of location + * @param int $locationId id of location to get name for + * @return string|false Name of location, false if locationId doesn't exist + */ public static function getName($locationId) { self::getLocationsAssoc(); @@ -46,6 +51,26 @@ class Location return self::$assocLocationCache[$locationId]['locationname']; } + /** + * Get all the names of the given location and its parents, up + * to the root element. Array keys will be locationids, value the names. + * @param int $locationId + * @return array|false locations, from furthest to nearest or false if locationId doesn't exist + */ + public static function getNameChain($locationId) + { + self::getLocationsAssoc(); + settype($locationId, 'int'); + if (!isset(self::$assocLocationCache[$locationId])) + return false; + $ret = array(); + while (isset(self::$assocLocationCache[$locationId])) { + $ret[$locationId] = self::$assocLocationCache[$locationId]['locationname']; + $locationId = self::$assocLocationCache[$locationId]['parentlocationid']; + } + return array_reverse($ret, true); + } + public static function getLocationsAssoc() { if (self::$assocLocationCache === false) { @@ -255,16 +280,25 @@ class Location * Ignores any manually assigned locationid (fixedlocationid). * * @param string $ip IP address of client + * @param bool $honorRoomPlanner consider a fixed location assigned manually by roomplanner * @return bool|int locationid, or false if no match */ - public static function getFromIp($ip) + public static function getFromIp($ip, $honorRoomPlanner = false) { if (Module::get('statistics') !== false) { // Shortcut - try to use subnetlocationid in machine table - $ret = Database::queryFirst("SELECT subnetlocationid FROM machine WHERE clientip = :ip", compact('ip')); + if ($honorRoomPlanner) { + $ret = Database::queryFirst("SELECT locationid AS loc FROM machine + WHERE clientip = :ip + ORDER BY lastseen DESC LIMIT 1", compact('ip')); + } else { + $ret = Database::queryFirst("SELECT subnetlocationid AS loc FROM machine + WHERE clientip = :ip + ORDER BY lastseen DESC LIMIT 1", compact('ip')); + } if ($ret !== false) { - if ($ret['subnetlocationid'] > 0) { - return (int)$ret['subnetlocationid']; + if ($ret['loc'] > 0) { + return (int)$ret['loc']; } return false; } |