Skip to content
3 changes: 3 additions & 0 deletions Api/Data/ActivityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface ActivityInterface
public const MODULE = 'module';
public const FULLACTION = 'fullaction';
public const ITEM_NAME = 'item_name';
public const ITEM_PATH = 'item_path';
public const ITEM_URL = 'item_url';
public const IS_REVERTABLE = 'is_revertable';
public const REVERT_BY = 'revert_by';
Expand Down Expand Up @@ -73,6 +74,8 @@ public function setFullaction(string $fullaction): self;

public function getItemName(): string;
public function setItemName(string $itemName): self;
public function getItemPath(): string;
public function setItemPath(string $itemPath): self;

public function getItemUrl(): ?string;
public function setItemUrl(?string $itemUrl): self;
Expand Down
71 changes: 45 additions & 26 deletions Block/Adminhtml/ActivityLogListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use MageOS\AdminActivityLog\Api\ActivityRepositoryInterface;
use MageOS\AdminActivityLog\Helper\Browser;

Expand All @@ -25,42 +28,48 @@
*/
class ActivityLogListing extends Template
{
/**
* Path to template file in theme.
* @var string
*/

protected $_template = 'MageOS_AdminActivityLog::log_listing.phtml';

/**
* ActivityLogListing constructor.
* @param Context $context
* @param ActivityRepositoryInterface $activityRepository
* @param Browser $browser
*/
public function __construct(
Context $context,
protected readonly ActivityRepositoryInterface $activityRepository,
protected readonly Browser $browser
protected readonly Browser $browser,
Context $context,
array $data = [],
?JsonHelper $jsonHelper = null,
?DirectoryHelper $directoryHelper = null
) {
parent::__construct($context);
parent::__construct(
$context,
$data,
$jsonHelper,
$directoryHelper
);
}

/**
* Get admin activity log listing
* @return array
*
* @return null|array<int, array{
* entity_id: string,
* activity_id: string,
* field_name: string,
* old_value: string,
* new_value: string
* }>
*/
public function getLogListing()
public function getLogListing(): ?array
{
$id = $this->getRequest()->getParam('id');
$data = $this->activityRepository->getActivityLog($id);
return $data->getData();
}

/**
* Get admin activity details
* @return array
* @return array<string, string>
* @throws NoSuchEntityException
*/
public function getAdminDetails()
public function getAdminDetails(): array
{
$id = $this->getRequest()->getParam('id');
$activity = $this->activityRepository->getActivityById($id);
Expand All @@ -69,14 +78,24 @@ public function getAdminDetails()
$this->browser->setUserAgent($activity->getUserAgent());
$browser = $this->browser->__toString();

$logData = [];
$logData['username'] = $activity->getUsername();
$logData['module'] = $activity->getModule();
$logData['name'] = $activity->getName();
$logData['fullaction'] = $activity->getFullaction();
$logData['browser'] = $browser;
$logData['date'] = $activity->getUpdatedAt();
return $logData;
$store = $this->_storeManager->getStore($activity->getStoreId());
if ($store->getId() == 0) {
$storeViewName = 'Default Config';
} else {
$storeViewName = sprintf('%s > %s > %s', $store->getWebsite()->getName(), $store->getGroup()->getName(), $store->getName());
}

return [
'username' => $activity->getUsername(),
'module' => $activity->getModule(),
'name' => $activity->getName(),
'fullaction' => $activity->getFullaction(),
'path' => $activity->getItemPath(),
'scope' => $activity->getScope(),
'store_name' => $storeViewName,
'browser' => $browser,
'date' => $activity->getUpdatedAt()
];
}

public function getActivityRepository(): ActivityRepositoryInterface
Expand Down
10 changes: 10 additions & 0 deletions Model/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ public function setItemName(string $itemName): ActivityInterface
return $this->setData(self::ITEM_NAME, $itemName);
}

public function getItemPath(): string
{
return (string)$this->getData(self::ITEM_PATH);
}

public function setItemPath(string $itemPath): ActivityInterface
{
return $this->setData(self::ITEM_PATH, $itemPath);
}

