Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement - Formularios web - Plantilla personalizada de email para usuario asignado #27

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 91 additions & 12 deletions modules/stic_Web_Forms/Catcher/Donation/DonationMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ public function sendAdminMail($objWeb, $payment, $formParams, $donator, $candida
break;
case DonationBO::DONATOR_NEW:
case DonationBO::DONATOR_UNIQUE:
$html = $this->newDonationMail($objWeb, $payment, $formParams, $donator, $donatorResult == DonationBO::DONATOR_NEW);

// Function that verify if the form have the 'custom_assigned_email_template' input
if(!empty($_REQUEST['custom_assigned_email_template'])) {
return $this->sendAssignedUserMail($_REQUEST['custom_assigned_email_template'], $objWeb, $payment);
// If the form doesn't have the input send the generic email
} else {
$html = $this->newDonationMail($objWeb, $payment, $formParams, $donator, $donatorResult == DonationBO::DONATOR_NEW);
}

break;
}

Expand All @@ -74,9 +82,9 @@ public function sendAdminMail($objWeb, $payment, $formParams, $donator, $candida
}
$html .= '</table><br><br>';
}

$this->body = $html;
$this->subject = "{$payment->transaction_code} - {$this->subject}";

return $this->send();
}

Expand Down Expand Up @@ -230,6 +238,35 @@ private function webObjectToHtml($objWeb, $formParams)
return $html;
}

/**
* Function to parse the email
*
* @param $templateId id of the template
* @param $payment data of the payment
* @param $replacementObjects array with the object
* @param $lang
* @return void
*/
public function parsingEmail($templateId, $payment, $replacementObjects, $lang){
$payment = $replacementObjects[1];

// Function to get the object
if ($payment->load_relationship('stic_payments_stic_payment_commitments')) {
$relatedBeans = $payment->stic_payments_stic_payment_commitments->getBeans();
foreach ($relatedBeans as $fpBean) {
$replacementObjects[] = $fpBean;
}
}

// Parse the template
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Parsing template [{$templateId}]...");

if (false === parent::parseEmailTemplateById($templateId, $replacementObjects, $lang)) {
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error parsing the template.");
return false;
}
}

/**
* Send the notification email to the registered user
*/
Expand All @@ -251,21 +288,63 @@ public function sendUserMail($templateId, $objWeb, $payment, $lang = null)
$replacementObjects = array();
$replacementObjects[0] = $objWeb;
$replacementObjects[1] = $payment;
if ($payment->load_relationship('stic_payments_stic_payment_commitments')) {
$relatedBeans = $payment->stic_payments_stic_payment_commitments->getBeans();
foreach ($relatedBeans as $fpBean) {
$replacementObjects[] = $fpBean;
}

// Function to parse the email
$this->parsingEmail($templateId, $replacementObjects[1], $replacementObjects, $lang);

// Send the mail
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Sending mail...");

// If there's a template for the user send the mail
if(!empty($templateId)) {
return $this->send();
}
}

/**
* Send the notification email to the assigned user
*
* @param $templateId id of the template
* @param $objWeb data from the form
* @param $payment data of the payment
* @param $lang
* @return void
*/
public function sendAssignedUserMail($templateId, $objWeb, $payment, $lang = null)
{
// Reset the recipient list
$this->resetDest();

// Parse the template
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Parsing template...");
// Add the recipient
$user = BeanFactory::getBean('Users', $_REQUEST['assigned_user_id']);
// Use the primary address of the assigned user
$userEmail = $user->emailAddress->getPrimaryAddress($user);

if (false === parent::parseEmailTemplateById($templateId, $replacementObjects, $lang)) {
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error parsing the template.");
return false;
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Adding recipient [{$userEmail}] ...");
$this->addMailsDest($userEmail);

// Get the Contact from the CRM
include_once 'SticInclude/Utils.php';
$contactBean = SticUtils::getRelatedBeanObject($payment, 'stic_payments_contacts');

// Build the array of objects to parse
$replacementObjects = array();
$replacementObjects[0] = $objWeb;
$replacementObjects[1] = $payment;
$replacementObjects[2] = $user;
$replacementObjects[3] = $contactBean;

// If there is an attached document it is added to the array
if(!empty($contactBean->documents)){
$documents = $contactBean->documents->tempBeans;
foreach($documents as $key => $valueDocument) {
$replacementObjects[4] = $valueDocument;
}
}

// Function to parse the email
$this->parsingEmail($templateId, $replacementObjects[1], $replacementObjects, $lang);

// Send the mail
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Sending mail...");
return $this->send();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ public function sendAdminMail()
break;
case EventInscriptionBO::CONTACT_NEW:
case EventInscriptionBO::CONTACT_UNIQUE:
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Generating information for CONTACT_NEW or CONTACT_UNIQUE");
$html .= $this->newObjectBodyHTML($objWeb, $formParams, $contactObject, $contactResult == EventInscriptionBO::CONTACT_NEW);

// Function that verify if the form have the 'custom_assigned_email_template' input
if(!empty($_REQUEST['custom_assigned_email_template'])) {
return $this->sendAssignedUserMail($_REQUEST['custom_assigned_email_template'], $objWeb, $this->eventInscriptionBO->getEvent(), $this->eventInscriptionBO->getInscriptionObject(), null, $this->payment);
// If the form doesn't have the input send the generic email
} else {
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Generating information for CONTACT_NEW or CONTACT_UNIQUE");
$html .= $this->newObjectBodyHTML($objWeb, $formParams, $contactObject, $contactResult == EventInscriptionBO::CONTACT_NEW);
}

break;
}

Expand Down Expand Up @@ -131,7 +139,15 @@ public function sendAdminMail()
break;
case EventInscriptionBO::ACCOUNT_NEW:
case EventInscriptionBO::ACCOUNT_UNIQUE:
$html .= $this->newObjectBodyHTML($objWeb, $formParams, $accountObject, $accountResult == EventInscriptionBO::ACCOUNT_NEW);

// Function that verify if the form have the 'custom_assigned_email_template' input
if(!empty($_REQUEST['custom_assigned_email_template'])) {
return $this->sendAssignedUserMail($_REQUEST['custom_assigned_email_template'], $objWeb, $this->eventInscriptionBO->getEvent(), $this->eventInscriptionBO->getInscriptionObject(), $accountObject, $this->payment);
// If the form doesn't have the input send the generic email
} else {
$html .= $this->newObjectBodyHTML($objWeb, $formParams, $accountObject, $accountResult == EventInscriptionBO::ACCOUNT_NEW);
}

break;
case EventInscriptionBO::ACCOUNT_NO_DATA:
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": The form does not include organizational data.");
Expand Down Expand Up @@ -174,6 +190,41 @@ public function sendUserMail($templateId, $lang = null)
$lang);
}

