Schema_TableFactory.php 2.3 KB

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