1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Settings; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Thinktomorrow\Chief\Fields\Types\InputField; |
7
|
|
|
|
8
|
|
|
class Setting extends Model |
9
|
|
|
{ |
10
|
|
|
const HOMEPAGE = 'homepage'; |
11
|
|
|
|
12
|
|
|
public $table = 'settings'; |
13
|
|
|
public $timestamps = false; |
14
|
|
|
public $guarded = []; |
15
|
|
|
public $casts = [ |
16
|
|
|
'value' => 'json', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
private static $fieldsFromConfig; |
20
|
|
|
|
21
|
6 |
|
public static function findByKey(string $key) |
22
|
|
|
{ |
23
|
6 |
|
return static::where('key', $key)->first(); |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public function getFieldAttribute() |
27
|
|
|
{ |
28
|
2 |
|
$fields = $this->fieldsFromConfig(); |
29
|
|
|
|
30
|
|
|
// TODO: a fieldgroup should be used here to filter / find by key |
31
|
2 |
|
foreach ($fields as $field) { |
32
|
1 |
|
if (is_callable($field)) { |
33
|
|
|
$field = call_user_func($field); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
if ($field->key != $this->key) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return $field; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
return $this->defaultField(); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
private static function fieldsFromConfig() |
47
|
|
|
{ |
48
|
2 |
|
if (static::$fieldsFromConfig) { |
|
|
|
|
49
|
|
|
return static::$fieldsFromConfig; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
return static::$fieldsFromConfig = config('thinktomorrow.chief.settingFields', []); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
private function defaultField() |
56
|
|
|
{ |
57
|
1 |
|
return InputField::make($this->key) |
58
|
1 |
|
->label(ucfirst(str_replace(['-','_','.'], ' ', $this->key))); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public static function refreshFieldsFromConfig() |
62
|
|
|
{ |
63
|
1 |
|
static::$fieldsFromConfig = null; |
|
|
|
|
64
|
1 |
|
} |
65
|
|
|
} |
66
|
|
|
|