public function getItemUrl(): ?string
{
$value = $this->getData(self::ITEM_URL);
Expand Down
52 changes: 45 additions & 7 deletions Model/Activity/SystemConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

namespace MageOS\AdminActivityLog\Model\Activity;

use Magento\Config\Model\Config\Structure;
use Magento\Config\Model\Config\Structure\Element\Group;
use Magento\Config\Model\Config\Structure\Element\Section;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Config\ValueFactory;
Expand All @@ -26,16 +29,11 @@
*/
class SystemConfig implements ModelInterface
{
/**
* SystemConfig constructor.
* @param DataObject $dataObject
* @param ValueFactory $valueFactory
* @param WriterInterface $configWriter
*/
public function __construct(
protected readonly DataObject $dataObject,
protected readonly ValueFactory $valueFactory,
protected readonly WriterInterface $configWriter
protected readonly WriterInterface $configWriter,
private readonly Structure $configStructure
) {
}

Expand All @@ -58,6 +56,46 @@ public function getPath($model): string
return '';
}

public function getHumanReadablePath(string $path): string
{
$labels = [__('System Configuration')];
[$sectionId, $groupId, $fieldId] = explode('/', $path);

$section = $this->configStructure->getElement($sectionId);
if (!$section instanceof Section) {
return $path;
}

$tabId = $section->getAttribute('tab');
if ($tabId) {
foreach ($this->configStructure->getTabs() as $tab) {
if ($tab->getId() !== $tabId) {
continue;
}

$labels[] = $tab->getLabel();
}
}

$labels[] = $section->getLabel();
foreach ($section->getChildren() as $group) {
if (!$group instanceof Group || $group->getId() !== $groupId) {
continue;
}

$labels[] = $group->getLabel();
foreach ($group->getChildren() as $field) {
if ($field->getId() !== $fieldId) {
continue;
}

$labels[] = $field->getLabel();
}
}

return implode(' > ', $labels);
}

/**
* Get old activity data of system config module
* @param DataObject $model
Expand Down
28 changes: 23 additions & 5 deletions Model/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use MageOS\AdminActivityLog\Api\ActivityRepositoryInterface;
use MageOS\AdminActivityLog\Helper\Data as Helper;
use MageOS\AdminActivityLog\Model\Activity\Status;
use MageOS\AdminActivityLog\Model\Activity\SystemConfig;
use MageOS\AdminActivityLog\Model\Handler\PostDispatch;

/**
Expand Down Expand Up @@ -117,7 +118,8 @@ public function __construct(
protected readonly RequestInterface $request,
protected readonly Http $httpRequest,
protected readonly Status $status,
protected readonly PostDispatch $postDispatch
protected readonly PostDispatch $postDispatch,
private readonly SystemConfig $systemConfig
) {
}

Expand Down Expand Up @@ -190,6 +192,10 @@ public function getEditUrl($model)
|| !empty($model->getParentId()))) {
$id = ($model->getOrderId()) ? $model->getOrderId() : $model->getParentId();
}
if ($this->eventConfig['module'] === 'system_configuration') {
$id = $model->getData('field_config')['path'];
}

return str_replace(
$this->urlParams,
[
Expand Down Expand Up @@ -325,6 +331,7 @@ public function saveLogs()
*/
public function initLog()
{
/** @var Activity $activity */
$activity = $this->activityFactory->create();

if ($this->authSession->isLoggedIn()) {
Expand Down Expand Up @@ -359,11 +366,22 @@ public function initActivity($model)
$activity = $this->initLog();

$activity->setStoreId($this->getStoreId($model));
$activity->setItemName(
$model->getData(
$this->config->getActivityModuleItemField($this->eventConfig['module'])
)

$itemName = $model->getData(
$this->config->getActivityModuleItemField($this->eventConfig['module'])
);

$itemPath = '';
if ($this->eventConfig['module'] === 'system_configuration') {
if (isset($itemName['label'])) {
$itemName = $itemName['label'];
}

$itemPath = $this->systemConfig->getHumanReadablePath($model->getPath());
}

$activity->setItemName($itemName);
$activity->setItemPath($itemPath);
$activity->setItemUrl($this->getEditUrl($model));

return $activity;
Expand Down
Loading