Skip to content

Commit

Permalink
text variables in report header (e.g. %lastUpdate%) #145
Browse files Browse the repository at this point in the history
  • Loading branch information
Rello committed Jul 13, 2021
1 parent a9e3edd commit 7cb5a08
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
11 changes: 5 additions & 6 deletions js/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ OCA.Analytics.Sidebar.Dataset = {
handleNameHint: function () {
let text = t('analytics', 'Text variables can be used in title and subheader.<br>They are replaced when the report is executed.') +
'<br><br>' +
'%lastUpdate%<br>' +
'%lastUpdateDate%<br>' +
'%lastUpdateTime%<br>' +
'%currentDate%<br>' +
'%currentTime%<br>' +
'%owner%';
OCA.Analytics.Notification.dialog(t('analytics', 'Text variables'), text, 'info');
},
Expand Down Expand Up @@ -716,16 +718,13 @@ OCA.Analytics.Sidebar.Backend = {
},

deleteDataset: function (datasetId) {
const button = document.getElementById('sidebarDatasetDeleteButton');
button.classList.add('loading');
button.disabled = true;
document.getElementById('navigationDatasets').innerHTML = '<div style="text-align:center; padding-top:100px" class="get-metadata icon-loading"></div>';
$.ajax({
type: 'DELETE',
url: OC.generateUrl('apps/analytics/dataset/') + datasetId,
success: function (data) {
button.classList.remove('loading');
button.disabled = false;
OCA.Analytics.Navigation.init();
OCA.Analytics.Navigation.handleOverviewButton();
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
$appName,
IRequest $request,
LoggerInterface $logger,
ShareService $ShareService,
ShareService $ShareService
)
{
parent::__construct($appName, $request);
Expand Down
10 changes: 8 additions & 2 deletions lib/Service/ShareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ class ShareService
private $groupManager;
/** @var IUserManager */
private $userManager;
private $VariableService;

public function __construct(
LoggerInterface $logger,
ShareMapper $ShareMapper,
ActivityManager $ActivityManager,
IGroupManager $groupManager,
ISecureRandom $secureRandom,
IUserManager $userManager
IUserManager $userManager,
VariableService $VariableService
)
{
$this->logger = $logger;
Expand All @@ -52,6 +54,7 @@ public function __construct(
$this->groupManager = $groupManager;
$this->ActivityManager = $ActivityManager;
$this->userManager = $userManager;
$this->VariableService = $VariableService;
}

/**
Expand Down Expand Up @@ -114,7 +117,10 @@ public function read($datasetId)
*/
public function getDatasetByToken($token)
{
return $this->ShareMapper->getDatasetByToken($token);
$dataset = $this->ShareMapper->getDatasetByToken($token);
$dataset = $this->VariableService->replaceTextVariables($dataset);

return $dataset;
}

/**
Expand Down
19 changes: 14 additions & 5 deletions lib/Service/VariableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
namespace OCA\Analytics\Service;

use OCA\Analytics\Db\DatasetMapper;
use OCP\IDateTimeFormatter;
use Psr\Log\LoggerInterface;

class VariableService
{
private $logger;
private $DatasetMapper;
private $IDateTimeFormatter;

public function __construct(
LoggerInterface $logger,
DatasetMapper $DatasetMapper
DatasetMapper $DatasetMapper,
IDateTimeFormatter $IDateTimeFormatter
)
{
$this->logger = $logger;
$this->DatasetMapper = $DatasetMapper;
$this->IDateTimeFormatter = $IDateTimeFormatter;
}

/**
Expand All @@ -38,16 +42,21 @@ public function replaceTextVariables($datasetMetadata)
{
$fields = ['name', 'subheader'];
foreach ($fields as $field) {
$name = $datasetMetadata[$field];
isset($datasetMetadata[$field]) ? $name = $datasetMetadata[$field] : $name = '';

preg_match_all("/%.*?%/", $name, $matches);
if (count($matches[0]) > 0) {
foreach ($matches[0] as $match) {
if ($match === '%currentDate%') {
$replace = date("m.d.y");
} elseif ($match === '%lastUpdate%') {
$replace = $this->IDateTimeFormatter->formatDate(time(), 'short');
} elseif ($match === '%currentTime%') {
$replace = $this->IDateTimeFormatter->formatTime(time(), 'short');
} elseif ($match === '%lastUpdateDate%') {
$timestamp = $this->DatasetMapper->getLastUpdate($datasetMetadata['id']);
$replace = date('d.m.Y', $timestamp);
$replace = $this->IDateTimeFormatter->formatDate($timestamp, 'short');
} elseif ($match === '%lastUpdateTime%') {
$timestamp = $this->DatasetMapper->getLastUpdate($datasetMetadata['id']);
$replace = $this->IDateTimeFormatter->formatTime($timestamp, 'short');
} elseif ($match === '%owner%') {
$owner = $this->DatasetMapper->getOwner($datasetMetadata['id']);
$replace = $owner;
Expand Down

0 comments on commit 7cb5a08

Please sign in to comment.