|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Analytics |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the LICENSE.md file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Marcel Scherello <[email protected]> |
|
9
|
|
|
* @copyright 2021 Marcel Scherello |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Service; |
|
13
|
|
|
|
|
14
|
|
|
use OCA\Analytics\Db\DatasetMapper; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
|
|
17
|
|
|
class VariableService |
|
18
|
|
|
{ |
|
19
|
|
|
private $logger; |
|
20
|
|
|
private $DatasetMapper; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
LoggerInterface $logger, |
|
24
|
|
|
DatasetMapper $DatasetMapper |
|
25
|
|
|
) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->logger = $logger; |
|
28
|
|
|
$this->DatasetMapper = $DatasetMapper; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* replace %*% text variables in name and subheader |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $datasetMetadata |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function replaceTextVariables($datasetMetadata) |
|
38
|
|
|
{ |
|
39
|
|
|
$fields = ['name', 'subheader']; |
|
40
|
|
|
foreach ($fields as $field) { |
|
41
|
|
|
$name = $datasetMetadata[$field]; |
|
42
|
|
|
|
|
43
|
|
|
preg_match_all("/%.*?%/", $name, $matches); |
|
44
|
|
|
if (count($matches[0]) > 0) { |
|
45
|
|
|
foreach ($matches[0] as $match) { |
|
46
|
|
|
if ($match === '%currentDate%') { |
|
47
|
|
|
$replace = date("m.d.y"); |
|
48
|
|
|
} elseif ($match === '%lastUpdate%') { |
|
49
|
|
|
$timestamp = $this->DatasetMapper->getLastUpdate($datasetMetadata['id']); |
|
50
|
|
|
$replace = date('d.m.Y', $timestamp); |
|
51
|
|
|
} elseif ($match === '%owner%') { |
|
52
|
|
|
$owner = $this->DatasetMapper->getOwner($datasetMetadata['id']); |
|
53
|
|
|
$replace = $owner; |
|
54
|
|
|
} |
|
55
|
|
|
$datasetMetadata[$field] = preg_replace('/' . $match . '/', $replace, $datasetMetadata[$field]); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
return $datasetMetadata; |
|
60
|
|
|
} |
|
61
|
|
|
} |