summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2019-07-29 15:01:39 +0200
committerSimon Rettberg2019-07-29 15:01:39 +0200
commitcddd600996de531861f38bd8859c932e48514dd5 (patch)
tree904dff463b0f5116db032ced807196eca3b5dca8
parent[roomplanner] Fix check for successful DB write (diff)
downloadslx-admin-cddd600996de531861f38bd8859c932e48514dd5.tar.gz
slx-admin-cddd600996de531861f38bd8859c932e48514dd5.tar.xz
slx-admin-cddd600996de531861f38bd8859c932e48514dd5.zip
[locations] Optimize some functions in Location class
* Casts are faster than settype. * Check if assoc location cache is already populated. * getLocationRootChain can use the parents array from that location instead of building the array from scratch.
-rw-r--r--modules-available/locations/inc/location.inc.php49
-rw-r--r--modules-available/locations/inc/locationutil.inc.php1
2 files changed, 28 insertions, 22 deletions
diff --git a/modules-available/locations/inc/location.inc.php b/modules-available/locations/inc/location.inc.php
index bd4be245..f1f05402 100644
--- a/modules-available/locations/inc/location.inc.php
+++ b/modules-available/locations/inc/location.inc.php
@@ -44,8 +44,10 @@ class Location
*/
public static function getName($locationId)
{
- self::getLocationsAssoc();
$locationId = (int)$locationId;
+ if (self::$assocLocationCache === false) {
+ self::getLocationsAssoc();
+ }
if (!isset(self::$assocLocationCache[$locationId]))
return false;
return self::$assocLocationCache[$locationId]['locationname'];
@@ -59,8 +61,10 @@ class Location
*/
public static function getNameChain($locationId)
{
- self::getLocationsAssoc();
- settype($locationId, 'int');
+ if (self::$assocLocationCache === false) {
+ self::getLocationsAssoc();
+ }
+ $locationId = (int)$locationId;
if (!isset(self::$assocLocationCache[$locationId]))
return false;
$ret = array();
@@ -117,9 +121,7 @@ class Location
public static function getLocations($selected = 0, $excludeId = 0, $addNoParent = false, $keepArrayKeys = false)
{
- if (is_string($selected)) {
- settype($selected, 'int');
- }
+ $selected = (int)$selected;
if (self::$flatLocationCache === false) {
$rows = self::getTree();
$rows = self::flattenTree($rows);
@@ -245,7 +247,7 @@ class Location
. 'FROM location '
. 'WHERE parentlocationid = :locationid', ['locationid' => $locationid]);
$result = $result['isleaf'];
- settype($result, 'bool');
+ $result = (bool)$result;
return $result;
}
@@ -355,31 +357,34 @@ class Location
*/
public static function getLocationRootChain($locationId)
{
- settype($locationId, 'integer');
- $locations = Location::getLocationsAssoc();
- $find = $locationId;
- $matchingLocations = array();
- while (isset($locations[$find]) && !in_array($find, $matchingLocations)) {
- $matchingLocations[] = $find;
- $find = (int)$locations[$find]['parentlocationid'];
- }
- return $matchingLocations;
+ $locationId = (int)$locationId;
+ if (self::$assocLocationCache === false) {
+ self::getLocationsAssoc();
+ }
+ if (!isset(self::$assocLocationCache[$locationId]))
+ return [];
+ $chain = self::$assocLocationCache[$locationId]['parents'];
+ $chain[] = $locationId;
+ return array_reverse($chain);
}
/**
+ * Used for baseconfig hook
* @param $locationId
* @return bool|array ('value' => x, 'display' => y), false if no parent or unknown id
*/
public static function getBaseconfigParent($locationId)
{
- settype($locationId, 'integer');
- $locations = Location::getLocationsAssoc();
- if (!isset($locations[$locationId]))
+ $locationId = (int)$locationId;
+ if (self::$assocLocationCache === false) {
+ self::getLocationsAssoc();
+ }
+ if (!isset(self::$assocLocationCache[$locationId]))
return false;
- $locationId = (int)$locations[$locationId]['parentlocationid'];
- if (!isset($locations[$locationId]))
+ $locationId = (int)self::$assocLocationCache[$locationId]['parentlocationid'];
+ if (!isset(self::$assocLocationCache[$locationId]))
return false;
- return array('value' => $locationId, 'display' => $locations[$locationId]['locationname']);
+ return array('value' => $locationId, 'display' => self::$assocLocationCache[$locationId]['locationname']);
}
/**
diff --git a/modules-available/locations/inc/locationutil.inc.php b/modules-available/locations/inc/locationutil.inc.php
new file mode 100644
index 00000000..b3d9bbc7
--- /dev/null
+++ b/modules-available/locations/inc/locationutil.inc.php
@@ -0,0 +1 @@
+<?php