Completed
Push — master ( 522461...c89cc2 )
by Fabien
02:24
created

FooterViewHelper::getDatabaseConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Fab\Media\ViewHelpers\Form;
4
5
/*
6
 * This file is part of the Fab/Media project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11
12
use Fab\Vidi\Service\DataService;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
15
use TYPO3\CMS\Core\Resource\File;
16
use TYPO3\CMS\Core\Utility\MathUtility;
17
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
18
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19
20
/**
21
 * View helper dealing with form footer.
22
 */
23
class FooterViewHelper extends AbstractViewHelper
24
{
25
26
    /**
27
     * Render a form footer.
28
     * Example:
29
     * Created on 30-12-12 by John Updated on 22-05-12 by Jane
30
     *
31
     * @return string
32
     */
33
    public function render()
34
    {
35
36
        /** @var File $file */
37
        $file = $this->templateVariableContainer->get('file');
38
        $template = '<span>%s %s %s</span> <span style="padding-left: 50px">%s %s %s</span>';
39
40
        $format = sprintf('%s @ %s',
41
            $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
42
            $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']
43
        );
44
45
        $arguments = array(
46
            'date' => null,
47
            'format' => $format,
48
            'base' => null
49
        );
50
51
        $result = sprintf($template,
52
            LocalizationUtility::translate('created_on', 'media'),
53
            $file->getProperty('crdate') ? $this->formatDate($arguments, $file->getProperty('crdate')) : '',
54
            $this->getUserName($file->getProperty('cruser_id')),
55
            LocalizationUtility::translate('updated_on', 'media'),
56
            $file->getProperty('tstamp') ? $this->formatDate($arguments, '@' . $file->getProperty('tstamp')) : '',
57
            $this->getUserName($file->getProperty('upuser_id'))
58
        );
59
60
        return $result;
61
    }
62
63
64
    /**
65
     * Get the User name to be displayed
66
     *
67
     * @param int $userIdentifier
68
     * @return string
69
     */
70
    public function getUserName($userIdentifier)
71
    {
72
73
        $username = '';
74
75
        if ($userIdentifier > 0) {
76
            $record = $this->getDataService()
77
                ->getRecord(
78
                    'be_users',
79
                    [
80
                        'uid' => $userIdentifier
81
                    ]
82
                );
83
            $username = sprintf('%s %s',
84
                LocalizationUtility::translate('by', 'media'),
85
                empty($record['realName']) ? $record['username'] : $record['realName']
86
            );
87
        }
88
89
        return $username;
90
    }
91
92
    /**
93
     * @return object|DataService
94
     */
95
    protected function getDataService(): DataService
96
    {
97
        return GeneralUtility::makeInstance(DataService::class);
98
    }
99
100
    /**
101
     * @param array $arguments
102
     * @param int|string $date
103
     * @return string
104
     * @throws Exception
105
     */
106
    public function formatDate(array $arguments, $date)
107
    {
108
        $format = $arguments['format'];
109
        $base = $date;
110
        if (is_string($base)) {
111
            $base = trim($base);
112
        }
113
114
        if ($format === '') {
115
            $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
116
        }
117
118
        if (is_string($date)) {
119
            $date = trim($date);
120
        }
121
122
        if ($date === '') {
123
            $date = 'now';
124
        }
125
126
        if (!$date instanceof \DateTimeInterface) {
127
            try {
128
                $base = $base instanceof \DateTimeInterface ? $base->format('U') : strtotime((MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
129
                $dateTimestamp = strtotime((MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
130
                $date = new \DateTime('@' . $dateTimestamp);
131
                $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
132
            } catch (\Exception $exception) {
133
                throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor: ' . $exception->getMessage(), 1241722579);
134
            }
135
        }
136
137
        if (strpos($format, '%') !== false) {
138
            return strftime($format, $date->format('U'));
139
        } else {
140
            return $date->format($format);
141
        }
142
    }
143
}
144