InstanceConfig.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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'{$namespace}'
  69. ", [
  70. ':namespace' => $namespace
  71. ]);
  72. }
  73. static function getNamespaceSiblings($namespace) {
  74. return array_map(function ($row) {
  75. return $row['namespace'];
  76. }, DB::getPDO()->fetchAll("
  77. select s.namespace
  78. from CRM_INSTANCE_CONFIG c
  79. join CRM_INSTANCE_CONFIG s on ( s.rootNamespace = c.rootNamespace and s.namespace != c.rootNamespace )
  80. where c.namespace = :namespace
  81. ", [
  82. 'namespace' => $namespace
  83. ]));
  84. }
  85. static function getFeatureNamespaces($namespace, $pk) {
  86. $instanceTable = self::getInstanceTable($namespace);
  87. return array_map(function ($row) {
  88. return $row['namespace'];
  89. }, DB::getPDO()->fetchAll("
  90. select c.namespace
  91. from `{$instanceTable}` i
  92. join `CRM_INSTANCE_CONFIG` c on ( c.id = i.idInstance )
  93. where i.pk = :pk
  94. ", [
  95. 'pk' => $pk,
  96. ]));
  97. }
  98. }