From f3402e2c4e6e453b416b354996d32308eeadc427 Mon Sep 17 00:00:00 2001 From: yuri Date: Wed, 14 Dec 2016 12:23:15 +0200 Subject: [PATCH] dont send invitation for self if accepted --- .../Crm/Resources/i18n/en_US/Meeting.json | 3 + .../Espo/Modules/Crm/Services/Meeting.php | 12 ++++ client/modules/crm/src/views/call/detail.js | 34 ++++++---- .../modules/crm/src/views/meeting/detail.js | 64 +++++++++++++++---- client/src/views/main.js | 8 +++ 5 files changed, 95 insertions(+), 26 deletions(-) diff --git a/application/Espo/Modules/Crm/Resources/i18n/en_US/Meeting.json b/application/Espo/Modules/Crm/Resources/i18n/en_US/Meeting.json index c2dc99e803..da931aa59d 100644 --- a/application/Espo/Modules/Crm/Resources/i18n/en_US/Meeting.json +++ b/application/Espo/Modules/Crm/Resources/i18n/en_US/Meeting.json @@ -45,5 +45,8 @@ "planned": "Planned", "held": "Held", "todays": "Today's" + }, + "messages": { + "nothingHasBeenSent": "Nothing were sent" } } diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index 9cd23d3589..08df0f2790 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -138,11 +138,19 @@ class Meeting extends \Espo\Services\Record $emailHash = array(); + $sentCount = 0; + $users = $entity->get('users'); foreach ($users as $user) { + if ($user->id === $this->getUser()->id) { + if ($entity->getLinkMultipleColumn('users', 'status', $user->id) === 'Accepted') { + continue; + } + } if ($user->get('emailAddress') && !array_key_exists($user->get('emailAddress'), $emailHash)) { $invitationManager->sendInvitation($entity, $user, 'users'); $emailHash[$user->get('emailAddress')] = true; + $sentCount ++; } } @@ -151,6 +159,7 @@ class Meeting extends \Espo\Services\Record if ($contact->get('emailAddress') && !array_key_exists($contact->get('emailAddress'), $emailHash)) { $invitationManager->sendInvitation($entity, $contact, 'contacts'); $emailHash[$user->get('emailAddress')] = true; + $sentCount ++; } } @@ -159,9 +168,12 @@ class Meeting extends \Espo\Services\Record if ($lead->get('emailAddress') && !array_key_exists($lead->get('emailAddress'), $emailHash)) { $invitationManager->sendInvitation($entity, $lead, 'leads'); $emailHash[$user->get('emailAddress')] = true; + $sentCount ++; } } + if (!$sentCount) return false; + return true; } diff --git a/client/modules/crm/src/views/call/detail.js b/client/modules/crm/src/views/call/detail.js index 9ee4a1953f..9cdaa8139f 100644 --- a/client/modules/crm/src/views/call/detail.js +++ b/client/modules/crm/src/views/call/detail.js @@ -26,26 +26,28 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ - Espo.define('crm:views/call/detail', 'views/detail', function (Dep) { + Espo.define('crm:views/call/detail', ['views/detail', 'crm:views/meeting/detail'], function (Dep, MeetingDetail) { return Dep.extend({ setup: function () { Dep.prototype.setup.call(this); - if (['Held', 'Not Held'].indexOf(this.model.get('status')) == -1) { - if (this.getAcl().checkModel(this.model, 'edit') && this.getAcl().checkScope('Email', 'create')) { - this.menu.buttons.push({ - 'label': 'Send Invitations', - 'action': 'sendInvitations', - 'acl': 'edit', - }); + + MeetingDetail.prototype.controlSendInvitationsButton.call(this); + this.listenTo(this.model, 'change', function () { + if ( + this.model.hasChanged('status') + || + this.model.hasChanged('teamsIds') + ) { + MeetingDetail.prototype.controlSendInvitationsButton.call(this); } - } + }.bind(this)); }, actionSendInvitations: function () { if (confirm(this.translate('confirmation', 'messages'))) { - this.$el.find('[data-action="sendInvitations"]').addClass('disabled'); + this.disableMenuItem('sendInvitations'); this.notify('Sending...'); $.ajax({ url: 'Call/action/sendInvitations', @@ -53,12 +55,16 @@ data: JSON.stringify({ id: this.model.id }), - success: function () { - this.notify('Sent', 'success'); - this.$el.find('[data-action="sendInvitations"]').removeClass('disabled'); + success: function (result) { + if (result) { + this.notify('Sent', 'success'); + } else { + Espo.Ui.warning(this.translate('nothingHasBeenSent', 'messages', 'Meeting')); + } + this.enableMenuItem('sendInvitations'); }.bind(this), error: function () { - this.$el.find('[data-action="sendInvitations"]').removeClass('disabled'); + this.enableMenuItem('sendInvitations'); }.bind(this), }); } diff --git a/client/modules/crm/src/views/meeting/detail.js b/client/modules/crm/src/views/meeting/detail.js index 2c6516fed1..d0a872c283 100644 --- a/client/modules/crm/src/views/meeting/detail.js +++ b/client/modules/crm/src/views/meeting/detail.js @@ -32,20 +32,55 @@ Espo.define('crm:views/meeting/detail', 'views/detail', function (Dep) { setup: function () { Dep.prototype.setup.call(this); - if (['Held', 'Not Held'].indexOf(this.model.get('status')) == -1) { - if (this.getAcl().checkModel(this.model, 'edit') && this.getAcl().checkScope('Email', 'create')) { - this.menu.buttons.push({ - 'label': 'Send Invitations', - 'action': 'sendInvitations', - 'acl': 'edit', - }); + this.controlSendInvitationsButton(); + this.listenTo(this.model, 'change', function () { + if ( + this.model.hasChanged('status') + || + this.model.hasChanged('teamsIds') + ) { + this.controlSendInvitationsButton(); } + }.bind(this)); + }, + + controlSendInvitationsButton: function () { + var show = true;; + + if ( + ~['Held', 'Not Held'].indexOf(this.model.get('status')) + ) { + show = false; + } + + if (show && (!this.getAcl().checkModel(this.model, 'edit') || !this.getAcl().checkScope('Email', 'create'))) { + show = false; + } + + if (show) { + var userIdList = this.model.getLinkMultipleIdList('users'); + var contactIdList = this.model.getLinkMultipleIdList('contacts'); + var leadIdList = this.model.getLinkMultipleIdList('leads'); + + if (!contactIdList.length && !leadIdList.length && !userIdList.length) { + show = false; + } + } + + if (show) { + this.addMenuItem('buttons', { + label: 'Send Invitations', + action: 'sendInvitations', + acl: 'edit', + }); + } else { + this.removeMenuItem('sendInvitations'); } }, actionSendInvitations: function () { if (confirm(this.translate('confirmation', 'messages'))) { - this.$el.find('button[data-action="sendInvitations"]').addClass('disabled'); + this.disableMenuItem('sendInvitations'); this.notify('Sending...'); $.ajax({ url: 'Meeting/action/sendInvitations', @@ -53,12 +88,17 @@ Espo.define('crm:views/meeting/detail', 'views/detail', function (Dep) { data: JSON.stringify({ id: this.model.id }), - success: function () { - this.notify('Sent', 'success'); - this.$el.find('[data-action="sendInvitations"]').removeClass('disabled'); + success: function (result) { + if (result) { + this.notify('Sent', 'success'); + } else { + Espo.Ui.warning(this.translate('nothingHasBeenSent', 'messages', 'Meeting')); + } + + this.enableMenuItem('sendInvitations'); }.bind(this), error: function () { - this.$el.find('[data-action="sendInvitations"]').removeClass('disabled'); + this.enableMenuItem('sendInvitations'); }.bind(this), }); } diff --git a/client/src/views/main.js b/client/src/views/main.js index a656523062..962049fe35 100644 --- a/client/src/views/main.js +++ b/client/src/views/main.js @@ -142,6 +142,14 @@ Espo.define('views/main', 'view', function (Dep) { } }, + disableMenuItem: function (action) { + this.$el.find('.header .header-buttons [data-action="'+action+'"]').addClass('disabled'); + }, + + enableMenuItem: function (action) { + this.$el.find('.header .header-buttons [data-action="'+action+'"]').removeClass('disabled'); + }, + removeMenuItem: function (name, doNotReRender) { var index = -1; var type = false;