|
@@ -98,6 +98,7 @@ class ActionRepository
|
|
|
'object_id' => $action['object_id'],
|
|
|
'object_type' => $action['object_type'],
|
|
|
'object_name' => data_get($objectNames, sprintf("%s.%d", $action['object_type'], $action['object_id'])),
|
|
|
+ 'comment' => $action['comment'],
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -156,4 +157,59 @@ class ActionRepository
|
|
|
|
|
|
return $objectNames;
|
|
|
}
|
|
|
+
|
|
|
+ public static function actionWithHistory(ActionObjectType $actionObjectType, string $objectId): array
|
|
|
+ {
|
|
|
+ $actions = Action::query()
|
|
|
+ ->with(['histories', 'createdBy'])
|
|
|
+ ->where("object_type", $actionObjectType->value)
|
|
|
+ ->where("object_id", $objectId)
|
|
|
+ ->orderBy("created_at")
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $objectNames = self::objectNamesGroupByType($actions);
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach ($actions as $action) {
|
|
|
+ $item = self::actionFormat($action->toArray(), $objectNames);
|
|
|
+ $item['histories'] = self::formatHistories($actionObjectType, $action->histories);
|
|
|
+
|
|
|
+ $items[] = $item;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function formatHistories(ActionObjectType $actionObjectType, Collection $histories): array
|
|
|
+ {
|
|
|
+ $detector = $actionObjectType->detectorClassName();
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach ($histories as $history) {
|
|
|
+ $labelKey = sprintf("fields.%s", $history->field);
|
|
|
+
|
|
|
+ $item = [
|
|
|
+ 'field' => $history->field,
|
|
|
+ 'field_label' => app('translator')->has($labelKey) ? __($labelKey) : $history->field,
|
|
|
+ 'new' => self::coverFieldValue($detector, $history->field, $history->new),
|
|
|
+ 'old' => self::coverFieldValue($detector, $history->field, $history->old),
|
|
|
+ 'diff' => (string)$history->diff,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $items[] = $item;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function coverFieldValue(string $detector, string $field, string $value): mixed
|
|
|
+ {
|
|
|
+ if (! $detector) {
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ $converter = call_user_func([$detector, "converter"], $field);
|
|
|
+
|
|
|
+ return $converter ? $converter->handle($value) : $value;
|
|
|
+ }
|
|
|
}
|