231 lines
5.8 KiB
PHP
231 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace database;
|
|
|
|
include_once "../../global.inc.php";
|
|
include_once PATH_UTILITTY . "/StringManager.php";
|
|
require_once PATH_DATABASE . "/SqlManager.php";
|
|
|
|
/**
|
|
* Description of SpsSqlManager
|
|
*
|
|
* @author stubbfel
|
|
* @since 20.06.2013
|
|
*/
|
|
class SpsSqlManager extends SQLManager {
|
|
|
|
/**
|
|
* Fieldname of the placeID
|
|
* @var string
|
|
*/
|
|
public static $placeId = "id";
|
|
|
|
/**
|
|
* Fieldname of the parendId
|
|
* @var string
|
|
*/
|
|
public static $parentId = "parent";
|
|
|
|
/**
|
|
* String for the select part of the query
|
|
* @var string
|
|
*/
|
|
private static $selectTerm = "SELECT DISTINCT id, parent FROM sps WHERE ";
|
|
|
|
/**
|
|
* String for the alias part of the query
|
|
* @var string
|
|
*/
|
|
private static $aliasTerm = "alias = ";
|
|
|
|
/**
|
|
* String for the did part of the query
|
|
* @var string
|
|
*/
|
|
private static $domainTerm = "did = ";
|
|
|
|
/**
|
|
* String for the dNamet part of the query
|
|
* @var string
|
|
*/
|
|
private static $domainNameTerm = "dName = ";
|
|
|
|
/**
|
|
* first part of intersect-function
|
|
* @var string
|
|
*/
|
|
private static $interSectTermStart = "Intersects(";
|
|
|
|
/**
|
|
* last part of intersect-function
|
|
* @var string
|
|
*/
|
|
private static $interSectTermEnd = "),plan)";
|
|
|
|
/**
|
|
* first part of GeomFromText('Polygon-function
|
|
* @var string
|
|
*/
|
|
private static $polyStartStr = "GeomFromText('Polygon((";
|
|
|
|
/**
|
|
* last part of GeomFromText('Polygon-function
|
|
* @var string
|
|
*/
|
|
private static $polyEndStr = "))'";
|
|
|
|
/**
|
|
* maximium length of the value-string for an aliasname
|
|
* @var int
|
|
*/
|
|
private static $aliasMaxLenght = 32;
|
|
|
|
/**
|
|
* maximium length of the value-string for a domainname
|
|
* @var int
|
|
*/
|
|
private static $domainMaxLenght = 32;
|
|
|
|
/**
|
|
* Default-Constructor
|
|
*/
|
|
public function __construct() {
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Default-DEConstructor
|
|
*/
|
|
public function __destruct() {
|
|
parent::__destruct();
|
|
}
|
|
|
|
/**
|
|
* Methods send an query for the sps-service depends of alias
|
|
* @param array $queryArgs
|
|
* @return array [num][assoc]
|
|
*/
|
|
public function sendSpsAliasQuery($queryArgs) {
|
|
|
|
// check arguments of the query
|
|
if (array_key_exists(\api\SpsApi::$keyAlias, $queryArgs)) {
|
|
$alias = $queryArgs[\api\SpsApi::$keyAlias];
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
$domain = null;
|
|
if (array_key_exists(\api\SpsApi::$keyDomain, $queryArgs)) {
|
|
$domain = $queryArgs[\api\SpsApi::$keyDomain];
|
|
}
|
|
|
|
// build query string
|
|
$query = self::$selectTerm;
|
|
if ($this->validAliasString($alias)) {
|
|
$query .= self::$aliasTerm . self::$quoteTerm . $alias . self::$quoteTerm . self::$addDomainTerm($domain);
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
// send query
|
|
return $this->query($query);
|
|
}
|
|
|
|
/**
|
|
* Methods send an query for the sps-service depends of coordinates
|
|
* @param array $queryArgs
|
|
* @return array [num][assoc]
|
|
*/
|
|
public function sendSpsCoordinateQuery($queryArgs) {
|
|
|
|
// check arguments of the query
|
|
if (array_key_exists(\api\SpsApi::$keyPoly, $queryArgs)) {
|
|
$poly = $queryArgs[\api\SpsApi::$keyPoly];
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
$domain = null;
|
|
if (array_key_exists(\api\SpsApi::$keyDomain, $queryArgs)) {
|
|
$domain = $queryArgs[\api\SpsApi::$keyDomain];
|
|
}
|
|
|
|
// build query string
|
|
if ($this->validPolyString($poly)) {
|
|
$query = self::$selectTerm . self::$interSectTermStart . $poly . self::$interSectTermEnd . $this->addDomainTerm($domain);
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
// send query
|
|
return $this->query($query);
|
|
}
|
|
|
|
/**
|
|
* Method create the correct domain part depends of $domain. If it is a number => did
|
|
* otherwise => dName
|
|
* @param string $domain
|
|
* @return string
|
|
*/
|
|
private function addDomainTerm($domain) {
|
|
$result = null;
|
|
if ($domain != null && $this->validDomainString($domain)) {
|
|
if ($this->isDid($domain)) {
|
|
$result .= self::$andTerm . self::$domainTerm . self::$quoteTerm . $domain . self::$quoteTerm;
|
|
} else {
|
|
$result .= self::$andTerm . self::$domainNameTerm . self::$quoteTerm . $domain . self::$quoteTerm;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Method check if the input value for the alias is valid
|
|
* @param string $alias
|
|
* @return boolean
|
|
*/
|
|
private function validAliasString($alias) {
|
|
if (\utiliy\StringManager::validSQLString($alias) && ctype_alnum($alias) && strlen($alias) <= self::$aliasMaxLenght) {
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method check if the input value for the alias is valid
|
|
* @param string $domain
|
|
* @return boolean
|
|
*/
|
|
private function validDomainString($domain) {
|
|
if (\utiliy\StringManager::validSQLString($domain) && ctype_alnum($domain) && strlen($domain) <= self::$domainMaxLenght) {
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method check if the input value for the polygon is valid
|
|
* @param string $poly
|
|
* @return boolean
|
|
*/
|
|
private function validPolyString($poly) {
|
|
if (\utiliy\StringManager::validSQLString($poly) && \utiliy\StringManager::startsWith($poly, self::$polyStartStr)
|
|
&& \utiliy\StringManager::endsWith($poly, self::$polyEndStr)) {
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* Method check if the ntring is a did
|
|
* @param string $string
|
|
* @return boolean
|
|
*/
|
|
private function isDid($string) {
|
|
return ctype_digit($string);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|