SystemObjectStorageAcl.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. Lib::loadClass('Router');
  5. Lib::loadClass('SchemaVersionUpgrade');
  6. class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
  7. public $_simpleSchema = [
  8. 'root' => [
  9. '@namespace' => 'default_objects/SystemObject',
  10. '@primaryKey' => 'namespace',
  11. 'idZasob' => [ '@type' => 'xsd:integer' ],
  12. 'idDatabase' => [ '@type' => 'xsd:integer' ],
  13. 'namespace' => [ '@type' => 'xsd:string' ],
  14. '_rootTableName' => [ '@type' => 'xsd:string' ],
  15. '_type' => [ '@type' => 'xsd:string' ],
  16. 'hasStruct' => [ '@type' => 'xsd:integer' ], // 0 - removed, old, 1 - has config, structure
  17. 'isStructInstalled' => [ '@type' => 'xsd:integer' ], // installed
  18. 'isObjectActive' => [ '@type' => 'xsd:integer' ], // (0,1) - admin settings with restrictions: (hasStruct, isStructInstalled, all fields installed and with idZasob)
  19. 'description' => [ '@type' => 'xsd:string' ],
  20. 'name' => [ '@type' => 'p5:string' ],
  21. 'typeName' => [ '@type' => 'p5:string' ],
  22. 'nsPrefix' => [ '@type' => 'p5:string' ],
  23. 'reinstallLink' => [ '@type' => 'p5:www_link' ],
  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. 'field' => [ '@ref' => 'default_objects/SystemObjectField', '@maxOccurs' => 'unbounded' ]
  29. ]
  30. ];
  31. // public $_rootTableName = 'CRM_LISTA_ZASOBOW';
  32. public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT';
  33. public function __construct($simpleSchema = null) {
  34. parent::__construct($simpleSchema);
  35. SchemaVersionUpgrade::upgradeSchema();
  36. }
  37. public function updateCache($idDatabase = null) {
  38. DBG::simpleLog('schema', "SystemObject::updateCache...");
  39. // DB::getPDO()->execSql(" drop table if exists `{$this->_rootTableName}` "); // TODO: DBG
  40. DB::getPDO()->execSql(" update `{$this->_rootTableName}` set hasStruct = 0 ");
  41. $idDefDB = DB::getPDO()->getZasobId();
  42. $sourceStorage = SchemaFactory::loadDefaultObject('SystemSource');
  43. foreach ($sourceStorage->getItems([ 'f_hasConfig' => 1 ]) as $source) {
  44. if ('default_objects' == $source['nsPrefix']) {
  45. $clsFiles = array_map(function ($clsFile) {
  46. return substr($clsFile, strlen(APP_PATH_LIB . "/Schema/"), -1 * strlen('StorageAcl.php'));
  47. // return str_replace('/', '_', substr($clsFile, strlen(APP_PATH_LIB . "/Schema/"), -1 * strlen('StorageAcl.php')));
  48. }, array_merge(
  49. glob(APP_PATH_LIB . "/Schema/*StorageAcl.php", GLOB_NOSORT),
  50. glob(APP_PATH_LIB . "/Schema/*/*StorageAcl.php", GLOB_NOSORT)
  51. ));
  52. DBG::log($clsFiles, 'array', "DBG glob default_objects");
  53. foreach ($clsFiles as $clsName) {
  54. try {
  55. $acl = SchemaFactory::loadDefaultObject($clsName);
  56. $namespace = $acl->getNamespace();
  57. DB::getPDO()->insertOrUpdate($this->_rootTableName, [
  58. 'namespace' => $namespace,
  59. 'idDatabase' => $source['idZasob'],
  60. '_type' => "StorageAcl",
  61. '_rootTableName' => $acl->getRootTableName(),
  62. 'hasStruct' => 1
  63. ]);
  64. } catch (Exception $e) {
  65. UI::alert('danger', $e->getMessage());
  66. }
  67. }
  68. DB::getPDO()->execSql("
  69. insert into `{$this->_rootTableName}` (namespace, idZasob, idDatabase, description, hasStruct)
  70. select concat('{$source['nsPrefix']}/', t.`DESC`)
  71. , t.ID as idZasob
  72. , '{$source['idZasob']}' as idDatabase
  73. , t.`OPIS` as description
  74. , 1 as hasStruct
  75. from CRM_LISTA_ZASOBOW t
  76. where t.`TYPE` = 'TABELA'
  77. and t.A_STATUS in('NORMAL', 'WAITING')
  78. and t.PARENT_ID = {$source['idZasob']}
  79. and t.`DESC` not like '%/%'
  80. on duplicate key update idZasob = t.ID
  81. , hasStruct = 1
  82. ");
  83. }
  84. else {
  85. try {
  86. $dbName = DB::getPDO($source['idZasob'])->getDatabaseName();
  87. $dbType = DB::getPDO($source['idZasob'])->getType();
  88. if ('mysql' == $dbType) { // TODO: if the same database DB::getPDO($source['idZasob'])->getID === DB::getPDO()->getID
  89. UI::alert('warning', "TODO: { id: {$source['idZasob']}, nsPrefix: '{$source['nsPrefix']}', dbName: '{$dbName}', dbType: '{$dbType}'} ...");
  90. // TODO: if another DB split select and insert
  91. DB::getPDO()->execSql("
  92. insert into `{$this->_rootTableName}` (namespace, idDatabase, _rootTableName, _type, description, hasStruct, isStructInstalled)
  93. select concat('{$source['nsPrefix']}/', t.TABLE_NAME) as namespace
  94. , '{$source['idZasob']}' as idDatabase
  95. , t.TABLE_NAME as _rootTableName
  96. , 'TableAcl' as _type
  97. , t.TABLE_COMMENT as description
  98. , 1 as hasStruct
  99. , 1 as isStructInstalled
  100. from INFORMATION_SCHEMA.TABLES t
  101. where t.TABLE_SCHEMA = '{$dbName}'
  102. and t.TABLE_NAME not like '%#%'
  103. on duplicate key update _rootTableName = t.TABLE_NAME
  104. , hasStruct = 1
  105. , isStructInstalled = 1
  106. ");
  107. DB::getPDO()->execSql("
  108. insert into `{$this->_rootTableName}` (namespace, idZasob, idDatabase, description, hasStruct)
  109. select IF(t.`DESC` like 'default_db/%',
  110. t.`DESC`,
  111. concat('{$source['nsPrefix']}/', t.`DESC`)
  112. ) as namespace
  113. , t.ID as idZasob
  114. , '{$source['idZasob']}' as idDatabase
  115. , t.`OPIS` as description
  116. , 1 as hasStruct
  117. from CRM_LISTA_ZASOBOW t
  118. where t.`TYPE` = 'TABELA'
  119. and t.A_STATUS in('NORMAL', 'WAITING')
  120. and t.PARENT_ID = {$source['idZasob']}
  121. on duplicate key update idZasob = t.ID
  122. , hasStruct = 1
  123. ");
  124. // } else if ('pgsql' == $dbType) {// TODO: use pgsql @see Storage Pgsql getTables from information_schema
  125. } else {
  126. UI::alert('warning', "TODO: { id: {$source['idZasob']}, nsPrefix: '{$source['nsPrefix']}', dbName: '{$dbName}', dbType: <b>'{$dbType}'</b>} ...");
  127. }
  128. } catch (Exception $e) {
  129. UI::alert('danger', "Error source '{$source['idZasob']}' " . $e->getMessage());
  130. continue;
  131. }
  132. }
  133. }
  134. // Ant objects in: SE/schema/ant-object/
  135. foreach (glob(APP_PATH_SCHEMA . "/ant-object/*/*/build.xml", GLOB_NOSORT) as $buildXmlPath) {
  136. // SE/schema/ant-object/default_db.test_perms/TestPermsAnt/build.xml
  137. $file = substr($buildXmlPath, strlen(APP_PATH_SCHEMA . '/ant-object/'), -1 * strlen('/build.xml'));
  138. DBG::nicePrint($file, "file({$file})");
  139. list($partSource, $name) = explode('/', $file);
  140. list($sourceName, $rootTableName) = explode('.', $partSource);
  141. DBG::nicePrint([$sourceName, $rootTableName], "\$name='{$name}' - [\$lowerSource, \$rootTableName]");
  142. // $clsName = substr(basename($file), 0, -1 * strlen('StorageAcl.php'));
  143. try {
  144. // Lib::loadClass('AntAclBase');
  145. // $acl = AntAclBase::buildInstance(0, [
  146. // 'source' => $sourceName,
  147. // 'rootTableName' => $rootTableName,
  148. // 'name' => $name
  149. // ]);
  150. // $acl = SchemaFactory::loadDefaultObject($clsName);
  151. // $namespace = $acl->getNamespace();
  152. // $name = $acl->getName();
  153. $idDatabase = DB::getPDO($sourceName)->getZasobId();
  154. $namespace = "{$sourceName}/{$rootTableName}/{$name}";
  155. DB::getPDO()->insertOrUpdate($this->_rootTableName, [
  156. 'namespace' => $namespace,
  157. 'idDatabase' => $idDatabase,
  158. '_type' => "AntAcl",
  159. '_rootTableName' => $rootTableName,
  160. 'hasStruct' => 1
  161. ]);
  162. } catch (Exception $e) {
  163. UI::alert('danger', $e->getMessage());
  164. }
  165. }
  166. if ($activeProject = Config::getProjectPath()) {
  167. $baseAntObjectPath = "{$activeProject}/schema/ant-object";
  168. DBG::nicePrint($baseAntObjectPath, "\$baseAntObjectPath");
  169. foreach (glob("{$baseAntObjectPath}/*/*/build.xml", GLOB_NOSORT) as $file) {
  170. // SE/schema/ant-object/default_db.test_perms/TestPermsAnt/build.xml
  171. $file = substr($file, strlen("{$baseAntObjectPath}/"), -1 * strlen('/build.xml'));
  172. DBG::nicePrint($file, '$file');
  173. list($partSource, $name) = explode('/', $file);
  174. list($sourceName, $rootTableName) = explode('.', $partSource);
  175. DBG::nicePrint([$sourceName, $rootTableName, $name], '[$lowerSource, $rootTableName, $name]');
  176. // $clsName = substr(basename($file), 0, -1 * strlen('StorageAcl.php'));
  177. try {
  178. // Lib::loadClass('AntAclBase');
  179. // $acl = AntAclBase::buildInstance(0, [
  180. // 'source' => $sourceName,
  181. // 'rootTableName' => $rootTableName,
  182. // 'name' => $name
  183. // ]);
  184. // $acl = SchemaFactory::loadDefaultObject($clsName);
  185. // $namespace = $acl->getNamespace();
  186. // $name = $acl->getName();
  187. $idDatabase = DB::getPDO($sourceName)->getZasobId();
  188. $namespace = "{$sourceName}/{$rootTableName}/{$name}";
  189. DB::getPDO()->insertOrUpdate($this->_rootTableName, [
  190. 'namespace' => $namespace,
  191. 'idDatabase' => $idDatabase,
  192. '_type' => "AntAcl",
  193. '_rootTableName' => $rootTableName,
  194. 'hasStruct' => 1
  195. ]);
  196. } catch (Exception $e) {
  197. UI::alert('danger', $e->getMessage());
  198. }
  199. }
  200. }
  201. // Fix objects AntAcl which struct is not installed
  202. $listAntAclObjectsToFix = DB::getPDO()->fetchAll("
  203. select t.*
  204. from `CRM_#CACHE_ACL_OBJECT` t
  205. where t._type = 'AntAcl'
  206. and t.idZasob is not NULL
  207. and t.hasStruct = 1
  208. and t.isStructInstalled = 0
  209. ");
  210. if (!empty($listAntAclObjectsToFix)) {
  211. UI::alert('info', "Fix AntAcl objects which is not installed (total: ".count($listAntAclObjectsToFix).")");
  212. foreach ($listAntAclObjectsToFix as $antAclInfo) {
  213. $namespace = $antAclInfo['namespace'];
  214. DBG::nicePrint($antAclInfo, "\$antAclInfo ({$namespace})");
  215. $zasobyStruct = DB::getPDO()->fetchAll("
  216. select z.ID, z.`DESC`
  217. from `CRM_LISTA_ZASOBOW` z
  218. where z.PARENT_ID = :parent_id
  219. and z.`TYPE` = 'KOMORKA'
  220. and z.A_STATUS not in ('DELETED')
  221. ", [ ':parent_id' => $antAclInfo['idZasob'] ]);
  222. DBG::nicePrint($zasobyStruct, "\$zasobyStruct ({$namespace})");
  223. ob_start();
  224. {
  225. Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
  226. $objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
  227. $objFieldAcl->updateCache($namespace);
  228. $reinstallLog = ob_get_clean();
  229. }
  230. // DBG::nicePrint($reinstallLog, "\$reinstallLog ({$namespace})");
  231. $fieldCacheStruct = DB::getPDO()->fetchAll("
  232. select t.namespace, t.fieldNamespace
  233. from `CRM_#CACHE_ACL_OBJECT_FIELD` t
  234. where t.objectNamespace = :namespace
  235. and t.idZasob is NULL
  236. ", [ ':namespace' => $namespace ]);
  237. DBG::nicePrint($fieldCacheStruct, "\$fieldCacheStruct ({$namespace})");
  238. $fieldsToFix = [];
  239. foreach ($fieldCacheStruct as $cacheField) {
  240. $fieldName = $cacheField['fieldNamespace'];
  241. foreach ($zasobyStruct as $fieldZasob) {
  242. if ($fieldZasob['DESC'] === $fieldName) {
  243. $fieldsToFix[] = [
  244. 'idZasob' => $fieldZasob['ID'],
  245. 'namespace' => $cacheField['namespace'],
  246. ];
  247. }
  248. }
  249. }
  250. DBG::nicePrint($fieldsToFix, "\$fieldsToFix ({$namespace})");
  251. foreach ($fieldsToFix as $fixField) {
  252. $affected = SchemaFactory::loadDefaultObject('SystemObjectField')->updateItem([
  253. 'namespace' => $fixField['namespace'],
  254. 'idZasob' => $fixField['idZasob']
  255. ]);
  256. if (!$affected) UI::alert('warning', "field ({$fixField['namespace']}) update idZasob failed");
  257. }
  258. $affected = SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
  259. 'namespace' => $namespace,
  260. 'isObjectActive' => 1
  261. ]);
  262. ($affected)
  263. ? UI::alert('success', "object ({$namespace}) activated")
  264. : UI::alert('warning', "object ({$namespace}) activation failed");
  265. }
  266. }
  267. // // foreach ... DB::getPDO($idDatabase)->fetchAll(select real _rootTableName)
  268. // foreach (Core_AclHelper::getCustomAclList() as $typeName) {
  269. // $ns = Core_AclHelper::parseTypeName($typeName);
  270. // $namespace = str_replace('__x3A__', '/', $ns['prefix']) . "/{$ns['name']}";
  271. // $sqlNs = DB::getPDO()->quote($namespace, PDO::PARAM_STR);
  272. // $idZasob = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where `DESC` = {$sqlNs} and `TYPE` = 'TABELA' and A_STATUS in('WAITING', 'NORMAL') ");
  273. // if (!$idZasob) {
  274. // DBG::nicePrint($ns, "TODO: insert zasob PARENT_ID = ?");
  275. // }
  276. // }
  277. SchemaVersionUpgrade::fixSystemObjectCoreTablesStructInstalled();
  278. }
  279. public function _parseWhere($params = []) {
  280. $sqlWhere = [];
  281. DBG::log($params, 'array', "SystemObject::_parseWhere");
  282. if (!empty($params['#refFrom'])) {
  283. // '#refFrom' => [
  284. // 'namespace' => 'default_objects/SystemSource',
  285. // 'primaryKey' => $sourceItem['idZasob']
  286. // ]
  287. if (empty($params['#refFrom']['namespace'])) throw new Exception("Missing refFrom/namespace");
  288. if (empty($params['#refFrom']['primaryKey'])) throw new Exception("Missing refFrom/primaryKey");
  289. if ('default_objects/SystemSource' != $params['#refFrom']['namespace']) throw new Exception("Unsupported refFrom/namespace '{$params['#refFrom']['namespace']}'");
  290. $sqlWhere[] = "t.idDatabase = " . DB::getPDO()->quote($params['#refFrom']['primaryKey'], PDO::PARAM_INT);
  291. }
  292. {
  293. $filterParams = [];
  294. $xsdFields = $this->getXsdTypes();
  295. foreach ($params as $k => $v) {
  296. if ('f_' != substr($k, 0, 2)) continue;
  297. $fieldName = substr($k, 2);
  298. if (!array_key_exists($fieldName, $xsdFields)) {
  299. // TODO: check query by xpath or use different param prefix
  300. throw new Exception("Field '{$fieldName}' not found in '{$this->_namespace}'");
  301. }
  302. if ('p5:www_link' == $xsdFields[$fieldName]) {
  303. continue;
  304. }
  305. $filterParams[$fieldName] = $v;
  306. }
  307. }
  308. if (!empty($filterParams)) {
  309. DBG::log($filterParams, 'array', "SystemObject::_parseWhere TODO \$filterParams");
  310. foreach ($filterParams as $fieldName => $value) {
  311. if (is_array($value)) {
  312. DBG::log($value, 'array', "TODO SystemObject::_parseWhere array value for \$filterParams[{$fieldName}]");
  313. } else if (is_scalar($value)) {
  314. if ('=' == substr($value, 0, 1)) {
  315. $sqlWhere[] = "t.{$fieldName} = " . DB::getPDO()->quote(substr($value, 1), PDO::PARAM_STR);
  316. } else {
  317. $sqlWhere[] = "t.{$fieldName} like " . DB::getPDO()->quote("%{$value}%", PDO::PARAM_STR);
  318. }
  319. } else {
  320. DBG::log($value, 'array', "BUG SystemObject::_parseWhere unknown type for \$filterParams[{$fieldName}]");
  321. }
  322. }
  323. }
  324. return (!empty($sqlWhere)) ? "where " . implode(" and ", $sqlWhere) : '';
  325. }
  326. public function getTotal($params = []) {
  327. $sqlWhere = $this->_parseWhere($params);
  328. return DB::getPDO()->fetchValue("
  329. select count(1) as cnt
  330. from `{$this->_rootTableName}` t
  331. {$sqlWhere}
  332. ");
  333. }
  334. public function clearGetItemCache($pk = null) {
  335. if (!$this->_cache) return;
  336. if (!$pk) $this->_cache = [];
  337. else if (array_key_exists($pk, $this->_cache)) unset($this->_cache[$pk]);
  338. }
  339. public function getItem($pk, $params = []) {
  340. // TODO: ceche query for: $pk = 'default_db/CRM_PROCES/PROCES', $params = [ 'propertyName' => "*,field" ]
  341. $pk = ACL::getBaseNamespace($pk);
  342. if (!$this->_cache) $this->_cache = [];
  343. if (1 === count($params) && "*,field" === V::get('propertyName', '', $params)) {
  344. if (array_key_exists($pk, $this->_cache)) return $this->_cache[$pk];
  345. $this->_cache[$pk] = $this->_fetchItem($pk, $params);
  346. } else {
  347. return $this->_fetchItem($pk, $params);
  348. }
  349. return $this->_cache[$pk];
  350. }
  351. public function _fetchItem($pk, $params = []) {
  352. if (!$pk) throw new Exception("Missing primary key '{$this->_namespace}'");
  353. $pkField = $this->getSqlPrimaryKeyField();
  354. if (!$pkField) throw new Exception("Missing primary key field defined in '{$this->_namespace}'");
  355. $sqlPk = DB::getPDO()->quote($pk, PDO::PARAM_STR);
  356. $item = DB::getPDO()->fetchFirst("
  357. select t.*
  358. from `{$this->_rootTableName}` t
  359. where t.`{$pkField}` = {$sqlPk}
  360. ");
  361. if (!$item) throw new Exception("Item '{$pk}' not exists - type '{$this->_namespace}'");
  362. return $this->buildFeatureFromSqlRow($item, $params);
  363. }
  364. public function getItems($params = []) {
  365. $sqlWhere = $this->_parseWhere($params);
  366. $currSortCol = V::get('order_by', 'idZasob', $params);
  367. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  368. // TODO: validate $currSortCol is in field list
  369. // TODO: validate $currSortFlip ('asc' or 'desc')
  370. $xsdFields = $this->getXsdTypes();
  371. if (!array_key_exists($currSortCol, $xsdFields)) throw new Exception("Field '{$currSortCol}' not found in '{$this->_namespace}'");
  372. if (!in_array($currSortFlip, ['asc', 'desc'])) throw new Exception("Sort dir not allowed");
  373. $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}";
  374. $limit = V::get('limit', 0, $params, 'int');
  375. $limit = ($limit < 0) ? 0 : $limit;
  376. $offset = V::get('limitstart', 0, $params, 'int');
  377. $offset = ($offset < 0) ? 0 : $offset;
  378. $sqlLimit = ($limit > 0)
  379. ? "limit {$limit} offset {$offset}"
  380. : '';
  381. Lib::loadClass('AclQueryItems');
  382. $query = new AclQueryItems($this);
  383. $query->setParams($params);
  384. $query->setSource('default_db');
  385. $query->setRawSql("
  386. select t.*
  387. from `{$this->_rootTableName}` t
  388. {$sqlWhere}
  389. {$sqlOrderBy}
  390. {$sqlLimit}
  391. ");
  392. return $query->fetchAll();
  393. }
  394. public function buildFeatureFromSqlRow($item, $params = []) {
  395. DBG::log($params, 'array', "buildFeatureFromSqlRow... '{$item['namespace']}'");
  396. $exNs = explode('/', $item['namespace']);
  397. $item['name'] = array_pop($exNs);
  398. $item['nsPrefix'] = implode('__x3A__', $exNs);
  399. $item['typeName'] = implode('__x3A__', $exNs) . ':' . $item['name'];
  400. $item['reinstallLink'] = Router::getRoute('Storage_AclReinstall')->getLink('', [ 'namespace' => $item['namespace'] ]);
  401. if (!empty($params['propertyName'])) {
  402. if (is_string($params['propertyName'])) $params['propertyName'] = explode(',', $params['propertyName']);
  403. if (!is_array($params['propertyName'])) throw new Exception("Wrong param propertyName - expected array or string");
  404. foreach ($params['propertyName'] as $fetchField) {
  405. if ('*' == $fetchField) continue;
  406. if ('field' == $fetchField) {
  407. $item['field'] = SchemaFactory::loadDefaultObject('SystemObjectField')->getItems([
  408. '__backRef' => [
  409. 'namespace' => 'default_objects/SystemObject',
  410. 'primaryKey' => $item['namespace']
  411. ],
  412. 'order_by' => 'sortPrio',
  413. 'order_dir' => 'asc',
  414. ]);
  415. }
  416. }
  417. }
  418. return $item;
  419. }
  420. public function updateItem($itemPatch) { // @required [ 'namespace' => ... ] (primaryKey)
  421. $pkField = $this->getPrimaryKeyField();
  422. $pk = V::get($pkField, null, $itemPatch);
  423. if (null === $pk) throw new Exception("BUG missing primary key field for '{$this->_namespace}' updateItem");
  424. $this->clearGetItemCache($pk);
  425. DBG::log(['updateItem $itemPatch', $itemPatch]);
  426. unset($itemPatch[$pkField]);
  427. if (empty($itemPatch)) return 0;
  428. foreach ($itemPatch as $fieldName => $value) {
  429. if ('isStructInstalled' == $fieldName) continue;
  430. if ('isObjectActive' == $fieldName) continue;
  431. if ('primaryKey' == $fieldName) continue;
  432. throw new Exception("Update field '{$fieldName}' not allowed for '{$this->_namespace}'");
  433. }
  434. return DB::getPDO()->update($this->_rootTableName, $pkField, $pk, $itemPatch);
  435. }
  436. }