1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Attributes\Models; |
6
|
|
|
|
7
|
|
|
use Vinkla\Hashids\Facades\Hashids; |
8
|
|
|
use Rinvex\Tenants\Traits\Tenantable; |
9
|
|
|
use Cortex\Foundation\Traits\Auditable; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
12
|
|
|
use Rinvex\Attributes\Models\Attribute as BaseAttribute; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cortex\Attributes\Models\Attribute. |
16
|
|
|
* |
17
|
|
|
* @property int $id |
18
|
|
|
* @property string $name |
19
|
|
|
* @property array $name |
20
|
|
|
* @property array $description |
21
|
|
|
* @property int $sort_order |
22
|
|
|
* @property string $group |
23
|
|
|
* @property string $type |
24
|
|
|
* @property bool $is_required |
25
|
|
|
* @property bool $is_collection |
26
|
|
|
* @property string $default |
27
|
|
|
* @property \Carbon\Carbon|null $created_at |
28
|
|
|
* @property \Carbon\Carbon|null $updated_at |
29
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[] $activity |
30
|
|
|
* @property array $entities |
31
|
|
|
* @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values |
32
|
|
|
* |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute ordered($direction = 'asc') |
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereCreatedAt($value) |
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDefault($value) |
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDescription($value) |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereGroup($value) |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereId($value) |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsCollection($value) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsRequired($value) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($value) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($value) |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereSortOrder($value) |
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereType($value) |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereUpdatedAt($value) |
46
|
|
|
* @mixin \Eloquent |
47
|
|
|
*/ |
48
|
|
|
class Attribute extends BaseAttribute |
49
|
|
|
{ |
50
|
|
|
use Auditable; |
51
|
|
|
use Tenantable; |
52
|
|
|
use LogsActivity; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Indicates whether to log only dirty attributes or all. |
56
|
|
|
* |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
protected static $logOnlyDirty = true; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The attributes that are logged on change. |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected static $logFillable = true; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The attributes that are ignored on change. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected static $ignoreChangedAttributes = [ |
74
|
|
|
'created_at', |
75
|
|
|
'updated_at', |
76
|
|
|
'deleted_at', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the value of the model's route key. |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function getRouteKey() |
85
|
|
|
{ |
86
|
|
|
return Hashids::encode($this->getAttribute($this->getRouteKeyName())); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieve the model for a bound value. |
91
|
|
|
* |
92
|
|
|
* @param mixed $value |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
94
|
|
|
*/ |
95
|
|
|
public function resolveRouteBinding($value) |
96
|
|
|
{ |
97
|
|
|
$value = Hashids::decode($value)[0]; |
98
|
|
|
|
99
|
|
|
return $this->where($this->getRouteKeyName(), $value)->first(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the route key for the model. |
104
|
|
|
* |
105
|
|
|
* @param \Illuminate\Database\Eloquent\Model $entity |
106
|
|
|
* @param string $accessArea |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\View\View |
|
|
|
|
109
|
|
|
*/ |
110
|
|
|
public function render(Model $entity, string $accessArea): string |
111
|
|
|
{ |
112
|
|
|
$default = ''; |
113
|
|
|
$selected = ''; |
114
|
|
|
|
115
|
|
|
switch ($this->type) { |
116
|
|
|
case 'select': |
|
|
|
|
117
|
|
|
|
118
|
|
|
$default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use (&$selected) { |
|
|
|
|
119
|
|
|
if (mb_strpos($item, '=')) { |
120
|
|
|
$key = mb_strstr($item, '=', true); |
121
|
|
|
$value = str_replace_first('=', '', mb_strstr($item, '=')); |
122
|
|
|
|
123
|
|
|
// Check for SELECTED itmes (marked by asterisk) |
124
|
|
|
! str_contains($value, '*') || $selected = $key; |
125
|
|
|
! str_contains($value, '*') || $value = str_replace_first('*', '', $value); |
126
|
|
|
} else { |
127
|
|
|
$key = $value = $item; |
128
|
|
|
|
129
|
|
|
// Check for SELECTED itmes (marked by asterisk) |
130
|
|
|
! str_contains($value, '*') || $key = $value = $selected = str_replace_first('*', '', $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return [$key => $value]; |
134
|
|
|
})->collapse(); |
135
|
|
|
|
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return view("cortex/attributes::$accessArea.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default, 'selected' => $selected])->render(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.