|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Traits\Collection; |
|
4
|
|
|
|
|
5
|
|
|
use Coduo\PHPHumanizer\StringHumanizer; |
|
6
|
|
|
use Terranet\Administrator\Contracts\AutoTranslatable; |
|
7
|
|
|
use Terranet\Administrator\Scaffolding; |
|
8
|
|
|
use Terranet\Administrator\Traits\AutoTranslatesInstances; |
|
9
|
|
|
|
|
10
|
|
|
abstract class ElementContainer implements AutoTranslatable |
|
11
|
|
|
{ |
|
12
|
|
|
use AutoTranslatesInstances; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Element id. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $id; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Element title. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $title; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Keep original id or extract only last part. |
|
30
|
|
|
* |
|
31
|
|
|
* @example: When using this Container in Forms, the original id should be kept to allow parsing relations. |
|
32
|
|
|
* When for columns there is a different logic to extract Relational data. |
|
33
|
|
|
* |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $keepOriginalID = true; |
|
37
|
|
|
|
|
38
|
|
|
protected $module; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Element constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param $id |
|
44
|
|
|
*/ |
|
45
|
15 |
|
public function __construct($id) |
|
46
|
|
|
{ |
|
47
|
15 |
|
$this->setId($id); |
|
48
|
|
|
|
|
49
|
15 |
|
if ($this->translator()->has($key = $this->translationKey())) { |
|
50
|
|
|
$this->setTitle(trans($key)); |
|
51
|
|
|
} else { |
|
52
|
15 |
|
$this->setTitle( |
|
53
|
15 |
|
'id' === $id |
|
54
|
|
|
? 'ID' |
|
55
|
15 |
|
: StringHumanizer::humanize(str_replace(['_id', '-', '_'], ['', ' ', ' '], $id)) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
15 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set element ID. |
|
62
|
|
|
* |
|
63
|
|
|
* @param $id |
|
64
|
|
|
* |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
15 |
|
public function setId($id) |
|
68
|
|
|
{ |
|
69
|
15 |
|
$this->id = $this->buildId($id); |
|
70
|
|
|
|
|
71
|
15 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set element title. |
|
76
|
|
|
* |
|
77
|
|
|
* @param $title |
|
78
|
|
|
* |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
15 |
|
public function setTitle($title) |
|
82
|
|
|
{ |
|
83
|
|
|
// handle relations-style titles |
|
84
|
15 |
|
$title = preg_replace('~^(\w+)\.(\w+)$~si', '$1 $2', $title); |
|
85
|
|
|
|
|
86
|
15 |
|
$this->title = $title; |
|
87
|
|
|
|
|
88
|
15 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get element id. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function id() |
|
97
|
|
|
{ |
|
98
|
2 |
|
return $this->id; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get element title. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
3 |
|
public function title() |
|
107
|
|
|
{ |
|
108
|
3 |
|
return $this->title; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
15 |
|
public function translationKey() |
|
115
|
|
|
{ |
|
116
|
15 |
|
$key = sprintf('administrator::columns.%s.%s', $this->module()->url(), $this->id); |
|
117
|
|
|
|
|
118
|
15 |
|
if (!$this->translator()->has($key)) { |
|
119
|
15 |
|
$key = sprintf('administrator::columns.%s.%s', 'global', $this->id); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
15 |
|
return $key; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function setModule($module) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->module = $module; |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $id |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
15 |
|
protected function buildId($id) |
|
138
|
|
|
{ |
|
139
|
15 |
|
$parts = explode('.', $id); |
|
140
|
|
|
$parts = array_map(function ($part) { |
|
141
|
15 |
|
return str_slug($part, '_'); |
|
142
|
15 |
|
}, $parts); |
|
143
|
15 |
|
$id = implode('.', $parts); |
|
144
|
|
|
|
|
145
|
15 |
|
return $id; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return Scaffolding |
|
150
|
|
|
*/ |
|
151
|
15 |
|
protected function module() |
|
152
|
|
|
{ |
|
153
|
15 |
|
return $this->module ?: app('scaffold.module'); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|