DistAppearanceTemplate.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use App\Admin\Repositories\DistAppearanceTemplateLog;
  4. use App\Models\DistAppearanceTemplate as Model;
  5. use App\Models\SiteAppearanceTemplate;
  6. use Dcat\Admin\Repositories\EloquentRepository;
  7. use DOMDocument;
  8. use Illuminate\Support\Carbon;
  9. use Symfony\Component\DomCrawler\Crawler;
  10. class DistAppearanceTemplate extends EloquentRepository
  11. {
  12. /**
  13. * Model.
  14. *
  15. * @var string
  16. */
  17. protected $eloquentClass = Model::class;
  18. /*
  19. * 同步模版到正式表上
  20. */
  21. public static function syncAppearanceTemplates($appearanceId,$distId)
  22. {
  23. $model = new Model();
  24. return $model->syncAppearanceTemplates($appearanceId,$distId);
  25. }
  26. /*
  27. * 得到独立页的模版数组
  28. */
  29. public static function getLandingPageTemplateOptions()
  30. {
  31. $distInfo = DistAdminDistributor::getInfo();
  32. $distId = $distInfo->id;
  33. $appearanceId = $distInfo->appearance_id;
  34. $model = new Model();
  35. $result = $model->where('appearance_id', $appearanceId)->where('dist_id', $distId)->select('file_name')->get();
  36. $options = ['pages_detail.liquid'=>config('dictionary.landing_page_default_template')];
  37. foreach ($result as $key => $value) {
  38. // 检查字符串是否以 "pages_sp_" 开头
  39. if (strpos($value->file_name, 'pages_sp_') === 0) {
  40. // 提取 "xxxx" 部分
  41. // $part = str_replace('pages_sp_', '', $value->file_name);
  42. // $part = str_replace('.liquid', '', $part);
  43. $part = $value->file_name;
  44. $options[$part] = $part;
  45. }
  46. }
  47. return $options;
  48. }
  49. public static function previewSave($data)
  50. {
  51. //echo $data;exit;
  52. $distInfo = DistAdminDistributor::getInfo();
  53. $distId = $distInfo->id;
  54. $appearanceId = $distInfo->appearance_id;
  55. //查找$data中是否存在mtb_id
  56. $inputElements = self::findElementsWithMtbId($data);
  57. foreach ($inputElements as $prefix => $elements) {
  58. //找查找对应的模板
  59. $template = Model::where('dist_id', $distId)->where('appearance_id', $appearanceId)->where('template_code',$prefix)->first();
  60. if ($template) {
  61. //替换模板内容
  62. $oldContent = self::contentChange($template->content,$template->template_code);//旧模板内容
  63. $newContent = self::contentReplace($oldContent,$elements);
  64. //更新模板内容
  65. $template->content = $newContent;
  66. $template->updated_at = Carbon::now();
  67. $template->save();
  68. //记录日志
  69. DistAppearanceTemplateLog::insertLog($appearanceId,$distId,$template->file_name,$template->file_path,$template->template_code,$newContent,$oldContent);
  70. }
  71. }
  72. }
  73. /*
  74. * 替换模板内容
  75. * @param $content 模板内容
  76. * @param $elements Array([mtb_id] => 676a6d11f1a57_1,[outHtml] => <div mtb_id="abc_2"><p>Again3</p></div>)
  77. */
  78. public static function contentReplace($content,$elements) {
  79. foreach ($elements as $element) {
  80. $mtbId = $element['mtb_id'];
  81. $outHtml = $element['outHtml'];
  82. $content = preg_replace_callback(
  83. '/<([a-zA-Z0-9]+)[^>]*mtb_id="' . preg_quote($mtbId, '/') . '"[^>]*>(.*?)<\/\1>/s',
  84. function ($matches) use ($outHtml) {
  85. // 返回替换后的内容
  86. return $outHtml;
  87. },
  88. $content
  89. );
  90. }
  91. return $content;
  92. }
  93. /*
  94. * 查找带有 mtb_id 属性的 HTML 元素
  95. */
  96. public static function findElementsWithMtbId($html) {
  97. // 定义正则表达式来匹配所有带有 mtb_id 属性的 HTML 元素
  98. $pattern = '/<([a-zA-Z0-9-_]+)([^>]*\smtb_id="([^"]+)")([^>]*)>(.*?)<\/\1>/is';
  99. preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
  100. // 存储结果
  101. $result = [];
  102. foreach ($matches as $match) {
  103. // 提取 mtb_id 和匹配的 HTML 内容
  104. $outHtml = $match[0];
  105. //去掉mtb_id属性
  106. $outHtml = preg_replace('/mtb_id="[^"]*"/', '', $outHtml);
  107. $result[] = [
  108. 'mtb_id' => $match[3], // mtb_id 的值
  109. 'outHtml' => $outHtml, // 匹配到的完整 HTML 元素
  110. ];
  111. }
  112. $result = array_reduce($result, function ($carry, $item) {
  113. // 获取 mtb_id 的前缀
  114. $prefix = explode('_', $item['mtb_id'])[0];
  115. // 如果前缀不存在,初始化一个空数组
  116. if (!isset($carry[$prefix])) {
  117. $carry[$prefix] = [];
  118. }
  119. // 将当前项添加到对应的前缀数组中
  120. $carry[$prefix][] = $item;
  121. return $carry;
  122. }, []);
  123. return $result;
  124. }
  125. /*
  126. * TODO: 模板内容增加 mtb_id 属性,用于区分不同模板
  127. */
  128. public static function contentChange($content,$templateCode)
  129. {
  130. $count = 1;
  131. $newContent = preg_replace_callback(
  132. '/(<[^>]+?mtb_edit=[\'"][^\'"]+[\'"][^>]*>)/',
  133. function ($matches) use (&$count, $templateCode) {
  134. // 在匹配到的 HTML 元素后添加 mtb_id 属性
  135. return preg_replace('/(mtb_edit=[\'"][^\'"]+[\'"])([^>]*)>/', '$1 mtb_id="' . $templateCode .'_'. $count++ . '"$2>', $matches[0]);
  136. },
  137. $content
  138. );
  139. return $newContent;
  140. }
  141. }