-
-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* Base for concrete DAO/BAO classes which are defined with a schema/xml file. | ||
*/ | ||
abstract class CRM_Core_DAO_Base extends CRM_Core_DAO { | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
protected function getPrimaryKey(): array { | ||
return static::getEntityDefinition()['primaryKey'] ?? ['id']; | ||
} | ||
|
||
/** | ||
* Returns system paths related to this entity (as defined in the xml schema) | ||
* | ||
* @return array | ||
*/ | ||
public static function getEntityPaths(): array { | ||
return static::getEntityDefinition()['paths'] ?? []; | ||
} | ||
|
||
/** | ||
* Returns user-friendly description of this entity based on the xml table <comment>. | ||
* | ||
* @return string | ||
*/ | ||
public static function getEntityDescription(): ?string { | ||
return static::getEntityDefinition()['description'] ?? NULL; | ||
} | ||
|
||
/** | ||
* Returns localized name of this table | ||
* | ||
* @return string | ||
*/ | ||
public static function getTableName() { | ||
return static::getEntityDefinition()['table']; | ||
} | ||
|
||
/** | ||
* Returns if this table needs to be logged | ||
* | ||
* @return bool | ||
*/ | ||
public function getLog(): bool { | ||
return static::getEntityDefinition()['log'] ?? FALSE; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getEntityIcon(string $entityName, int $entityId = NULL): ?string { | ||
return static::getEntityDefinition()['icon'] ?? NULL; | ||
} | ||
|
||
/** | ||
* @return string | ||
* Version in which table was added | ||
*/ | ||
protected static function getTableAddVersion(): string { | ||
return static::getEntityDefinition()['add'] ?? '1.0'; | ||
} | ||
|
||
public static function getExtensionName(): ?string { | ||
return static::getEntityDefinition()['module'] ?? 'civicrm'; | ||
} | ||
|
||
/** | ||
* Returns all the column names of this table | ||
* | ||
* @return array | ||
*/ | ||
public static function &fields() { | ||
$fields = (Civi::$statics[static::class]['fields'] ??= static::getSchemaFields()); | ||
return $fields; | ||
} | ||
|
||
private static function getSchemaFields(): array { | ||
$fields = []; | ||
$definition = static::getEntityDefinition(); | ||
$baoName = CRM_Core_DAO_AllCoreTables::getBAOClassName(static::class); | ||
$primaryKey = (string) ($definition->primaryKey->name ?? ''); | ||
|
||
$foreignKeys = []; | ||
foreach ($definition->foreignKey ?? [] as $fkXml) { | ||
if (empty($fkXml->drop)) { | ||
$foreignKeys[(string) $fkXml->name] = [ | ||
'FKClassName' => CRM_Core_DAO_AllCoreTables::getClassForTable($fkXml->table), | ||
'FKColumnName' => (string) ($fkXml->key ?? 'id'), | ||
]; | ||
} | ||
} | ||
foreach ($definition->dynamicForeignKey ?? [] as $dfkXml) { | ||
if (empty($dfkXml->drop)) { | ||
$foreignKeys[(string) $dfkXml->idColumn] = [ | ||
'DFKEntityColumn' => (string) $dfkXml->typeColumn, | ||
'FKColumnName' => (string) ($dfkXml->key ?? 'id'), | ||
]; | ||
} | ||
} | ||
|
||
foreach ($definition->field ?? [] as $fieldXml) { | ||
if (!empty($fieldXml->drop)) { | ||
continue; | ||
} | ||
$fieldName = (string) $fieldXml->name; | ||
$fieldTypeAttrs = CRM_Utils_Schema::getTypeAttributes($fieldXml); | ||
$field = [ | ||
'name' => $fieldName, | ||
'type' => $fieldTypeAttrs['crmTypeValue'], | ||
'title' => static::_ts(((string) ($fieldXml->title ?? '')) ?: CRM_Utils_Schema::composeTitle($fieldName)), | ||
]; | ||
if (!empty($fieldXml->comment)) { | ||
$field['description'] = static::_ts((string) $fieldXml->comment); | ||
} | ||
if (strtolower((string) ($fieldXml->required ?? '')) === 'true') { | ||
$field['required'] = TRUE; | ||
} | ||
if (!empty($fieldTypeAttrs['length'])) { | ||
$field['maxlength'] = $fieldTypeAttrs['length']; | ||
} | ||
if (!empty($fieldTypeAttrs['precision'])) { | ||
$field['precision'] = explode(',', $fieldTypeAttrs['precision']); | ||
} | ||
if (!empty($fieldTypeAttrs['size'])) { | ||
$field['size'] = is_numeric($fieldTypeAttrs['size']) ? (int) $fieldTypeAttrs['size'] : constant($fieldTypeAttrs['size']); | ||
} | ||
if (isset($fieldTypeAttrs['rows'])) { | ||
$field['rows'] = $fieldTypeAttrs['rows']; | ||
} | ||
if (isset($fieldTypeAttrs['cols'])) { | ||
$field['cols'] = $fieldTypeAttrs['cols']; | ||
} | ||
$field['usage'] = CRM_Utils_Schema::getFieldUsage($fieldXml); | ||
if ($field['usage']['import']) { | ||
$field['import'] = TRUE; | ||
} | ||
$field['where'] = ((string) $definition->name) . ".$fieldName"; | ||
if (!empty($fieldXml->headerPattern)) { | ||
$field['headerPattern'] = (string) $fieldXml->headerPattern; | ||
} | ||
if (!empty($fieldXml->dataPattern)) { | ||
$field['dataPattern'] = (string) $fieldXml->dataPattern; | ||
} | ||
if ($field['usage']['export'] || (!$field['usage']['export'] && $field['usage']['import'])) { | ||
$field['export'] = $field['usage']['export']; | ||
} | ||
if (!empty($fieldXml->contactType)) { | ||
$field['contactType'] = (string) $fieldXml->contactType; | ||
} | ||
if (!empty($fieldXml->rule)) { | ||
$field['rule'] = (string) $fieldXml->rule; | ||
} | ||
$permission = CRM_Utils_Schema::getFieldPermission($fieldXml); | ||
if ($permission) { | ||
$field['permission'] = $permission; | ||
} | ||
if (isset($fieldXml->default)) { | ||
// Default value in the xml may or may not be quoted | ||
$default = trim((string) $fieldXml->default, '"\''); | ||
$field['default'] = $default === 'NULL' ? NULL : $default; | ||
} | ||
$field['table_name'] = (string) $definition->name; | ||
$field['entity'] = (string) ($definition->entity ?? $definition->class); | ||
$field['bao'] = $baoName; | ||
$field['localizable'] = (int) ($fieldXml->localizable ?? 0); | ||
if (!empty($fieldXml->localize_context)) { | ||
$field['localize_context'] = (string) $fieldXml->localize_context; | ||
} | ||
$field += $foreignKeys[$fieldName] ?? []; | ||
if (!empty($fieldXml->component)) { | ||
$field['component'] = (string) $fieldXml->component; | ||
} | ||
if (!empty($fieldXml->serialize)) { | ||
$field['unique_title'] = constant('CRM_Core_DAO::SERIALIZE_' . $fieldXml->serialize); | ||
} | ||
if (!empty($fieldXml->uniqueTitle)) { | ||
$field['unique_title'] = static::_ts((string) $fieldXml->uniqueTitle); | ||
} | ||
if (!empty($fieldXml->deprecated)) { | ||
$field['deprecated'] = TRUE; | ||
} | ||
$html = CRM_Utils_Schema::getFieldHtml($fieldXml); | ||
if ($html) { | ||
$field['html'] = $html; | ||
} | ||
$pseudoconstant = CRM_Utils_Schema::getFieldPseudoconstant($fieldXml); | ||
if ($pseudoconstant) { | ||
$field['pseudoconstant'] = $pseudoconstant; | ||
} | ||
if ($fieldName === $primaryKey || !empty($fieldXml->readonly)) { | ||
$field['readonly'] = TRUE; | ||
} | ||
$field['add'] = CRM_Utils_Schema::toString('add', $fieldXml); | ||
$fieldKey = (string) ($fieldXml->uniqueName ?? $fieldName); | ||
$fields[$fieldKey] = $field; | ||
} | ||
CRM_Core_DAO_AllCoreTables::invoke(static::class, 'fields_callback', $fields); | ||
return $fields; | ||
} | ||
|
||
/** | ||
* Returns the list of indices | ||
* | ||
* @param bool $localize | ||
* | ||
* @return array | ||
*/ | ||
public static function indices(bool $localize = TRUE): array { | ||
$definition = static::getEntityDefinition(); | ||
$indices = []; | ||
// TODO | ||
return ($localize && $indices) ? CRM_Core_DAO_AllCoreTables::multilingualize(static::class, $indices) : $indices; | ||
} | ||
|
||
private static function getEntityDefinition(): array { | ||
$entityName = \Civi\Schema\EntityRepository::getClassIndex()[static::class]; | ||
return \Civi\Schema\EntityRepository::getEntity($entityName); | ||
} | ||
|
||
} |