Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
yurikuzn committed Jun 20, 2023
1 parent f53553e commit 4f2651d
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 136 deletions.
220 changes: 220 additions & 0 deletions client/src/helpers/model/defaults-populator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/

/**
* Defaults populator.
*/
class DefaultsPopulator {

/**
* @param {module:models/user} user
* @param {module:models/preferences} preferences
* @param {module:acl-manager} acl
* @param {module:models/settings} config
*/
constructor(user, preferences, acl, config) {
this.user = user;
this.preferences = preferences;
this.acl = acl;
this.config = config;
}

/**
* Populate default values.
*
* @param {module:model} model A model.
*/
populate(model) {
model.populateDefaults();

let defaultHash = {};

if (!this.user.isPortal()) {
this.prepare(model, defaultHash);
}

if (this.user.isPortal()) {
this.prepareForPortal(model, defaultHash);
}

for (let attr in defaultHash) {
if (model.has(attr)) {
delete defaultHash[attr];
}
}

model.set(defaultHash, {silent: true});
}

/**
* @param {module:model} model
* @param {Object.<string, *>} defaultHash
* @private
*/
prepare(model, defaultHash) {
let hasAssignedUsers =
model.hasField('assignedUsers') &&
model.getLinkParam('assignedUsers', 'entity') === 'User';

if (model.hasField('assignedUser') || hasAssignedUsers) {
let assignedUserField = 'assignedUser';

if (hasAssignedUsers) {
assignedUserField = 'assignedUsers';
}

let fillAssignedUser = true;

if (this.preferences.get('doNotFillAssignedUserIfNotRequired')) {
fillAssignedUser = false;

if (model.getFieldParam(assignedUserField, 'required')) {
fillAssignedUser = true;
}
else if (this.acl.getPermissionLevel('assignmentPermission') === 'no') {
fillAssignedUser = true;
}
else if (
this.acl.getPermissionLevel('assignmentPermission') === 'team' &&
!this.user.get('defaultTeamId')
) {
fillAssignedUser = true;
}
else if (
this.acl.getScopeForbiddenFieldList(model.name, 'edit').includes(assignedUserField)
) {
fillAssignedUser = true;
}
}

if (fillAssignedUser) {
if (hasAssignedUsers) {
defaultHash['assignedUsersIds'] = [this.user.id];
defaultHash['assignedUsersNames'] = {};
defaultHash['assignedUsersNames'][this.user.id] = this.user.get('name');
}
else {
defaultHash['assignedUserId'] = this.user.id;
defaultHash['assignedUserName'] = this.user.get('name');
}
}
}

let defaultTeamId = this.user.get('defaultTeamId');

if (defaultTeamId) {
if (
model.hasField('teams') &&
!model.getFieldParam('teams', 'default') &&
Espo.Utils.lowerCaseFirst(model.getLinkParam('teams', 'relationName') || '') === 'entityTeam'
) {
defaultHash['teamsIds'] = [defaultTeamId];
defaultHash['teamsNames'] = {};
defaultHash['teamsNames'][defaultTeamId] = this.user.get('defaultTeamName');
}
}
}

/**
* @param {module:model} model
* @param {Object.<string, *>} defaultHash
* @private
*/
prepareForPortal(model, defaultHash) {
if (
model.hasField('account') &&
['belongsTo', 'hasOne'].includes(model.getLinkType('account')) &&
model.getLinkParam('account', 'entity') === 'Account'
) {
if (this.user.get('accountId')) {
defaultHash['accountId'] = this.user.get('accountId');
defaultHash['accountName'] = this.user.get('accountName');
}
}

if (
model.hasField('contact') &&
['belongsTo', 'hasOne'].includes(model.getLinkType('contact'))&&
model.getLinkParam('contact', 'entity') === 'Contact'
) {
if (this.user.get('contactId')) {
defaultHash['contactId'] = this.user.get('contactId');
defaultHash['contactName'] = this.user.get('contactName');
}
}

if (model.hasField('parent') && model.getLinkType('parent') === 'belongsToParent') {
if (!this.config.get('b2cMode')) {
if (this.user.get('accountId')) {
if ((model.getFieldParam('parent', 'entityList') || []).includes('Account')) {
defaultHash['parentId'] = this.user.get('accountId');
defaultHash['parentName'] = this.user.get('accountName');
defaultHash['parentType'] = 'Account';
}
}
}
else {
if (this.user.get('contactId')) {
if ((model.getFieldParam('parent', 'entityList') || []).includes('Contact')) {
defaultHash['contactId'] = this.user.get('contactId');
defaultHash['parentName'] = this.user.get('contactName');
defaultHash['parentType'] = 'Contact';
}
}
}
}

if (
model.hasField('accounts') &&
model.getLinkType('accounts') === 'hasMany' &&
model.getLinkParam('accounts', 'entity') === 'Account'
) {
if (this.user.get('accountsIds')) {
defaultHash['accountsIds'] = this.user.get('accountsIds');
defaultHash['accountsNames'] = this.user.get('accountsNames');
}
}

if (
model.hasField('contacts') &&
model.getLinkType('contacts') === 'hasMany'&&
model.getLinkParam('contacts', 'entity') === 'Contact'
) {
if (this.user.get('contactId')) {
defaultHash['contactsIds'] = [this.user.get('contactId')];

let names = {};

names[this.user.get('contactId')] = this.user.get('contactName');
defaultHash['contactsNames'] = names;
}
}
}
}

