frontend populate defaults all fields

This commit is contained in:
Yuri Kuznetsov
2023-11-04 11:45:59 +02:00
parent c55d2454c3
commit bd8260d139
21 changed files with 138 additions and 19 deletions
@@ -97,5 +97,6 @@
"update",
"add",
"remove"
]
],
"default": []
}
@@ -9,5 +9,6 @@
},
"hookClassName": "Espo\\Tools\\FieldManager\\Hooks\\AutoincrementType",
"textFilter": true,
"readOnly": true
"readOnly": true,
"default": null
}
@@ -54,5 +54,6 @@
"type": "varchar",
"len": 255
},
"validatorClassName": "Espo\\Classes\\FieldValidators\\VarcharType"
"validatorClassName": "Espo\\Classes\\FieldValidators\\VarcharType",
"default": null
}
@@ -20,5 +20,6 @@
"filter": true,
"fieldDefs": {
"notNull": true
}
},
"default": false
}
@@ -73,5 +73,6 @@
},
"translatedOptions": true,
"dynamicLogicOptions": true,
"personalData": true
"personalData": true,
"default": []
}
@@ -79,5 +79,6 @@
},
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\Date\\DateFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\Date\\DateAttributeExtractor"
"attributeExtractorClassName": "Espo\\Core\\Field\\Date\\DateAttributeExtractor",
"default": null
}
@@ -93,5 +93,6 @@
},
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\DateTime\\DateTimeFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\DateTime\\DateTimeAttributeExtractor"
"attributeExtractorClassName": "Espo\\Core\\Field\\DateTime\\DateTimeAttributeExtractor",
"default": null
}
@@ -101,5 +101,6 @@
"view": "views/fields/datetime-optional",
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\DateTimeOptional\\DateTimeOptionalFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\DateTimeOptional\\DateTimeOptionalAttributeExtractor"
"attributeExtractorClassName": "Espo\\Core\\Field\\DateTimeOptional\\DateTimeOptionalAttributeExtractor",
"default": null
}
@@ -57,5 +57,6 @@
"textFilter": true,
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\EmailAddress\\EmailAddressGroupFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\EmailAddress\\EmailAddressGroupAttributeExtractor"
"attributeExtractorClassName": "Espo\\Core\\Field\\EmailAddress\\EmailAddressGroupAttributeExtractor",
"default": null
}
@@ -105,5 +105,6 @@
"update",
"add",
"remove"
]
],
"default": []
}
@@ -36,5 +36,6 @@
},
"hookClassName": "Espo\\Tools\\FieldManager\\Hooks\\NumberType",
"textFilter": true,
"readOnly": true
"readOnly": true,
"default": null
}
@@ -76,5 +76,6 @@
"textFilter": true,
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\PhoneNumber\\PhoneNumberGroupFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\PhoneNumber\\PhoneNumberGroupAttributeExtractor"
"attributeExtractorClassName": "Espo\\Core\\Field\\PhoneNumber\\PhoneNumberGroupAttributeExtractor",
"default": null
}
@@ -63,5 +63,6 @@
"personalData": true,
"textFilter": true,
"textFilterForeign": true,
"fullTextSearch": true
"fullTextSearch": true,
"default": null
}
@@ -49,5 +49,6 @@
"fieldDefs": {
"type": "varchar"
},
"personalData": true
"personalData": true,
"default": null
}
@@ -54,5 +54,6 @@
"update",
"add",
"remove"
]
],
"default": []
}
@@ -71,5 +71,6 @@
"textFilter": true,
"textFilterForeign": true,
"dynamicLogicOptions": true,
"fullTextSearch": true
"fullTextSearch": true,
"default": null
}
@@ -56,5 +56,6 @@
"textFilter": true,
"fullTextSearch": true,
"duplicatorClassName": "Espo\\Classes\\FieldDuplicators\\Wysiwyg",
"validatorClassName": "Espo\\Classes\\FieldValidators\\TextType"
"validatorClassName": "Espo\\Classes\\FieldValidators\\TextType",
"default": null
}
+86 -1
View File
@@ -36,12 +36,14 @@ class DefaultsPopulator {
* @param {module:models/preferences} preferences
* @param {module:acl-manager} acl
* @param {module:models/settings} config
* @param {module:metadata} metadata
*/
constructor(user, preferences, acl, config) {
constructor(user, preferences, acl, config, metadata) {
this.user = user;
this.preferences = preferences;
this.acl = acl;
this.config = config;
this.metadata = metadata;
}
/**
@@ -62,6 +64,8 @@ class DefaultsPopulator {
this.prepareForPortal(model, defaultHash);
}
this.prepareFields(model, defaultHash);
for (const attr in defaultHash) {
if (model.has(attr)) {
delete defaultHash[attr];
@@ -215,6 +219,87 @@ class DefaultsPopulator {
}
}
}
/**
* @param {module:model} model
* @param {Object.<string, *>} defaultHash
* @private
*/
prepareFields(model, defaultHash) {
const set = (attribute, value) => {
if (
attribute in defaultHash ||
model.has(attribute)
) {
return;
}
defaultHash[attribute] = value;
};
model.getFieldList().forEach(field => {
const type = model.getFieldType(field);
if (!type) {
return;
}
/** @type {{default?: *}} */
const defs = this.metadata.get(`fields.${type}`) || {};
if ('default' in defs) {
set(field, defs.default);
return;
}
if (
type === 'link' ||
type === 'linkOne' ||
type === 'file' ||
type === 'image'
) {
set(field + 'Id', null);
set(field + 'Name', null);
return;
}
if (type === 'linkParent') {
set(field + 'Id', null);
set(field + 'Name', null);
set(field + 'Type', null);
return;
}
if (
type === 'linkMultiple' ||
type === 'attachmentMultiple'
) {
set(field + 'Ids', []);
set(field + 'Names', {});
return;
}
if (type === 'enum') {
/** @type {string[]} */
const options = model.getFieldParam(field, 'options') || [];
let value = options[0] || '';
value = value !== '' ? value : null;
set(field, value);
return;
}
if (type === 'currency') {
set(field, null);
set(field + 'Currency', null);
}
});
}
}
export default DefaultsPopulator;
+13
View File
@@ -831,6 +831,19 @@ class Model {
setRelate(data);
}
/**
* Get a field list.
*
* @return {string[]}
*/
getFieldList() {
if (!this.defs || !this.defs.fields) {
return [];
}
return Object.keys(this.defs.fields);
}
/**
* Get a field type.
*
+2 -1
View File
@@ -1287,7 +1287,8 @@ class BaseRecordView extends View {
this.getUser(),
this.getPreferences(),
this.getAcl(),
this.getConfig()
this.getConfig(),
this.getMetadata()
);
populator.populate(this.model);
+3
View File
@@ -121,6 +121,9 @@
},
"description": "Columns used in full-text search."
},
"default": {
"description": "A default value. Used when populating defaults for a new record in frontend. Usually to be set to null."
},
"valueFactoryClassName": {
"type": "string",
"description": "A factory that creates value-objects for this field. A value-object can be obtained by calling getValueObject method on an Entity. Should implement Espo\\ORM\\Value\\ValueFactory."