Files
crm.twinpol.com/modules/EcmAgreements/javascript/AgreementTemplates.js
2025-05-12 15:45:17 +00:00

454 lines
12 KiB
JavaScript
Executable File

$(document).ready(function () {
CKEDITOR.replace('template');
getListCodeTemplate();
$("#pdf_template_selected").val($('#templates').val());
$('#templates').change(function () {
$("#pdf_template_selected").val($('#templates').val());
getCodeTemplate();
});
$("#save").on('click',saveCodeTemplate);
$("#add").on('click',addCodeTemplate);
$("#edit").on('click',editLabel);
$("#remove").on("click",removeCodeTemplate);
$("#edit_save").on("click", editLabelCodeTemplate);
$("#add_save").on('click',addSaveCodeTemplate);
$("#preview_pdf").on('click',getPDFpreview);
$(".margin").on("change", marginChange);
});
function getPDFpreview(){
var id_template = $("#templates").val();
var html_template = CKEDITOR.instances['template'].getData();
margin_top = $("#margin_top").val();
margin_bottom = $("#margin_bottom").val();
margin_left = $("#margin_left").val();
margin_right = $("#margin_right").val();
zapisane_pozycje = [];
$.each($("#parseFieldsTable").find("input"),function (key,value){
zapisane_pozycje.push($(value).val());
});
aditional_fields = [];
$.each($('#pdf_template_aditional_fields').appendGrid('getAllValue'), function (key, row){
if(row.value0!=''){
row.value0 = trim(row.value0);
if(row.value0!=''){
var re = new RegExp(' ', 'g');
row.value0 = row.value0.replace(re, '_');
sprawdz_czy_juz_zapisana = false;
$.each(aditional_fields, function (k,v){
if(row.value0==v.value0){
sprawdz_czy_juz_zapisana = true;
}
});
if(!sprawdz_czy_juz_zapisana){
aditional_fields.push(row);
}
}
}
});
$('#pdf_template_aditional_fields').appendGrid('load',aditional_fields);
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "html",
async: false,
data: {
job: "saveCodeTemplate",
id_template: id_template,
html_template: html_template,
margin_top: margin_top,
margin_bottom: margin_bottom,
margin_left: margin_left,
margin_right: margin_right,
aditional_fields: aditional_fields,
},
success: function (data) {
refreshPraseList();
getCodeTemplate();
},
error: function (){
alert("Coś poszło nie tak... Nie udało się wygenerować podglądu");
return;
},
});
var id_template = $('#pdf_template_selected').val();
console.log(id_template);
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=createPDF&to_pdf=1",
dataType: "html",
data: {
generateFromTeplateExample: id_template,
record: 1,
},
success: function (data) {
window.open("pdfexample.pdf");
}
});
}
function refreshPraseList(){
var id_template= $("#templates").val();
console.log(id_template);
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "json",
data: {
job: "getParseFields",
id_template : id_template,
},
success: function(data) {
$("#parseFieldsTable").find('tr').remove();
$.each(data, function(key, value) {
$("#parseFieldsTable").append('<tr><td style="border-bottom:1pt solid black;"><b>' + value + '</b></td><td style="border-bottom:1pt solid black;" ><input type="text" onClick="this.select();" value="$' + key + '$"/></td></tr>');
});
}
});
}
function InitializeAdidionalFields(initData) {
// Initialize appendGrid
$('#pdf_template_aditional_fields').appendGrid({
hideRowNumColumn: false,
initRows: 1,
initData:initData,
columns: [
{
name: 'value0',
display: SUGAR.language.get("EcmAgreements", "LBL_NAME"),
type: 'text',
displayCss: {
'vertical-align': 'middle'
},
ctrlCss: {
'text-align': 'left'
},
ctrlClass: 'inputs',
onChange: function (evt, rowIndex) {
var linia = $('#pdf_template_aditional_fields').appendGrid('getRowValue', rowIndex);
var nazwa = '$' +linia.value0+ '$';
$.each($("#parseFieldsTable").find("input"),function (key,value){
if( nazwa ==$(value).val()){
alert("Podana nazwa jest już używana, proszę wybrać inną.");
}
});
}
},{
name: 'value3',
display: SUGAR.language.get("EcmAgreements", "LBL_DEFAULT_VALUE"),
type: 'text',
displayCss: {
'vertical-align': 'middle'
},
ctrlCss: {
'text-align': 'left'
},
ctrlClass: 'inputs',
},{
name: 'value2',
display: 'relatedTemplate',
type: 'hidden',
value: $("#templates").val(),
}, {
name: 'id',
display: 'id',
type: 'hidden',
}, {
name: 'name',
display: 'name',
type: 'hidden',
value : 'AgreementTemplatesAditionalFields',
}, {
name: 'module_name',
display: 'module_name',
type: 'hidden',
value : 'EcmAgreements',
},
],
i18n: {
append: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_APPEND"),
remove: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_REMOVE"),
insert: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_INSERT"),
moveUp: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_MOVEUP"),
moveDown: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_MOVEDOWN"),
removeLast: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_REMOVELAST"),
rowEmpty: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_ROWEMPTY"),
rowDrag: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_ROWDRAG"),
},
customGridButtons: {
insert: function () {
var button = document.createElement('img');
button.src = 'modules/EcmQuotes/images/add_position.gif';
return button;
},
remove: function () {
var button = document.createElement('img');
button.src = 'modules/EcmQuotes/images/deleterow.gif';
return button;
},
moveUp: function () {
var button = document.createElement('img');
button.src = 'modules/EcmQuotes/images/moverowup.gif';
return button;
},
moveDown: function () {
var button = document.createElement('img');
button.src = 'modules/EcmQuotes/images/moverowdown.gif';
return button;
},
},
afterRowRemoved: function (caller, rowIndex) {
var quantity = $('#pdf_template_aditional_fields').appendGrid(
'getRowCount');
if (quantity == 0) {
$('#pdf_template_aditional_fields').appendGrid('appendRow', 1);
}
},
hideButtons: {
removeLast: true,
append: true,
moveUp: true,
moveDown: true
}
});
}
function hide(){
$("#edit_div").hide();
$("#add_div").hide();
}
function marginChange(){
var value = $(this).val();
if(value==''){
$(this).val(0);
return;
}
if(value<0){
$(this).val(0);
return;
}
if(value>100){
$(this).val(100);
return;
}
}
function getListCodeTemplate() {
var selected = $("#pdf_template_selected").val();
$('#templates').find('option').remove();
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "json",
data: {
job: "getListCodeTemplate",
},
success: function(data) {
console.log(selected);
$.each(data, function(key, value) {
$("#templates").append('<option value="' + key + '">' + value + '</option>');
});
if(selected!=''){
$("#templates").val(selected);
}else{
$("#templates").val($("#templates option:first").val());
$('#pdf_template_selected').val($("#templates").val());
}
getCodeTemplate();
}
});
}
function getCodeTemplate() {
var id = $('#templates').val();
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "json",
async: false,
data: {
job: "getCodeTemplate",
id: id,
},
success: function (data) {
CKEDITOR.instances['template'].setData(data.pdf_template);
$("#margin_top").val(data.margin_top);
$("#margin_bottom").val(data.margin_bottom);
$("#margin_left").val(data.margin_left);
$("#margin_right").val(data.margin_right);
refreshPraseList();
InitializeAdidionalFields(data.aditional_fields);
}
});
}
function saveCodeTemplate() {
var id_template = $("#templates").val();
var html_template = CKEDITOR.instances['template'].getData();
margin_top = $("#margin_top").val();
margin_bottom = $("#margin_bottom").val();
margin_left = $("#margin_left").val();
margin_right = $("#margin_right").val();
zapisane_pozycje = [];
$.each($("#parseFieldsTable").find("input"),function (key,value){
zapisane_pozycje.push($(value).val());
});
aditional_fields = [];
$.each($('#pdf_template_aditional_fields').appendGrid('getAllValue'), function (key, row){
if(row.value0!=''){
row.value0 = trim(row.value0);
if(row.value0!=''){
var re = new RegExp(' ', 'g');
row.value0 = row.value0.replace(re, '_');
sprawdz_czy_juz_zapisana = false;
$.each(aditional_fields, function (k,v){
if(row.value0==v.value0){
sprawdz_czy_juz_zapisana = true;
}
});
if(!sprawdz_czy_juz_zapisana){
aditional_fields.push(row);
}
}
}
});
$('#pdf_template_aditional_fields').appendGrid('load',aditional_fields);
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "html",
async: false,
data: {
job: "saveCodeTemplate",
id_template: id_template,
html_template: html_template,
margin_top: margin_top,
margin_bottom: margin_bottom,
margin_left: margin_left,
margin_right: margin_right,
aditional_fields: aditional_fields,
},
success: function (data) {
refreshPraseList();
getCodeTemplate();
alert("Zapisano pomyślnie");
},
error: function (){
alert("Coś poszło nie tak... Zmiany nie zostały zapisane");
},
});
}
function addCodeTemplate(){
$("#add_div").show();
}
function addSaveCodeTemplate(){
var label = trim($("#add_label").val());
if(label == ''){
alert("Nie podano nazwy szablonu");
return false;
}
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "html",
async: false,
data: {
job: "addSaveCodeTemplate",
label: label,
},
success: function (data) {
if(data=='0'){
alert('Coś poszło nie tak. Nie zapisano żadnych zmian.');
return;
}
if(data=='1'){
alert('Podana nazwa już istnieje,wybierz inną');
return;
}
$("#pdf_template_selected").val(data);
alert('Pomyślnie dodano szablon.');
getListCodeTemplate();
},
error: function (){
alert("Coś poszło nie tak..<br>Zmiany nie zostały zapisane");
},
});
$("#add_label").val('');
$("#add_div").hide();
}
function editLabel(){
$("#edit_label").val($("#templates option:selected").html());
$("#edit_div").show();
}
function editLabelCodeTemplate(){
var id_template = $("#templates").val();
var label = trim($("#edit_label").val());
if(label == ''){
alert("Nie podano nazwy szablonu");
return false;
}
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "html",
async: false,
data: {
job: "editLabelCodeTemplate",
label: label,
id_template: id_template,
},
success: function (data) {
alert(data);
if(data=="Pomyślnie zmieniono nazwę szablonu."){
getListCodeTemplate();
}
},
error: function (){
alert("Coś poszło nie tak..<br>Zmiany nie zostały zapisane");
},
});
$("#edit_div").val('');
$("#edit_div").hide();
}
function removeCodeTemplate(){
var zgoda = confirm("Czy na pewno usunąć szablon?\nNie będzie można tego cofnąć bez kontaktu z administratorem.");
if(!zgoda){
return;
}
var id_template = $("#templates").val();
$("#pdf_template_selected").val('');
$.ajax({
type: "POST",
url: "index.php?module=EcmAgreements&action=dbpost&to_pdf=1",
dataType: "html",
async: false,
data: {
job: "removeCodeTemplate",
id_template: id_template,
},
success: function (data) {
getListCodeTemplate();
alert(data);
},
error: function (){
alert("Coś poszło nie tak..<br>Zmiany nie zostały zapisane");
},
});
}