|
@@ -0,0 +1,85 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\Models\DistAppearancePublishList;
|
|
|
+use App\Models\DistAppearanceTemplate;
|
|
|
+
|
|
|
+class PreviewTemplateUpdater
|
|
|
+{
|
|
|
+ protected static ?string $baseTemplatePath = null;
|
|
|
+
|
|
|
+ * 预览更新模板文件并更新数据库记录
|
|
|
+ *
|
|
|
+ * @param int $dist_id
|
|
|
+ * @param int $appearance_id
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ 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->file_name;
|
|
|
+
|
|
|
+ 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 "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * TODO: 模板内容增加 mtb_id 属性,用于区分不同模板
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|