1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/lav45/yii2-activity-logger |
4
|
|
|
* @copyright Copyright (c) 2017 LAV45 |
5
|
|
|
* @author Aleksey Loban <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace lav45\activityLogger\module\models; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
|
13
|
|
|
class ActivityLogViewModel extends ActivityLog |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var DataModel|string|array |
17
|
|
|
*/ |
18
|
|
|
public $dataModel = DataModel::class; |
19
|
|
|
/** |
20
|
|
|
* [ entity_name => Entity::class ] |
21
|
|
|
*/ |
22
|
|
|
public array $entityMap = []; |
23
|
|
|
|
24
|
|
|
private array $entityModel = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $row |
28
|
|
|
* @return ActivityLog|object|static |
29
|
|
|
*/ |
30
|
|
|
public static function instantiate($row) |
31
|
|
|
{ |
32
|
|
|
return Yii::createObject(static::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return \yii\base\Model|null |
37
|
|
|
*/ |
38
|
|
|
protected function getEntityModel() |
39
|
|
|
{ |
40
|
|
|
if (isset($this->entityModel[$this->entity_name]) === false) { |
41
|
|
|
$this->entityModel[$this->entity_name] = $this->getEntityObject($this->entity_name); |
42
|
|
|
} |
43
|
|
|
return $this->entityModel[$this->entity_name] ?: null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return false|\yii\base\Model |
48
|
|
|
*/ |
49
|
|
|
private function getEntityObject(string $id) |
50
|
|
|
{ |
51
|
|
|
if (isset($this->entityMap[$id]) === false) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
/** @var \yii\base\Model $class */ |
55
|
|
|
$class = $this->entityMap[$id]; |
56
|
|
|
return $class::instance(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return \Generator|DataModel[] |
61
|
|
|
*/ |
62
|
|
|
public function getData(): iterable |
63
|
|
|
{ |
64
|
|
|
foreach (parent::getData() as $attribute => $values) { |
65
|
|
|
if (is_string($values)) { |
66
|
|
|
$label = is_string($attribute) ? $this->getEntityAttributeLabel($attribute) : $attribute; |
67
|
|
|
yield $label => $values; |
68
|
|
|
} else { |
69
|
|
|
$dataModel = $this->getDataModel() |
70
|
|
|
->setFormat($this->getAttributeFormat($attribute)) |
71
|
|
|
->setData($values); |
72
|
|
|
|
73
|
|
|
yield $this->getEntityAttributeLabel($attribute) => $dataModel; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function getDataModel(): DataModel |
79
|
|
|
{ |
80
|
|
|
if (!is_object($this->dataModel)) { |
81
|
|
|
$this->dataModel = Yii::createObject($this->dataModel); |
82
|
|
|
} |
83
|
|
|
return $this->dataModel; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function getEntityAttributeLabel(string $attribute): string |
87
|
|
|
{ |
88
|
|
|
if ($entityModel = $this->getEntityModel()) { |
89
|
|
|
return $entityModel->getAttributeLabel($attribute); |
90
|
|
|
} |
91
|
|
|
return $this->generateAttributeLabel($attribute); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getEntityAttributeFormats(): array |
95
|
|
|
{ |
96
|
|
|
$entityModel = $this->getEntityModel(); |
97
|
|
|
if (null !== $entityModel && method_exists($entityModel, 'attributeFormats')) { |
98
|
|
|
return $entityModel->attributeFormats(); |
99
|
|
|
} |
100
|
|
|
return []; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getAttributeFormat(string $attribute): ?string |
104
|
|
|
{ |
105
|
|
|
$formats = $this->getEntityAttributeFormats(); |
106
|
|
|
return $formats[$attribute] ?? null; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|