|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Models; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
8
|
|
|
|
|
9
|
|
|
class LinkType extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
protected $fillable = ['id', 'typename', 'title', 'description', 'icon', 'custom_html']; |
|
12
|
|
|
|
|
13
|
|
|
// Assuming no database interaction, we can disable timestamps |
|
14
|
|
|
public $timestamps = false; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get all LinkTypes from the config.yml files in each subfolder of the blocks directory. |
|
18
|
|
|
* |
|
19
|
|
|
* @return Collection |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function get() |
|
22
|
|
|
{ |
|
23
|
|
|
$blocksPath = base_path('blocks/'); |
|
24
|
|
|
$directories = (new Filesystem)->directories($blocksPath); |
|
25
|
|
|
$linkTypes = collect(); |
|
26
|
|
|
|
|
27
|
|
|
// Prepend "predefined" entry to the $linkTypes list |
|
28
|
|
|
$predefinedLinkType = new self([ |
|
29
|
|
|
'id' => 1, |
|
30
|
|
|
'typename' => 'predefined', |
|
31
|
|
|
'title' => null, |
|
32
|
|
|
'description' => null, |
|
33
|
|
|
'icon' => 'bi bi-boxes', |
|
34
|
|
|
'custom_html' => false, |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
$linkTypes->prepend($predefinedLinkType); |
|
38
|
|
|
|
|
39
|
|
|
foreach ($directories as $dir) { |
|
40
|
|
|
$configPath = $dir . '/config.yml'; |
|
41
|
|
|
if (file_exists($configPath)) { |
|
42
|
|
|
$configData = Yaml::parse(file_get_contents($configPath)); |
|
43
|
|
|
|
|
44
|
|
|
// Create a new instance of LinkType for each config file |
|
45
|
|
|
$linkType = new self([ |
|
46
|
|
|
'id' => $configData['id'] ?? 0, |
|
47
|
|
|
'typename' => $configData['typename'] ?? null, |
|
48
|
|
|
'title' => $configData['title'] ?? null, |
|
49
|
|
|
'description' => $configData['description'] ?? null, |
|
50
|
|
|
'icon' => $configData['icon'] ?? null, |
|
51
|
|
|
'custom_html' => $configData['custom_html'] ?? [], |
|
52
|
|
|
]); |
|
53
|
|
|
$linkTypes->push($linkType); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$custom_order = [ |
|
58
|
|
|
'predefined', |
|
59
|
|
|
'link', |
|
60
|
|
|
'vcard', |
|
61
|
|
|
'email', |
|
62
|
|
|
'telephone', |
|
63
|
|
|
'heading', |
|
64
|
|
|
'spacer', |
|
65
|
|
|
'text', |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
$sorted = $linkTypes->sortBy(function ($item) use ($custom_order) { |
|
69
|
|
|
$index = array_search($item->typename, $custom_order); |
|
70
|
|
|
return $index !== false ? $index : count($custom_order); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
return $sorted->values(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Check if a LinkType with the given typename exists. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $typename |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function existsByTypename($typename) |
|
83
|
|
|
{ |
|
84
|
|
|
return self::get()->contains('typename', $typename); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Find a LinkType by its typename. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $typename |
|
91
|
|
|
* @return LinkType|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function findByTypename($typename) |
|
94
|
|
|
{ |
|
95
|
|
|
return self::get()->firstWhere('typename', $typename); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|