AclQueryFeatures.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. <?php
  2. Lib::loadClass('ACL');
  3. Lib::loadClass('SqlQueryWhereBuilder');
  4. Lib::loadClass('ParseOgcFilter');
  5. Lib::loadClass('TableAcl');
  6. // usage: (Acl class)::buildQuery($params): return new AclQueryFeatures($this, $params);
  7. // (view): $queryFeatures = $acl->buildQuery($params);
  8. // (view): $total = $queryFeatures->getTotal();
  9. // (view): $items = $queryFeatures->getItems();
  10. // example: @see TableAcl, TableAjax
  11. // Special Filter Access - btns visible only if user don't have super access perms. If has, then will always see all rows.
  12. class AclQueryFeatures {
  13. public $_params;
  14. public $_acl;
  15. public $_query;
  16. public $_total;
  17. public $_legacyMode;
  18. public $_selectLocalFields; // TODO: leave here or move to AclQueryBuilder? use join for perf?
  19. public $_selectRemote; // TODO: ...
  20. public $_dbgExecTime;
  21. public $_instanceID;
  22. public $_foundFeatures;
  23. public function __construct($acl, $params, $legacyMode = false) {
  24. $this->_acl = $acl;
  25. $this->_params = $params;
  26. $this->_query = null;
  27. $this->_total = null;
  28. $this->_legacyMode = $legacyMode;
  29. // TODO: _legacyMode = ($from instanceof simple schema or another programmed objects)
  30. $this->_selectLocalFields = null;
  31. $this->_selectRemote = null;
  32. $this->_foundFeatures = [];
  33. // 'skipFeaturesAsXlink' => $this->_foundFeatures
  34. if (array_key_exists('skipFeaturesAsXlink', $this->_params)) {
  35. $this->_foundFeatures = V::get('skipFeaturesAsXlink', [], $this->_params, 'array');
  36. unset($this->_params['skipFeaturesAsXlink']);
  37. }
  38. $DBG_LOG_TO_FILE = false; // TODO: read from ENV or REQUEST
  39. if ($DBG_LOG_TO_FILE) {
  40. Lib::loadClass('DebugExecutionTime');
  41. $this->_dbgExecTime = new DebugExecutionTime();
  42. $this->_dbgExecTime->setLogFile('/tmp/se-dbg-exec-time--AclQueryFeatures.csv', $format = 'csv'); // $format: 'csv', 'json'
  43. $this->_logToFile("from: '" . $this->_acl->getNamespace() . "'");
  44. }
  45. }
  46. public function _logToFile($msg) {
  47. if ($this->_dbgExecTime) {
  48. $idInstance = $this->_getInstanceID();
  49. $this->_dbgExecTime->logToFile("#{$idInstance}: {$msg}");
  50. }
  51. }
  52. public function _getInstanceID() {
  53. static $_INSTANCE_ID = 0;
  54. if (!$this->_instanceID) {
  55. $_INSTANCE_ID += 1;
  56. $this->_instanceID = $_INSTANCE_ID;
  57. }
  58. return $this->_instanceID;
  59. }
  60. public function parseQueryValue($fieldName, $searchQuery, $fieldType = 'xsd:string') {
  61. if ('!NULL' === $searchQuery) return ['is not null', null];
  62. if ('IS NOT NULL' === $searchQuery) return ['is not null', null];
  63. if ('NULL' === $searchQuery) return ['is null', null];
  64. if ('IS NULL' === $searchQuery) return ['is null', null];
  65. switch ($fieldType) {
  66. case 'p5Type:polygon':
  67. case 'p5Type:lineString':
  68. case 'p5Type:point':
  69. case 'gml:PolygonPropertyType':
  70. case 'gml:PointPropertyType':
  71. case 'gml:LineStringPropertyType':
  72. case 'gml:GeometryPropertyType': return $this->_parseGeomQuery($searchQuery);
  73. // $sqlFilter = $this->_sqlValueForGeomField($fldName, $v, 't');
  74. // if ('_CSV_NUM' == substr($fldName, -8)) { // if ($this->isCsvNumericField($fldName)) { // TODO: xsd type - p5:csv_num
  75. // $sqlFilter = $this->_sqlValueForCsvNumericField($fldName, $v, 't');
  76. // if ($sqlFilter) $sql_where_and[] = $sqlFilter;
  77. // continue;
  78. // }
  79. }
  80. switch (substr($searchQuery, 0, 1)) {
  81. case '=': return ['=', substr($searchQuery, 1)];
  82. case '>':
  83. switch (substr($searchQuery, 1, 1)) {
  84. case '=': return ['>=', substr($searchQuery, 2)];
  85. default: return ['>', substr($searchQuery, 1)];
  86. }
  87. case '<':
  88. switch (substr($searchQuery, 1, 1)) {
  89. case '=': return ['<=', substr($searchQuery, 2)];
  90. case '>': return ['!=', substr($searchQuery, 2)];
  91. default: return ['<', substr($searchQuery, 1)];
  92. }
  93. case '!':
  94. switch (substr($searchQuery, 1, 1)) {
  95. case '=': return ['!=', substr($searchQuery, 2)];
  96. default: return ['not like', substr($searchQuery, 1)];
  97. }
  98. default: {
  99. switch ($fieldType) {
  100. case 'xsd:long':
  101. case 'xsd:number':
  102. case 'xsd:int':
  103. case 'xsd:integer': {
  104. if (false !== strpos($searchQuery, '%')) return ['like', $searchQuery];
  105. return ['=', $searchQuery];
  106. }
  107. default: {
  108. if (false !== strpos($searchQuery, '%')) return ['like', $searchQuery];
  109. $queryWhereBuilder = new SqlQueryWhereBuilder();
  110. return ['and'
  111. , array_map(function ($word) use ($fieldName) {
  112. return [$fieldName, 'like', "%{$word}%"];
  113. }, $queryWhereBuilder->splitQueryToWords($searchQuery)
  114. )
  115. ];
  116. }
  117. }
  118. return ['=', $searchQuery];
  119. }
  120. }
  121. }
  122. public function _parseGeomQuery($searchQuery) { // _sqlValueForGeomField($fldName, $fltrValue, $tblPrefix = 't')
  123. // example: BBOX:54.40993961633866,18.583889010112824,54.337945760687454,18.397121431987586
  124. DBG::log($searchQuery, 'string', "\$searchQuery");
  125. if ('BBOX:' == substr($searchQuery, 0, 5)) {
  126. $valParts = explode(',', substr($searchQuery, 5));
  127. if (4 !== count($valParts)) throw new Exception("Wrong BBOX query");
  128. $valParts = array_filter($valParts, 'is_numeric');
  129. if (4 !== count($valParts)) throw new Exception("Wrong BBOX query - expected 4 numeric values");
  130. $bounds = "POLYGON((
  131. {$valParts[3]} {$valParts[2]},
  132. {$valParts[3]} {$valParts[0]},
  133. {$valParts[1]} {$valParts[0]},
  134. {$valParts[1]} {$valParts[2]},
  135. {$valParts[3]} {$valParts[2]}
  136. ))";
  137. // for mysql 5.6 use ST_Contains() @see http://dev.mysql.com/doc/refman/5.6/en/spatial-relation-functions.html
  138. return [ 'Intersects', $bounds ];
  139. }
  140. else if ('GeometryType=' == substr($fltrValue, 0, 13)) {
  141. return [ 'GeometryType', substr($fltrValue, 13) ];
  142. }
  143. throw new Exception("Not implemented geometry query string"); // TODO:? return null;
  144. }
  145. public function _sqlValueForCsvNumericField($fldName, $fltrValue, $tblPrefix = 't') {
  146. $sqlFilter = false;
  147. if (is_numeric($fltrValue)) {
  148. $sqlFilter = "FIND_IN_SET('{$fltrValue}', `{$fldName}`)>0";
  149. } else if (false !== strpos($fltrValue, ' ')) {
  150. $sqlGlue = " or ";
  151. $fltrValues = $fltrValue;
  152. if ('&' == substr($fltrValues, 0, 1)) {
  153. $fltrValues = substr($fltrValues, 1);
  154. $sqlGlue = " and ";
  155. }
  156. $fltrValues = explode(' ', $fltrValues);
  157. $sqlNumericValues = array();
  158. foreach ($fltrValues as $fltrVal) {
  159. if (is_numeric($fltrVal)) {
  160. $sqlNumericValues[] = "FIND_IN_SET('{$fltrVal}', `{$fldName}`)>0";
  161. }
  162. }
  163. if (!empty($sqlNumericValues)) {
  164. $sqlFilter = "(" . implode($sqlGlue, $sqlNumericValues) . ")";
  165. }
  166. }
  167. return $sqlFilter;
  168. }
  169. public function parseSpecialFilterMsgs($type) {
  170. $rootTableName = $this->_acl->getRootTableName();
  171. DBG::log($rootTableName, 'string', "parse SpecialFilter Msgs({$type}), \$rootTableName");
  172. $sqlHasFltrMsgs = "
  173. select 1
  174. from `CRM_UI_MSGS` m
  175. where m.`uiTargetName`=CONCAT('{$rootTableName}.', t.`ID`)
  176. and m.`uiTargetType`='default_db_table_record'
  177. and m.`A_STATUS` not in('DELETED')
  178. limit 1
  179. ";
  180. switch ($type) {
  181. case 'HAS_MSGS': return " ({$sqlHasFltrMsgs})=1 ";
  182. case 'NO_MSGS': return " ({$sqlHasFltrMsgs}) is null ";
  183. case 'NEW_MSGS': {
  184. $sqlNewFltrMsgs = "
  185. select 1
  186. from `CRM_UI_MSGS` m
  187. where m.`uiTargetName`=CONCAT('{$rootTableName}.', t.`ID`)
  188. and m.`uiTargetType`='default_db_table_record'
  189. and m.`A_STATUS` in('WAITING')
  190. limit 1
  191. ";
  192. return " ({$sqlNewFltrMsgs})=1 ";
  193. }
  194. }
  195. return null;
  196. }
  197. public function parseSpecialFilterProblemy($type) {
  198. DBG::log($type, 'string', "parse SpecialFilter Problemy");
  199. switch ($type) {
  200. case 'PROBLEM': return ['A_PROBLEM', '!=', ''];
  201. case 'WARNING': return ['A_PROBLEM', '=', 'WARNING'];
  202. case 'NORMAL': return ['A_PROBLEM', '=', 'NORMAL'];
  203. }
  204. return null;
  205. }
  206. public function parseSpecialFilterStatus($type) {
  207. DBG::log($type, 'string', "parse SpecialFilter Status");
  208. switch ($type) {
  209. case 'WAITING': return ['A_STATUS', '=', 'WAITING'];
  210. case 'AKTYWNI': return ['A_STATUS', 'or', [ // `A_STATUS` in('NORMAL', 'WARNING') ";
  211. ['A_STATUS', '=', 'NORMAL'],
  212. ['A_STATUS', '=', 'WARNING'],
  213. ] ];
  214. }
  215. return null;
  216. }
  217. public function parseSpecialFilterSpotkania($type) {
  218. DBG::log($type, 'string', "parse SpecialFilter Spotkania");
  219. switch ($type) {
  220. case 'OLD': return ['L_APPOITMENT_DATE', 'and', [
  221. ['L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_LESS_THAN_NOW'],
  222. ] ];
  223. // COALESCE(UNIX_TIMESTAMP(t.`L_APPOITMENT_DATE`), 0) < UNIX_TIMESTAMP()
  224. // and t.`L_APPOITMENT_DATE` != ''
  225. // and t.`L_APPOITMENT_DATE` != '0000-00-00 00:00:00'
  226. case 'NOW': return ['L_APPOITMENT_DATE', 'and', [
  227. [ 'L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_NOW_3600'],
  228. ] ];
  229. // COALESCE(UNIX_TIMESTAMP(t.`L_APPOITMENT_DATE`), 0) < UNIX_TIMESTAMP()+3600
  230. // and COALESCE(UNIX_TIMESTAMP(t.`L_APPOITMENT_DATE`), 0) > UNIX_TIMESTAMP()-3600
  231. case 'TODAY': return ['L_APPOITMENT_DATE', 'and', [
  232. ['L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_GREATER_THAN', mktime(0,0,0, date("m"), date("d"), date("Y"))],
  233. ['L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_LESS_THAN', mktime(0,0,0, date("m"), date("d") + 1, date("Y"))],
  234. ] ];
  235. // $start = mktime(0,0,0, date("m"), date("d"), date("Y"));
  236. // $end = mktime(0,0,0, date("m"), date("d") + 1, date("Y"));
  237. // $sqlFltr = "
  238. // COALESCE(UNIX_TIMESTAMP(t.`L_APPOITMENT_DATE`), 0) > '{$start}'
  239. // and COALESCE(UNIX_TIMESTAMP(t.`L_APPOITMENT_DATE`), 0) < '{$end}'
  240. // ";
  241. case 'TOMORROW': return ['L_APPOITMENT_DATE', 'and', [
  242. ['L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_GREATER_THAN', mktime(0,0,0, date("m"), date("d") + 1, date("Y"))],
  243. ['L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_LESS_THAN', mktime(0,0,0, date("m"), date("d") + 2, date("Y"))],
  244. ] ];
  245. case 'YESTERDAY': return ['L_APPOITMENT_DATE', 'and', [
  246. ['L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_GREATER_THAN', mktime(0,0,0, date("m"), date("d") - 2, date("Y"))],
  247. ['L_APPOITMENT_DATE', 'UNIX_TIMESTAMP_LESS_THAN', mktime(0,0,0, date("m"), date("d") - 1, date("Y"))],
  248. ] ];
  249. case 'BRAK': return ['L_APPOITMENT_DATE', 'or', [
  250. ['L_APPOITMENT_DATE', '=', ''],
  251. ['L_APPOITMENT_DATE', '=', '0000-00-00 00:00:00'],
  252. ] ];
  253. }
  254. return null;
  255. }
  256. public function parseSpecialFilterAccess() {
  257. $userLogin = User::getLogin();
  258. $usrAclGroups = User::getLdapGroupsNames();
  259. DBG::log(['ns'=>$this->_acl->getNamespace(), 'login'=>$userLogin, 'hasWriteField'=>$this->_acl->hasWriteGroupField(), 'hasReadField'=>$this->_acl->hasReadGroupField(), 'hasOwnerField'=>$this->_acl->hasOwnerField(), 'groups'=>$usrAclGroups], 'array', "parse SpecialFilter Access");
  260. $orWhere = [];
  261. if ($this->_acl->hasWriteGroupField()) {
  262. $orWhere[] = ['A_ADM_COMPANY', '=', ''];// TODO: allow empty for everyone?
  263. foreach ($usrAclGroups as $group) $orWhere[] = ['A_ADM_COMPANY', '=', $group];
  264. }
  265. if ($this->_acl->hasReadGroupField()) {
  266. $orWhere[] = ['A_CLASSIFIED', '=', ''];// TODO: allow empty for everyone?
  267. foreach ($usrAclGroups as $group) $orWhere[] = ['A_CLASSIFIED', '=', $group];
  268. }
  269. if (!empty($orWhere) && $this->_acl->hasOwnerField()) {
  270. $orWhere[] = ['L_APPOITMENT_USER', '=', $userLogin];
  271. }
  272. return (!empty($orWhere)) ? [null, 'or', $orWhere] : null;
  273. }
  274. public function parseOgcFilter($ogcFilter) {
  275. $parser = new ParseOgcFilter();
  276. $parser->loadOgcFilter($ogcFilter);
  277. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  278. return $queryWhereBuilder->getQueryWhere('t'); // TODO: $this->_fromPrefix
  279. }
  280. public function getQuery() {
  281. if ($this->_query) return clone($this->_query);
  282. // $ds = $this->_acl->getDataSource(); // TODO: only for TableAcl // TODO: move _parseSqlWhere to this class
  283. $filtrIsInstance = []; // $filtrIsInstance = [ $this->_acl->getNamespace() ];
  284. $filtrIsNotInstance = [];
  285. if (!empty($this->_params['f_is_instance'])) $filtrIsInstance = $this->_params['f_is_instance'];
  286. if (!empty($this->_params['f_is_not_instance'])) $filtrIsNotInstance = $this->_params['f_is_not_instance'];
  287. $this->_query = ACL::query($this->_acl)
  288. ->isInstance($filtrIsInstance)
  289. ->isNotInstance($filtrIsNotInstance);
  290. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance}" ])
  291. // $this->_query->where($ds->_parseSqlWhere($params))
  292. DBG::log($this->_params, 'array', "AclQueryFeatures::getQuery \$this->_params");
  293. foreach ($this->_params as $k => $v) {
  294. // DBG::log(['v'=>$v, 'is_numeric' => is_numeric($k), 'is_int' => is_int($k), 'is_array' => is_array($v)], 'array', "AclQueryFeatures::getQuery \$this->_params[{$k}]");
  295. if (is_int($k) && is_array($v)) {
  296. $this->_query->where($v); // TODO: check format [$fieldName, $comparisonSign, $value]
  297. } else if (is_int($k) && null === $v) { // skip NULL
  298. } else if ('f_is_instance' === $k) { // parsed before
  299. } else if ('f_is_not_instance' === $k) { // parsed before
  300. } else if ('@instances' === $k) { // skip - select
  301. } else if ('cols' === $k) { // skip - select
  302. } else if ('f_' === substr($k, 0, 2) && is_string($v) && strlen($k) > 3) {
  303. $fieldName = substr($k, 2);
  304. $fieldType = $this->_acl->getXsdFieldType($fieldName);
  305. list($comparisonSign, $value) = $this->parseQueryValue($fieldName, $v, $fieldType);
  306. DBG::log([ $fieldName, $comparisonSign, $value, $fieldType ], 'array', "parseQueryValue");
  307. $this->_query->where([$fieldName, $comparisonSign, $value]);
  308. } else if ('sf_' === substr($k, 0, 3) && is_string($v) && strlen($k) > 4) {
  309. switch (substr($k, 3)) {
  310. case 'Msgs': $this->_query->where($this->parseSpecialFilterMsgs($v)); break;
  311. case 'Problemy': $this->_query->where($this->parseSpecialFilterProblemy($v)); break;
  312. case 'Status': $this->_query->where($this->parseSpecialFilterStatus($v)); break;
  313. case 'Spotkania': $this->_query->where($this->parseSpecialFilterSpotkania($v)); break;
  314. case 'Access': break; // SKIP - used below
  315. default: {
  316. if (method_exists($this->_acl, 'parseSpecialFilter')) {
  317. $this->_query->where(
  318. $this->_acl->parseSpecialFilter(substr($k, 3), $v)
  319. );
  320. } else {
  321. throw new Exception("Not Implemented special filter '".substr($k, 3)."'");
  322. }
  323. }
  324. }
  325. } else if ('ogc:Filter' === $k) {
  326. $this->_query->where($this->parseOgcFilter($v));
  327. } else if ('primaryKey' === $k) {
  328. $fieldName = $this->_acl->getPrimaryKeyField();
  329. $fieldType = $this->_acl->getXsdFieldType($fieldName);
  330. if (is_array($v)) {
  331. DBG::log([ $fieldName, $v, $fieldType ], 'array', "parseQueryValue :: primaryKey, value is array");
  332. $this->_query->where([ $fieldName, 'or', array_map(function ($pk) use ($fieldName) {
  333. return [ $fieldName, '=', $pk ];
  334. }, $v) ]);
  335. } else {
  336. list($comparisonSign, $value) = $this->parseQueryValue($fieldName, $v, $fieldType);
  337. DBG::log([ $fieldName, $comparisonSign, $value, $fieldType ], 'array', "parseQueryValue");
  338. $this->_query->where([$fieldName, $comparisonSign, $value]);
  339. }
  340. } else if ('__backRef' === $k) { // skip - parse below
  341. } else if ('__childRef' === $k) { // skip - parse below
  342. } else if ('limit' === $k) {
  343. } else if ('limitstart' === $k) {
  344. } else if ('order_by' === $k) {
  345. } else if ('order_dir' === $k) {
  346. } else if ('sortBy' === $k) {
  347. } else {
  348. throw new Exception("Not Implemented param '{$k}' = '{$v}'");
  349. }
  350. }
  351. // sf_Access: if 'SHOW' then show all rows, but data with ***
  352. if ('SHOW' !== V::get('sf_Access', '', $this->_params)) $this->_query->where($this->parseSpecialFilterAccess());
  353. if (array_key_exists('__backRef', $this->_params)) {
  354. $backRef = $this->_params['__backRef'];
  355. if (!is_array($backRef)) throw new Exception("Wrong back ref structure - expected array");
  356. if (empty($backRef['namespace'])) throw new Exception("Wrong back ref structure - missing namespace");
  357. if (empty($backRef['primaryKey'])) throw new Exception("Wrong back ref structure - missing primaryKey");
  358. if (empty($backRef['fieldName'])) throw new Exception("Wrong back ref structure - missing fieldName");
  359. // TODO: $this->_query->where([ '__backRef' ]); or $this->_query->join([ '__backRef' ]);
  360. $refAcl = ACL::getAclByNamespace($backRef['namespace']);
  361. if ($refAcl->getSourceName() !== $this->_acl->getSourceName()) throw new Exception("Not implemented join with different source");
  362. $refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']);
  363. // TODO: 'in' operator? // $this->_query->where($pkField, 'in', "");
  364. $sqlPk = $this->getAclSqlPrimaryKeyField();
  365. $sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']);
  366. $limit = V::get('limit', 10, $this->_params, 'int');
  367. $offset = V::get('limitstart', 0, $this->_params, 'int');
  368. DBG::log(['limit' => $limit, 'offset' => $offset], 'array', "DBG: limit with __backRef \$this->_params");
  369. $sqlLimit = "limit {$limit} offset {$offset}";
  370. // $this->_query->where("
  371. // t.{$sqlPk} in (
  372. // select refTable.REMOTE_PRIMARY_KEY
  373. // from `{$refTable}` refTable
  374. // where refTable.PRIMARY_KEY = {$sqlBackRefPk}
  375. // order by refTable.REMOTE_PRIMARY_KEY DESC -- TODO refTable.SORT_PRIO
  376. // -- {$sqlLimit} -- BUG MariaDB not support limit in subquery
  377. // )
  378. // ");
  379. // TODO: convert `where t.ID in ( ... )` into `join` and `order by`
  380. // join `CRM__#REF_TABLE__120_VIEW` refTable on (refTable.REMOTE_PRIMARY_KEY = t.ID and refTable.PRIMARY_KEY = '37' )
  381. DBG::log([
  382. 'ACL::getRefTable(...)' => [$refAcl->getNamespace(), $backRef['fieldName']],
  383. '$backRef' => $backRef,
  384. '$refTable' => $refTable
  385. ], 'array', "DBG: join \$join class(".get_class($join).")");
  386. $this->_query->join($refTable, 'refTable', [ 'rawJoin' => "t.{$sqlPk} = refTable.REMOTE_PRIMARY_KEY and refTable.PRIMARY_KEY = {$sqlBackRefPk}" ]);
  387. // moved to getItems: $this->_query->orderBy("refTable.REMOTE_PRIMARY_KEY DESC"); // TODO: order by refTable.SORT_PRIO
  388. }
  389. if (array_key_exists('__childRef', $this->_params)) {
  390. $childRef = $this->_params['__childRef'];
  391. if (!is_array($childRef)) throw new Exception("Wrong child ref structure - expected array");
  392. if (empty($childRef['namespace'])) throw new Exception("Wrong child ref structure - missing namespace");
  393. if (empty($childRef['primaryKey'])) throw new Exception("Wrong child ref structure - missing primaryKey");
  394. $refAcl = ACL::getAclByNamespace($childRef['namespace']);
  395. if ($refAcl->getSourceName() !== $this->_acl->getSourceName()) throw new Exception("Not implemented join with different source");
  396. $refTypeName = Api_WfsNs::typeName($childRef['namespace']);
  397. DBG::log("\$refTypeName({$refTypeName})");
  398. $refTable = ACL::getRefTable($this->_acl->getNamespace(), $refTypeName);
  399. // TODO: 'in' operator? // $this->_query->where($pkField, 'in', "");
  400. $sqlPk = $this->getAclSqlPrimaryKeyField();
  401. $sqlChildRefPk = DB::getPDO()->quote($childRef['primaryKey']);
  402. $this->_query->where("
  403. t.{$sqlPk} in (
  404. select refTable.PRIMARY_KEY
  405. from `{$refTable}` refTable
  406. where refTable.REMOTE_PRIMARY_KEY = {$sqlChildRefPk}
  407. )
  408. ");
  409. }
  410. DBG::log($this->_query, 'array', "TODO: optimize \$this->_query");
  411. $this->_USE_TEMPORARY_TABLE = false; // TODO: ...
  412. if ($this->_USE_TEMPORARY_TABLE) {
  413. $sortBy = ($this->hasParam('sortBy')) ? $this->getParam('sortBy') : null;
  414. if (!$sortBy) {
  415. $sortBy = $this->hasParam('order_by')
  416. ? ( $this->hasParam('order_dir')
  417. ? $this->getParam('order_by') . " " . $this->getParam('order_dir')
  418. : $this->getParam('order_by')
  419. )
  420. : '';
  421. }
  422. $TMP_TABLE_NAME = $this->_acl->getRootTableName() . "__#TEMPORARY";
  423. DBG::log("\$TMP_TABLE_NAME='{$TMP_TABLE_NAME}'");
  424. Lib::loadClass('AntAclBase');
  425. if ($this->_acl instanceof AntAclBase) {
  426. $baseSql = $this->_query->generateSql();
  427. DB::getPDO()->execSql("
  428. create temporary table `{$TMP_TABLE_NAME}`
  429. {$baseSql}
  430. ");
  431. DBG::log(DB::getPDO()->fetchValue("select count(*) as cnt from `{$TMP_TABLE_NAME}`"), 'array', "select count(*) cnt from '{$TMP_TABLE_NAME}'");
  432. $tmpAclFrom = clone($this->_acl);
  433. $tmpAclFrom->_rootTableName = $TMP_TABLE_NAME;
  434. // $tmpAclFrom = AntAclBase::buildInstance($this->_acl->getID(), [
  435. // 'name' => $this->_acl->getName(),
  436. // '_rootTableName' => $TMP_TABLE_NAME,
  437. // 'idDatabase' => $this->_acl->getDatabaseID(),
  438. // 'namespace' => $this->_acl->getNamespace(),
  439. // 'primaryKey' => $this->_acl->getPrimaryKeyField(),
  440. // 'field' => $this->_acl->getFields(),
  441. // ]);
  442. Lib::loadClass('AclQueryBuilder');
  443. $query = new AclQueryBuilder();
  444. $query->from($tmpAclFrom, $prefix = 't');
  445. DBG::log($query, 'array', "TODO: \$query by temporary table");
  446. // $this->_query = $query;
  447. // return $this->_query;
  448. }
  449. }
  450. return clone($this->_query);
  451. }
  452. public function getTotal() {
  453. $this->_beforeFetchData();
  454. if ($this->_legacyMode) return $this->_acl->getTotal($this->_params);
  455. if (null !== $this->_total) return $this->_total;
  456. $this->_total = $this->getQuery()->fetchTotal();
  457. return $this->_total;
  458. }
  459. public function hasParam($key) { return !empty($this->_params[$key]); }
  460. public function getParam($key) { return V::get($key, '', $this->_params); }
  461. public function getItems() {
  462. $this->_logToFile('getItems() ...');
  463. $this->_beforeFetchData();
  464. if ($this->_legacyMode) return $this->_acl->getItems($this->_params);// TODO: array_map( $r => (array)$r )
  465. // 'limit' => 10,
  466. // 'limitstart' => 0,
  467. // 'order_by' => 'ID',
  468. // 'order_dir' => 'desc',
  469. // TODO: sortBy from wfs query
  470. $sortBy = ($this->hasParam('sortBy')) ? $this->getParam('sortBy') : null;
  471. if (!$sortBy && array_key_exists('__backRef', $this->_params)) {
  472. $sortBy = "refTable.REMOTE_PRIMARY_KEY DESC"; // TODO: order by refTable.SORT_PRIO
  473. }
  474. if (!$sortBy) {
  475. $sortBy = $this->hasParam('order_by')
  476. ? ( $this->hasParam('order_dir')
  477. ? $this->getParam('order_by') . " " . $this->getParam('order_dir')
  478. : $this->getParam('order_by')
  479. )
  480. : '';
  481. }
  482. $limit = V::get('limit', 10, $this->_params, 'int');
  483. $offset = V::get('limitstart', 0, $this->_params, 'int');
  484. DBG::log(['params' => $this->_params, 'sortBy' => $sortBy, 'limit' => $limit, 'offset' => $offset], 'array', '$this->_params');
  485. $select = $this->prepareSelect();
  486. DBG::log($select, 'array', "\$select is(TableAcl)=(".($this->_acl instanceof TableAcl).")");
  487. // DBG::log($this->getQuery(), 'array', "\$select is(TableAcl)=(".($this->_acl instanceof TableAcl).") \$this->getQuery()");
  488. $items = $this->fetchRowsRefs(
  489. $this->getQuery()
  490. ->select(array_merge(['@primaryKey'], $select))
  491. ->limit($limit)
  492. ->offset($offset)
  493. ->orderBy($sortBy)
  494. ->fetchAll()
  495. );
  496. $this->_logToFile('getItems() fetched.');
  497. return $items;
  498. }
  499. function _beforeFetchData() {
  500. if (method_exists($this->_acl, 'onBeforeFetchData')) $this->_acl->onBeforeFetchData();
  501. }
  502. public function getItem($primaryKey) { // TODO: throw exception if not found?
  503. $this->_beforeFetchData();
  504. if ($this->_legacyMode) return (array)$this->_acl->getItem($primaryKey, $this->_params);
  505. $select = $this->prepareSelect();
  506. $pkField = $this->_acl->getPrimaryKeyField();
  507. return $this->fetchRowRefs(
  508. $this->getQuery()
  509. ->select($select)
  510. ->where([$pkField, '=', $primaryKey])
  511. ->fetchFirst()
  512. );
  513. }
  514. public function prepareSelect() { // TODO: replace with getSelectLocal
  515. // TODO: select from params: 'cols' => [ fieldName, ... ]
  516. // TODO: select from params: '@instances' => 1
  517. // TODO: if no fields set, then '*'
  518. // TODO: select must contain primaryKey
  519. return $this->getSelectLocal();
  520. }
  521. public function getSelectLocal() { // @returns [ $fieldName, ... ]
  522. // TODO: rawSelect!
  523. if (null !== $this->_selectLocalFields) return $this->_selectLocalFields;
  524. $this->_selectLocalFields = [];
  525. $todoFetchAllCols = false; // select t.*
  526. if (!empty($this->_params['cols'])) {
  527. if (in_array('*', $this->_params['cols'])) $todoFetchAllCols = true;
  528. $acl = $this->_acl;
  529. $this->_selectLocalFields = array_filter($this->_params['cols'], function ($fieldQuery) use ($acl) {
  530. if ('*' === $fieldQuery) return false;
  531. list($fieldName, $subFieldQuery) = explode('/', $fieldQuery, 2);
  532. if (!empty($subFieldQuery)) return false;
  533. return $acl->isLocalField($fieldName);
  534. });
  535. }
  536. if (!empty($this->_params['@instances'])) $this->_selectLocalFields[] = '@instances';
  537. if (empty($this->_selectLocalFields)) $todoFetchAllCols = true;
  538. if (1 === count($this->_selectLocalFields) && in_array('@instances', $this->_selectLocalFields)) $todoFetchAllCols = true;
  539. // if ($this->_acl instanceof TableAcl) {
  540. // $rawSelect = $this->_acl->getDataSource()->_getSqlCols();
  541. // DBG::log($rawSelect, 'string', "DBG raw select");
  542. // if ('*' !== $rawSelect && 't.*' !== $rawSelect) {
  543. // $this->_selectLocalFields['rawSelect'] = $rawSelect;
  544. // }
  545. // }
  546. if (!empty($this->_params['cols'])) DBG::log($this->_params['cols'], 'array', "\$this->_params[cols] (" . ($this->_acl ? $this->_acl->getNamespace() : 'unknown') . ")");
  547. if ($todoFetchAllCols) {
  548. // $this->_selectLocalFields[] = '*'; // TODO: select all $this->from local fields
  549. DBG::log($this->_acl->getLocalFieldList(), 'array', "\$this->_acl->getLocalFieldList()");
  550. foreach ($this->_acl->getLocalFieldList() as $localFieldName) {
  551. $this->_selectLocalFields[] = $localFieldName;
  552. }
  553. }
  554. $primaryKeyField = $this->getAclSqlPrimaryKeyField();
  555. if (!in_array($primaryKeyField, $this->_selectLocalFields)) {
  556. $this->_selectLocalFields[] = $primaryKeyField;
  557. }
  558. // TODO: always add A_ADM_COMPANY, A_CLASSIFIED, L_APPOITMENT_USER ?
  559. DBG::log($this->_selectLocalFields, 'array', '$this->_selectLocalFields');
  560. return $this->_selectLocalFields;
  561. }
  562. public function getSelectRemote() { // @returns [ $fieldName => [ $fieldName, ... ] ]
  563. if (null !== $this->_selectRemote) return $this->_selectRemote;
  564. $this->_selectRemote = [];
  565. if (!empty($this->_params['cols'])) {
  566. $cols = $this->_params['cols'];
  567. if (in_array('*', $cols)) {
  568. $this->_selectLocalFields[] = '*';
  569. $cols = array_filter($cols, function ($fieldQuery) { return '*' !== $fieldQuery; });
  570. }
  571. $acl = $this->_acl;
  572. $cols = array_filter($cols, function ($fieldQuery) use ($acl) {
  573. list($fieldName, $subFieldQuery) = explode('/', $fieldQuery, 2);
  574. // if (empty($subFieldQuery)) return false;
  575. return !$acl->isLocalField($fieldName);
  576. });
  577. foreach ($cols as $fieldQuery) { // group by fieldName
  578. list($fieldName, $subFieldQuery) = explode('/', $fieldQuery, 2);
  579. if (!array_key_exists($fieldName, $this->_selectRemote)) $this->_selectRemote[$fieldName] = [];
  580. if (!empty($subFieldQuery)) $this->_selectRemote[$fieldName][] = $subFieldQuery;
  581. }
  582. }
  583. return $this->_selectRemote;
  584. }
  585. public function fetchRowsRefs($rows) {
  586. $pkField = $this->_acl->getPrimaryKeyField();
  587. $namespace = $this->_acl->getNamespace();
  588. $this->_foundFeatures = array_merge($this->_foundFeatures, array_map(function ($row) use ($namespace, $pkField) {
  589. return "{$namespace}." . V::get($pkField, '', $row);
  590. }, $rows));
  591. DBG::log($this->_foundFeatures, 'array', "_foundFeatures");
  592. return array_map([ $this, 'fetchRowRefs' ], $rows);
  593. }
  594. public function fetchRowRefs($row) {
  595. try {
  596. return $this->_fetchRowRefs($row);
  597. } catch (Exception $e) {
  598. DBG::log($e);
  599. }
  600. return $row;
  601. }
  602. public function _fetchRowRefs($row) {
  603. if (!$row) return $row;
  604. $sqlPk = $this->getAclSqlPrimaryKeyField();
  605. $primaryKey = $row[$sqlPk];
  606. DBG::log($row, 'array', "DBG primaryKey '{$primaryKey}'");
  607. if (!$primaryKey) throw new Exception("Missing primaryKey");
  608. $defaultRefLimit = 10; // TODO: get from $this->_params and pass to nested buildQuery
  609. $refLimitPlus1 = $defaultRefLimit + 1;
  610. foreach ($this->getSelectRemote() as $fieldName => $cols) {
  611. DBG::log($cols, 'array', "add select remote '{$fieldName}' \$cols");
  612. $xsdType = $this->_acl->getXsdFieldType($fieldName);
  613. if ('ref:' === substr($xsdType, 0, 4) && empty($cols)) {
  614. DBG::log("add remote xlink's '{$fieldName}' \$items[{$primaryKey}] ...");
  615. $refTable = ACL::getRefTable($this->_acl->getNamespace(), $fieldName);
  616. if (!$refTable) DBG::log("BUG: Missing refTable in add remote xlink's '{$fieldName}' \$items[{$primaryKey}]");
  617. if ($refTable) {
  618. $xlinks = DB::getPDO()->fetchAll("
  619. select r.REMOTE_PRIMARY_KEY
  620. from `{$refTable}` r
  621. where r.PRIMARY_KEY = '{$primaryKey}'
  622. order by r.REMOTE_PRIMARY_KEY DESC -- TODO r.SORT_PRIO
  623. limit {$refLimitPlus1}
  624. ");
  625. DBG::log($xlinks, 'array', "add remote xlink's for '{$fieldName}' \$items[{$primaryKey}]");
  626. $row[$fieldName] = array_map(function ($refInfo) use ($fieldName) {
  627. $ns = Core_AclHelper::parseTypeName($fieldName);
  628. return [
  629. // '_ns' => $ns,
  630. 'xlink' => "{$ns['url']}#{$ns['name']}.{$refInfo['REMOTE_PRIMARY_KEY']}",
  631. ];
  632. }, $xlinks);
  633. if (count($xlinks) > $defaultRefLimit) DBG::log('TODO: xlink FETCH MORE DATA...');
  634. if (count($xlinks) > $defaultRefLimit) $row[$fieldName][] = [ 'p5:links' => [
  635. 'p5:next' => [
  636. '@typeName' => $fieldName,
  637. '@backRefNS' => $this->_acl->getNamespace(),
  638. '@backRefPK' => $primaryKey,
  639. '@startIndex' => $defaultRefLimit,
  640. '@maxFeatures' => $defaultRefLimit,
  641. 'value' => Request::getScriptUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$fieldName}&REQUEST=GetFeature&backRefNS=".$this->_acl->getNamespace()."&backRefPK={$primaryKey}&backRefField={$fieldName}&maxFeatures={$defaultRefLimit}&startIndex={$defaultRefLimit}",
  642. ],
  643. ] ];
  644. DBG::log($row[$fieldName], 'array', "remote xlinks for \$items[{$primaryKey}][{$fieldName}]");
  645. }
  646. } else if ('ref:' === substr($xsdType, 0, 4) && !empty($cols)) {
  647. $refAcl = ACL::getAclByTypeName($fieldName);
  648. $refQuery = $refAcl->buildQuery([
  649. 'cols' => $cols,
  650. '__backRef' => [
  651. 'namespace' => $this->_acl->getNamespace(),
  652. 'primaryKey' => $primaryKey,
  653. 'fieldName' => $fieldName,
  654. ],
  655. 'limit' => $refLimitPlus1,
  656. 'skipFeaturesAsXlink' => $this->_foundFeatures
  657. // TODO: add $defaultRefLimit + 1
  658. ]);
  659. $totalRefs = $refQuery->getTotal();
  660. $items = $refQuery->getItems([ 'limit' => $defaultRefLimit ]);
  661. // TODO: if (count($items) > $defaultRefLimit) // TODO: add item for GUI - has more data + wfs link to fetch more (offset)
  662. DBG::log($items, 'array', "add remote items '{$fieldName}' \$items[{$primaryKey}][{$fieldName}] total({$totalRefs})");
  663. if ($totalRefs > $defaultRefLimit) {
  664. DBG::log('TODO: resolve recurse fetch more data link');
  665. array_pop($items); // remove last item
  666. $items[] = [ 'p5:links' => [
  667. 'p5:next' => [
  668. '@typeName' => $fieldName,
  669. '@backRefNS' => $this->_acl->getNamespace(),
  670. '@backRefPK' => $primaryKey,
  671. '@startIndex' => $defaultRefLimit,
  672. '@maxFeatures' => $defaultRefLimit,
  673. 'value' => Request::getScriptUri() . "?SERVICE=WFS&VERSION=1.0.0&TYPENAME={$fieldName}&REQUEST=GetFeature&backRefNS=".$this->_acl->getNamespace()."&backRefPK={$primaryKey}&backRefField={$fieldName}&maxFeatures={$defaultRefLimit}&startIndex={$defaultRefLimit}",
  674. ],
  675. ] ];
  676. }
  677. $refNs = $refAcl->getNamespace();
  678. $refPk = $refAcl->getPrimaryKeyField();
  679. $this__hasFeatureId = [ $this, 'hasFeatureId' ];
  680. $this__addFeatureId = [ $this, 'addFeatureId' ];
  681. $row[$fieldName] = array_map(function ($item) use ($fieldName, $refNs, $refPk, $this__hasFeatureId, $this__addFeatureId) {
  682. if (1 === count($item) && !empty($item['p5:links'])) return $item;
  683. $pk = V::get($refPk, '', $item);
  684. $featureId = "{$refNs}.{$pk}";
  685. if (!$this__hasFeatureId($featureId)) {
  686. $this__addFeatureId($featureId);
  687. return $item;
  688. } else {
  689. $ns = Core_AclHelper::parseTypeName($fieldName);
  690. return [
  691. 'xlink' => "{$ns['url']}#{$ns['name']}.{$pk}",
  692. ];
  693. }
  694. }, $items);
  695. } else { // TODO: field is not ref
  696. DBG::log($items, 'array', "NotImplemented - add remote items for non ref field \$items[{$primaryKey}][{$fieldName}]?");
  697. }
  698. }
  699. return $row;
  700. }
  701. public function hasFeatureId($featureId) {
  702. return in_array($featureId, $this->_foundFeatures);
  703. }
  704. public function addFeatureId($featureId) {
  705. $this->_foundFeatures[] = $featureId;
  706. DBG::log($this->_foundFeatures, 'array', "addFeatureId({$featureId})");
  707. }
  708. public function getAclSqlPrimaryKeyField() {
  709. return ($this->_acl instanceof Core_AclBase)
  710. ? $this->_acl->getSqlPrimaryKeyField()
  711. : 'ID';
  712. }
  713. }