(function() {
'use strict';
var Moderation = {
mainSettings : {
mainBtn: 'on_off_toggle',
modNshowPsubsBtn: 'nshow_per_subscriber_toggle',
modResPsubsBtn: 'res_per_subscriber_toggle',
modEventTypeBtn: 'event_category_toggle',
modEventLocationBtn: 'event_location_toggle',
modEventMonthBtn: 'event_month_toggle',
flexibleModBtn: 'flex_mod_toggle'
},
spinner: ``,
data: new FormData(),
current_mod_settings : {},
mainBtn: {
element: 'input[name=gm_mod_stat]',
collapse_element: ''
},
modNshowPsubsBtn: {
element: 'input[name=noshow_psub_stat]',
collapse_element: '#noshow_psub_stat_collapse',
subSettings: 'input[name=max_noshow], input[name=nshow_period]'
},
modResPsubsBtn: {
element: 'input[name=res_psub_stat]',
collapse_element: '#res_psub_stat_collapse',
subSettings: 'input[name=max_res], input[name=res_period_type]'
},
modEventTypeBtn: {
element: 'input[name=event_cat_stat]',
collapse_element: '#event_cat_stat_collapse',
subSettings: 'input[name=event_cat]'
},
modEventLocationBtn: {
element: 'input[name=event_loc_stat]',
collapse_element: '#event_loc_stat_collapse',
subSettings: 'input[name=event_location]'
},
modEventMonthBtn: {
element: 'input[name=event_month_stat]',
collapse_element: '#event_month_stat_collapse',
subSettings: 'input[name=event_month]'
},
flexibleModBtn: {
element: 'input[name=flex_mod_stat]',
collapse_element: '#flex_mod_stat_collapse',
subSettings: 'input[name=hrs_bef_event_closes], input[name=places_volume]'
},
}
/*****************************************************************************
*
* Event listeners for UI elements
*
****************************************************************************/
// ACTIVE MODERATION
$(Moderation.mainBtn['element']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['on_off_toggle'])) {
Moderation.toggleGlobalModerationStatus($(this).prop("checked"));
} else {
app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
// NOSHOW PER SUBSCRIBER
$(Moderation.modNshowPsubsBtn['element']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['nshow_per_subscriber_toggle'])) {
Moderation.toggleModNshowAttendanceStatus($(this).prop("checked"), 'main');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
$(Moderation.modNshowPsubsBtn['subSettings']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['nshow_per_subscriber_toggle'])) {
Moderation.toggleModNshowAttendanceStatus($(Moderation.modNshowPsubsBtn['element']).prop("checked"), 'sub');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
// RESERVATION PER SUBSCRIBER
$(Moderation.modResPsubsBtn['element']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['res_per_subscriber_toggle'])) {
Moderation.toggleReservationStatus($(this).prop("checked"), 'main');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
$(Moderation.modResPsubsBtn['subSettings']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['res_per_subscriber_toggle'])) {
Moderation.toggleReservationStatus($(Moderation.modResPsubsBtn['element']).prop("checked"), 'sub');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
// MODERATION EVENT TYPE/CATEGORY
$(Moderation.modEventTypeBtn['element']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['event_category_toggle'])) {
Moderation.toggleModEventTypeStatus($(this).prop("checked"), 'main');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
$(Moderation.modEventTypeBtn['subSettings']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['event_category_toggle'])) {
Moderation.toggleModEventTypeStatus($(Moderation.modEventTypeBtn['element']).prop("checked"), 'sub');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
// MODERATION EVENT LOCATION
$(Moderation.modEventLocationBtn['element']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['event_location_toggle'])) {
Moderation.toggleModEventLocationStatus($(this).prop("checked"), 'main');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
$(Moderation.modEventLocationBtn['subSettings']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['event_location_toggle'])) {
Moderation.toggleModEventLocationStatus($(Moderation.modEventLocationBtn['element']).prop("checked"), 'sub');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
// MODERATION EVENT MONTH
$(Moderation.modEventMonthBtn['element']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['event_month_toggle'])) {
Moderation.toggleModEventMonthStatus($(this).prop("checked"), 'main');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
$(Moderation.modEventMonthBtn['subSettings']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['event_month_toggle'])) {
Moderation.toggleModEventMonthStatus($(Moderation.modEventMonthBtn['element']).prop("checked"), 'sub');
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
// FLEXIBLE MODERATION
$(Moderation.flexibleModBtn['element']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['flex_mod_toggle'])) {
Moderation.toggleFlexibleModerationStatus($(this).prop("checked"));
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
$(Moderation.flexibleModBtn['subSettings']).on('change', function(event) {
event.preventDefault();
/* Act on the event */
if (authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']]['flex_mod_toggle'])) {
Moderation.saveFlexibleModerationSettings($(Moderation.flexibleModBtn['element']).prop("checked"), this.name);
} else {
if ($("#switch-lg").prop("checked")) app._notify("error", "Action non autorisée, les modifications n'ont pas été enregistrées.");
}
});
/*****************************************************************************
*
* Methods for dealing with moderation settings
*
****************************************************************************/
/**
* initializes the status of buttons (onload) base on the main buttons
*/
Moderation.initAllButtonStatus = function() {
// get current moderation settings
$.ajax({
url : app.get_vars().baseurl+'getModSettings/3',
type : "GET",
async: false,
processData: false,
contentType: false,
success : function(result){
Moderation.current_mod_settings = result;
// console.log(result.main_settings);
var input_type, value_list, cur_settings_is_used;
/*
* Display main settings
*/
if (result.main_settings['is_used']) {
cur_settings_is_used = (parseInt(result.main_settings['is_used']));
if (cur_settings_is_used) {
app._notify("info", "Toutes modifications apportées sur les paramètres de modération seront effectives et pris en compte uniquement pour les futures événements que vous créérez.");
}
}
$.each(result.main_settings,function(key, value) {
$(`input[name=${key}]`).prop('checked', parseInt(value));
});
/*
* Display sub settings
*/
$.each(result.sub_settings,function(key, value) {
if (value) {
switch (key) {
case 'event_cats':
value_list = value.split(',');
$.each(value_list, function(key1, value1) {
input_type = $(`input[name=event_cat][value=${value1}]`).prop('checked', true)
});
break;
case 'locations':
value_list = value.split(',');
$.each(value_list, function(key1, value1) {
input_type = $(`input[name=event_location][value=${value1}]`).prop('checked', true)
});
break;
case 'months':
value_list = value.split(',');
$.each(value_list, function(key1, value1) {
input_type = $(`input[name=event_month][value=${value1}]`).prop('checked', true)
});
break;
default:
input_type = $(`input[name=${key}]`).attr('type')
if (input_type == 'checkbox' || input_type == 'radio') {
input_type = $(`input[name=${key}][value=${value}]`).prop('checked', true);
} else {
$(`input[name=${key}]`).val(value)
}
}
}
// $(`input[name=${key}]`).prop('checked', parseInt(value));
});
// activate button status and the collapse if main button is activated
if ($(Moderation.mainBtn['element']).prop('checked')) {
// $('input.moderation_switch[type=checkbox]:not(.switch2)').removeAttr('disabled');
Moderation.toggleCollapse('.collapse.active_collapse', true);
$.each($('input.moderation_switch'), function(){
switch ($(this).attr("name")) {
case 'event_cat_stat':
case 'event_loc_stat':
case 'event_month_stat':
if (
$(Moderation.modNshowPsubsBtn['element']).prop("checked") == true ||
$(Moderation.modResPsubsBtn['element']).prop("checked") == true
) {
$(this).removeAttr('disabled');
Moderation.toggleCollapse(`#${this.name}_collapse`, $(this).prop("checked"));
}
break;
default:
$(this).removeAttr('disabled');
Moderation.toggleCollapse(`#${this.name}_collapse`, $(this).prop("checked"));
}
});
}
// activate the buttons with true value
$('input.moderation_switch[type=checkbox][value=true]:not(.switch2)').prop('checked', true);
for (const [key, value] of Object.entries(Moderation.mainSettings)) {
if ( !authorize(PAGE_CODE['moderation'], PRIVS[PAGE_CODE['moderation']][value])) {
$(Moderation[key].element).prop('disabled', "disabled");
$(Moderation[key].subSettings).prop('disabled', "disabled");
};
}
}
});
};
/**
* add loader in the switch toggle button
*/
Moderation.appendSwitchLoader = function(element){
var has_loader = $(element).parent().closest('span').parent().closest('div').has('.switch-loader').length;
if (!has_loader) {
$(``).insertAfter($(element).parent().closest('span'));
}
setTimeout(function () {
$('.switch-loader').remove();
}, 500);
};
/**
* turns the global moderation settings on or off
* @param elements = string that contains the selector for the elements
* @param status = turn off/on or disable/enable the buttons/s
* on/off will turn the button on or off while
* disable/enable will only make the button clickable or not
*/
Moderation.toggleDependentButtonsStatus = function(elements, status, disable_button) {
switch (status) {
case 'on':
$(elements).prop('checked', true);
$(elements).trigger('change');
$(elements).removeAttr('disabled');
break;
case 'off':
$(elements).prop('checked', false);
$(elements).trigger('change');
if (disable_button) {
$(elements).attr('disabled','disabled');
}
break;
case 'enable':
$(elements).removeAttr('disabled');
break;
case 'disable':
$(elements).attr('disabled', 'disabled');
break;
default:
}
};
/**
* toggles the collapsible element
* @param elements = string that contains the selector of elements to be toggled
* @param status = string that contains the selector for the elements
*/
Moderation.toggleCollapse = function(elements, status) {
$(elements).collapse((status? 'show':'hide'));
};
/******** ACTIVE MODERATION ********/
/**
* turns the global moderation settings on or off
* @param status = status of the button
*/
Moderation.toggleGlobalModerationStatus = function(status) {
Moderation.data= new FormData();
Moderation.appendSwitchLoader(Moderation.mainBtn['element']);
if (status == true) {
Moderation.data.append('type','gm_mod_stat');
Moderation.data.append('gm_mod_stat',status?1:0);
Moderation.saveMainSettings();
Moderation.initAllButtonStatus();
} else {
app.modal.confirm_box({
"message" : `Etes vous sûr de vouloir désactiver la modération ?
Note: Cette action désactivera toute les
modérations déjà paramétrées sur tous
les événements.`,
"_continue" : function() {
Moderation.data.append('type','active_moderation');
Moderation.data.append('gm_mod_stat',status?1:0);
Moderation.saveMainSettings();
Moderation.toggleDependentButtonsStatus(`input.moderation_switch[type=checkbox]:not(${Moderation.mainBtn['element']})`, 'disable', false);
Moderation.toggleDependentButtonsStatus(Moderation.flexibleModBtn['element'], 'off', false);
Moderation.toggleCollapse('.collapse', false);
},
"_close" : function() {
$(Moderation.mainBtn['element']).prop("checked", true);
}
});
}
};
/******** MODERATION ATTENDACE ********/
/**
* turns the moderation attendance setting on or off
* @param status = status of the button
*/
Moderation.toggleModNshowAttendanceStatus = function(status, type) {
Moderation.appendSwitchLoader(Moderation.modNshowPsubsBtn['element']);
switch (type) {
case 'sub':
var max_noshow = $.trim($('input[name=max_noshow]').val());
var nshow_period = $.trim($('input[name=nshow_period]').val());
Moderation.data = new FormData();
Moderation.data.append('type','noshow_psub_sub');
Moderation.data.append('max_noshow',max_noshow ? max_noshow:0);
Moderation.data.append('nshow_period',nshow_period? nshow_period: 0);
Moderation.saveSubSettings();
break;
default:
if (status == false && $(Moderation.modResPsubsBtn['element']).prop("checked") == false) {
Moderation.toggleDependentButtonsStatus(
`${Moderation.modEventTypeBtn['element']}, ${Moderation.modEventLocationBtn['element']}, ${Moderation.modEventMonthBtn['element']}`,
"off", true
);
}
if (status == true && $(Moderation.modResPsubsBtn['element']).prop("checked") == false) {
Moderation.toggleDependentButtonsStatus(
`${Moderation.modEventTypeBtn['element']}, ${Moderation.modEventLocationBtn['element']}, ${Moderation.modEventMonthBtn['element']}`,
"on", false
);
}
Moderation.toggleCollapse(Moderation.modNshowPsubsBtn['collapse_element'], status);
Moderation.saveMainSettings();
}
};
/******** MODERATION PER RESERVATION ********/
/**
* turns the moderation name setting on or off
* @param status = status of the button
*/
Moderation.toggleReservationStatus = function(status, type) {
Moderation.appendSwitchLoader(Moderation.modResPsubsBtn['element']);
switch (type) {
case 'sub':
var max_res = $('input[name=max_res]').val();
var res_period_type = $('input[type=radio][name=res_period_type]:checked').val();
Moderation.data = new FormData();
Moderation.data.append('type','reservation_psub');
Moderation.data.append('max_res',max_res? max_res: 0);
Moderation.data.append('res_period_type',res_period_type? res_period_type: 0);
Moderation.saveSubSettings();
break;
default:
if (status == false && $(Moderation.modNshowPsubsBtn['element']).prop("checked") == false) {
Moderation.toggleDependentButtonsStatus(
`${Moderation.modEventTypeBtn['element']}, ${Moderation.modEventLocationBtn['element']}, ${Moderation.modEventMonthBtn['element']}`,
"off", true
);
}
if (status == true && $(Moderation.modNshowPsubsBtn['element']).prop("checked") == false) {
Moderation.toggleDependentButtonsStatus(
`${Moderation.modEventTypeBtn['element']}, ${Moderation.modEventLocationBtn['element']}, ${Moderation.modEventMonthBtn['element']}`,
"on", false
);
}
Moderation.toggleCollapse(Moderation.modResPsubsBtn['collapse_element'], status);
Moderation.saveMainSettings();
}
};
/******** MODERATION EVENT TYPE ********/
/**
* turns the moderation event type setting on or off
* @param status = status of the button
*/
Moderation.toggleModEventTypeStatus = function(status, type) {
Moderation.appendSwitchLoader(Moderation.modEventTypeBtn['element']);
switch (type) {
case 'sub':
var selected_event_cats = [];
/**
* Store checked event ID's
*/
$.each($('input[name=event_cat]:checked'), function(){
selected_event_cats.push($(this).val());
});
Moderation.data = new FormData();
Moderation.data.append('type','event_category');
Moderation.data.append('event_cats', selected_event_cats.join(","));
Moderation.saveSubSettings();
break;
default:
Moderation.toggleCollapse(Moderation.modEventTypeBtn['collapse_element'], status);
Moderation.saveMainSettings();
}
};
/******** MODERATION EVENT LOCATION ********/
/**
* turns the moderation event location setting on or off
* @param status = status of the button
*/
Moderation.toggleModEventLocationStatus = function(status, type) {
Moderation.appendSwitchLoader(Moderation.modEventLocationBtn['element']);
switch (type) {
case 'sub':
var selected_loc_ids = [];
/**
* Store checked event ID's
*/
$.each($('input[name=event_location]:checked'), function(){
selected_loc_ids.push($(this).val());
});
Moderation.data = new FormData();
Moderation.data.append('type','event_location');
Moderation.data.append('event_locations', selected_loc_ids.join(","));
Moderation.saveSubSettings();
break;
default:
Moderation.toggleCollapse(Moderation.modEventLocationBtn['collapse_element'], status);
Moderation.saveMainSettings();
}
};
/******** MODERATION EVENT MONTH ********/
/**
* turns the moderation event month setting on or off
* @param status = status of the button
*/
Moderation.toggleModEventMonthStatus = function(status, type) {
Moderation.appendSwitchLoader(Moderation.modEventMonthBtn['element']);
switch (type) {
case 'sub':
var selected_month_ids = [];
/**
* Store checked event ID's
*/
$.each($('input[name=event_month]:checked'), function(){
selected_month_ids.push($(this).val());
});
Moderation.data = new FormData();
Moderation.data.append('type','event_month');
Moderation.data.append('event_months', selected_month_ids.join(","));
Moderation.saveSubSettings();
break;
default:
Moderation.toggleCollapse(Moderation.modEventMonthBtn['collapse_element'], status);
Moderation.saveMainSettings();
}
};
/******** FLEXIBLE MODERATION ********/
/**
* turns the flexible moderation setting on or off
* @param status = status of the button
*/
Moderation.toggleFlexibleModerationStatus = function(status) {
Moderation.appendSwitchLoader(Moderation.flexibleModBtn['element']);
if ($(Moderation.mainBtn['element']).prop("checked") == false) {
Moderation.saveFlexibleModerationSettings(status, 'all');
} else {
if (status == false) {
app.modal.confirm_box({
"message" : `Etes vous sûr de vouloir désactiver la modération flexible?
Note: Cette action désactivera toute les
modérations flexibles déjà paramétrées sur
tous les événements.`,
"_continue" : function() {
Moderation.saveFlexibleModerationSettings(false, 'all');
},
"_close" : function() {
$(Moderation.flexibleModBtn['element']).prop("checked", true);
Moderation.toggleCollapse(Moderation.flexibleModBtn['collapse_element'], true);
}
});
} else {
Moderation.saveFlexibleModerationSettings(status, 'all');
}
}
};
/**
* saves the date related to flexible moderation
* @param status = status of the button
* @param type = save all settings or inidividually
*/
Moderation.saveFlexibleModerationSettings = function(status, type) {
Moderation.toggleCollapse(Moderation.flexibleModBtn['collapse_element'], status);
if (type == 'all') {
Moderation.saveMainSettings();
} else {
Moderation.data= new FormData();
var hrs_bef_event_closes = $.trim($('input[name=hrs_bef_event_closes]').val());
var places_volume = $.trim($('input[name=places_volume]').val());
Moderation.data.append('type','flex_moderation');
Moderation.data.append('hrs_bef_event_closes',hrs_bef_event_closes?hrs_bef_event_closes:0);
Moderation.data.append('places_volume',places_volume?places_volume:0);
Moderation.saveSubSettings();
}
};
/**
*
* @param {*} data = list of form data to update in table
*/
Moderation.saveSubSettings = function(){
$.ajax({
url : app.get_vars().baseurl+'saveSubModSettings/2',
type : "POST",
data : Moderation.data,
async: false,
processData: false,
contentType: false,
success : function(result){
}
});
}
/**
*
* @param {*} data = list of form data to update in table
*/
Moderation.saveMainSettings = function(){
Moderation.data= new FormData();
Moderation.data.append('type', 'main_settings');
Moderation.data.append('gm_mod_stat',$(Moderation.mainBtn['element']).prop("checked")?1:0);
Moderation.data.append('noshow_psub_stat',$(Moderation.modNshowPsubsBtn['element']).prop("checked")?1:0);
Moderation.data.append('res_psub_stat',$(Moderation.modResPsubsBtn['element']).prop("checked")?1:0);
Moderation.data.append('event_cat_stat',$(Moderation.modEventTypeBtn['element']).prop("checked")?1:0);
Moderation.data.append('event_loc_stat',$(Moderation.modEventLocationBtn['element']).prop("checked")?1:0);
Moderation.data.append('event_month_stat',$(Moderation.modEventMonthBtn['element']).prop("checked")?1:0);
Moderation.data.append('flex_mod_stat',$(Moderation.flexibleModBtn['element']).prop("checked")?1:0);
$.ajax({
url : app.get_vars().baseurl+'saveModSettings',
type : "POST",
data : Moderation.data,
async: false,
processData: false,
contentType: false,
success : function(result){
// if (Moderation.current_mod_settings.main_settings['is_used'] != 0) {
// Moderation.current_mod_settings = result;
// $(Moderation.modNshowPsubsBtn['subSettings']).trigger("change");
// $(Moderation.modResPsubsBtn['subSettings']).trigger("change");
// $(Moderation.modEventTypeBtn['subSettings']).trigger("change");
// $(Moderation.modEventLocationBtn['subSettings']).trigger("change");
// $(Moderation.modEventMonthBtn['subSettings']).trigger("change");
// $(Moderation.flexibleModBtn['subSettings']).trigger("change");
// }
// app._notify("success","Moderation settings was successfully updated");
if(result.hasErrors){
$("input[name="+result.name+"]").select();
}
}
});
}
/*****************************************************************************
*
* Main execution
*
****************************************************************************/
Moderation.initAllButtonStatus();
})();