Skip to content
Snippets Groups Projects
Commit 181fa351 authored by S.Listl's avatar S.Listl
Browse files

Email fixed

parent 3458ece7
No related branches found
No related tags found
No related merge requests found
import("Employee_lib");
import("system.swing");
import("system.text");
import("system.db");
......@@ -12,16 +13,16 @@ import("DocumentTemplate_lib");
/*
* Creates a Address Object
*
* @param {String} pRelationID req relationid for which address should be retrieved
* @param {String} pAddressID opt addressid for which address should be retrieved
* @param {String} pContactId req relationid for which address should be retrieved
* @param {String} pAddressId opt addressid for which address should be retrieved
* @param {boolean} pPerson whether the address is from a person, not an organisation
*
* @return {String} the formatted address
*/
function AddrObject( pRelationID, pPerson, pAddressID )
function AddrObject(pContactId, pPerson, pAddressId)
{
this.Data = fetchAddressData( [ pRelationID ] , [["", "addressformat", ""]], pAddressID, pPerson );
this.Data = fetchAddressData([pContactId], [new Placeholder("", Placeholder.types.ADDRESSFORMAT, "")], pAddressId, pPerson);
this.fmt = this.Data[0][0][26];
/*
......@@ -58,13 +59,13 @@ function getAddressesData( pCondition, pConfig, pSenderID, pAddressID )
{
switch (pConfig[i].target)
{
case PlaceholderUtils.targets.EMPLOYEE:
case Placeholder.targets.EMPLOYEE:
employeeconfig.push(pConfig[i]);
break;
case PlaceholderUtils.targets.SENDER:
case Placeholder.targets.SENDER:
senderconfig.push(pConfig[i]);
break;
case PlaceholderUtils.targets.RECIPIENT:
case Placeholder.targets.RECIPIENT:
default:
config.push(pConfig[i]);
break;
......@@ -75,7 +76,7 @@ function getAddressesData( pCondition, pConfig, pSenderID, pAddressID )
if (senderconfig.length > 0)
var senderdata = getAddressData([pSenderID], senderconfig);
if (employeeconfig.length > 0)
var employeedata = getAddressData([vars.get("$global.user").relationid], employeeconfig);
var employeedata = getAddressData([EmployeeUtils.getCurrentContactId()], employeeconfig);
if (data.length > 0 && (senderconfig.length > 0 || employeeconfig.length > 0))
{
var ze = data[0];
......@@ -141,13 +142,13 @@ function fetchAddressData( pCondition, pConfig, AddressID, pPerson )
{
switch( pConfig[i].type )
{
case PlaceholderUtils.types.SQLPART: //sql part
case Placeholder.types.SQLPART: //sql part
fields.push( pConfig[i].valueDefinition ); //TODO: maybe do vars.resolveVariables
output.push([pos++, pConfig[i].type]);
header.push( pConfig[i].placeholderName );
break;
case PlaceholderUtils.types.SQLPARTFUNCTION: // adito SQL functions
fields.push(pConfig[i].valueDefinition.call());
case Placeholder.types.SQLPARTFUNCTION: // adito SQL functions
fields.push("(" + pConfig[i].valueDefinition.call() + ")");
output.push([pos++, pConfig[i].type]);
header.push( pConfig[i].placeholderName );
break;
......@@ -168,7 +169,7 @@ function fetchAddressData( pCondition, pConfig, AddressID, pPerson )
output.push([pos++, pConfig[i].type]);
header.push( pConfig[i].placeholderName );
break;
case PlaceholderUtils.types.ADDRESSFORMAT:
case Placeholder.types.ADDRESSFORMAT:
if ( posaddrfields == -1 )
{
var sortfields = ["ORGANISATION.NAME", "PERSON.LASTNAME"];
......@@ -266,14 +267,14 @@ function setAddressData( pData )
{
switch( output[z][1] )
{
case PlaceholderUtils.types.SQLPART:
case PlaceholderUtils.types.SQLPARTFUNCTION:
case Placeholder.types.SQLPART:
case Placeholder.types.SQLPARTFUNCTION:
case "afunction":
case "resolveIDFunction":
case "select":
row[z] = sqlresult[i][output[z][0]];
break;
case PlaceholderUtils.types.ADDRESSFORMAT:
case Placeholder.types.ADDRESSFORMAT:
if (addrdata.length == 0) addrdata = _getAddrData( sqlresult[i].slice(output[z][0], output[z][0] + addrfields.length) );
row[z] = _formatAddrData( addrdata, output[z][2], false );
break;
......
import("Communication_lib");
import("system.neon");
import("system.logging");
import("Employee_lib");
import("KeywordRegistry_basic");
import("Document_lib");
......@@ -62,6 +62,13 @@ DocumentTemplate.loadTemplate = function (pTemplateId)
return new DocumentTemplate(); //TODO: throw error?
}
DocumentTemplate._replaceText = function (pText, pReplacements)
{
for (let placeholder in pReplacements)
pText = pText.replace(placeholder, pReplacements[placeholder], "ig");
return pText;
}
DocumentTemplate.prototype.toString = function ()
{
return this.content;
......@@ -83,7 +90,8 @@ DocumentTemplate.prototype.getReplacedContent = function (pReplacements)
for (let i in pReplacements)
pReplacements[i] = text.replaceAll(pReplacements[i], {"\n" : "<br>"});
case DocumentTemplate.types.TXT:
return text.replaceAll(this.content, pReplacements);
let decodedContent = util.decodeBase64String(this.content)
return DocumentTemplate._replaceText(decodedContent, pReplacements);
case DocumentTemplate.types.EML:
return this._getReplacedEML(pReplacements);
case DocumentTemplate.types.ODT:
......@@ -116,24 +124,11 @@ DocumentTemplate.prototype.getReplacedContentByContactId = function (pContactId)
*/
DocumentTemplate.prototype._getReplacedEML = function (pReplacements)
{
var email = mail.parseRFC(this.content, "UTF-8");
var email = mail.parseRFC(util.decodeBase64String(this.content));
var htmlText = email[mail.MAIL_HTMLTEXT];
return text.replaceAll(htmlText, pReplacements);
}
/**
* replaces placeholders for plain text
*
* @param {Object} pReplacements mapping with replacements for every placeholder
*
* @return {String} the replaced content
*/
DocumentTemplate.prototype._getReplacedText = function (pReplacements)
{
return text.replaceAll(this.content, pReplacements);
return DocumentTemplate._replaceText(htmlText, pReplacements);
}
/*
* replaces a given Odt-File on the server and returns the replaced base64-file
*
......@@ -212,10 +207,14 @@ DocumentTemplate.prototype._getReplacedODT = function (pReplacements)
*/
DocumentTemplate.prototype._getReplacedDOCX = function (pReplacements)
{
var replacements = {};
for (let placeholder in pReplacements)
replacements[placeholder.slice(3, -3)] = pReplacements[placeholder];
//this is executed as a process because of better performance
var documentData = process.execute("getDocxDocument_serverProcess", {
templateb64: this.content,
placeholderConfig: JSON.stringify(PlaceholderUtils.removePreAndPostFix(pReplacements)) //process.execute is only able to handle strings
placeholderConfig: JSON.stringify(replacements) //process.execute is only able to handle strings
});
return documentData;
......@@ -238,13 +237,27 @@ DocumentTemplateUtils.getSingleReplacedDocument = function (pTemplateId, pContac
return template.getReplacedTextByContactId(pContactId);
}
/**
* represents a placeholder
*
* @param {String} pName name of the placeholder (without prefix and postfix)
* @param {String} pType type of the placeholder, see PlaceholderUtils.types
* @param {String|Function} pValueDef string or function (depends on the type) defining the value
* @param {String} [pTarget] what contact is used to get the data, see PlaceholderUtils.targets
*/
function Placeholder (pName, pType, pValueDef, pTarget)
{
this.placeholderName = PlaceholderUtils.formatPlaceholder(pName);
this.type = pType;
this.target = pTarget || Placeholder.targets.RECIPIENT;
this.valueDefinition = pValueDef;
}
function PlaceholderUtils () {}
/**
* placeholder types, defines how the value is acquired
*/
PlaceholderUtils.types = {
Placeholder.types = {
ADDRESSFORMAT : "ADDRESSFORMAT",
SQLPART : "SQLPART",
SQLPARTFUNCTION : "SQLPARTFUNCTION"
......@@ -252,76 +265,72 @@ PlaceholderUtils.types = {
/**
* placeholder targets, defines whose data is used
*/
PlaceholderUtils.targets = {
Placeholder.targets = {
/**
* use the data of the recipient (default)
*/
RECIPIENT : "RECIPIENT",
/**
* use the data of the sender
*/
SENDER : "SENDER",
/**
* use the data of the user
*/
EMPLOYEE : "EMPLOYEE"
};
function PlaceholderUtils () {}
/**
* Returns the placeholder with the required prefix and postfix added.
* This function defines the format for placeholders.
*/
PlaceholderUtils.formatPlaceholder = function (pPlaceholder)
PlaceholderUtils.formatPlaceholder = function (pPlaceholder, pEscapeForRegex)
{
return "{@" + pPlaceholder + "@}";
}
/**
* Removes the prefix and postfix from the pReplacements map. This is required for the docxTemplater
* where they are added separately
*/
PlaceholderUtils.removePreAndPostFix = function (pReplacements)
{
var unformattedReplacements = {};
for (let placeholder in pReplacements)
{
let unformatted = placeholder.slice(2, -2);
unformattedReplacements[unformatted] = pReplacements[placeholder]
}
return unformattedReplacements;
}
/**
* gets the placeholder configuration
* placeholder configuration
*
* @return {Array} array of placeholders
*/
PlaceholderUtils.getPlaceholders = function ()
{
//these functions should make adding placeholders easier and require less code:
/**
* represents a placeholder
*
* @param {String} pName name of the placeholder (without prefix and postfix)
* @param {String} pType type of the placeholder, see PlaceholderUtils.types
* @param {String|Function} pValueDef string or function (depends on the type) defining the value
* @param {String} [pTarget] what contact is used to get the data, see PlaceholderUtils.targets
* add an address format placeholder to placeholders
*/
function Placeholder (pName, pType, pValueDef, pTarget)
{
this.placeholderName = PlaceholderUtils.formatPlaceholder(pName);
this.type = pType;
this.target = pTarget || PlaceholderUtils.targets.RECIPIENT;
this.valueDefinition = pValueDef;
}
function _addAddressFormat (pName, pFormat, pTarget)
{
placeholders.push(new Placeholder(pName, PlaceholderUtils.types.ADDRESSFORMAT, pFormat, pTarget));
placeholders.push(new Placeholder(pName, Placeholder.types.ADDRESSFORMAT, pFormat, pTarget));
}
function _addSqlPart (pName, pSqlPart, pAddBraces)
/**
* Add a sub-sql placeholder to placeholders. For further information regarding the full query, you can take
* a look at the function 'fetchAddressData' in Address_lib. You can use fields from CONTACT, ADDRESS, ORGANISATION and PERSON.
*/
function _addSqlPart (pName, pSqlPart, pTarget, pAddBraces)
{
placeholders.push(new Placeholder(pName, PlaceholderUtils.types.SQLPART, pAddBraces ? "(" + pSqlPart + ")" : pSqlPart));
placeholders.push(new Placeholder(pName, Placeholder.types.SQLPART, pAddBraces ? "(" + pSqlPart + ")" : pSqlPart, pTarget));
}
/**
* Add a placeholder to placeholders with a function that returns a sub-sql. The function will be called
* with no parameters. You have to deliver an actual function, don't call it here.
*/
function _addSqlPartFunction (pName, pSqlPartFunction, pTarget)
{
placeholders.push(new Placeholder(pName, PlaceholderUtils.types.SQLPARTFUNCTION, pSqlPartFunction, pTarget));
placeholders.push(new Placeholder(pName, Placeholder.types.SQLPARTFUNCTION, pSqlPartFunction, pTarget));
}
var placeholders = [];
//placeholders should be added here:
_addAddressFormat("address", "{street} {buildingno}");
_addAddressFormat("zipCode", "{zip}");
_addAddressFormat("city", "{city}");
......@@ -330,10 +339,18 @@ PlaceholderUtils.getPlaceholders = function ()
_addAddressFormat("country", "{country}");
_addAddressFormat("letterSalutation", "{letter_salutation}");
_addAddressFormat("fullAddress", "");
_addAddressFormat("senderOrgname", "{organisation_name}", PlaceholderUtils.targets.SENDER);
_addAddressFormat("senderAddress", "{street} {buildingno}", PlaceholderUtils.targets.SENDER);
_addAddressFormat("senderZipCity", "{country} - {zip} {city}", PlaceholderUtils.targets.SENDER);
_addAddressFormat("senderFullAddress", "", PlaceholderUtils.targets.SENDER);
_addAddressFormat("senderOrgname", "{organisation_name}", Placeholder.targets.SENDER);
_addAddressFormat("senderAddress", "{street} {buildingno}", Placeholder.targets.SENDER);
_addAddressFormat("senderZipCity", "{country} - {zip} {city}", Placeholder.targets.SENDER);
_addAddressFormat("senderFullAddress", "", Placeholder.targets.SENDER);
_addSqlPart("orgname", "ORGANISATION.NAME");
_addSqlPartFunction("phone", CommUtil.getStandardSubSqlPhone);
_addSqlPartFunction("email", CommUtil.getStandardSubSqlMail);
_addSqlPartFunction("senderPhone", CommUtil.getStandardSubSqlPhone, Placeholder.targets.SENDER);
_addSqlPartFunction("senderEmail", CommUtil.getStandardSubSqlMail, Placeholder.targets.SENDER);
return placeholders;
}
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment