diff --git a/application/Espo/Modules/Crm/Resources/i18n/en_US/Email.json b/application/Espo/Modules/Crm/Resources/i18n/en_US/Email.json index b5e4fbe32c..f479d736a1 100644 --- a/application/Espo/Modules/Crm/Resources/i18n/en_US/Email.json +++ b/application/Espo/Modules/Crm/Resources/i18n/en_US/Email.json @@ -2,6 +2,8 @@ "labels": { "Create Lead": "Create Lead", "Create Contact": "Create Contact", + "Add to Contact": "Add to Contact", + "Add to Lead": "Add to Lead", "Create Task": "Create Task", "Create Case": "Create Case" } diff --git a/client/src/views/email/fields/email-address-varchar.js b/client/src/views/email/fields/email-address-varchar.js index cd0af27d75..cc3092df40 100644 --- a/client/src/views/email/fields/email-address-varchar.js +++ b/client/src/views/email/fields/email-address-varchar.js @@ -66,6 +66,18 @@ Espo.define('views/email/fields/email-address-varchar', ['views/fields/varchar', 'click [data-action="createContact"]': function (e) { var address = $(e.currentTarget).data('address'); From.prototype.createPerson.call(this, 'Contact', address); + }, + 'click [data-action="createLead"]': function (e) { + var address = $(e.currentTarget).data('address'); + From.prototype.createPerson.call(this, 'Lead', address); + }, + 'click [data-action="addToContact"]': function (e) { + var address = $(e.currentTarget).data('address'); + From.prototype.addToPerson.call(this, 'Contact', address); + }, + 'click [data-action="addToLead"]': function (e) { + var address = $(e.currentTarget).data('address'); + From.prototype.addToPerson.call(this, 'Lead', address); } }, diff --git a/client/src/views/email/fields/from-address-varchar.js b/client/src/views/email/fields/from-address-varchar.js index efcb2951f6..0b6af9372b 100644 --- a/client/src/views/email/fields/from-address-varchar.js +++ b/client/src/views/email/fields/from-address-varchar.js @@ -43,6 +43,18 @@ Espo.define('views/email/fields/from-address-varchar', 'views/fields/varchar', f 'click [data-action="createContact"]': function (e) { var address = $(e.currentTarget).data('address'); this.createPerson('Contact', address); + }, + 'click [data-action="createLead"]': function (e) { + var address = $(e.currentTarget).data('address'); + this.createPerson('Lead', address); + }, + 'click [data-action="addToContact"]': function (e) { + var address = $(e.currentTarget).data('address'); + this.addToPerson('Contact', address); + }, + 'click [data-action="addToLead"]': function (e) { + var address = $(e.currentTarget).data('address'); + this.addToPerson('Lead', address); } }, @@ -100,7 +112,7 @@ Espo.define('views/email/fields/from-address-varchar', 'views/fields/varchar', f if (id) { lineHtml = '
' + '' + name + ' » ' + addressHtml + '
'; } else { - if (this.getAcl().check('Contact', 'edit')) { + if (this.getAcl().check('Contact', 'create') || this.getAcl().check('Lead', 'create')) { lineHtml += this.getCreateHtml(address); } if (name) { @@ -109,11 +121,6 @@ Espo.define('views/email/fields/from-address-varchar', 'views/fields/varchar', f lineHtml += addressHtml; } } - /*if (!id) { - if (this.getAcl().check('Contact', 'edit')) { - lineHtml += this.getCreateHtml(address); - } - }*/ lineHtml = '
' + lineHtml + '
'; return lineHtml; }, @@ -124,9 +131,24 @@ Espo.define('views/email/fields/from-address-varchar', 'views/fields/varchar', f '' + '' + '' + + ''; + + if (this.getAcl().check('Contact', 'create')) { + html += '
  • '+this.translate('Create Contact', 'labels', 'Email')+'
  • '; + } + if (this.getAcl().check('Lead', 'create')) { + html += '
  • '+this.translate('Create Lead', 'labels', 'Email')+'
  • '; + } + if (this.getAcl().check('Contact', 'edit')) { + html += '
  • '+this.translate('Add to Contact', 'labels', 'Email')+'
  • '; + } + if (this.getAcl().check('Contact', 'edit')) { + html += '
  • '+this.translate('Add to Lead', 'labels', 'Email')+'
  • '; + } + + html += '' + ''; + return html; }, @@ -203,6 +225,87 @@ Espo.define('views/email/fields/from-address-varchar', 'views/fields/varchar', f }.bind(this)); }, + addToPerson: function (scope, address) { + var address = address; + + var fromString = this.model.get('fromString') || this.model.get('fromName'); + var name = this.nameHash[address] || null; + + if (!name) { + if (this.name == 'from') { + name = this.parseNameFromStringAddress(fromString) || null; + } + } + + var attributes = { + emailAddress: address + }; + + if (this.model.get('accountId') && scope == 'Contact') { + attributes.accountId = this.model.get('accountId'); + attributes.accountName = this.model.get('accountName'); + } + + var viewName = this.getMetadata().get('clientDefs.' + scope + '.modalViews.select') || 'views/modals/select-records'; + + Espo.Ui.notify(this.translate('pleaseWait', 'messages')); + + this.createView('dialog', viewName, { + scope: scope, + createButton: false, + /*filters: { + + }*/ + }, function (view) { + view.render(); + Espo.Ui.notify(false); + this.listenToOnce(view, 'select', function (model) { + var afterSave = function () { + var nameHash = Espo.Utils.clone(this.model.get('nameHash') || {}); + var typeHash = Espo.Utils.clone(this.model.get('typeHash') || {}); + var idHash = Espo.Utils.clone(this.model.get('idHash') || {}); + + idHash[address] = model.id; + nameHash[address] = model.get('name'); + typeHash[address] = scope; + + this.idHash = idHash; + this.nameHash = nameHash; + this.typeHash = typeHash; + + var attributes = { + nameHash: nameHash, + idHash: idHash, + typeHash: typeHash + }; + + setTimeout(function () { + this.model.set(attributes); + }.bind(this), 50); + }.bind(this); + + if (!model.get('emailAddress')) { + model.save({ + 'emailAddress': address + }, {patch: true}).then(afterSave); + } else { + model.fetch().then(function () { + var emailAddressData = model.get('emailAddressData') || []; + var item = { + emailAddress: address, + primary: emailAddressData.length === 0 + }; + emailAddressData.push(item); + model.save({ + 'emailAddressData': emailAddressData + }, {patch: true}).then(afterSave); + }.bind(this)); + } + + }, this); + }.bind(this)); + } + }); });