12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Services;
- use App\Models\DistAppearancePublishList;
- use App\Models\DistAppearanceTemplate;
- class PreviewTemplateUpdater
- {
- protected static ?string $baseTemplatePath = null;
-
- public static function updateTemplates($dist): string
- {
- if(!$dist->appearance_id)
- {
- return "No appearance found for dist_id: $dist->id.";
- }
-
- $template_dist_id = $dist->id ?? trim(config('liquid.template_dist_id'));
- $template_name = $dist->appearance->folder;
- if (self::$baseTemplatePath === null) {
- self::$baseTemplatePath = rtrim(config('liquid.preview_template_path'), '/') . '/' .
- trim($template_dist_id). '/' .
- ltrim($template_name, '/');
- }
-
- $templates = DistAppearanceTemplate::where('dist_id', $dist->id)
- ->where('appearance_id', $dist->appearance_id)
- ->get();
- if ($templates->isEmpty()) {
- return "No templates found in dist_appearance_template for dist_id: $dist->id, appearance_id: $dist->appearance_id.";
- }
-
- foreach ($templates as $template) {
- $content = self::contentChange($template->content,$template->template_code);
-
-
- $filePath = self::$baseTemplatePath.'/'.formatDirectory($template->file_path).$template->file_name;
-
- formatAndCreateAbsoluteDirectory($filePath);
-
- $contentUtf8 = mb_convert_encoding($content, 'UTF-8', 'auto');
-
- file_put_contents($filePath, $contentUtf8);
- }
- return "";
- }
-
- public static function contentChange($content,$templateCode)
- {
- $count = 1;
- $newContent = preg_replace_callback(
- '/(<[^>]+?mtb_edit=[\'"][^\'"]+[\'"][^>]*>)/',
- function ($matches) use (&$count, $templateCode) {
-
- return preg_replace('/(mtb_edit=[\'"][^\'"]+[\'"])([^>]*)>/', '$1 mtb_id="' . $templateCode .'_'. $count++ . '"$2>', $matches[0]);
- },
- $content
- );
- return $newContent;
- }
- }
|