dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
4.1.0-001-media-usage-language.migration.php
Go to the documentation of this file.
1<?php
2
5use dbx\dbxContent\dbxContentMediaUsageScope;
6
7return array(
8 'id' => 'core-4.1.0-media-usage-language',
9 'version' => '4.1.0',
10 'description' => 'Sprachsichere und idempotente Medienverwendung.',
11 'affected_dd' => array('dbx|dbxMediaUsage'),
12 'operations' => array(
13 array('type' => 'sync_dd', 'dd' => 'dbx|dbxMediaUsage'),
14 ),
15 'up' => static function($db, $dd): void {
16 require_once dirname(__DIR__, 2) . '/dbxContent/include/dbxContent_bootstrap_sync.php';
17 // Migrations run before the staged program files become active. Load
18 // this new dependency from the migration's own release tree instead
19 // of relying on the previous installation's bootstrap/autoloader.
20 require_once dirname(__DIR__, 2) . '/dbxContent/include/dbxContentMediaUsageScope.class.php';
21
22 $languages = array_values(array_unique(array_map(
23 static fn($lng): string => dbxContentMediaUsageScope::language((string)$lng),
24 dbxContentLngSync::accessibleLngs()
25 )));
26 if (!$languages) $languages = array('de');
27 $master = dbxContentMediaUsageScope::language(dbxContentLngSync::masterLng());
28
29 $pageCache = array();
30 $folderCache = array();
31 foreach ($languages as $lng) {
32 $pageCache[$lng] = array();
33 $folderCache[$lng] = array();
34 $pages = $db->select(dbxContentLng::ddContent($lng), '', 'id,folder,hero_image_id,content', 'id', 'ASC', '', 0, 0, 0);
35 foreach (is_array($pages) ? $pages : array() as $page) {
36 if (is_array($page) && (int)($page['id'] ?? 0) > 0) {
37 $pageCache[$lng][(int)$page['id']] = $page;
38 }
39 }
40 $folders = $db->select(dbxContentLng::ddFolder($lng), '', 'id,hero_image_id', 'id', 'ASC', '', 0, 0, 0);
41 foreach (is_array($folders) ? $folders : array() as $folder) {
42 if (is_array($folder) && (int)($folder['id'] ?? 0) > 0) {
43 $folderCache[$lng][(int)$folder['id']] = $folder;
44 }
45 }
46 }
47
48 $containsMedia = static function(string $html, int $mediaId): bool {
49 if ($mediaId <= 0 || $html === '') return false;
50 return preg_match('/data-cms-media-id=["\']?' . $mediaId . '(?:["\'\s>]|$)/i', $html) === 1
51 || preg_match('/(?:dbx_mid|media_id)=' . $mediaId . '(?:[^0-9]|$)/i', $html) === 1;
52 };
53
54 $rows = $db->select('dbxMediaUsage', '', '*', 'id', 'ASC', '', 0, 0, 0);
55 $seen = array();
56 foreach (is_array($rows) ? $rows : array() as $row) {
57 if (!is_array($row)) continue;
58 $id = (int)($row['id'] ?? 0);
59 if ($id <= 0) continue;
60 if ((int)($row['active'] ?? 0) !== 1) {
61 $db->delete('dbxMediaUsage', $id, 1, 0);
62 continue;
63 }
64
65 $mediaId = (int)($row['media_id'] ?? 0);
66 $contentId = (int)($row['content_id'] ?? 0);
67 $folderId = (int)($row['folder_id'] ?? 0);
68 $slot = strtolower(trim((string)($row['slot'] ?? '')));
69 $candidates = array();
70
71 if ($slot === 'shop') {
72 $candidates = array($master);
73 } elseif ($contentId > 0) {
74 foreach ($languages as $lng) {
75 $page = $pageCache[$lng][$contentId] ?? null;
76 if (!is_array($page)) continue;
77 if ($folderId > 0 && (int)($page['folder'] ?? 0) !== $folderId) continue;
78 if ($slot === 'hero' && (int)($page['hero_image_id'] ?? 0) !== $mediaId) continue;
79 if ($slot === 'inline' && !$containsMedia((string)($page['content'] ?? ''), $mediaId)) continue;
80 $candidates[] = $lng;
81 }
82 if (!$candidates) {
83 foreach ($languages as $lng) {
84 if (isset($pageCache[$lng][$contentId])) $candidates[] = $lng;
85 }
86 }
87 } elseif ($folderId > 0) {
88 foreach ($languages as $lng) {
89 $folder = $folderCache[$lng][$folderId] ?? null;
90 if (!is_array($folder)) continue;
91 if ($slot === 'hero' && (int)($folder['hero_image_id'] ?? 0) !== $mediaId) continue;
92 $candidates[] = $lng;
93 }
94 if (!$candidates) {
95 foreach ($languages as $lng) {
96 if (isset($folderCache[$lng][$folderId])) $candidates[] = $lng;
97 }
98 }
99 }
100
101 $lng = count($candidates) === 1
102 ? $candidates[0]
103 : (in_array($master, $candidates, true) ? $master : ($candidates[0] ?? $master));
104 $key = $lng . ':' . $contentId . ':' . $folderId . ':' . $slot . ':' . $mediaId;
105 if (isset($seen[$key])) {
106 $db->delete('dbxMediaUsage', $id, 1, 0);
107 continue;
108 }
109 $seen[$key] = $id;
110 if ((string)($row['content_lng'] ?? '') !== $lng) {
111 $db->update('dbxMediaUsage', array('content_lng' => $lng), $id, 0, 1, 1, 0);
112 }
113 }
114 },
115);
if(!defined( 'IMG_WEBP')) define( 'IMG_WEBP'
if($cinematic===''||!str_contains($cinematic, 'data-dbx-cinema')) $page