From 476096fd54c2eb367c181388219df4718d15eeec Mon Sep 17 00:00:00 2001 From: colemanw Date: Sat, 2 Mar 2024 23:51:21 -0500 Subject: [PATCH] WIP - CRM_Core_DAO_Base --- CRM/Core/CRM/Core/DAO/Base.php | 232 +++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 CRM/Core/CRM/Core/DAO/Base.php diff --git a/CRM/Core/CRM/Core/DAO/Base.php b/CRM/Core/CRM/Core/DAO/Base.php new file mode 100644 index 000000000000..e0ee2bfc0890 --- /dev/null +++ b/CRM/Core/CRM/Core/DAO/Base.php @@ -0,0 +1,232 @@ +. + * + * @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); + } + +}