From e0b80ac30e9bb34278cec7144e4569445a08c979 Mon Sep 17 00:00:00 2001 From: std-odoo Date: Wed, 9 Feb 2022 13:29:27 +0000 Subject: [PATCH] [IMP] mass_mailing: filter the mailing templates based on the target Purpose ======= Filter the mailing templates based on the model defined on the mailing, so the user will see the most appropriate templates. Task-2593231 Part-of: odoo/odoo#79174 --- addons/mass_mailing/models/mailing.py | 10 +++- .../mailing_mailing_view_form_full_width.js | 57 +++++++++++++++++++ .../static/src/js/mass_mailing_widget.js | 11 ++++ .../static/src/xml/mass_mailing.xml | 7 ++- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/addons/mass_mailing/models/mailing.py b/addons/mass_mailing/models/mailing.py index eeefee168a8..22d5c66488d 100644 --- a/addons/mass_mailing/models/mailing.py +++ b/addons/mass_mailing/models/mailing.py @@ -1250,16 +1250,20 @@ class MassMailing(models.Model): return body_html @api.model - def action_fetch_favorites(self): + def action_fetch_favorites(self, extra_domain=None): """Return all mailings set as favorite and skip mailings with empty body. Return archived mailing templates as well, so the user can archive the templates while keeping using it, without cluttering the Kanban view if they're a lot of templates. """ + domain = [('favorite', '=', True)] + if extra_domain: + domain = expression.AND([domain, extra_domain]) + values_list = self.with_context(active_test=False).search_read( - domain=[('favorite', '=', True)], - fields=['id', 'subject', 'body_arch', 'user_id'], + domain=domain, + fields=['id', 'subject', 'body_arch', 'user_id', 'mailing_model_id'], order='favorite_date DESC', ) diff --git a/addons/mass_mailing/static/src/js/mailing_mailing_view_form_full_width.js b/addons/mass_mailing/static/src/js/mailing_mailing_view_form_full_width.js index 40adf1db3d4..bfbb55e8ddf 100644 --- a/addons/mass_mailing/static/src/js/mailing_mailing_view_form_full_width.js +++ b/addons/mass_mailing/static/src/js/mailing_mailing_view_form_full_width.js @@ -10,6 +10,7 @@ import config from 'web.config'; const MassMailingFullWidthFormController = FormController.extend({ custom_events: _.extend({}, FormController.prototype.custom_events,{ iframe_updated: '_onIframeUpdated', + themes_loaded: '_onThemesLoaded', }), /** @@ -37,9 +38,57 @@ const MassMailingFullWidthFormController = FormController.extend({ this._super(...arguments); }, + /** + * If we change the "mailing_model_id" in the form view, we want to update + * the mass mailing widget, because some templates might be hidden / displayed + * depending on if they belong to the new selected model. + * + * @override + */ + _applyChanges: function (dataPointID, changes, event) { + if (changes.mailing_model_id) { + this._hideIrrelevantTemplates(changes.mailing_model_id); + } + return this._super(...arguments); + }, + //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- + /** + * This method will take the model in argument and will hide all mailing template + * in the mass mailing widget that do not belong to this model. + * + * This will also update the help message in the same widget to include the + * new model name. + * + * @params {id: , display_name: } + * + * @private + */ + _hideIrrelevantTemplates: function (model) { + const iframeContent = $('.o_field_widget[name="body_arch"] iframe').contents(); + + iframeContent + .find(`.o_mail_template_preview[model-id!="${model.id}"]`) + .addClass('d-none') + .removeClass('d-inline-block'); + + const sameModelTemplates = iframeContent + .find(`.o_mail_template_preview[model-id="${model.id}"]`); + + sameModelTemplates + .removeClass('d-none') + .addClass('d-inline-block'); + + // Hide or show the help message if some templates are visible + if (sameModelTemplates.length) { + iframeContent.find('.o_mailing_template_message').addClass('d-none'); + } else { + iframeContent.find('.o_mailing_template_message').removeClass('d-none'); + iframeContent.find('.o_mailing_template_message span').text(model.display_name); + } + }, /** * Resize the mailing editor's iframe container so its height fits its @@ -151,6 +200,14 @@ const MassMailingFullWidthFormController = FormController.extend({ }); } }, + + /** + * The themes have been loaded by the mass mailing widget. + */ + _onThemesLoaded() { + const model = this.initialState.data.mailing_model_id.data; + this._hideIrrelevantTemplates(model); + }, /** * Switch "scrolling modes" on toggle fullscreen mode: in fullscreen mode, * the scroll happens within the iframe whereas in regular mode we pretend diff --git a/addons/mass_mailing/static/src/js/mass_mailing_widget.js b/addons/mass_mailing/static/src/js/mass_mailing_widget.js index 6a15ed3027c..b08706eb757 100644 --- a/addons/mass_mailing/static/src/js/mass_mailing_widget.js +++ b/addons/mass_mailing/static/src/js/mass_mailing_widget.js @@ -371,14 +371,22 @@ var MassMailingFieldHtml = FieldHtml.extend({ this.wysiwyg.$iframeBody.find('.iframe-utils-zone').addClass('d-none'); + // Filter the fetched templates based on the current model + const args = this.nodeOptions.filterTemplates + ? [[['mailing_model_id', '=', this.recordData.mailing_model_id.res_id]]] + : []; + // Templates taken from old mailings const result = await this._rpc({ model: 'mailing.mailing', method: 'action_fetch_favorites', + args: args, }); const templatesParams = result.map(values => { return { id: values.id, + modelId: values.mailing_model_id[0], + modelName: values.mailing_model_id[1], name: `template_${values.id}`, nowrap: true, subject: values.subject, @@ -458,6 +466,7 @@ var MassMailingFieldHtml = FieldHtml.extend({ const $themeSelectorNew = $(core.qweb.render("mass_mailing.theme_selector_new", { themes: themesParams, templates: templatesParams, + modelName: this.recordData.mailing_model_id.data.display_name || '', })); @@ -604,6 +613,8 @@ var MassMailingFieldHtml = FieldHtml.extend({ } this.wysiwyg.$iframeBody.find('.iframe-utils-zone').removeClass('d-none'); + + this.trigger_up('themes_loaded'); }, /** * @override diff --git a/addons/mass_mailing/static/src/xml/mass_mailing.xml b/addons/mass_mailing/static/src/xml/mass_mailing.xml index 560c64e9e41..95f08235936 100644 --- a/addons/mass_mailing/static/src/xml/mass_mailing.xml +++ b/addons/mass_mailing/static/src/xml/mass_mailing.xml @@ -8,12 +8,13 @@
-
- Click on the ⭐ next to the subject to save this mailing as a template +
+ Click on the ⭐ next to the subject to save this mailing as a template