diff --git a/app/src/Lib/Traits/TableMetaTrait.php b/app/src/Lib/Traits/TableMetaTrait.php index f43e3cfce..0f8b42329 100644 --- a/app/src/Lib/Traits/TableMetaTrait.php +++ b/app/src/Lib/Traits/TableMetaTrait.php @@ -29,6 +29,8 @@ namespace App\Lib\Traits; +use Cake\Utility\Inflector; + trait TableMetaTrait { // Does this Table represent Registry objects or configuration? private $confTable = false; @@ -54,4 +56,63 @@ public function getIsConfigurationTable() { public function setIsConfigurationTable(bool $confTable) { $this->confTable = $confTable; } + + + /** + * Filter metadata fields. + * + * @since COmanage Registry v5.0.0 + * @return array An array of columns distinguished in metadata and non-metadata + */ + + protected function filterMetadataFields() { + // Get the list of columns + $coltype = $this->getSchema()->typeMap(); + $entity = $this->getEntityClass(); + $entity_namespace = explode('\\', $entity); + $modelName = end($entity_namespace); + + // Get the list of belongs_to associations and construct an exclude array + $assc_keys = []; + foreach ($this->associations() as $assc) { + if($assc->type() === "manyToOne") { + $assc_keys[] = Inflector::underscore(Inflector::classify($assc->getClassName())) . "_id"; + } + } + // Map the model (eg: Person) to the changelog key (person_id) + $mfk = Inflector::underscore($modelName) . "_id"; + + + $meta_fields = [ + ...$assc_keys, + $mfk, + 'actor_identifier', + // 'provisioning_target_id', + 'created', // todo: I might need to revisit this. We might want to filter according to date in some occassions. Like petitions + 'deleted', + 'id', + 'modified', + 'revision', + // 'source_ad_hoc_attribute_id', + // 'source_address_id', + // 'source_email_address_id', + // 'source_identifier_id', + // 'source_name_id', + // 'source_external_identity_id', + // 'source_telephone_number_id', + ]; + + $newa = array(); + foreach($coltype as $clmn => $type) { + if(in_array($clmn, $meta_fields,true)) { + // Move the value to metadata + $newa['meta'][$clmn] = $type; + } else { + // Just copy the value + $newa[$clmn] = $type; + } + } + + return $newa ?? []; + } }