TableFactory.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. Lib::loadClass('SchemaException');
  3. Lib::loadClass('Schema_TableBase');
  4. /**
  5. * Base class for all tables - Schema_TableBase
  6. * CoreTableClass extends from Schema_TableBase
  7. * CompanyTableClass extend from CoreTableClass
  8. *
  9. * WARNING: to add CompanyTableClass, add CoreTableClass first
  10. */
  11. class Schema_TableFactory {
  12. public static function build($tableName, $idSource = null, $hostName = '') {
  13. //_schemaClass
  14. $cleanHostName = str_replace(array(".", "-"), '_', $hostName);
  15. $cleanTableName = strtolower($tableName);
  16. $cleanSourceName = ($idSource == DB::getPDO()->getZasobId())? 'default_db' : "p5_{$idSource}";
  17. DBG::_('DBG_SCH', '>2', "cleanHostName", $cleanHostName, __CLASS__, __FUNCTION__, __LINE__);
  18. DBG::_('DBG_SCH', '>2', "cleanTableName", $cleanTableName, __CLASS__, __FUNCTION__, __LINE__);
  19. DBG::_('DBG_SCH', '>2', "cleanSourceName", $cleanSourceName, __CLASS__, __FUNCTION__, __LINE__);
  20. // TODO: fetch form widgets from file config
  21. // "schema/gui/core/{$cleanSourceName}.{$cleanTableName}.php";
  22. // class Schema_Core_{$cleanSourceName}_{$cleanTableName};
  23. // TODO: override by company file config
  24. // "schema/gui/{$cleanHostName}/{$cleanSourceName}.{$cleanTableName}.php";
  25. // class Schema_{$cleanHostName}_{$cleanSourceName}_{$cleanTableName};
  26. $pathCoreClass = APP_PATH_SCHEMA . "/gui/core/{$cleanSourceName}.{$cleanTableName}.php";
  27. if (!file_exists($pathCoreClass)) throw new SchemaException("Core Class file not found '{$cleanSourceName}.{$cleanTableName}'");
  28. require_once $pathCoreClass;
  29. $coreClassName = "Schema__Core__{$cleanSourceName}__{$cleanTableName}";
  30. if (!class_exists($coreClassName)) throw new SchemaException("Config error for schema core class {$cleanSourceName}:{$cleanTableName}");
  31. if ($cleanHostName) {
  32. $pathCompanyClass = APP_PATH_SCHEMA . "/gui/company/{$cleanHostName}/{$cleanSourceName}.{$cleanTableName}.php";
  33. $companyClassName = "Schema__{$cleanHostName}__{$cleanSourceName}__{$cleanTableName}";
  34. if (!file_exists($pathCompanyClass)) throw new SchemaException("Host Class file not found '{$cleanHostName}/{$cleanSourceName}.{$cleanTableName}'");
  35. require_once $pathCompanyClass;
  36. if (!class_exists($companyClassName)) throw new SchemaException("Config error for schema company class {$cleanSourceName}:{$cleanTableName}");
  37. return new $companyClassName();
  38. } else {
  39. return new $coreClassName();
  40. }
  41. }
  42. }