Skip to content

Commit 7970095

Browse files
committed
feat: Aggiunto supporto alle risorse JS per i collegamenti, prevista colonna TEXT "assets" in "zz_links"
1 parent 0fa427e commit 7970095

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

src/Models/Link.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class Link extends Model
4141
'title',
4242
];
4343

44+
protected $casts = [
45+
'assets' => 'array',
46+
];
47+
4448
public function parent()
4549
{
4650
return $this->belongsTo(self::class, 'parent');

src/NavbarLinks.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,64 @@ public static function validateValue(string $type, string $value): bool
212212

213213
return true;
214214
}
215+
216+
/**
217+
* Risolve un singolo entry di `zz_links.assets` in path relativo da OSM root.
218+
*
219+
* Regole:
220+
* - entry contiene `/` → trattato come path completo da OSM root.
221+
* - entry senza `/` → shorthand `modules/{link.id_module.directory}/assets/dist/js/{entry}`.
222+
* Se `id_module` non valorizzato o modulo non trovato → null (skip).
223+
*/
224+
public static function resolveAssetPath(string $entry, Link $link): ?string
225+
{
226+
$entry = trim($entry);
227+
if ($entry === '') {
228+
return null;
229+
}
230+
231+
if (str_contains($entry, '/')) {
232+
return '/'.ltrim($entry, '/');
233+
}
234+
235+
if (!$link->id_module) {
236+
return null;
237+
}
238+
239+
$mod = Module::find($link->id_module);
240+
if (!$mod || empty($mod->directory)) {
241+
return null;
242+
}
243+
244+
return '/modules/'.$mod->directory.'/assets/dist/js/'.$entry;
245+
}
246+
247+
/**
248+
* Raccoglie tutti gli asset JS dei link enabled, dedup, e ritorna array path relativi.
249+
*/
250+
public static function collectEnabledAssets(): array
251+
{
252+
$out = [];
253+
$links = Link::where('enabled', 1)
254+
->whereNotNull('assets')
255+
->get(['id', 'id_module', 'assets']);
256+
257+
foreach ($links as $lk) {
258+
$files = $lk->assets ?: [];
259+
if (!is_array($files)) {
260+
continue;
261+
}
262+
foreach ($files as $entry) {
263+
if (!is_string($entry)) {
264+
continue;
265+
}
266+
$resolved = self::resolveAssetPath($entry, $lk);
267+
if ($resolved && !in_array($resolved, $out, true)) {
268+
$out[] = $resolved;
269+
}
270+
}
271+
}
272+
273+
return $out;
274+
}
215275
}

update/2_11.sql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ CREATE TABLE IF NOT EXISTS `zz_links` (
339339
`value` VARCHAR(500) NOT NULL,
340340
`parent` INT(11) NULL DEFAULT NULL,
341341
`id_module` INT(11) NULL DEFAULT NULL,
342-
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
343-
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
342+
`assets` TEXT NULL DEFAULT NULL,
344343
PRIMARY KEY (`id`),
345344
UNIQUE KEY `uk_zz_links_name` (`name`),
346345
KEY `idx_zz_links_parent` (`parent`),
@@ -358,8 +357,6 @@ CREATE TABLE IF NOT EXISTS `zz_links_lang` (
358357
`id_record` INT(11) NOT NULL,
359358
`label` VARCHAR(255) NOT NULL DEFAULT '',
360359
`title` VARCHAR(255) NOT NULL DEFAULT '',
361-
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
362-
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
363360
PRIMARY KEY (`id`),
364361
UNIQUE KEY `uk_zz_links_lang_record` (`id_lang`, `id_record`),
365362
KEY `idx_zz_links_lang_record` (`id_record`),

update/tables.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
'zz_imports',
204204
'zz_imports_lang',
205205
'zz_langs',
206+
'zz_links',
207+
'zz_links_lang',
206208
'zz_logs',
207209
'zz_marche',
208210
'zz_modules',

0 commit comments

Comments
 (0)