Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
marco76tv authored and Malebestia committed Sep 4, 2023
1 parent 3c80d41 commit 1a30cf8
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions Actions/ApplyModelAttrsToThemeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
namespace Modules\Notify\Actions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Modules\Notify\Models\NotifyTheme;
use Spatie\QueueableAction\QueueableAction;

class ApplyModelAttrsToThemeAction {
class ApplyModelAttrsToThemeAction
{
use QueueableAction;

public function __construct() {
public function __construct()
{
}

/**
* Execute the action.
*/
public function execute(Model $model, string $name, array $extra = []): NotifyTheme {

public function execute(Model $model, string $name, array $extra = [], ?Collection $options): NotifyTheme
{

$theme = NotifyTheme::firstOrCreate(
[
'lang' => app()->getLocale(),
Expand All @@ -37,23 +41,46 @@ public function execute(Model $model, string $name, array $extra = []): NotifyTh
$theme->save();
}

if (!empty($options)) {
$string_to_replace = \Str::between($theme->body_html, '##foreach##', '##endforeach##');
$items='';
foreach ($options as $option) {
$extra = [
'highlights' => collect($option->highlights)->pluck('txt')->implode('<hr/>'),
];

$item = $this->replacer($option, $extra, $string_to_replace);
$items.= $item;
}

$theme->body_html = \str_replace($string_to_replace, $items, $theme->body_html);
}

$data = $model->toArray();

$data = array_merge($data, $extra);

$html = $theme->body_html;
$html = $this->replacer($model, $extra, $html);

$theme->mail_body = $html;

return $theme;
}

public function replacer($model, $extra, $html)
{
$data = $model->toArray();
$data = array_merge($data, $extra);

foreach ($data as $k => $v) {
if (\is_string($v)) {
$html = str_replace('##'.$k.'##', $v, $html);

$html = str_replace('##' . $k . '##', $v, $html);
}
}

$html = preg_replace('/##\w+##/', 'N.D.', $html);

$theme->mail_body = $html;

// dddx($theme->mail_body);

return $theme;
return $html;
}
}

0 comments on commit 1a30cf8

Please sign in to comment.