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