-
-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathLinkType.php
More file actions
98 lines (85 loc) · 2.75 KB
/
LinkType.php
File metadata and controls
98 lines (85 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\Yaml\Yaml;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
class LinkType extends Model
{
protected $fillable = ['id', 'typename', 'title', 'description', 'icon', 'custom_html'];
// 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,
]);
$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,
'custom_html' => $configData['custom_html'] ?? [],
]);
$linkTypes->push($linkType);
}
}
$custom_order = [
'predefined',
'link',
'vcard',
'email',
'telephone',
'heading',
'spacer',
'text',
'xmpp',
];
$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);
}
/**
* Find a LinkType by its typename.
*
* @param string $typename
* @return LinkType|null
*/
public static function findByTypename($typename)
{
return self::get()->firstWhere('typename', $typename);
}
}