InstanceConfig.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class InstanceConfig {
  3. static function createInstanceConfigTable() {
  4. DB::getPDO()->execSql("
  5. create table if not exists `CRM_INSTANCE_CONFIG` (
  6. `id` int(11) not null AUTO_INCREMENT,
  7. `namespace` varchar(255) NOT NULL DEFAULT '',
  8. `rootNamespace` varchar(255) NOT NULL DEFAULT '',
  9. `idInstanceBase` int(11) NOT NULL DEFAULT 0,
  10. `_createdAt` datetime NOT NULL,
  11. UNIQUE KEY `namespace` (`namespace`),
  12. KEY `rootNamespace` (`rootNamespace`),
  13. PRIMARY KEY (`id`)
  14. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  15. ");
  16. }
  17. static function getInstanceTable($namespace) { // @returns tableName with struct { pk, idInstance, _createdAt }
  18. $conf = self::getInstanceConfig($namespace);
  19. if (!empty($conf['idInstanceBase'])) return "CRM__#INSTANCE_TABLE__{$conf['idInstanceBase']}";
  20. $rootNs = $conf['rootNamespace'];
  21. $rootConf = self::getInstanceConfig($rootNs);
  22. $instanceTableName = "CRM__#INSTANCE_TABLE__{$rootConf['id']}";
  23. if (!empty($rootConf['idInstance'])) {
  24. $affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
  25. 'idInstanceBase' => $rootConf['id']
  26. ]);
  27. return $instanceTableName;
  28. }
  29. // TODO: fetch primaryKeyType - TODO: store primaryKey and primaryKeyType in SystemObject item
  30. $pkType = 'int';
  31. DB::getPDO()->exec("
  32. CREATE TABLE IF NOT EXISTS `{$instanceTableName}` (
  33. `pk` int(11) NOT NULL COMMENT 'primary key'
  34. , `idInstance` int(11) NOT NULL
  35. , `_createdAt` datetime NOT NULL
  36. , KEY `pk` (`pk`)
  37. , KEY `idInstance` (`idInstance`)
  38. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COMMENT='{$rootNs} #INSTANCE';
  39. ");
  40. $affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
  41. 'idInstanceBase' => $rootConf['id']
  42. ]);
  43. return $instanceTableName;
  44. }
  45. static function getInstanceConfig($namespace) { // @returns { id, namespace, rootNamespace, idInstanceBase, _createdAt }
  46. try {
  47. $conf = self::fetchInstanceConfig($namespace);
  48. } catch (Exception $e) {
  49. self::createInstanceConfigTable();
  50. // TODO:?: `_tableInstalled` tinyint(1) not null default 0,
  51. $conf = self::fetchInstanceConfig($namespace);
  52. }
  53. if (!$conf) {
  54. $id = DB::getPDO()->insert("CRM_INSTANCE_CONFIG", [
  55. 'namespace' => $namespace,
  56. 'rootNamespace' => self::getRootNamespace($namespace),
  57. '_createdAt' => 'NOW()',
  58. ]);
  59. $conf = self::fetchInstanceConfig($namespace);
  60. }
  61. if (!$conf) throw new Exception("Instance not found in config table '{$namespace}'");
  62. return $conf;
  63. }
  64. static function fetchInstanceConfig($namespace) {
  65. return DB::getPDO()->fetchFirst("
  66. select c.*
  67. from `CRM_INSTANCE_CONFIG` c
  68. where c.namespace = :namespace
  69. ", [
  70. ':namespace' => $namespace
  71. ]);
  72. }
  73. static function getRootNamespace($namespace) { // TODO: works only for relative urls! - mv to Acl->getRootNamespace
  74. Lib::loadClass('SchemaFactory');
  75. try {
  76. $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace);
  77. } catch (Exception $e) {
  78. throw new Exception("Object not installed '{$namespace}'");
  79. }
  80. if (!$objectItem['isStructInstalled']) throw new Exception("Object structure not installed '{$namespace}'");
  81. if ($objectItem['idDatabase'] != DB::getPDO()->getZasobId()) {
  82. if ('StorageAcl' === $objectItem['_type']) {
  83. DBG::log("getRootNamespace...");
  84. return $objectItem['namespace'];
  85. }
  86. else {
  87. throw new Exception("Only default_db supported"); // TODO: support more Sources
  88. }
  89. }
  90. return "default_db/{$objectItem['_rootTableName']}";
  91. }
  92. static function getNamespaceSiblings($namespace) {
  93. return array_map(function ($row) {
  94. return $row['namespace'];
  95. }, DB::getPDO()->fetchAll("
  96. select s.namespace
  97. from CRM_INSTANCE_CONFIG c
  98. join CRM_INSTANCE_CONFIG s on ( s.rootNamespace = c.rootNamespace and s.namespace != c.rootNamespace )
  99. where c.namespace = :namespace
  100. ", [
  101. 'namespace' => $namespace
  102. ]));
  103. }
  104. static function getFeatureNamespaces($namespace, $pk) {
  105. $instanceTable = self::getInstanceTable($namespace);
  106. return array_map(function ($row) {
  107. return $row['namespace'];
  108. }, DB::getPDO()->fetchAll("
  109. select c.namespace
  110. from `{$instanceTable}` i
  111. join `CRM_INSTANCE_CONFIG` c on ( c.id = i.idInstance )
  112. where i.pk = :pk
  113. ", [
  114. 'pk' => $pk,
  115. ]));
  116. }
  117. }