123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace App\Distributor\Repositories;
- use App\Admin\Repositories\DistAppearanceTemplateLog;
- use App\Models\DistAppearanceTemplate as Model;
- use App\Models\SiteAppearanceTemplate;
- use Dcat\Admin\Repositories\EloquentRepository;
- use DOMDocument;
- use Illuminate\Support\Carbon;
- use Symfony\Component\DomCrawler\Crawler;
- class DistAppearanceTemplate extends EloquentRepository
- {
-
- protected $eloquentClass = Model::class;
-
- public static function syncAppearanceTemplates($appearanceId,$distId)
- {
- $model = new Model();
- return $model->syncAppearanceTemplates($appearanceId,$distId);
- }
-
- public static function getLandingPageTemplateOptions()
- {
- $distInfo = DistAdminDistributor::getInfo();
- $distId = $distInfo->id;
- $appearanceId = $distInfo->appearance_id;
- $model = new Model();
- $result = $model->where('appearance_id', $appearanceId)->where('dist_id', $distId)->select('file_name')->get();
- $options = ['pages_detail.liquid'=>config('dictionary.landing_page_default_template')];
- foreach ($result as $key => $value) {
-
- if (strpos($value->file_name, 'pages_sp_') === 0) {
-
-
-
- $part = $value->file_name;
- $options[$part] = $part;
- }
- }
- return $options;
- }
- public static function previewSave($data)
- {
- $distInfo = DistAdminDistributor::getInfo();
- $distId = $distInfo->id;
- $appearanceId = $distInfo->appearance_id;
-
- $inputElements = self::findElementsWithMtbId($data);
- foreach ($inputElements as $prefix => $elements) {
-
- $template = Model::where('dist_id', $distId)->where('appearance_id', $appearanceId)->where('template_code',$prefix)->first();
- if ($template) {
-
- $oldContent = self::contentChange($template->content,$template->template_code);
- $newContent = self::contentReplace($oldContent,$elements);
-
- $template->content = $newContent;
- $template->updated_at = Carbon::now();
- $template->save();
-
- DistAppearanceTemplateLog::insertLog($appearanceId,$distId,$template->file_name,$template->file_path,$template->template_code,$newContent,$oldContent);
- }
- }
- }
-
- public static function contentReplace($content,$elements) {
- foreach ($elements as $element) {
- $mtbId = $element['mtb_id'];
- $outHtml = $element['outHtml'];
- $content = preg_replace_callback(
- '/<([a-zA-Z0-9]+)[^>]*mtb_id="' . preg_quote($mtbId, '/') . '"[^>]*>(.*?)<\/\1>/s',
- function ($matches) use ($outHtml) {
-
- return $outHtml;
- },
- $content
- );
-
- $content = preg_replace_callback(
- '/<([a-zA-Z0-9]+)[^>]*mtb_id="' . preg_quote($mtbId, '/') . '"[^>]*>/s',
- function ($matches) use ($outHtml) {
-
- return $outHtml;
- },
- $content
- );
- }
- return $content;
- }
-
- public static function findElementsWithMtbId($html) {
-
- $pattern = '/<([a-zA-Z0-9-_]+)([^>]*\smtb_id="([^"]+)")([^>]*)>(.*?)<\/\1>/is';
- preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
-
- $result = [];
- foreach ($matches as $match) {
-
- $outHtml = $match[0];
-
- $pattern = '/>(?=<)/';
- $replacement = '> ';
- $outHtml = preg_replace($pattern, $replacement, $outHtml, 1);
-
- $outHtml = preg_replace('/\s*mtb_id="[^"]*"/', '', $outHtml);
- $result[] = [
- 'mtb_id' => $match[3],
- 'outHtml' => $outHtml,
- ];
- }
-
- $pattern = '/<([a-zA-Z0-9]+)[^>]*\smtb_id="([^"]+)"[^>]*>/is';
- preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
- foreach ($matches as $match) {
-
- $outHtml = $match[0];
- $outHtml = preg_replace('/\s*mtb_id="[^"]*"/', '', $outHtml);
- $result[] = [
- 'mtb_id' => $match[2],
- 'outHtml' => $outHtml,
- ];
- }
- $result = array_reduce($result, function ($carry, $item) {
-
- $prefix = explode('_', $item['mtb_id'])[0];
-
- if (!isset($carry[$prefix])) {
- $carry[$prefix] = [];
- }
-
- $carry[$prefix][] = $item;
- return $carry;
- }, []);
- return $result;
- }
-
- 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;
- }
- }
|