SystemObjectFieldStorageAcl.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. Lib::loadClass('Router');
  5. class Schema_SystemObjectFieldStorageAcl extends Core_AclSimpleSchemaBase {
  6. public $_simpleSchema = [
  7. 'root' => [
  8. '@namespace' => 'default_objects/SystemObjectField',
  9. '@primaryKey' => 'namespace',
  10. 'idZasob' => [ '@type' => 'xsd:integer' ],
  11. 'idDatabase' => [ '@type' => 'xsd:integer' ],
  12. '_rootTableName' => [ '@type' => 'xsd:string' ],
  13. 'namespace' => [ '@type' => 'xsd:string' ],
  14. 'objectNamespace' => [ '@type' => 'xsd:string' ],
  15. 'fieldNamespace' => [ '@type' => 'xsd:string' ],
  16. 'xsdType' => [ '@type' => 'xsd:string' ],
  17. 'xsdRestrictions' => [ '@type' => 'xsd:string' ],
  18. 'appInfo' => [ '@type' => 'xsd:string' ],
  19. 'minOccurs' => [ '@type' => 'xsd:string' ],
  20. 'maxOccurs' => [ '@type' => 'xsd:string' ], // '0..unbounded',
  21. 'isActive' => [ '@type' => 'xsd:integer' ], // installed
  22. 'description' => [ '@type' => 'xsd:string' ],
  23. 'isLocal' => [ '@type' => 'xsd:integer', '@comment' => "is in _rootTableName" ],
  24. // 'A_RECORD_CREATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'autor' ],
  25. // 'A_RECORD_CREATE_DATE' => [ '@type' => 'xsd:date' , '@label' => 'utworzono' ],
  26. // 'A_RECORD_UPDATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'zaktualizował' ],
  27. // 'A_RECORD_UPDATE_DATE' => [ '@type' => 'xsd:date', '@label' => 'zaktualizowano' ],
  28. ]
  29. ];
  30. // public $_rootTableName = 'CRM_LISTA_ZASOBOW';
  31. public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT_FIELD';
  32. public $_version = '1';
  33. public function updateCache($namespace = null) {
  34. DBG::simpleLog('schema', "SystemObjectField::updateCache({$namespace})...");
  35. // DB::getPDO()->execSql(" drop table if exists `{$this->_rootTableName}` "); // TODO: DBG
  36. DB::getPDO()->execSql("
  37. create table if not exists `{$this->_rootTableName}` (
  38. `namespace` varchar(255) DEFAULT '',
  39. `fieldNamespace` varchar(255) DEFAULT '',
  40. `idZasob` int(11) DEFAULT NULL,
  41. `idDatabase` int(11) NOT NULL,
  42. `_rootTableName` varchar(255) DEFAULT '',
  43. `objectNamespace` varchar(255) DEFAULT '',
  44. `xsdType` varchar(255) DEFAULT '',
  45. `xsdRestrictions` varchar(1000) DEFAULT '',
  46. `appInfo` varchar(1000) DEFAULT '',
  47. `minOccurs` int(11) DEFAULT '0',
  48. `maxOccurs` varchar(11) DEFAULT '1' COMMENT '0..unbounded',
  49. `isActive` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'installed',
  50. `description` varchar(255) DEFAULT '',
  51. `isLocal` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'is in _rootTableName',
  52. UNIQUE KEY `idZasob` (idZasob),
  53. PRIMARY KEY (`namespace`),
  54. KEY `isActive` (isActive)
  55. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  56. ");
  57. try {
  58. DB::getPDO()->execSql(" ALTER TABLE `{$this->_rootTableName}` ADD `appInfo` VARCHAR(1000) NOT NULL DEFAULT '' AFTER `xsdRestrictions` ");
  59. } catch (Exception $e) {
  60. DBG::log($e);
  61. }
  62. try {
  63. DB::getPDO()->execSql(" ALTER TABLE `{$this->_rootTableName}` ADD `isLocal` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'is in _rootTableName' AFTER `description` ");
  64. } catch (Exception $e) {
  65. DBG::log($e);
  66. }
  67. // DB::getPDO()->execSql(" drop table if exists `{$this->_rootTableName}_enum` ");// TODO: DBG
  68. DB::getPDO()->execSql("
  69. create table if not exists `{$this->_rootTableName}_enum` (
  70. `namespace` varchar(255) DEFAULT '' COMMENT 'concat obj ns / field ns / value',
  71. `fieldNamespace` varchar(255) DEFAULT '',
  72. `objectNamespace` varchar(255) DEFAULT '',
  73. `value` varchar(255) DEFAULT '',
  74. `label` varchar(255) DEFAULT '',
  75. `isActive` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'installed',
  76. KEY `objectNamespace` (`objectNamespace`),
  77. KEY `fieldNamespace` (`fieldNamespace`),
  78. KEY `isActive` (isActive)
  79. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  80. ");
  81. if (!$namespace) return;
  82. DB::getPDO()->update($this->_rootTableName, 'objectNamespace', $namespace, ['isActive' => 0]);
  83. DB::getPDO()->update("{$this->_rootTableName}_enum", 'objectNamespace', $namespace, ['isActive' => 0]);
  84. if (!$namespace) throw new Exception("Missing namespace '{$namespace}'");
  85. $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace);
  86. DBG::nicePrint($objectItem, '$objectItem');
  87. DBG::log($objectItem, 'array', '$objectItem');
  88. switch ($objectItem['_type']) {
  89. case 'AntAcl': $this->updateCacheAntAcl($objectItem); break;
  90. case 'TableAcl': $this->updateCacheTableAcl($objectItem); break;
  91. default: throw new Exception("TODO: Not Implemented type '{$objectItem['_type']}'");
  92. }
  93. // TODO: mv from methods: SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
  94. // 'namespace' => $item['namespace'],
  95. // 'isStructInstalled' => 1
  96. // ]);
  97. $dbName = DB::getPDO()->getDatabaseName();
  98. DB::getPDO()->execSql("
  99. update `{$this->_rootTableName}` t
  100. set t.isLocal = IF(
  101. ( select c.COLUMN_NAME
  102. from information_schema.COLUMNS c
  103. where c.COLUMN_NAME = t.fieldNamespace
  104. and c.TABLE_SCHEMA = '{$dbName}'
  105. and c.TABLE_NAME = '{$objectItem['_rootTableName']}'
  106. limit 1
  107. ) is null
  108. , 0, 1)
  109. where t.objectNamespace = '{$objectItem['namespace']}'
  110. and t._rootTableName = '{$objectItem['_rootTableName']}'
  111. ");
  112. }
  113. public function updateCacheAntAcl($item) {
  114. Lib::loadClass('AntAclBase');
  115. $antAclPath = APP_PATH_SCHEMA . DS . 'ant-object' . DS . str_replace(['__x3A__', ':'], ['.', '/'], $item['typeName']);
  116. if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists");
  117. Lib::loadClass('XML');
  118. $xsdType = XML::getXsdTypeFromXsdSchema("{$antAclPath}/{$item['name']}.xsd", $namespace = $item['namespace'], $name = $item['name']);
  119. DBG::nicePrint($xsdType, '$xsdType');
  120. if (empty($xsdType['struct'])) throw new Exception("Field list not found for '{$item['namespace']}'");
  121. foreach ($xsdType['struct'] as $fieldName => $x) {
  122. $listEnum = [];
  123. if (!empty($x['restrictions']['enumeration'])) {
  124. $listEnum = $x['restrictions']['enumeration'];
  125. unset($x['restrictions']['enumeration']);
  126. }
  127. DB::getPDO()->insertOrUpdate($this->_rootTableName, [
  128. 'namespace' => "{$item['namespace']}/{$fieldName}",
  129. 'objectNamespace' => $item['namespace'],
  130. 'idDatabase' => $item['idDatabase'],
  131. '_rootTableName' => $item['_rootTableName'],
  132. 'fieldNamespace' => $fieldName,
  133. 'xsdType' => $x['type'],
  134. 'xsdRestrictions' => json_encode($x['restrictions']),
  135. 'appInfo' => json_encode($x['appInfo']),
  136. 'minOccurs' => $x['minOccurs'],
  137. 'maxOccurs' => $x['maxOccurs'],
  138. 'isActive' => 1
  139. ]);
  140. if (!empty($listEnum)) {
  141. DBG::nicePrint($listEnum, '$listEnum');
  142. foreach ($listEnum as $value => $label) {
  143. DB::getPDO()->insertOrUpdate("{$this->_rootTableName}_enum", [
  144. 'namespace' => "{$item['namespace']}/{$fieldName}/@{$value}",
  145. 'fieldNamespace' => $fieldName,
  146. 'objectNamespace' => $item['namespace'],
  147. 'value' => $value,
  148. 'label' => $label,
  149. 'isActive' => 1
  150. ]);
  151. }
  152. }
  153. }
  154. SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
  155. 'namespace' => $item['namespace'],
  156. 'isStructInstalled' => 1
  157. ]);
  158. $zasobTableName = substr($item['objectNamespace'], strlen('default_db/'));
  159. $zasobTableName = (false !== strpos($zasobTableName, '/'))
  160. ? $item['objectNamespace']
  161. : $zasobTableName;
  162. DB::getPDO()->execSql("
  163. update `{$this->_rootTableName}` t
  164. join CRM_LISTA_ZASOBOW zp on(zp.PARENT_ID = {$item['idDatabase']} and zp.`DESC` = '{$zasobTableName}')
  165. join CRM_LISTA_ZASOBOW z on(z.PARENT_ID = zp.ID and z.`DESC` = t.fieldNamespace)
  166. set t.idZasob = z.ID
  167. where t.objectNamespace = '{$item['namespace']}'
  168. ");
  169. {// TODO: DBG
  170. DBG::nicePrint($schema, '$schema');
  171. Lib::loadClass('Core_XmlWriter');
  172. ob_start();
  173. $xmlWriter = new Core_XmlWriter();
  174. $xmlWriter->openUri('php://output');
  175. $xmlWriter->setIndent(true);
  176. $xmlWriter->startDocument('1.0','UTF-8');
  177. $xmlWriter->h($schema[0], $schema[1], $schema[2]);
  178. $xmlWriter->endDocument();
  179. echo UI::h('pre', ['style' => 'max-height:400px; overflow:scroll'], htmlspecialchars(ob_get_clean()));
  180. }
  181. }
  182. public function updateCacheTableAcl($item) {
  183. // TODO: xsdType - convert mysql type to xsdType, xsdRestrictions
  184. $xsdInfo = [];
  185. Lib::loadClass('SchemaHelper');
  186. $dbName = DB::getPDO()->getDatabaseName();
  187. $mysqlTypes = DB::getPDO()->fetchAll("
  188. select c.COLUMN_NAME, c.COLUMN_TYPE, c.DATA_TYPE, c.IS_NULLABLE, c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, c.NUMERIC_SCALE
  189. from `information_schema`.`COLUMNS` c
  190. where c.TABLE_SCHEMA = '{$dbName}'
  191. and c.TABLE_NAME = '{$item['_rootTableName']}'
  192. ");
  193. foreach ($mysqlTypes as $mysqlType) {
  194. $xsdInfo[ $mysqlType['COLUMN_NAME'] ] = SchemaHelper::convertMysqlTypeToSchemaXsd($mysqlType);
  195. }
  196. DBG::nicePrint($xsdInfo, '$xsdInfo');
  197. foreach ($xsdInfo as $fieldName => $x) {
  198. $listEnum = [];
  199. if (!empty($x['restrictions']['enumeration'])) {
  200. $listEnum = $x['restrictions']['enumeration'];
  201. unset($x['restrictions']['enumeration']);
  202. }
  203. DB::getPDO()->insertOrUpdate($this->_rootTableName, [
  204. 'namespace' => "{$item['namespace']}/{$fieldName}",
  205. 'fieldNamespace' => $fieldName,
  206. 'isActive' => 1,
  207. 'idDatabase' => $item['idDatabase'],
  208. '_rootTableName' => $item['_rootTableName'],
  209. 'objectNamespace' => $item['namespace'],
  210. 'xsdType' => $x['type'],
  211. 'xsdRestrictions' => json_encode($x['restrictions']),
  212. 'appInfo' => json_encode([]),
  213. ]);
  214. if (!empty($listEnum)) {
  215. DBG::nicePrint($listEnum, '$listEnum');
  216. foreach ($listEnum as $value => $label) {
  217. DB::getPDO()->insertOrUpdate("{$this->_rootTableName}_enum", [
  218. 'namespace' => "{$item['namespace']}/{$fieldName}/@{$value}",
  219. 'fieldNamespace' => $fieldName,
  220. 'objectNamespace' => $item['namespace'],
  221. 'value' => $value,
  222. 'label' => $label,
  223. 'isActive' => 1
  224. ]);
  225. }
  226. }
  227. }
  228. SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
  229. 'namespace' => $item['namespace'],
  230. 'isStructInstalled' => 1
  231. ]);
  232. $zasobTableName = substr($item['namespace'], strlen('default_db/'));
  233. $zasobTableName = (false !== strpos($zasobTableName, '/'))
  234. ? $item['namespace']
  235. : $zasobTableName;
  236. DB::getPDO()->execSql("
  237. update `{$this->_rootTableName}` t
  238. join CRM_LISTA_ZASOBOW zp on(zp.PARENT_ID = {$item['idDatabase']} and zp.`DESC` = '{$zasobTableName}' and zp.PARENT_ID = {$item['idDatabase']})
  239. join CRM_LISTA_ZASOBOW z on(z.PARENT_ID = zp.ID and z.`DESC` = t.fieldNamespace)
  240. set t.idZasob = z.ID
  241. where t.objectNamespace = '{$item['namespace']}'
  242. ");
  243. $struct = $this->getItems([
  244. '__backRef' => [
  245. 'namespace' => 'default_objects/SystemObject',
  246. 'primaryKey' => $item['namespace']
  247. ]
  248. ]);
  249. DBG::nicePrint($struct, '$struct');
  250. {// TODO: DBG
  251. $schema = ['xsd:schema', [
  252. 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema",
  253. 'xmlns:gml' => "http://www.opengis.net/gml",
  254. 'xmlns:p5' => "https://biuro.biall-net.pl/wfs",
  255. 'xmlns:default_db' => "https://biuro.biall-net.pl/wfs/default_db",
  256. 'xmlns:default_objects' => "https://biuro.biall-net.pl/wfs/default_objects",
  257. 'elementFormDefault' => "qualified",
  258. 'version' => "1.0.0",
  259. ], []];
  260. Lib::loadClass('Api_WfsNs');
  261. $tnsUri = Api_WfsNs::getNsUri($item['nsPrefix']);
  262. $schema[1]['xmlns:' . Api_WfsNs::getNsPrefix($tnsUri)] = $tnsUri;
  263. $schema[1]['targetNamespace'] = $tnsUri;
  264. $schema[2][] = ['xsd:import', ['namespace'=>"http://www.opengis.net/gml", 'schemaLocation'=>"https://biuro.biall-net.pl/dev-pl/se-master/schema/gml/2.1.2/feature.xsd"], null];
  265. $schema[2][] = ['xsd:complexType', [ 'name'=> $item['name'] . "Type" ], [
  266. [ 'xsd:complexContent', [], [
  267. [ 'xsd:extension', ['base'=>"gml:AbstractFeatureType"], [
  268. [ 'xsd:sequence', [], array_map(function ($field) {
  269. $attrs = [
  270. 'name' => $field['fieldNamespace'],
  271. // <xsd:element minOccurs="1" maxOccurs="1" name="ID" type="xsd:integer" nillable="true"/>
  272. 'minOccurs' => $field['minOccurs'],
  273. 'maxOccurs' => $field['maxOccurs'],
  274. ];
  275. $childrens = null;
  276. $restrictions = [];
  277. foreach ($this->getXsdRestrictionsValue($field) as $k => $v) {
  278. if ('enumeration' == $k) {
  279. foreach ($v as $enumValue) {
  280. $restrictions[] = [ 'xsd:enumeration', [ 'value' => $enumValue ], null ];
  281. }
  282. } else if ('maxLength' == $k) {
  283. $restrictions[] = [ 'xsd:maxLength', [ 'value' => $v ], null ];
  284. } else if ('totalDigits' == $k) {
  285. $restrictions[] = [ 'xsd:totalDigits', [ 'value' => $v ], null ];
  286. } else if ('fractionDigits' == $k) {
  287. $restrictions[] = [ 'xsd:fractionDigits', [ 'value' => $v ], null ];
  288. } else if ('nillable' == $k) {
  289. if ($v) $attrs['nillable'] = "true";
  290. } else {
  291. DBG::log(['TODO' => $k, $v]);
  292. }
  293. }
  294. if (!empty($restrictions)) {
  295. $childrens = [
  296. [ 'xsd:simpleType', [], [
  297. [ 'xsd:restriction', [ 'base' => $field['xsdType'] ], $restrictions ]
  298. ] ]
  299. ];
  300. } else {
  301. $attrs['type'] = $field['xsdType'];
  302. }
  303. return [ 'xsd:element', $attrs, $childrens ];
  304. }, $struct) ]
  305. ] ]
  306. ] ]
  307. ] ];
  308. $schema[2][] = ['xsd:element', [ 'name' => $item['name'], 'type' => "{$item['nsPrefix']}:{$item['name']}Type", 'substitutionGroup' => "gml:_Feature" ], null];
  309. DBG::nicePrint($schema, '$schema');
  310. Lib::loadClass('Core_XmlWriter');
  311. ob_start();
  312. $xmlWriter = new Core_XmlWriter();
  313. $xmlWriter->openUri('php://output');
  314. $xmlWriter->setIndent(true);
  315. $xmlWriter->startDocument('1.0','UTF-8');
  316. $xmlWriter->h($schema[0], $schema[1], $schema[2]);
  317. $xmlWriter->endDocument();
  318. echo UI::h('pre', ['style' => 'max-height:400px; overflow:scroll'], htmlspecialchars(ob_get_clean()));
  319. }
  320. }
  321. public function _parseWhere($params = []) {
  322. $sqlWhere = [];
  323. DBG::log($params, 'array', "SystemObject::_parseWhere");
  324. if (!empty($params['__backRef'])) {
  325. // '__backRef' => [
  326. // 'namespace' => 'default_objects/SystemObject',
  327. // 'primaryKey' => 'default_db/TEST_PERMS',
  328. // ),
  329. if (empty($params['__backRef']['namespace'])) throw new Exception("Missing refFrom/namespace");
  330. if (empty($params['__backRef']['primaryKey'])) throw new Exception("Missing refFrom/primaryKey");
  331. if ('default_objects/SystemObject' != $params['__backRef']['namespace']) throw new Exception("Unsupported refFrom/namespace '{$params['__backRef']['namespace']}'");
  332. $sqlWhere[] = "t.objectNamespace = " . DB::getPDO()->quote($params['__backRef']['primaryKey'], PDO::PARAM_INT);
  333. }
  334. {
  335. $filterParams = [];
  336. $xsdFields = $this->getXsdTypes();
  337. DBG::log($xsdFields);
  338. foreach ($params as $k => $v) {
  339. if ('f_' != substr($k, 0, 2)) continue;
  340. $fieldName = substr($k, 2);
  341. if (!array_key_exists($fieldName, $xsdFields)) {
  342. // TODO: check query by xpath or use different param prefix
  343. throw new Exception("Field '{$fieldName}' not found in '{$this->_namespace}'");
  344. }
  345. if ('p5:' == substr($xsdFields[$fieldName]['xsdType'], 0, 3)) {
  346. continue;
  347. }
  348. $filterParams[$fieldName] = $v;
  349. }
  350. }
  351. if (!empty($filterParams)) {
  352. DBG::log($filterParams, 'array', "SystemObject::_parseWhere TODO \$filterParams");
  353. foreach ($filterParams as $fieldName => $value) {
  354. if (is_array($value)) {
  355. DBG::log($value, 'array', "TODO SystemObject::_parseWhere array value for \$filterParams[{$fieldName}]");
  356. } else if (is_scalar($value)) {
  357. if ('=' == substr($value, 0, 1)) {
  358. $sqlWhere[] = "t.{$fieldName} = " . DB::getPDO()->quote(substr($value, 1), PDO::PARAM_STR);
  359. } else {
  360. $sqlWhere[] = "t.{$fieldName} like " . DB::getPDO()->quote("%{$value}%", PDO::PARAM_STR);
  361. }
  362. } else {
  363. DBG::log($value, 'array', "BUG SystemObject::_parseWhere unknown type for \$filterParams[{$fieldName}]");
  364. }
  365. }
  366. }
  367. return (!empty($sqlWhere)) ? "where " . implode(" and ", $sqlWhere) : '';
  368. }
  369. public function getTotal($params = []) {
  370. $sqlWhere = $this->_parseWhere($params);
  371. return DB::getPDO()->fetchValue("
  372. select count(1) as cnt
  373. from `{$this->_rootTableName}` t
  374. {$sqlWhere}
  375. ");
  376. }
  377. public function getItem($pk, $params = []) {
  378. if (!$pk) throw new Exception("Missing primary key '{$this->_namespace}'");
  379. $pkField = $this->getSqlPrimaryKeyField();
  380. if (!$pkField) throw new Exception("Missing primary key field defined in '{$this->_namespace}'");
  381. $sqlPk = DB::getPDO()->quote($pk, PDO::PARAM_STR);
  382. $item = DB::getPDO()->fetchFirst("
  383. select t.*
  384. from `{$this->_rootTableName}` t
  385. where t.`{$pkField}` = {$sqlPk}
  386. ");
  387. if (!$item) throw new Exception("Item '{$pk}' not exists - type '{$this->_namespace}'");
  388. return $this->buildFeatureFromSqlRow($item, $params);
  389. }
  390. public function getItems($params = []) {
  391. $sqlWhere = $this->_parseWhere($params);
  392. $currSortCol = V::get('order_by', 'idZasob', $params);
  393. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  394. // TODO: validate $currSortCol is in field list
  395. // TODO: validate $currSortFlip ('asc' or 'desc')
  396. $xsdFields = $this->getXsdTypes();
  397. if (!array_key_exists($currSortCol, $xsdFields)) throw new Exception("Field '{$currSortCol}' not found in '{$this->_namespace}'");
  398. if (!in_array($currSortFlip, ['asc', 'desc'])) throw new Exception("Sort dir not allowed");
  399. $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}";
  400. $limit = V::get('limit', 0, $params, 'int');
  401. $limit = ($limit < 0) ? 0 : $limit;
  402. $offset = V::get('limitstart', 0, $params, 'int');
  403. $offset = ($offset < 0) ? 0 : $offset;
  404. $sqlLimit = ($limit > 0)
  405. ? "limit {$limit} offset {$offset}"
  406. : '';
  407. return array_map(array($this, 'buildFeatureFromSqlRow'), DB::getPDO()->fetchAll("
  408. select t.*
  409. from `{$this->_rootTableName}` t
  410. {$sqlWhere}
  411. {$sqlOrderBy}
  412. {$sqlLimit}
  413. "));
  414. }
  415. public function getXsdRestrictionsValue($item) {
  416. $xsdRestrictions = @json_decode($item['xsdRestrictions'], $assoc = true);
  417. if (empty($xsdRestrictions)) $xsdRestrictions = [];
  418. if (array_key_exists('__DBG__', $xsdRestrictions)) unset($xsdRestrictions['__DBG__']);
  419. return $xsdRestrictions;
  420. }
  421. public function buildFeatureFromSqlRow($item) {
  422. if ('p5:enum' == V::get('xsdType', '', $item)) {
  423. $xsdRestrictions = @json_decode($item['xsdRestrictions'], $assoc = true);
  424. $xsdRestrictions['enumeration'] = [];
  425. foreach (DB::getPDO()->fetchAll("
  426. select t.value, t.label
  427. from `{$this->_rootTableName}_enum` t
  428. where t.objectNamespace = '{$item['objectNamespace']}'
  429. and t.fieldNamespace = '{$item['fieldNamespace']}'
  430. and t.isActive = 1
  431. ") as $enum) {
  432. $xsdRestrictions['enumeration'][ $enum['value'] ] = $enum['label'];
  433. }
  434. $item['xsdRestrictions'] = json_encode($xsdRestrictions);
  435. }
  436. return $item;
  437. }
  438. public function updateItem($itemPatch) {
  439. SchemaFactory::loadDefaultObject('SystemObject')->clearGetItemCache();
  440. $pkField = $this->getPrimaryKeyField();
  441. $pk = V::get($pkField, null, $itemPatch);
  442. if (null === $pk) throw new Exception("BUG missing primary key field for '{$this->_namespace}' updateItem");
  443. DBG::log(['updateItem $itemPatch', $itemPatch]);
  444. unset($itemPatch[$pkField]);
  445. if (empty($itemPatch)) return 0;
  446. foreach ($itemPatch as $fieldName => $value) {
  447. if ('idZasob' == $fieldName) continue;
  448. throw new Exception("Update field '{$fieldName}' not allowed for '{$this->_namespace}'");
  449. }
  450. return DB::getPDO()->update($this->_rootTableName, $pkField, $pk, $itemPatch);
  451. }
  452. }