StorageUpdateCache.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_Test_StorageUpdateCache extends RouteBase {
  4. function defaultAction() { UI::layout([ $this, 'defaultView' ]); }
  5. function defaultView() {
  6. // $this->checkSourceListView();
  7. // $this->checkAntAclPathListView();
  8. $this->fixAntAclWithStructNotInstalledView();
  9. }
  10. function checkSourceListView() {
  11. $sourceStorage = SchemaFactory::loadDefaultObject('SystemSource');
  12. $listSourceToUpdate = $sourceStorage->getItems([ 'f_hasConfig' => 1 ]);
  13. UI::table([ 'caption' => "\$listSourceToUpdate:", 'rows' => $listSourceToUpdate ]);
  14. }
  15. function checkAntAclPathListView() {
  16. $basePath = APP_PATH_SCHEMA . "/ant-object";
  17. foreach (glob("{$basePath}/*/*/build.xml", GLOB_NOSORT) as $buildXmlPath) {
  18. try {
  19. // $this->_updateAntObjectCache($buildXmlPath, $basePath);
  20. $relativePath = substr($buildXmlPath, strlen("{$basePath}/")); // expected: "{prefix}.{rootTableName}/{objectName}/build.xml"
  21. $ret = preg_match_all('/^([0-9a-zA-Z_]*)\.([0-9a-zA-Z_]*)\/([0-9a-zA-Z_-]*)\/build\.xml$/', $relativePath, $matches, PREG_SET_ORDER);
  22. DBG::nicePrint($matches, "ret({$ret}) \$matches for({$relativePath})");
  23. $sourceName = $matches[0][1];
  24. $rootTableName = $matches[0][2];
  25. $name = $matches[0][3];
  26. DBG::nicePrint([$sourceName, $rootTableName], "\$name='{$name}' - [\$lowerSource, \$rootTableName]");
  27. } catch (Exception $e) {
  28. UI::alert('danger', $e->getMessage());
  29. }
  30. }
  31. }
  32. function fixAntAclWithStructNotInstalledView() {
  33. Lib::loadClass('Schema_SystemObjectStorageAcl');
  34. // Fix objects AntAcl which struct is not installed
  35. $listAntAclObjectsToFix = DB::getPDO()->fetchAll("
  36. select t.*
  37. from `CRM_#CACHE_ACL_OBJECT` t
  38. where t._type = 'AntAcl'
  39. and t.idZasob is not NULL
  40. and t.hasStruct = 1
  41. and t.isStructInstalled = 0
  42. ");
  43. if (!empty($listAntAclObjectsToFix)) {
  44. UI::alert('info', "Fix AntAcl objects which is not installed (total: ".count($listAntAclObjectsToFix).")");
  45. foreach ($listAntAclObjectsToFix as $antAclInfo) {
  46. $namespace = $antAclInfo['namespace'];
  47. DBG::nicePrint($antAclInfo, "\$antAclInfo ({$namespace})");
  48. $zasobyStruct = DB::getPDO()->fetchAll("
  49. select z.ID, z.`DESC`
  50. from `CRM_LISTA_ZASOBOW` z
  51. where z.PARENT_ID = :parent_id
  52. and z.`TYPE` = 'KOMORKA'
  53. and z.A_STATUS not in ('DELETED')
  54. ", [ ':parent_id' => $antAclInfo['idZasob'] ]);
  55. DBG::nicePrint($zasobyStruct, "\$zasobyStruct ({$namespace})");
  56. ob_start();
  57. {
  58. Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
  59. { // TODO
  60. $item = [];
  61. $exNs = explode('/', $namespace);
  62. $item['name'] = array_pop($exNs);
  63. $item['nsPrefix'] = implode('__x3A__', $exNs);
  64. $item['typeName'] = implode('__x3A__', $exNs) . ':' . $item['name'];
  65. $antAclPath = Schema_SystemObjectFieldStorageAcl::getAntAclXsdBasePath($item['typeName']);
  66. if (!file_exists("{$antAclPath}/build.xml")) {
  67. UI::alert('danger', "Ant build file not exists for namespace '{$antAclInfo['namespace']}' - Removing AntAcl");
  68. Schema_SystemObjectStorageAcl::deleteObjectFromCache($antAclInfo['idZasob'], $antAclInfo['namespace']);
  69. throw new Exception("Ant build file not exists #TEST_OFF");
  70. // continue;
  71. }
  72. } // TODO
  73. $objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
  74. try {
  75. $objFieldAcl->updateCache($namespace);
  76. } catch (Exception $e) {
  77. UI::alert('danger', $e->getMessage());
  78. Schema_SystemObjectStorageAcl::deleteObjectFromCache($antAclInfo['idZasob'], $antAclInfo['namespace']);
  79. continue;
  80. }
  81. $reinstallLog = ob_get_clean();
  82. }
  83. // DBG::nicePrint($reinstallLog, "\$reinstallLog ({$namespace})");
  84. $fieldCacheStruct = DB::getPDO()->fetchAll("
  85. select t.namespace, t.fieldNamespace
  86. from `CRM_#CACHE_ACL_OBJECT_FIELD` t
  87. where t.objectNamespace = :namespace
  88. and t.idZasob is NULL
  89. ", [ ':namespace' => $namespace ]);
  90. DBG::nicePrint($fieldCacheStruct, "\$fieldCacheStruct ({$namespace})");
  91. $fieldsToFix = [];
  92. foreach ($fieldCacheStruct as $cacheField) {
  93. $fieldName = $cacheField['fieldNamespace'];
  94. foreach ($zasobyStruct as $fieldZasob) {
  95. if ($fieldZasob['DESC'] === $fieldName) {
  96. $fieldsToFix[] = [
  97. 'idZasob' => $fieldZasob['ID'],
  98. 'namespace' => $cacheField['namespace'],
  99. ];
  100. }
  101. }
  102. }
  103. DBG::nicePrint($fieldsToFix, "\$fieldsToFix ({$namespace})");
  104. foreach ($fieldsToFix as $fixField) {
  105. $affected = SchemaFactory::loadDefaultObject('SystemObjectField')->updateItem([
  106. 'namespace' => $fixField['namespace'],
  107. 'idZasob' => $fixField['idZasob']
  108. ]);
  109. if (!$affected) UI::alert('warning', "field ({$fixField['namespace']}) update idZasob failed");
  110. }
  111. $affected = SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
  112. 'namespace' => $namespace,
  113. 'isObjectActive' => 1
  114. ]);
  115. ($affected)
  116. ? UI::alert('success', "object ({$namespace}) activated")
  117. : UI::alert('warning', "object ({$namespace}) activation failed");
  118. }
  119. }
  120. }
  121. }