|
|
@@ -179,7 +179,7 @@ SQL;
|
|
|
return Core_AclHelper::parseNamespaceUrl($namespace);
|
|
|
}
|
|
|
|
|
|
- public static function getRefTable($rootObjectNamespace, $childName) {
|
|
|
+ public static function getRefTable($rootObjectNamespace, $childName) { // CRM_REF_CONFIG
|
|
|
static $cacheRefTables = array();
|
|
|
$cacheKey = "{$rootObjectNamespace}/{$childName}";
|
|
|
if (array_key_exists($cacheKey, $cacheRefTables)) return $cacheRefTables[$cacheKey];
|
|
|
@@ -254,4 +254,89 @@ SQL;
|
|
|
return $refTableName;
|
|
|
}
|
|
|
|
|
|
+ public static function getInstanceId($namespace) { // CRM_INSTANCE_CONFIG
|
|
|
+ $conf = self::getInstanceConfig($namespace);
|
|
|
+ return $conf['id'];
|
|
|
+ }
|
|
|
+ public static function getInstanceConfig($namespace) { // CRM_INSTANCE_CONFIG
|
|
|
+ try {
|
|
|
+ $conf = self::fetchInstanceConfig($namespace);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ DB::getPDO()->execSql("
|
|
|
+ create table if not exists `CRM_INSTANCE_CONFIG` (
|
|
|
+ `id` int(11) not null AUTO_INCREMENT,
|
|
|
+ `namespace` varchar(255) NOT NULL DEFAULT '',
|
|
|
+ `rootNamespace` varchar(255) NOT NULL DEFAULT '',
|
|
|
+ `tableName` varchar(255) NOT NULL DEFAULT '',
|
|
|
+ `_createdAt` datetime NOT NULL,
|
|
|
+ UNIQUE KEY `namespace` (`namespace`),
|
|
|
+ KEY `rootNamespace` (`rootNamespace`),
|
|
|
+ PRIMARY KEY (`id`)
|
|
|
+ ) ENGINE=MyISAM DEFAULT CHARSET=latin2
|
|
|
+ ");
|
|
|
+ // TODO:?: `_tableInstalled` tinyint(1) not null default 0,
|
|
|
+ $conf = self::fetchInstanceConfig($namespace);
|
|
|
+ }
|
|
|
+ if (!$conf) {
|
|
|
+ $id = DB::getPDO()->insert("CRM_INSTANCE_CONFIG", [
|
|
|
+ 'namespace' => $namespace,
|
|
|
+ 'rootNamespace' => self::getRootNamespace($namespace),
|
|
|
+ '_createdAt' => 'NOW()',
|
|
|
+ ]);
|
|
|
+ $conf = self::fetchInstanceConfig($namespace);
|
|
|
+ }
|
|
|
+ if (!$conf) throw new Exception("Instance not found in config table '{$namespace}'");
|
|
|
+ return $conf;
|
|
|
+ }
|
|
|
+ public static function fetchInstanceConfig($namespace) {
|
|
|
+ return DB::getPDO()->fetchFirst("
|
|
|
+ select c.*
|
|
|
+ from `CRM_INSTANCE_CONFIG` c
|
|
|
+ where c.namespace = '{$namespace}'
|
|
|
+ ");
|
|
|
+ }
|
|
|
+ public static function getRootNamespace($namespace) { // TODO: works only for relative urls! - mv to Acl->getRootNamespace
|
|
|
+ Lib::loadClass('SchemaFactory');
|
|
|
+ try {
|
|
|
+ $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ throw new Exception("Object not installed '{$namespace}'");
|
|
|
+ }
|
|
|
+ if (!$objectItem['isStructInstalled']) throw new Exception("Object structure not installed '{$namespace}'");
|
|
|
+
|
|
|
+ if ($objectItem['idDatabase'] != DB::getPDO()->getZasobId()) throw new Exception("Only default_db supported"); // TODO: support more Sources
|
|
|
+
|
|
|
+ return "default_db/{$objectItem['_rootTableName']}";
|
|
|
+ }
|
|
|
+ public static function getInstanceTable($namespace) {
|
|
|
+ $conf = self::getInstanceConfig($namespace);
|
|
|
+ if (!empty($conf['tableName'])) return $conf['tableName'];
|
|
|
+
|
|
|
+ $rootNs = $conf['rootNamespace'];
|
|
|
+ $rootConf = self::getInstanceConfig($rootNs);
|
|
|
+ $instanceTableName = "CRM__#INSTANCE_TABLE__{$rootConf['id']}";
|
|
|
+ if (!empty($rootConf['tableName'])) {
|
|
|
+ $affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
|
|
|
+ 'tableName' => $instanceTableName
|
|
|
+ ]);
|
|
|
+ return $rootConf['tableName'];
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: fetch primaryKeyType - TODO: store primaryKey and primaryKeyType in SystemObject item
|
|
|
+ $pkType = 'int';
|
|
|
+ DB::getPDO()->exec("
|
|
|
+ CREATE TABLE IF NOT EXISTS `{$instanceTableName}` (
|
|
|
+ `pk` int(11) NOT NULL COMMENT 'primary key'
|
|
|
+ , `idInstance` int(11) NOT NULL
|
|
|
+ , `_createdAt` datetime NOT NULL
|
|
|
+ , KEY `pk` (`pk`)
|
|
|
+ , KEY `idInstance` (`idInstance`)
|
|
|
+ ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COMMENT='{$rootNs} #INSTANCE';
|
|
|
+ ");
|
|
|
+ $affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
|
|
|
+ 'tableName' => $instanceTableName
|
|
|
+ ]);
|
|
|
+ return $instanceTableName;
|
|
|
+ }
|
|
|
+
|
|
|
}
|