SystemObjectStorageAcl.php 19 KB

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