From 8bd5e00354abc33ce4a1f0c2a3a1919b56d47fe8 Mon Sep 17 00:00:00 2001 From: Abdelrahman Saeed Elhassan Yousif Date: Mon, 23 Sep 2024 09:29:52 +0200 Subject: [PATCH] Allow template attribute to beused inside email tempaltes --- src/DefaultTokenHelper.php | 52 +++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/DefaultTokenHelper.php b/src/DefaultTokenHelper.php index 4bbe8e9..809223d 100644 --- a/src/DefaultTokenHelper.php +++ b/src/DefaultTokenHelper.php @@ -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 */ @@ -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); } } @@ -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); } @@ -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) {