1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Merge pull request #373 from Destination-Design/feature_email_templating

Reimplemented customer email templating in laravel5 branch.
This commit is contained in:
Hillel Coren 2015-07-15 16:39:10 +03:00
commit 71b8ba5f14
2 changed files with 48 additions and 1 deletions

View File

@ -677,4 +677,29 @@ class Utils
fwrite($output, "\n");
}
public static function stringToObjectResolution($baseObject, $rawPath)
{
$val = '';
if (!is_object($baseObject)) {
return $val;
}
$path = preg_split('/->/', $rawPath);
$node = $baseObject;
while (($prop = array_shift($path)) !== null) {
if (property_exists($node, $prop)) {
$val = $node->$prop;
$node = $node->$prop;
} else if (is_object($node) && isset($node->$prop)) {
$node = $node->{$prop};
} else if ( method_exists($node, $prop)) {
$val = call_user_func(array($node, $prop));
}
}
return $val;
}
}

View File

@ -23,6 +23,8 @@ class ContactMailer extends Mailer
$emailTemplate = $invoice->account->getEmailTemplate($entityType);
$invoiceAmount = Utils::formatMoney($invoice->getRequestedAmount(), $invoice->client->getCurrencyId());
$this->initClosure($invoice);
foreach ($invoice->invitations as $invitation) {
if (!$invitation->user || !$invitation->user->email || $invitation->user->trashed()) {
return false;
@ -40,7 +42,8 @@ class ContactMailer extends Mailer
'$client' => $invoice->client->getDisplayName(),
'$account' => $accountName,
'$contact' => $invitation->contact->getDisplayName(),
'$amount' => $invoiceAmount
'$amount' => $invoiceAmount,
'$advancedRawInvoice->' => '$'
];
// Add variables for available payment types
@ -49,6 +52,7 @@ class ContactMailer extends Mailer
}
$data['body'] = str_replace(array_keys($variables), array_values($variables), $emailTemplate);
$data['body'] = preg_replace_callback('/\{\{\$?(.*)\}\}/', $this->advancedTemplateHandler, $data['body']);
$data['link'] = $invitation->getLink();
$data['entityType'] = $entityType;
$data['invoice_id'] = $invoice->id;
@ -123,4 +127,22 @@ class ContactMailer extends Mailer
$this->sendTo($email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
}
private function initClosure($object)
{
$this->advancedTemplateHandler = function($match) use ($object) {
for ($i = 1; $i < count($match); $i++) {
$blobConversion = $match[$i];
if (isset($$blobConversion)) {
return $$blobConversion;
} else if (preg_match('/trans\(([\w\.]+)\)/', $blobConversion, $regexTranslation)) {
return trans($regexTranslation[1]);
} else if (strpos($blobConversion, '->') !== false) {
return Utils::stringToObjectResolution($object, $blobConversion);
}
}
};
}
}