/**
* Function to parse the email
*
* @param $templateId id of the template
* @param $account data of the account
* @param $payment data of the payment
* @param $replacementObjects array with the object
* @param $lang
* @return void
*/
public function parsingEmail($templateId, $account, $payment, $replacementObjects, $lang){
// Function to get the object
if (!empty($account)) {
$replacementObjects[] = $account;
}

if (!empty($payment)) {
$replacementObjects[] = $payment;
if ($payment->load_relationship('stic_payments_stic_payment_commitments')) {
$relatedBeans = $payment->stic_payments_stic_payment_commitments->getBeans();
foreach ($relatedBeans as $fpBean) {
$replacementObjects[] = $fpBean;
}
}
}

// Parse the template
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Parsing template [{$templateId}]...");

if (false === parent::parseEmailTemplateById($templateId, $replacementObjects, $lang)) {
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error parsing the template.");
return false;
}
}

/**
* Send the notification email to the registered user
*/
Expand All @@ -197,26 +248,73 @@ protected function __sendUserMail($templateId, $objContactWeb, $event, $inscript
$replacementObjects[1] = $event;
$replacementObjects[2] = $inscription;

if (!empty($account)) {
$replacementObjects[] = $account;
// Function to parse the email
$this->parsingEmail($templateId, $account, $payment, $replacementObjects, $lang);

// Send the mail
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Sending mail ...");

// If there's a template for the user send the mail
if(!empty($templateId)) {
return $this->send();
}
}

if (!empty($payment)) {
$replacementObjects[] = $payment;
if ($payment->load_relationship('stic_payments_stic_payment_commitments')) {
$relatedBeans = $payment->stic_payments_stic_payment_commitments->getBeans();
foreach ($relatedBeans as $fpBean) {
$replacementObjects[] = $fpBean;
}

/**
* Send the notification email to the assigned user
*
* @param $templateId id of the template
* @param $objContactWeb data of the contact from the form
* @param $event data of the event
* @param $inscription data of the inscription
* @param $account data of the account if this exist
* @param $payment data of the payment
* @param $lang
* @return void
*/
protected function sendAssignedUserMail($templateId, $objWeb, $event, $inscription, $account = null, $payment, $lang = null)
{
// Reset the recipient list
$this->resetDest();

// Get the candidates from the form
$candidates = $this->eventInscriptionBO->getContactCandidates();
$objWeb = array_pop($candidates);

// Get the accounts of the candidates from the form
$candidates = $this->eventInscriptionBO->getAccountCandidates();
$account = (!empty($candidates) ? array_pop($candidates) : null);

// Add the recipient
$user = BeanFactory::getBean('Users', $_REQUEST['assigned_user_id']);
// Use the primary address of the assigned user
$userEmail = $user->emailAddress->getPrimaryAddress($user);
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Adding recipient [{$userEmail}] ...");
$this->addMailsDest($userEmail);

// Get the Contact from the CRM
include_once 'SticInclude/Utils.php';
$contactBean = SticUtils::getRelatedBeanObject($payment, 'stic_payments_contacts');

// Build the array of objects to parse
$replacementObjects = array();
$replacementObjects[0] = $objWeb;
$replacementObjects[1] = $event;
$replacementObjects[2] = $inscription;
$replacementObjects[3] = $user;
$replacementObjects[4] = $contactBean;

// If there is an attached document it is added to the array
if(!empty($contactBean->documents)){
$documents = $contactBean->documents->tempBeans;
foreach($documents as $key => $valueDocument) {
$replacementObjects[5] = $valueDocument;
}
}

// Parse the template
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Parsing template ...");
if (false === parent::parseEmailTemplateById($templateId, $replacementObjects, $lang)) {
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Error parsing the template.");
return false;
}
// Function to parse the email
$this->parsingEmail($templateId, $account, $payment, $replacementObjects, $lang);

// Send the mail
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Sending mail ...");
Expand Down
28 changes: 22 additions & 6 deletions modules/stic_Web_Forms/Catcher/Include/Mailer/WebFormMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ public function getCampaingData($id, $link = true)
*/
public function parseEmailTemplateById($templateId, $replacementObjects, $lang = null)
{
// Calling the object from the form to parse the entire template
$objWeb = $replacementObjects[0];

if (empty($templateId)) {
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": No ID received.");
return false;
Expand All @@ -325,7 +328,7 @@ public function parseEmailTemplateById($templateId, $replacementObjects, $lang =
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Template with ID [{$templateId}] not found.");
return false;
}
return $this->parseEmailTemplate($template, $replacementObjects, $lang);
return $this->parseEmailTemplate($template, $replacementObjects, $objWeb, $lang);
}

/**
Expand All @@ -338,6 +341,9 @@ public function parseEmailTemplateById($templateId, $replacementObjects, $lang =
*/
public function parseEmailTemplateByName($templateName, $replacementObjects, $lang = null, $type = 'email')
{
// Calling the object from the form to parse the entire template
$objWeb = $replacementObjects[0];

if (empty($templateName)) {
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": No name has been received.");
return false;
Expand All @@ -351,7 +357,7 @@ public function parseEmailTemplateByName($templateName, $replacementObjects, $la
$GLOBALS['log']->error('Line ' . __LINE__ . ': ' . __METHOD__ . ": Template not found with name [{$templateName}]");
return false;
}
return $this->parseEmailTemplate($template, $replacementObjects, $lang);
return $this->parseEmailTemplate($template, $replacementObjects, $objWeb, $lang);
}

/**
Expand All @@ -361,7 +367,7 @@ public function parseEmailTemplateByName($templateName, $replacementObjects, $la
* @param $replacementObjects Array of objects to be parsed
* @return String Mail body in html
*/
protected function parseEmailTemplate($template, $replacementObjects, $lang)
protected function parseEmailTemplate($template, $replacementObjects, $objWeb, $lang)
{
global $current_language, $app_list_strings, $app_strings;

Expand All @@ -385,11 +391,11 @@ protected function parseEmailTemplate($template, $replacementObjects, $lang)
$app_strings = return_application_language($current_language);
$app_list_strings = return_app_list_strings_language($current_language);

$parseArr = array("subject0" => $template->subject, "text0" => $template->body, "html0" => $template->body_html);
$parseArr = array("subject1" => $template->subject, "text1" => $template->body, "html1" => $template->body_html);
$replacementObjectsLength = (empty($replacementObjects) || !is_array($replacementObjects) ? 0 : count($replacementObjects));

$j = 0;
for ($i = 0; $i < $replacementObjectsLength; $i++) {
$j = 1;
for ($i = 1; $i < $replacementObjectsLength ; $i++) {
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Parsing object [{$i}] [{$replacementObjects[$i]->module_dir}] ... ");
$macro_nv = array();
$obj = $this->prepareBean2EmailTemplate($replacementObjects[$i]);
Expand All @@ -402,6 +408,16 @@ protected function parseEmailTemplate($template, $replacementObjects, $lang)
$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Result [{$i}] -> " . $parseArr["html{$j}"]);
}

// Replace on the email template the param of form_contact to contact
$parseArr["html{$i}"] = str_replace('$form_contact', '$contact', $parseArr["html{$i}"]);
$obj = $this->prepareBean2EmailTemplate($objWeb);
$parseArr = $template->parse_email_template($parseArr, $obj->module_dir, $obj, $macro_nv);

// Parse the entire email template again
$parseArr["text{$j}"] = $parseArr["text{$i}"];
$parseArr["html{$j}"] = $parseArr["html{$i}"];
$parseArr["subject{$j}"] = $parseArr["subject{$i}"];

$GLOBALS['log']->debug('Line ' . __LINE__ . ': ' . __METHOD__ . ": Recovering original language files ...");
$current_language = $prev_lang;
$app_strings = return_application_language($current_language);
Expand Down
Loading