2019-04-05 19:39:55 -04:00
|
|
|
/* Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
|
|
|
|
* License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */
|
|
|
|
|
|
|
|
/* This JS module replaces core AJAX submission because it is impossible
|
|
|
|
* to extend it as it is currently designed. It is almost a copy+paste from
|
2021-07-21 16:53:13 -04:00
|
|
|
* upstream, to allow easier version/patch updates, so linter is disabled
|
|
|
|
* and prettier is ignore. */
|
2020-08-26 08:57:06 +02:00
|
|
|
/* eslint-disable */
|
2021-07-21 16:53:13 -04:00
|
|
|
// prettier-ignore
|
2021-07-21 16:49:42 -04:00
|
|
|
odoo.define("mass_mailing_custom_unsubscribe.unsubscribe", function (require) {
|
2021-01-26 14:06:17 +00:00
|
|
|
"use strict";
|
2019-04-05 19:39:55 -04:00
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
var session = require('web.session');
|
|
|
|
var ajax = require('web.ajax');
|
|
|
|
var core = require('web.core');
|
|
|
|
require('web.dom_ready');
|
2019-04-05 19:39:55 -04:00
|
|
|
|
2018-05-10 11:17:04 +01:00
|
|
|
var _t = core._t;
|
[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate.
- Imported last updates from v8.
- Adapted to v9.
- Added a saner default to `mass_mailing.salt` configuration parameter by
reusing `database.secret` if available, hoping that some day
https://github.com/odoo/odoo/pull/12040 gets merged.
- Updated README.
- Increase security, drop backwards compatibility.
Security got improved upstream, which would again break compatibility among current addon and future master upstream.
I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features.
- Includes tour tests.
- Removes outdated tests.
- Extends the mailing list management form when unsubscriber is a contact.
- Adds a reason form even if he is not.
- Avoids all methods that were not model-agnostic.
[FIX][mass_mailing_custom_unsubscribe] Reasons noupdate
After this fix, when you update the addon, you will not lose your customized reasons.
[FIX] Compatibilize with mass_mailing_partner
Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets.
Now records are created from an empty recordset, and thus tests work everywhere.
Update instructions
If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon.
[FIX] Use the right operator to preserve recordsets order
Using `|=` sorts records at will each time (treating them as Python's `set`).
Using `+=` always appends a record to the end of the set.
Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
2016-11-11 14:41:20 +01:00
|
|
|
|
2019-04-05 19:39:55 -04:00
|
|
|
var email = $("input[name='email']").val();
|
|
|
|
var mailing_id = parseInt($("input[name='mailing_id']").val(), 10);
|
|
|
|
var res_id = parseInt($("input[name='res_id']").val(), 10);
|
2021-07-21 16:53:13 -04:00
|
|
|
var token = (location.search.split('token' + '=')[1] || '').split('&')[0];
|
2019-04-05 19:39:55 -04:00
|
|
|
var $mailing_lists = $("input[name='contact_ids']");
|
|
|
|
var $reasons = $("#custom_div_feedback");
|
|
|
|
var $details = $("textarea[name='details']");
|
|
|
|
var $radio = $(":radio");
|
|
|
|
var $info_state = $("#info_state, #custom_div_feedback");
|
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
if (!$('.o_unsubscribe_form').length) {
|
|
|
|
return Promise.reject("DOM doesn't contain '.o_unsubscribe_form'");
|
|
|
|
}
|
|
|
|
$radio.on('change click', function (e) {
|
2019-04-05 19:39:55 -04:00
|
|
|
$details.prop(
|
2021-07-21 16:53:13 -04:00
|
|
|
'required',
|
|
|
|
$(e.target).is('[data-details-required]') && $(e.target).is(':visible')
|
2019-04-05 19:39:55 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Display reasons form only if there are unsubscriptions
|
2021-07-21 16:49:42 -04:00
|
|
|
var toggle_reasons = function () {
|
2019-04-05 19:39:55 -04:00
|
|
|
// Find contacts that were checked and now are unchecked
|
2021-07-21 16:49:42 -04:00
|
|
|
var $disabled = $mailing_lists.filter(function () {
|
2019-04-05 19:39:55 -04:00
|
|
|
var $this = $(this);
|
2021-07-21 16:53:13 -04:00
|
|
|
return !$this.prop('checked') && $this.attr('checked');
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
// Hide reasons form if you are only subscribing
|
2021-07-21 16:53:13 -04:00
|
|
|
$reasons.toggleClass('d-none', !$disabled.length);
|
|
|
|
var $radios = $reasons.find(':radio');
|
|
|
|
if ($reasons.is(':hidden')) {
|
2019-04-05 19:39:55 -04:00
|
|
|
// Uncheck chosen reason
|
2021-01-26 14:06:17 +00:00
|
|
|
$radios
|
2021-07-21 16:53:13 -04:00
|
|
|
.prop('checked', false)
|
2021-01-26 14:06:17 +00:00
|
|
|
// Unrequire specifying a reason
|
2021-07-21 16:53:13 -04:00
|
|
|
.prop('required', false)
|
2021-01-26 14:06:17 +00:00
|
|
|
// Remove possible constraints for details
|
2021-07-21 16:53:13 -04:00
|
|
|
.trigger('change');
|
2019-04-05 19:39:55 -04:00
|
|
|
// Clear textarea
|
2021-07-21 16:53:13 -04:00
|
|
|
$details.val('');
|
2019-04-05 19:39:55 -04:00
|
|
|
} else {
|
|
|
|
// Require specifying a reason
|
2021-07-21 16:53:13 -04:00
|
|
|
$radios.prop('required', true);
|
2019-04-05 19:39:55 -04:00
|
|
|
}
|
|
|
|
};
|
2021-07-21 16:49:42 -04:00
|
|
|
$mailing_lists.change(function (e) {
|
2019-04-05 19:39:55 -04:00
|
|
|
toggle_reasons();
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#info_state').addClass('invisible');
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
session.load_translations().then(function () {
|
|
|
|
if (email != '' && email != undefined){
|
|
|
|
ajax.jsonRpc('/mailing/blacklist/check', 'call', {'email': email, 'mailing_id': mailing_id, 'res_id': res_id, 'token': token})
|
|
|
|
.then(function (result) {
|
|
|
|
if (result == 'unauthorized'){
|
|
|
|
$('#button_add_blacklist').hide();
|
|
|
|
$('#button_remove_blacklist').hide();
|
|
|
|
}
|
|
|
|
else if (result == true) {
|
|
|
|
$('#button_remove_blacklist').show();
|
|
|
|
toggle_opt_out_section(false);
|
|
|
|
}
|
|
|
|
else if (result == false) {
|
|
|
|
$('#button_add_blacklist').show();
|
|
|
|
toggle_opt_out_section(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#subscription_info').html(_t('An error occurred. Please try again later or contact us.'));
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-warning').addClass('alert-error');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.guardedCatch(function () {
|
|
|
|
$('#subscription_info').html(_t('An error occurred. Please try again later or contact us.'));
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-warning').addClass('alert-error');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#div_blacklist').hide();
|
|
|
|
}
|
2019-04-05 19:39:55 -04:00
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
var unsubscribed_list = $("input[name='unsubscribed_list']").val();
|
|
|
|
if (unsubscribed_list){
|
|
|
|
$('#subscription_info').html(_.str.sprintf(
|
2021-01-26 14:06:17 +00:00
|
|
|
_t("You have been <strong>successfully unsubscribed from %s</strong>."),
|
2021-07-21 16:53:13 -04:00
|
|
|
_.escape(unsubscribed_list)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$('#subscription_info').html(_t('You have been <strong>successfully unsubscribed</strong>.'));
|
|
|
|
}
|
|
|
|
});
|
2019-04-05 19:39:55 -04:00
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#unsubscribe_form').on('submit', function (e) {
|
2019-04-05 19:39:55 -04:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var checked_ids = [];
|
2021-07-21 16:49:42 -04:00
|
|
|
$("input[type='checkbox']:checked").each(function (i) {
|
2021-07-21 16:53:13 -04:00
|
|
|
checked_ids[i] = parseInt($(this).val(), 10);
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
var unchecked_ids = [];
|
2021-07-21 16:49:42 -04:00
|
|
|
$("input[type='checkbox']:not(:checked)").each(function (i) {
|
2021-07-21 16:53:13 -04:00
|
|
|
unchecked_ids[i] = parseInt($(this).val(), 10);
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
var values = {
|
2021-01-26 14:06:17 +00:00
|
|
|
opt_in_ids: checked_ids,
|
|
|
|
opt_out_ids: unchecked_ids,
|
|
|
|
email: email,
|
|
|
|
mailing_id: mailing_id,
|
|
|
|
res_id: res_id,
|
|
|
|
token: token,
|
2019-04-05 19:39:55 -04:00
|
|
|
};
|
|
|
|
// Only send reason and details if an unsubscription was found
|
2021-07-21 16:53:13 -04:00
|
|
|
if ($reasons.is(':visible')) {
|
2019-04-05 19:39:55 -04:00
|
|
|
values.reason_id = parseInt(
|
2021-07-21 16:53:13 -04:00
|
|
|
$reasons.find("[name='reason_id']:checked").val(), 10
|
2019-04-05 19:39:55 -04:00
|
|
|
);
|
|
|
|
values.details = $details.val();
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
ajax.jsonRpc('/mail/mailing/unsubscribe', 'call', values)
|
2021-07-21 16:49:42 -04:00
|
|
|
.then(function (result) {
|
2021-07-21 16:53:13 -04:00
|
|
|
if (result == 'unauthorized'){
|
|
|
|
$('#subscription_info').html(_t('You are not authorized to do this!'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-error').addClass('alert-warning');
|
|
|
|
}
|
|
|
|
else if (result == true) {
|
|
|
|
$('#subscription_info').html(_t('Your changes have been saved.'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-info').addClass('alert-success');
|
2019-04-05 19:39:55 -04:00
|
|
|
// Store checked status, to enable further changes
|
2021-07-21 16:49:42 -04:00
|
|
|
$mailing_lists.each(function () {
|
2019-04-05 19:39:55 -04:00
|
|
|
var $this = $(this);
|
2021-07-21 16:53:13 -04:00
|
|
|
$this.attr('checked', $this.prop('checked'));
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
toggle_reasons();
|
2021-07-21 16:53:13 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#subscription_info').html(_t('An error occurred. Your changes have not been saved, try again later.'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-info').addClass('alert-warning');
|
2019-04-05 19:39:55 -04:00
|
|
|
}
|
|
|
|
})
|
2021-07-21 16:49:42 -04:00
|
|
|
.guardedCatch(function () {
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#subscription_info').html(_t('An error occurred. Your changes have not been saved, try again later.'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-info').addClass('alert-warning');
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// ==================
|
|
|
|
// Blacklist
|
|
|
|
// ==================
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#button_add_blacklist').click(function (e) {
|
2019-04-05 19:39:55 -04:00
|
|
|
e.preventDefault();
|
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
if ($reasons.is(':hidden')) {
|
|
|
|
$reasons.toggleClass('d-none', false);
|
|
|
|
$reasons.find(':radio').prop('required', true);
|
2019-04-05 19:39:55 -04:00
|
|
|
}
|
2021-07-21 16:53:13 -04:00
|
|
|
if (!$('#unsubscribe_form')[0].reportValidity()) {
|
2019-04-05 19:39:55 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
ajax.jsonRpc('/mailing/blacklist/add', 'call', {'email': email, 'mailing_id': mailing_id, 'res_id': res_id, 'token': token,
|
|
|
|
'reason_id': parseInt($reasons.find("[name='reason_id']:checked").val(), 10),
|
|
|
|
'details': $details.val(),
|
2019-04-05 19:39:55 -04:00
|
|
|
})
|
2021-07-21 16:49:42 -04:00
|
|
|
.then(function (result) {
|
2021-07-21 16:53:13 -04:00
|
|
|
if (result == 'unauthorized'){
|
|
|
|
$('#subscription_info').html(_t('You are not authorized to do this!'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-error').addClass('alert-warning');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-05 19:39:55 -04:00
|
|
|
if (result) {
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#subscription_info').html(_t('You have been successfully <strong>added to our blacklist</strong>. '
|
|
|
|
+ 'You will not be contacted anymore by our services.'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-warning').removeClass('alert-info').removeClass('alert-error').addClass('alert-success');
|
2019-04-05 19:39:55 -04:00
|
|
|
toggle_opt_out_section(false);
|
|
|
|
// set mailing lists checkboxes to previous state
|
2021-07-21 16:49:42 -04:00
|
|
|
$mailing_lists.each(function () {
|
2019-04-05 19:39:55 -04:00
|
|
|
var $this = $(this);
|
2021-07-21 16:53:13 -04:00
|
|
|
$this.prop('checked', $(this)[0].hasAttribute('checked'));
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
// Hide reasons and reset reason fields
|
2021-07-21 16:53:13 -04:00
|
|
|
$reasons.toggleClass('d-none', true).find(':radio').prop('checked', false);
|
|
|
|
$details.val('').prop('required', false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#subscription_info').html(_t('An error occurred. Please try again later or contact us.'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-warning').addClass('alert-error');
|
2019-04-05 19:39:55 -04:00
|
|
|
}
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#button_add_blacklist').hide();
|
|
|
|
$('#button_remove_blacklist').show();
|
|
|
|
$('#unsubscribed_info').hide();
|
2019-04-05 19:39:55 -04:00
|
|
|
}
|
|
|
|
})
|
2021-07-21 16:49:42 -04:00
|
|
|
.guardedCatch(function () {
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#subscription_info').html(_t('An error occured. Please try again later or contact us.'));
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-warning').addClass('alert-error');
|
[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate.
- Imported last updates from v8.
- Adapted to v9.
- Added a saner default to `mass_mailing.salt` configuration parameter by
reusing `database.secret` if available, hoping that some day
https://github.com/odoo/odoo/pull/12040 gets merged.
- Updated README.
- Increase security, drop backwards compatibility.
Security got improved upstream, which would again break compatibility among current addon and future master upstream.
I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features.
- Includes tour tests.
- Removes outdated tests.
- Extends the mailing list management form when unsubscriber is a contact.
- Adds a reason form even if he is not.
- Avoids all methods that were not model-agnostic.
[FIX][mass_mailing_custom_unsubscribe] Reasons noupdate
After this fix, when you update the addon, you will not lose your customized reasons.
[FIX] Compatibilize with mass_mailing_partner
Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets.
Now records are created from an empty recordset, and thus tests work everywhere.
Update instructions
If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon.
[FIX] Use the right operator to preserve recordsets order
Using `|=` sorts records at will each time (treating them as Python's `set`).
Using `+=` always appends a record to the end of the set.
Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
2016-11-11 14:41:20 +01:00
|
|
|
});
|
|
|
|
});
|
2018-05-10 11:17:04 +01:00
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#button_remove_blacklist').click(function (e) {
|
2019-04-05 19:39:55 -04:00
|
|
|
e.preventDefault();
|
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
ajax.jsonRpc('/mailing/blacklist/remove', 'call', {'email': email, 'mailing_id': mailing_id, 'res_id': res_id, 'token': token})
|
2021-07-21 16:49:42 -04:00
|
|
|
.then(function (result) {
|
2021-07-21 16:53:13 -04:00
|
|
|
if (result == 'unauthorized'){
|
|
|
|
$('#subscription_info').html(_t('You are not authorized to do this!'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-error').addClass('alert-warning');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-05 19:39:55 -04:00
|
|
|
if (result) {
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#subscription_info').html(_t("You have been successfully <strong>removed from our blacklist</strong>. "
|
|
|
|
+ "You are now able to be contacted by our services."));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-warning').removeClass('alert-info').removeClass('alert-error').addClass('alert-success');
|
2019-04-05 19:39:55 -04:00
|
|
|
toggle_opt_out_section(true);
|
|
|
|
}
|
2021-07-21 16:53:13 -04:00
|
|
|
else {
|
|
|
|
$('#subscription_info').html(_t('An error occured. Please try again later or contact us.'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-warning').addClass('alert-error');
|
|
|
|
}
|
|
|
|
$('#button_add_blacklist').show();
|
|
|
|
$('#button_remove_blacklist').hide();
|
|
|
|
$('#unsubscribed_info').hide();
|
2019-04-05 19:39:55 -04:00
|
|
|
}
|
|
|
|
})
|
2021-07-21 16:49:42 -04:00
|
|
|
.guardedCatch(function () {
|
2021-07-21 16:53:13 -04:00
|
|
|
$('#subscription_info').html(_t('An error occured. Please try again later or contact us.'));
|
|
|
|
$('#info_state').removeClass('invisible');
|
|
|
|
$info_state.removeClass('alert-success').removeClass('alert-info').removeClass('alert-warning').addClass('alert-error');
|
2019-04-05 19:39:55 -04:00
|
|
|
});
|
|
|
|
});
|
[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate.
- Imported last updates from v8.
- Adapted to v9.
- Added a saner default to `mass_mailing.salt` configuration parameter by
reusing `database.secret` if available, hoping that some day
https://github.com/odoo/odoo/pull/12040 gets merged.
- Updated README.
- Increase security, drop backwards compatibility.
Security got improved upstream, which would again break compatibility among current addon and future master upstream.
I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features.
- Includes tour tests.
- Removes outdated tests.
- Extends the mailing list management form when unsubscriber is a contact.
- Adds a reason form even if he is not.
- Avoids all methods that were not model-agnostic.
[FIX][mass_mailing_custom_unsubscribe] Reasons noupdate
After this fix, when you update the addon, you will not lose your customized reasons.
[FIX] Compatibilize with mass_mailing_partner
Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets.
Now records are created from an empty recordset, and thus tests work everywhere.
Update instructions
If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon.
[FIX] Use the right operator to preserve recordsets order
Using `|=` sorts records at will each time (treating them as Python's `set`).
Using `+=` always appends a record to the end of the set.
Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
2016-11-11 14:41:20 +01:00
|
|
|
});
|
2019-04-05 19:39:55 -04:00
|
|
|
|
2021-07-21 16:53:13 -04:00
|
|
|
// prettier-ignore
|
2019-04-05 19:39:55 -04:00
|
|
|
function toggle_opt_out_section(value) {
|
|
|
|
var result = !value;
|
2021-07-21 16:53:13 -04:00
|
|
|
$("#div_opt_out").find('*').attr('disabled',result);
|
|
|
|
$("#button_add_blacklist").attr('disabled', false);
|
|
|
|
$("#button_remove_blacklist").attr('disabled', false);
|
|
|
|
if (value) { $('[name="button_subscription"]').addClass('clickable'); }
|
|
|
|
else { $('[name="button_subscription"]').removeClass('clickable'); }
|
2019-04-05 19:39:55 -04:00
|
|
|
}
|