[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
This commit is contained in:
std-odoo
2022-03-16 13:16:13 +01:00
parent 5508ca04e1
commit e0b80ac30e
4 changed files with 79 additions and 6 deletions
+7 -3
View File
@@ -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',
)
@@ -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: <model id>, display_name: <model 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
@@ -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
@@ -8,12 +8,13 @@
</t>
</div>
<div t-name="mass_mailing.theme_selector_new" class="o_mail_theme_selector_new">
<div t-if="!templates.length" class="text-white text-left mx-4 pt-3 px-1">
Click on the ⭐ next to the subject to save this mailing as a template
<div t-attf-class="o_mailing_template_message text-white text-left mx-4 pt-3 px-1 #{templates.length ? 'd-none': ''}">
Click on the ⭐ next to the subject to save this mailing as a <span t-esc="modelName"/> template
</div>
<div class="d-inline-block w-100">
<div t-foreach="templates" t-as="template"
class="o_mail_template_preview d-inline-block dropdown-item" t-att-id="template.name">
class="o_mail_template_preview d-inline-block dropdown-item"
t-att-id="template.name" t-att-model-id="template.modelId">
<div class="d-inline-flex flex-row align-items-center text-white border px-2 py-2 w-100">
<i class="fa fa-star text-warning mr-2"/>
<span class="text-truncate" t-esc="template.subject"/>