2022-11-08 16:11:59 +01:00
|
|
|
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-06-20 12:24:54 +02:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Filesystem\Filesystem;
|
2022-11-08 16:11:59 +01:00
|
|
|
|
|
|
|
class LinkType extends Model
|
|
|
|
{
|
2024-09-23 12:53:08 +02:00
|
|
|
protected $fillable = ['id', 'typename', 'title', 'description', 'icon', 'custom_html', 'ignore_container', 'include_libraries'];
|
2024-06-20 12:24:54 +02:00
|
|
|
|
|
|
|
// Assuming no database interaction, we can disable timestamps
|
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all LinkTypes from the config.yml files in each subfolder of the blocks directory.
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public static function get()
|
|
|
|
{
|
|
|
|
$blocksPath = base_path('blocks/');
|
|
|
|
$directories = (new Filesystem)->directories($blocksPath);
|
|
|
|
$linkTypes = collect();
|
|
|
|
|
|
|
|
// Prepend "predefined" entry to the $linkTypes list
|
|
|
|
$predefinedLinkType = new self([
|
|
|
|
'id' => 1,
|
|
|
|
'typename' => 'predefined',
|
|
|
|
'title' => null,
|
|
|
|
'description' => null,
|
|
|
|
'icon' => 'bi bi-boxes',
|
|
|
|
'custom_html' => false,
|
2024-09-19 12:53:21 +02:00
|
|
|
'ignore_container' => false,
|
2024-09-23 12:53:08 +02:00
|
|
|
'include_libraries' => [],
|
2024-06-20 12:24:54 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$linkTypes->prepend($predefinedLinkType);
|
|
|
|
|
|
|
|
foreach ($directories as $dir) {
|
|
|
|
$configPath = $dir . '/config.yml';
|
|
|
|
if (file_exists($configPath)) {
|
|
|
|
$configData = Yaml::parse(file_get_contents($configPath));
|
|
|
|
|
|
|
|
// Create a new instance of LinkType for each config file
|
|
|
|
$linkType = new self([
|
|
|
|
'id' => $configData['id'] ?? 0,
|
|
|
|
'typename' => $configData['typename'] ?? null,
|
|
|
|
'title' => $configData['title'] ?? null,
|
|
|
|
'description' => $configData['description'] ?? null,
|
|
|
|
'icon' => $configData['icon'] ?? null,
|
2024-09-19 12:53:21 +02:00
|
|
|
'custom_html' => $configData['custom_html'] ?? false,
|
|
|
|
'ignore_container' => $configData['ignore_container'] ?? false,
|
2024-09-23 12:53:08 +02:00
|
|
|
'include_libraries' => $configData['include_libraries'] ?? [],
|
2024-06-20 12:24:54 +02:00
|
|
|
]);
|
|
|
|
$linkTypes->push($linkType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$custom_order = [
|
|
|
|
'predefined',
|
|
|
|
'link',
|
|
|
|
'vcard',
|
|
|
|
'email',
|
|
|
|
'telephone',
|
|
|
|
'heading',
|
|
|
|
'spacer',
|
|
|
|
'text',
|
|
|
|
];
|
|
|
|
|
|
|
|
$sorted = $linkTypes->sortBy(function ($item) use ($custom_order) {
|
|
|
|
$index = array_search($item->typename, $custom_order);
|
|
|
|
return $index !== false ? $index : count($custom_order);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $sorted->values();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a LinkType with the given typename exists.
|
|
|
|
*
|
|
|
|
* @param string $typename
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function existsByTypename($typename)
|
|
|
|
{
|
|
|
|
return self::get()->contains('typename', $typename);
|
|
|
|
}
|
2022-11-08 16:11:59 +01:00
|
|
|
|
2024-06-20 12:24:54 +02:00
|
|
|
/**
|
|
|
|
* Find a LinkType by its typename.
|
|
|
|
*
|
|
|
|
* @param string $typename
|
|
|
|
* @return LinkType|null
|
|
|
|
*/
|
|
|
|
public static function findByTypename($typename)
|
|
|
|
{
|
|
|
|
return self::get()->firstWhere('typename', $typename);
|
|
|
|
}
|
2022-11-08 16:11:59 +01:00
|
|
|
}
|