export default DefaultsPopulator;
144 changes: 8 additions & 136 deletions client/src/views/record/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import ViewRecordHelper from 'view-record-helper';
import DynamicLogic from 'dynamic-logic';
import _ from 'lib!underscore';
import $ from 'lib!jquery';
import DefaultsPopulator from 'helpers/model/defaults-populator';

/**
* A base record view. To be extended.
Expand Down Expand Up @@ -1228,143 +1229,14 @@ class BaseRecordView extends View {
* Populate defaults.
*/
populateDefaults() {
this.model.populateDefaults();

let defaultHash = {};

if (!this.getUser().get('portalId')) {
if (this.model.hasField('assignedUser') || this.model.hasField('assignedUsers')) {
let assignedUserField = 'assignedUser';

if (this.model.hasField('assignedUsers')) {
assignedUserField = 'assignedUsers';
}

let fillAssignedUser = true;

if (this.getPreferences().get('doNotFillAssignedUserIfNotRequired')) {
fillAssignedUser = false;

if (this.model.getFieldParam(assignedUserField, 'required')) {
fillAssignedUser = true;
}
else if (this.getAcl().get('assignmentPermission') === 'no') {
fillAssignedUser = true;
}
else if (
this.getAcl().get('assignmentPermission') === 'team' &&
!this.getUser().get('defaultTeamId')
) {
fillAssignedUser = true;
}
else if (
~this.getAcl()
.getScopeForbiddenFieldList(this.model.name, 'edit').indexOf(assignedUserField)
) {

fillAssignedUser = true;
}
}

if (fillAssignedUser) {
if (assignedUserField === 'assignedUsers') {
defaultHash['assignedUsersIds'] = [this.getUser().id];
defaultHash['assignedUsersNames'] = {};

defaultHash['assignedUsersNames'][this.getUser().id] = this.getUser().get('name');
}
else {
defaultHash['assignedUserId'] = this.getUser().id;

defaultHash['assignedUserName'] = this.getUser().get('name');
}
}
}

let defaultTeamId = this.getUser().get('defaultTeamId');

if (defaultTeamId) {
if (
this.model.hasField('teams') &&
!this.model.getFieldParam('teams', 'default') &&
Espo.Utils
.lowerCaseFirst(this.model.getLinkParam('teams', 'relationName') || '') === 'entityTeam'
) {
defaultHash['teamsIds'] = [defaultTeamId];
defaultHash['teamsNames'] = {};
defaultHash['teamsNames'][defaultTeamId] = this.getUser().get('defaultTeamName');
}
}
}

if (this.getUser().get('portalId')) {
if (
this.model.hasField('account') &&
~['belongsTo', 'hasOne'].indexOf(this.model.getLinkType('account'))
) {
if (this.getUser().get('accountId')) {
defaultHash['accountId'] = this.getUser().get('accountId');
defaultHash['accountName'] = this.getUser().get('accountName');
}
}

if (
this.model.hasField('contact') &&
~['belongsTo', 'hasOne'].indexOf(this.model.getLinkType('contact'))
) {
if (this.getUser().get('contactId')) {
defaultHash['contactId'] = this.getUser().get('contactId');
defaultHash['contactName'] = this.getUser().get('contactName');
}
}

if (this.model.hasField('parent') && this.model.getLinkType('parent') === 'belongsToParent') {
if (!this.getConfig().get('b2cMode')) {
if (this.getUser().get('accountId')) {
if (~(this.model.getFieldParam('parent', 'entityList') || []).indexOf('Account')) {
defaultHash['parentId'] = this.getUser().get('accountId');
defaultHash['parentName'] = this.getUser().get('accountName');
defaultHash['parentType'] = 'Account';
}
}
}
else {
if (this.getUser().get('contactId')) {
if (~(this.model.getFieldParam('parent', 'entityList') || []).indexOf('Contact')) {
defaultHash['contactId'] = this.getUser().get('contactId');
defaultHash['parentName'] = this.getUser().get('contactName');
defaultHash['parentType'] = 'Contact';
}
}
}
}

if (this.model.hasField('accounts') && this.model.getLinkType('accounts') === 'hasMany') {
if (this.getUser().get('accountsIds')) {
defaultHash['accountsIds'] = this.getUser().get('accountsIds');
defaultHash['accountsNames'] = this.getUser().get('accountsNames');
}
}

if (this.model.hasField('contacts') && this.model.getLinkType('contacts') === 'hasMany') {
if (this.getUser().get('contactId')) {
defaultHash['contactsIds'] = [this.getUser().get('contactId')];

let names = {};

names[this.getUser().get('contactId')] = this.getUser().get('contactName');
defaultHash['contactsNames'] = names;
}
}
}

for (let attr in defaultHash) {
if (this.model.has(attr)) {
delete defaultHash[attr];
}
}
const populator = new DefaultsPopulator(
this.getUser(),
this.getPreferences(),
this.getAcl(),
this.getConfig()
);

this.model.set(defaultHash, {silent: true});
populator.populate(this.model);
}

/**
Expand Down

0 comments on commit 4f2651d

Please sign in to comment.