1: <?php
2:
3: namespace database;
4:
5: include_once "../../global.inc.php";
6: include_once PATH_UTILITTY . "/StringManager.php";
7: require_once PATH_DATABASE . "/SqlManager.php";
8:
9: /**
10: * Description of SpsSqlManager
11: *
12: * @author stubbfel
13: * @since 20.06.2013
14: */
15: class SpsSqlManager extends SQLManager {
16:
17: /**
18: * Fieldname of the placeID
19: * @var string
20: */
21: public static $placeId = "id";
22:
23: /**
24: * Fieldname of the parendId
25: * @var string
26: */
27: public static $parentId = "parent";
28:
29: /**
30: * Fieldname of the refpoint
31: * @var string
32: */
33: public static $refpoint = "refpoint";
34:
35: /**
36: * String for the select part of the query
37: * @var string
38: */
39: private static $selectTerm = "SELECT DISTINCT id, parent, refpoint FROM sps WHERE ";
40:
41: /**
42: * String for the alias part of the query
43: * @var string
44: */
45: private static $aliasTerm = "alias = ";
46:
47: /**
48: * String for the did part of the query
49: * @var string
50: */
51: private static $domainTerm = "did = ";
52:
53: /**
54: * String for the dNamet part of the query
55: * @var string
56: */
57: private static $domainNameTerm = "dName = ";
58:
59: /**
60: * first part of intersect-function
61: * @var string
62: */
63: private static $interSectTermStart = "Intersects(";
64:
65: /**
66: * last part of intersect-function
67: * @var string
68: */
69: private static $interSectTermEnd = "),plan)";
70:
71: /**
72: * first part of GeomFromText('Polygon-function
73: * @var string
74: */
75: private static $polyStartStr = "GeomFromText('Polygon((";
76:
77: /**
78: * last part of GeomFromText('Polygon-function
79: * @var string
80: */
81: private static $polyEndStr = "))'";
82:
83: /**
84: * maximium length of the value-string for an aliasname
85: * @var int
86: */
87: private static $aliasMaxLenght = 32;
88:
89: /**
90: * maximium length of the value-string for a domainname
91: * @var int
92: */
93: private static $domainMaxLenght = 32;
94:
95: /**
96: * Default-Constructor
97: */
98: public function __construct() {
99: parent::__construct();
100: }
101:
102: /**
103: * Default-DEConstructor
104: */
105: public function __destruct() {
106: parent::__destruct();
107: }
108:
109: /**
110: * Methods send an query for the sps-service depends of alias
111: * @param array $queryArgs
112: * @return array [num][assoc]
113: */
114: public function sendSpsAliasQuery($queryArgs) {
115:
116: // check arguments of the query
117: if (array_key_exists(\api\SpsApi::$keyAlias, $queryArgs)) {
118: $alias = $queryArgs[\api\SpsApi::$keyAlias];
119: } else {
120: return null;
121: }
122:
123: $domain = null;
124: if (array_key_exists(\api\SpsApi::$keyDomain, $queryArgs)) {
125: $domain = $queryArgs[\api\SpsApi::$keyDomain];
126: }
127:
128: // build query string
129: $query = self::$selectTerm;
130: if ($this->validAliasString($alias)) {
131: $query .= self::$aliasTerm . self::$quoteTerm . $alias . self::$quoteTerm . $this->addDomainTerm($domain);
132: } else {
133: return null;
134: }
135:
136: // send query
137: return $this->query($query);
138: }
139:
140: /**
141: * Methods send an query for the sps-service depends of coordinates
142: * @param array $queryArgs
143: * @return array [num][assoc]
144: */
145: public function sendSpsCoordinateQuery($queryArgs) {
146:
147: // check arguments of the query
148: if (array_key_exists(\api\SpsApi::$keyPoly, $queryArgs)) {
149: $poly = $queryArgs[\api\SpsApi::$keyPoly];
150: } else {
151: return null;
152: }
153:
154: $domain = null;
155: if (array_key_exists(\api\SpsApi::$keyDomain, $queryArgs)) {
156: $domain = $queryArgs[\api\SpsApi::$keyDomain];
157: }
158:
159: // build query string
160: if ($this->validPolyString($poly)) {
161: $query = self::$selectTerm . self::$interSectTermStart . $poly . self::$interSectTermEnd . $this->addDomainTerm($domain);
162: } else {
163: return null;
164: }
165:
166: // send query
167: return $this->query($query);
168: }
169:
170: /**
171: * Method create the correct domain part depends of $domain. If it is a number => did
172: * otherwise => dName
173: * @param string $domain
174: * @return string
175: */
176: private function addDomainTerm($domain) {
177: $result = null;
178: if ($domain != null && $this->validDomainString($domain)) {
179: if ($this->isDid($domain)) {
180: $result .= self::$andTerm . self::$domainTerm . self::$quoteTerm . $domain . self::$quoteTerm;
181: } else {
182: $result .= self::$andTerm . self::$domainNameTerm . self::$quoteTerm . $domain . self::$quoteTerm;
183: }
184: }
185: return $result;
186: }
187:
188: /**
189: * Method check if the input value for the alias is valid
190: * @param string $alias
191: * @return boolean
192: */
193: private function validAliasString($alias) {
194: if (\utiliy\StringManager::validSQLString($alias) && ctype_alnum($alias) && strlen($alias) <= self::$aliasMaxLenght) {
195: return TRUE;
196: }
197: return FALSE;
198: }
199:
200: /**
201: * Method check if the input value for the alias is valid
202: * @param string $domain
203: * @return boolean
204: */
205: private function validDomainString($domain) {
206: if (\utiliy\StringManager::validSQLString($domain) && ctype_alnum($domain) && strlen($domain) <= self::$domainMaxLenght) {
207: return TRUE;
208: }
209: return FALSE;
210: }
211:
212: /**
213: * Method check if the input value for the polygon is valid
214: * @param string $poly
215: * @return boolean
216: */
217: private function validPolyString($poly) {
218: if (\utiliy\StringManager::validSQLString($poly) && \utiliy\StringManager::startsWith($poly, self::$polyStartStr) && \utiliy\StringManager::endsWith($poly, self::$polyEndStr)) {
219: return TRUE;
220: }
221: return FALSE;
222: }
223:
224: /**
225: * Method check if the ntring is a did
226: * @param string $string
227: * @return boolean
228: */
229: private function isDid($string) {
230: return ctype_digit($string);
231: }
232:
233: }
234:
235: ?>
236: