Skip to content

Commit

Permalink
Allow template attribute to beused inside email tempaltes
Browse files Browse the repository at this point in the history
  • Loading branch information
abdosaeedelhassan committed Sep 23, 2024
1 parent 94312c3 commit 8bd5e00
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/DefaultTokenHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

use Illuminate\Support\Facades\View;
use Visualbuilder\EmailTemplates\Contracts\TokenReplacementInterface;
use Visualbuilder\EmailTemplates\Models\EmailTemplate;


class DefaultTokenHelper implements TokenReplacementInterface
{

/**
* Replace tokens in the content with actual values from the models.
*
* @param string $content The content with tokens to be replaced
* @param array $models The models containing the values for the tokens
* @param string $content The content with tokens to be replaced
* @param array $models The models containing the values for the tokens
*
* @return string The content with replaced tokens
*/
Expand All @@ -25,26 +25,25 @@ public function replaceTokens(string $content, $models): string
* Replace singular tokens for password reset and validations
* Add custom tokens in the config
*/
foreach (config('filament-email-templates.known_tokens') as $key){
foreach (config('filament-email-templates.known_tokens') as $key) {
if (isset($models->{$key})) {
$content = str_replace("##$key##", $models->{$key}, $content);
}
}

/**
* Replace model-attribute tokens.
* Will look for pattern ##model.attribute## and replace the value if found.
* Replace model-attribute and template attribute tokens.
* Will look for pattern ##model.attribute## or ##attribute## and replace the value if found.
* Eg ##user.name## or create your own accessors in a model
*/
preg_match_all('/##(.*?)\.(.*?)##/', $content, $matches);
preg_match_all('/##(.*?)##/', $content, $matches);

if (count($matches) > 0 && count($matches[0]) > 0) {
for ($i = 0; $i < count($matches[0]); $i++) {
$modelKey = $matches[1][$i];
$attributeKey = $matches[2][$i];
$replacement = (isset($models->$modelKey) && isset($models->$modelKey->$attributeKey))?$models->$modelKey->$attributeKey:"";
$content = str_replace($matches[0][$i], $replacement, $content);
if (count($matches) > 0 && count($matches[1]) > 0) {

for ($i = 0; $i < count($matches[1]); $i++) {
$attributes = explode('.', $matches[1][$i]);
$replacement = $this->getObjectAttribute($models, $attributes);
$content = str_replace($matches[0][$i], $replacement, $content);
}
}

Expand All @@ -68,7 +67,7 @@ public function replaceTokens(string $content, $models): string
}


if(isset($models->emailTemplate)){
if (isset($models->emailTemplate)) {
$button = $this->buildEmailButton($content, $models->emailTemplate);
$content = self::replaceButtonToken($content, $button);
}
Expand All @@ -77,15 +76,32 @@ public function replaceTokens(string $content, $models): string
return $content;
}

/**
* @Access model attribute using attribute path
*/
public function getObjectAttribute($object, $path)
{
foreach ($path as $key) {
if (is_object($object) && isset($object->$key)) {
$object = $object->$key; // Move to the next level in the object
} elseif (is_array($object) && isset($object[$key])) {
$object = $object[$key]; // Move to the next level in the array
} else {
return null; // Return null if the path is invalid
}
}
return $object; // Return the final value
}

private function buildEmailButton($content, $emailTemplate)
{
$title = $url = '';
if (preg_match('/(?<=##button).*?(?=#)/', $content, $matches)) {
if ($check1 = preg_match("/(?<=url=').*?(?='\s)/", $matches[ 0 ], $url)) {
$url = $url[ 0 ];
if ($check1 = preg_match("/(?<=url=').*?(?='\s)/", $matches[0], $url)) {
$url = $url[0];
}
if ($check2 = preg_match("/(?<=title=').*?(?=')/", $matches[ 0 ], $title)) {
$title = $title[ 0 ];
if ($check2 = preg_match("/(?<=title=').*?(?=')/", $matches[0], $title)) {
$title = $title[0];
}
if ($check1 && $check2) {

Expand Down

0 comments on commit 8bd5e00

Please sign in to comment.