AclQueryFeatures.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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_@selected' === $k) { // skip - f_@selected
  303. Lib::loadClass('FeatureAttrSelected');
  304. $userSelectedTableName = FeatureAttrSelected::getAttributeTableName($typeName = $this->_acl->getNamespace(), $idUser = User::getID());
  305. $pkFieldName = $this->_acl->getPrimaryKeyField();
  306. if ('YES' === $v) {
  307. $this->_query->join($userSelectedTableName, 't_selected', [ 'rawJoin' => "t.{$pkFieldName} = t_selected.primaryKey" ]);
  308. } else if ('NO' === $v) {
  309. $this->_query->leftJoin($userSelectedTableName, 't_selected', [ 'rawJoin' => "t.{$pkFieldName} = t_selected.primaryKey" ]);
  310. $this->_query->where(['t_selected.primaryKey', 'is null']);
  311. } else {
  312. throw new Exception("Not implemented @selected filter value '{$v}'");
  313. }
  314. } else if ('f_' === substr($k, 0, 2) && is_string($v) && strlen($k) > 3) {
  315. $fieldName = substr($k, 2);
  316. $fieldType = $this->_acl->getXsdFieldType($fieldName);
  317. list($comparisonSign, $value) = $this->parseQueryValue($fieldName, $v, $fieldType);
  318. DBG::log([ $fieldName, $comparisonSign, $value, $fieldType ], 'array', "parseQueryValue");
  319. $this->_query->where([$fieldName, $comparisonSign, $value]);
  320. } else if ('sf_' === substr($k, 0, 3) && is_string($v) && strlen($k) > 4) {
  321. switch (substr($k, 3)) {
  322. case 'Msgs': $this->_query->where($this->parseSpecialFilterMsgs($v)); break;
  323. case 'Problemy': $this->_query->where($this->parseSpecialFilterProblemy($v)); break;
  324. case 'Status': $this->_query->where($this->parseSpecialFilterStatus($v)); break;
  325. case 'Spotkania': $this->_query->where($this->parseSpecialFilterSpotkania($v)); break;
  326. case 'Access': break; // SKIP - used below
  327. default: {
  328. if (method_exists($this->_acl, 'parseSpecialFilter')) {
  329. $this->_query->where(
  330. $this->_acl->parseSpecialFilter(substr($k, 3), $v)
  331. );
  332. } else {
  333. throw new Exception("Not Implemented special filter '".substr($k, 3)."'");
  334. }
  335. }
  336. }
  337. } else if ('ogc:Filter' === $k) {
  338. $this->_query->where($this->parseOgcFilter($v));
  339. } else if ('primaryKey' === $k) {
  340. $fieldName = $this->_acl->getPrimaryKeyField();
  341. $fieldType = $this->_acl->getXsdFieldType($fieldName);
  342. if (is_array($v)) {
  343. DBG::log([ $fieldName, $v, $fieldType ], 'array', "parseQueryValue :: primaryKey, value is array");
  344. $this->_query->where([ $fieldName, 'or', array_map(function ($pk) use ($fieldName) {
  345. return [ $fieldName, '=', $pk ];
  346. }, $v) ]);
  347. } else {
  348. list($comparisonSign, $value) = $this->parseQueryValue($fieldName, $v, $fieldType);
  349. DBG::log([ $fieldName, $comparisonSign, $value, $fieldType ], 'array', "parseQueryValue");
  350. $this->_query->where([$fieldName, $comparisonSign, $value]);
  351. }
  352. } else if ('__backRef' === $k) { // skip - parse below
  353. } else if ('__childRef' === $k) { // skip - parse below
  354. } else if ('limit' === $k) {
  355. } else if ('limitstart' === $k) {
  356. } else if ('order_by' === $k) {
  357. } else if ('order_dir' === $k) {
  358. } else if ('sortBy' === $k) {
  359. } else {
  360. throw new Exception("Not Implemented param '{$k}' = '{$v}'");
  361. }
  362. }
  363. // sf_Access: if 'SHOW' then show all rows, but data with ***
  364. if ('SHOW' !== V::get('sf_Access', '', $this->_params)) $this->_query->where($this->parseSpecialFilterAccess());
  365. if (array_key_exists('__backRef', $this->_params)) {
  366. $backRef = $this->_params['__backRef'];
  367. if (!is_array($backRef)) throw new Exception("Wrong back ref structure - expected array");
  368. if (empty($backRef['namespace'])) throw new Exception("Wrong back ref structure - missing namespace");
  369. if (empty($backRef['primaryKey'])) throw new Exception("Wrong back ref structure - missing primaryKey");
  370. if (empty($backRef['fieldName'])) throw new Exception("Wrong back ref structure - missing fieldName");
  371. // TODO: $this->_query->where([ '__backRef' ]); or $this->_query->join([ '__backRef' ]);
  372. $refAcl = ACL::getAclByNamespace($backRef['namespace']);
  373. if ($refAcl->getSourceName() !== $this->_acl->getSourceName()) throw new Exception("Not implemented join with different source");
  374. $refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']);
  375. // TODO: 'in' operator? // $this->_query->where($pkField, 'in', "");
  376. $sqlPk = $this->getAclSqlPrimaryKeyField();
  377. $sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']);
  378. $limit = V::get('limit', 10, $this->_params, 'int');
  379. $offset = V::get('limitstart', 0, $this->_params, 'int');
  380. DBG::log(['limit' => $limit, 'offset' => $offset], 'array', "DBG: limit with __backRef \$this->_params");
  381. $sqlLimit = "limit {$limit} offset {$offset}";
  382. // $this->_query->where("
  383. // t.{$sqlPk} in (
  384. // select refTable.REMOTE_PRIMARY_KEY
  385. // from `{$refTable}` refTable
  386. // where refTable.PRIMARY_KEY = {$sqlBackRefPk}
  387. // order by refTable.REMOTE_PRIMARY_KEY DESC -- TODO refTable.SORT_PRIO
  388. // -- {$sqlLimit} -- BUG MariaDB not support limit in subquery
  389. // )
  390. // ");
  391. // TODO: convert `where t.ID in ( ... )` into `join` and `order by`
  392. // join `CRM__#REF_TABLE__120_VIEW` refTable on (refTable.REMOTE_PRIMARY_KEY = t.ID and refTable.PRIMARY_KEY = '37' )
  393. DBG::log([
  394. 'ACL::getRefTable(...)' => [$refAcl->getNamespace(), $backRef['fieldName']],
  395. '$backRef' => $backRef,
  396. '$refTable' => $refTable
  397. ], 'array', "DBG: join \$join class(".get_class($join).")");
  398. $this->_query->join($refTable, 'refTable', [ 'rawJoin' => "t.{$sqlPk} = refTable.REMOTE_PRIMARY_KEY and refTable.PRIMARY_KEY = {$sqlBackRefPk}" ]);
  399. // moved to getItems: $this->_query->orderBy("refTable.REMOTE_PRIMARY_KEY DESC"); // TODO: order by refTable.SORT_PRIO
  400. }
  401. if (array_key_exists('__childRef', $this->_params)) {
  402. $childRef = $this->_params['__childRef'];
  403. if (!is_array($childRef)) throw new Exception("Wrong child ref structure - expected array");
  404. if (empty($childRef['namespace'])) throw new Exception("Wrong child ref structure - missing namespace");
  405. if (empty($childRef['primaryKey'])) throw new Exception("Wrong child ref structure - missing primaryKey");
  406. $refAcl = ACL::getAclByNamespace($childRef['namespace']);
  407. if ($refAcl->getSourceName() !== $this->_acl->getSourceName()) throw new Exception("Not implemented join with different source");
  408. $refTypeName = Api_WfsNs::typeName($childRef['namespace']);
  409. DBG::log("\$refTypeName({$refTypeName})");
  410. $refTable = ACL::getRefTable($this->_acl->getNamespace(), $refTypeName);
  411. // TODO: 'in' operator? // $this->_query->where($pkField, 'in', "");
  412. $sqlPk = $this->getAclSqlPrimaryKeyField();
  413. $sqlChildRefPk = DB::getPDO()->quote($childRef['primaryKey']);
  414. $this->_query->where("
  415. t.{$sqlPk} in (
  416. select refTable.PRIMARY_KEY
  417. from `{$refTable}` refTable
  418. where refTable.REMOTE_PRIMARY_KEY = {$sqlChildRefPk}
  419. )
  420. ");
  421. }
  422. DBG::log($this->_query, 'array', "TODO: optimize \$this->_query");
  423. $this->_USE_TEMPORARY_TABLE = false; // TODO: ...
  424. if ($this->_USE_TEMPORARY_TABLE) {
  425. $sortBy = ($this->hasParam('sortBy')) ? $this->getParam('sortBy') : null;
  426. if (!$sortBy) {
  427. $sortBy = $this->hasParam('order_by')
  428. ? ( $this->hasParam('order_dir')
  429. ? $this->getParam('order_by') . " " . $this->getParam('order_dir')
  430. : $this->getParam('order_by')
  431. )
  432. : '';
  433. }
  434. $TMP_TABLE_NAME = $this->_acl->getRootTableName() . "__#TEMPORARY";
  435. DBG::log("\$TMP_TABLE_NAME='{$TMP_TABLE_NAME}'");
  436. Lib::loadClass('AntAclBase');
  437. if ($this->_acl instanceof AntAclBase) {
  438. $baseSql = $this->_query->generateSql();
  439. DB::getPDO()->execSql("
  440. create temporary table `{$TMP_TABLE_NAME}`
  441. {$baseSql}
  442. ");
  443. DBG::log(DB::getPDO()->fetchValue("select count(*) as cnt from `{$TMP_TABLE_NAME}`"), 'array', "select count(*) cnt from '{$TMP_TABLE_NAME}'");
  444. $tmpAclFrom = clone($this->_acl);
  445. $tmpAclFrom->_rootTableName = $TMP_TABLE_NAME;
  446. // $tmpAclFrom = AntAclBase::buildInstance($this->_acl->getID(), [
  447. // 'name' => $this->_acl->getName(),
  448. // '_rootTableName' => $TMP_TABLE_NAME,
  449. // 'idDatabase' => $this->_acl->getDatabaseID(),
  450. // 'namespace' => $this->_acl->getNamespace(),
  451. // 'primaryKey' => $this->_acl->getPrimaryKeyField(),
  452. // 'field' => $this->_acl->getFields(),
  453. // ]);
  454. Lib::loadClass('AclQueryBuilder');
  455. $query = new AclQueryBuilder();
  456. $query->from($tmpAclFrom, $prefix = 't');
  457. DBG::log($query, 'array', "TODO: \$query by temporary table");
  458. // $this->_query = $query;
  459. // return $this->_query;
  460. }
  461. }
  462. return clone($this->_query);
  463. }
  464. public function getTotal() {
  465. $this->_beforeFetchData();
  466. if ($this->_legacyMode) return $this->_acl->getTotal($this->_params);
  467. if (null !== $this->_total) return $this->_total;
  468. $this->_total = $this->getQuery()->fetchTotal();
  469. return $this->_total;
  470. }
  471. public function hasParam($key) { return !empty($this->_params[$key]); }
  472. public function getParam($key) { return V::get($key, '', $this->_params); }
  473. public function getItems() {
  474. $this->_logToFile('getItems() ...');
  475. $this->_beforeFetchData();
  476. if ($this->_legacyMode) return $this->_acl->getItems($this->_params);// TODO: array_map( $r => (array)$r )
  477. // 'limit' => 10,
  478. // 'limitstart' => 0,
  479. // 'order_by' => 'ID',
  480. // 'order_dir' => 'desc',
  481. // TODO: sortBy from wfs query
  482. $sortBy = ($this->hasParam('sortBy')) ? $this->getParam('sortBy') : null;
  483. if (!$sortBy && array_key_exists('__backRef', $this->_params)) {
  484. $sortBy = "refTable.REMOTE_PRIMARY_KEY DESC"; // TODO: order by refTable.SORT_PRIO
  485. }
  486. if (!$sortBy) {
  487. $sortBy = $this->hasParam('order_by')
  488. ? ( $this->hasParam('order_dir')
  489. ? $this->getParam('order_by') . " " . $this->getParam('order_dir')
  490. : $this->getParam('order_by')
  491. )
  492. : '';
  493. }
  494. $limit = V::get('limit', 10, $this->_params, 'int');
  495. $offset = V::get('limitstart', 0, $this->_params, 'int');
  496. DBG::log(['params' => $this->_params, 'sortBy' => $sortBy, 'limit' => $limit, 'offset' => $offset], 'array', '$this->_params');
  497. $select = $this->prepareSelect();
  498. DBG::log($select, 'array', "\$select is(TableAcl)=(".($this->_acl instanceof TableAcl).")");
  499. // DBG::log($this->getQuery(), 'array', "\$select is(TableAcl)=(".($this->_acl instanceof TableAcl).") \$this->getQuery()");
  500. $items = $this->fetchRowsRefs(
  501. $this->getQuery()
  502. ->select(array_merge(['@primaryKey'], $select))
  503. ->limit($limit)
  504. ->offset($offset)
  505. ->orderBy($sortBy)
  506. ->fetchAll()
  507. );
  508. $this->_logToFile('getItems() fetched.');
  509. return $items;
  510. }
  511. public function getRawQueryPrimaryKeys() {
  512. if ($this->_legacyMode) throw new Exception("getRawQueryPrimaryKeys not supported in legacyMode");
  513. $sortBy = ($this->hasParam('sortBy')) ? $this->getParam('sortBy') : null;
  514. if (!$sortBy && array_key_exists('__backRef', $this->_params)) {
  515. $sortBy = "refTable.REMOTE_PRIMARY_KEY DESC"; // TODO: order by refTable.SORT_PRIO
  516. }
  517. if (!$sortBy) {
  518. $sortBy = $this->hasParam('order_by')
  519. ? ( $this->hasParam('order_dir')
  520. ? $this->getParam('order_by') . " " . $this->getParam('order_dir')
  521. : $this->getParam('order_by')
  522. )
  523. : '';
  524. }
  525. $limit = V::get('limit', 10, $this->_params, 'int');
  526. $offset = V::get('limitstart', 0, $this->_params, 'int');
  527. return $this->getQuery()
  528. ->select(['@primaryKey'])
  529. ->limit($limit)
  530. ->offset($offset)
  531. ->orderBy($sortBy)
  532. ->generateSql();
  533. }
  534. function _beforeFetchData() {
  535. if (method_exists($this->_acl, 'onBeforeFetchData')) $this->_acl->onBeforeFetchData();
  536. }
  537. public function getItem($primaryKey) { // TODO: throw exception if not found?
  538. $this->_beforeFetchData();
  539. if ($this->_legacyMode) return (array)$this->_acl->getItem($primaryKey, $this->_params);
  540. $select = $this->prepareSelect();
  541. $pkField = $this->_acl->getPrimaryKeyField();
  542. return $this->fetchRowRefs(
  543. $this->getQuery()
  544. ->select($select)
  545. ->where([$pkField, '=', $primaryKey])
  546. ->fetchFirst()
  547. );
  548. }
  549. public function prepareSelect() { // TODO: replace with getSelectLocal
  550. // TODO: select from params: 'cols' => [ fieldName, ... ]
  551. // TODO: select from params: '@instances' => 1
  552. // TODO: if no fields set, then '*'
  553. // TODO: select must contain primaryKey
  554. return $this->getSelectLocal();
  555. }
  556. public function getSelectLocal() { // @returns [ $fieldName, ... ]
  557. // TODO: rawSelect!
  558. if (null !== $this->_selectLocalFields) return $this->_selectLocalFields;
  559. $this->_selectLocalFields = [];
  560. $todoFetchAllCols = false; // select t.*
  561. if (!empty($this->_params['cols'])) {
  562. if (in_array('*', $this->_params['cols'])) $todoFetchAllCols = true;
  563. $acl = $this->_acl;
  564. $this->_selectLocalFields = array_filter($this->_params['cols'], function ($fieldQuery) use ($acl) {
  565. if ('*' === $fieldQuery) return false;
  566. list($fieldName, $subFieldQuery) = explode('/', $fieldQuery, 2);
  567. if (!empty($subFieldQuery)) return false;
  568. return $acl->isLocalField($fieldName);
  569. });
  570. }
  571. if (!empty($this->_params['@instances'])) $this->_selectLocalFields[] = '@instances';
  572. if (empty($this->_selectLocalFields)) $todoFetchAllCols = true;
  573. if (1 === count($this->_selectLocalFields) && in_array('@instances', $this->_selectLocalFields)) $todoFetchAllCols = true;
  574. // if ($this->_acl instanceof TableAcl) {
  575. // $rawSelect = $this->_acl->getDataSource()->_getSqlCols();
  576. // DBG::log($rawSelect, 'string', "DBG raw select");
  577. // if ('*' !== $rawSelect && 't.*' !== $rawSelect) {
  578. // $this->_selectLocalFields['rawSelect'] = $rawSelect;
  579. // }
  580. // }
  581. if (!empty($this->_params['cols'])) DBG::log($this->_params['cols'], 'array', "\$this->_params[cols] (" . ($this->_acl ? $this->_acl->getNamespace() : 'unknown') . ")");
  582. if ($todoFetchAllCols) {
  583. // $this->_selectLocalFields[] = '*'; // TODO: select all $this->from local fields
  584. DBG::log($this->_acl->getLocalFieldList(), 'array', "\$this->_acl->getLocalFieldList()");
  585. foreach ($this->_acl->getLocalFieldList() as $localFieldName) {
  586. $this->_selectLocalFields[] = $localFieldName;
  587. }
  588. }
  589. $primaryKeyField = $this->getAclSqlPrimaryKeyField();
  590. if (!in_array($primaryKeyField, $this->_selectLocalFields)) {
  591. $this->_selectLocalFields[] = $primaryKeyField;
  592. }
  593. // TODO: always add A_ADM_COMPANY, A_CLASSIFIED, L_APPOITMENT_USER ?
  594. DBG::log($this->_selectLocalFields, 'array', '$this->_selectLocalFields');
  595. return $this->_selectLocalFields;
  596. }
  597. public function getSelectRemote() { // @returns [ $fieldName => [ $fieldName, ... ] ]
  598. if (null !== $this->_selectRemote) return $this->_selectRemote;
  599. $this->_selectRemote = [];
  600. if (!empty($this->_params['cols'])) {
  601. $cols = $this->_params['cols'];
  602. if (in_array('*', $cols)) {
  603. $this->_selectLocalFields[] = '*';
  604. $cols = array_filter($cols, function ($fieldQuery) { return '*' !== $fieldQuery; });
  605. }
  606. $acl = $this->_acl;
  607. $cols = array_filter($cols, function ($fieldQuery) use ($acl) {
  608. list($fieldName, $subFieldQuery) = explode('/', $fieldQuery, 2);
  609. // if (empty($subFieldQuery)) return false;
  610. return !$acl->isLocalField($fieldName);
  611. });
  612. foreach ($cols as $fieldQuery) { // group by fieldName
  613. list($fieldName, $subFieldQuery) = explode('/', $fieldQuery, 2);
  614. if (!array_key_exists($fieldName, $this->_selectRemote)) $this->_selectRemote[$fieldName] = [];
  615. if (!empty($subFieldQuery)) $this->_selectRemote[$fieldName][] = $subFieldQuery;
  616. }
  617. }
  618. return $this->_selectRemote;
  619. }
  620. public function fetchRowsRefs($rows) {
  621. $pkField = $this->_acl->getPrimaryKeyField();
  622. $namespace = $this->_acl->getNamespace();
  623. $this->_foundFeatures = array_merge($this->_foundFeatures, array_map(function ($row) use ($namespace, $pkField) {
  624. return "{$namespace}." . V::get($pkField, '', $row);
  625. }, $rows));
  626. DBG::log($this->_foundFeatures, 'array', "_foundFeatures");
  627. return array_map([ $this, 'fetchRowRefs' ], $rows);
  628. }
  629. public function fetchRowRefs($row) {
  630. try {
  631. return $this->_fetchRowRefs($row);
  632. } catch (Exception $e) {
  633. DBG::log($e);
  634. }
  635. return $row;
  636. }
  637. public function _fetchRowRefs($row) {
  638. if (!$row) return $row;
  639. $sqlPk = $this->getAclSqlPrimaryKeyField();
  640. $primaryKey = $row[$sqlPk];
  641. DBG::log($row, 'array', "DBG primaryKey '{$primaryKey}'");
  642. if (!$primaryKey) throw new Exception("Missing primaryKey");
  643. $defaultRefLimit = 10; // TODO: get from $this->_params and pass to nested buildQuery
  644. $refLimitPlus1 = $defaultRefLimit + 1;
  645. foreach ($this->getSelectRemote() as $fieldName => $cols) {
  646. DBG::log($cols, 'array', "add select remote '{$fieldName}' \$cols");
  647. $xsdType = $this->_acl->getXsdFieldType($fieldName);
  648. if ('ref:' === substr($xsdType, 0, 4) && empty($cols)) {
  649. DBG::log("add remote xlink's '{$fieldName}' \$items[{$primaryKey}] ...");
  650. $refTable = ACL::getRefTable($this->_acl->getNamespace(), $fieldName);
  651. if (!$refTable) DBG::log("BUG: Missing refTable in add remote xlink's '{$fieldName}' \$items[{$primaryKey}]");
  652. if ($refTable) {
  653. $xlinks = DB::getPDO()->fetchAll("
  654. select r.REMOTE_PRIMARY_KEY
  655. from `{$refTable}` r
  656. where r.PRIMARY_KEY = '{$primaryKey}'
  657. order by r.REMOTE_PRIMARY_KEY DESC -- TODO r.SORT_PRIO
  658. limit {$refLimitPlus1}
  659. ");
  660. DBG::log($xlinks, 'array', "add remote xlink's for '{$fieldName}' \$items[{$primaryKey}]");
  661. $row[$fieldName] = array_map(function ($refInfo) use ($fieldName) {
  662. $ns = Core_AclHelper::parseTypeName($fieldName);
  663. return [
  664. // '_ns' => $ns,
  665. 'xlink' => "{$ns['url']}#{$ns['name']}.{$refInfo['REMOTE_PRIMARY_KEY']}",
  666. ];
  667. }, $xlinks);
  668. if (count($xlinks) > $defaultRefLimit) DBG::log('TODO: xlink FETCH MORE DATA...');
  669. if (count($xlinks) > $defaultRefLimit) $row[$fieldName][] = [ 'p5:links' => [
  670. 'p5:next' => [
  671. '@typeName' => $fieldName,
  672. '@backRefNS' => $this->_acl->getNamespace(),
  673. '@backRefPK' => $primaryKey,
  674. '@startIndex' => $defaultRefLimit,
  675. '@maxFeatures' => $defaultRefLimit,
  676. '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}",
  677. ],
  678. ] ];
  679. DBG::log($row[$fieldName], 'array', "remote xlinks for \$items[{$primaryKey}][{$fieldName}]");
  680. }
  681. } else if ('ref:' === substr($xsdType, 0, 4) && !empty($cols)) {
  682. $refAcl = ACL::getAclByTypeName($fieldName);
  683. $refQuery = $refAcl->buildQuery([
  684. 'cols' => $cols,
  685. '__backRef' => [
  686. 'namespace' => $this->_acl->getNamespace(),
  687. 'primaryKey' => $primaryKey,
  688. 'fieldName' => $fieldName,
  689. ],
  690. 'limit' => $refLimitPlus1,
  691. 'skipFeaturesAsXlink' => $this->_foundFeatures
  692. // TODO: add $defaultRefLimit + 1
  693. ]);
  694. $totalRefs = $refQuery->getTotal();
  695. $items = $refQuery->getItems([ 'limit' => $defaultRefLimit ]);
  696. // TODO: if (count($items) > $defaultRefLimit) // TODO: add item for GUI - has more data + wfs link to fetch more (offset)
  697. DBG::log($items, 'array', "add remote items '{$fieldName}' \$items[{$primaryKey}][{$fieldName}] total({$totalRefs})");
  698. if ($totalRefs > $defaultRefLimit) {
  699. DBG::log('TODO: resolve recurse fetch more data link');
  700. array_pop($items); // remove last item
  701. $items[] = [ 'p5:links' => [
  702. 'p5:next' => [
  703. '@typeName' => $fieldName,
  704. '@backRefNS' => $this->_acl->getNamespace(),
  705. '@backRefPK' => $primaryKey,
  706. '@startIndex' => $defaultRefLimit,
  707. '@maxFeatures' => $defaultRefLimit,
  708. '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}",
  709. ],
  710. ] ];
  711. }
  712. $refNs = $refAcl->getNamespace();
  713. $refPk = $refAcl->getPrimaryKeyField();
  714. $this__hasFeatureId = [ $this, 'hasFeatureId' ];
  715. $this__addFeatureId = [ $this, 'addFeatureId' ];
  716. $row[$fieldName] = array_map(function ($item) use ($fieldName, $refNs, $refPk, $this__hasFeatureId, $this__addFeatureId) {
  717. if (1 === count($item) && !empty($item['p5:links'])) return $item;
  718. $pk = V::get($refPk, '', $item);
  719. $featureId = "{$refNs}.{$pk}";
  720. if (!$this__hasFeatureId($featureId)) {
  721. $this__addFeatureId($featureId);
  722. return $item;
  723. } else {
  724. $ns = Core_AclHelper::parseTypeName($fieldName);
  725. return [
  726. 'xlink' => "{$ns['url']}#{$ns['name']}.{$pk}",
  727. ];
  728. }
  729. }, $items);
  730. } else { // TODO: field is not ref
  731. DBG::log($items, 'array', "NotImplemented - add remote items for non ref field \$items[{$primaryKey}][{$fieldName}]?");
  732. }
  733. }
  734. return $row;
  735. }
  736. public function hasFeatureId($featureId) {
  737. return in_array($featureId, $this->_foundFeatures);
  738. }
  739. public function addFeatureId($featureId) {
  740. $this->_foundFeatures[] = $featureId;
  741. DBG::log($this->_foundFeatures, 'array', "addFeatureId({$featureId})");
  742. }
  743. public function getAclSqlPrimaryKeyField() {
  744. return ($this->_acl instanceof Core_AclBase)
  745. ? $this->_acl->getSqlPrimaryKeyField()
  746. : 'ID';
  747. }
  748. }