Issues (3627)

CoreBundle/Templating/Helper/DateHelper.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Templating\Helper;
13
14
use Mautic\CoreBundle\Helper\CoreParametersHelper;
15
use Mautic\CoreBundle\Helper\DateTimeHelper;
16
use Symfony\Component\Templating\Helper\Helper;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
class DateHelper extends Helper
20
{
21
    /**
22
     * @var string[]
23
     */
24
    protected $formats;
25
26
    /**
27
     * @var DateTimeHelper
28
     */
29
    protected $helper;
30
31
    /**
32
     * @var TranslatorInterface
33
     */
34
    protected $translator;
35
36
    /**
37
     * @var CoreParametersHelper
38
     */
39
    private $coreParametersHelper;
40
41
    /**
42
     * @param string $dateFullFormat
43
     * @param string $dateShortFormat
44
     * @param string $dateOnlyFormat
45
     * @param string $timeOnlyFormat
46
     */
47
    public function __construct(
48
        $dateFullFormat,
49
        $dateShortFormat,
50
        $dateOnlyFormat,
51
        $timeOnlyFormat,
52
        TranslatorInterface $translator,
53
        CoreParametersHelper $coreParametersHelper
54
    ) {
55
        $this->formats = [
56
            'datetime' => $dateFullFormat,
57
            'short'    => $dateShortFormat,
58
            'date'     => $dateOnlyFormat,
59
            'time'     => $timeOnlyFormat,
60
        ];
61
62
        $this->helper               = new DateTimeHelper(null, null, 'local');
63
        $this->translator           = $translator;
64
        $this->coreParametersHelper = $coreParametersHelper;
65
    }
66
67
    /**
68
     * @param string           $type
69
     * @param \DateTime|string $datetime
70
     * @param string           $timezone
71
     * @param string           $fromFormat
72
     *
73
     * @return string
74
     */
75
    protected function format($type, $datetime, $timezone, $fromFormat)
76
    {
77
        if (empty($datetime)) {
78
            return '';
79
        } else {
80
            $this->helper->setDateTime($datetime, $fromFormat, $timezone);
81
82
            return $this->helper->toLocalString(
83
                $this->formats[$type]
84
            );
85
        }
86
    }
87
88
    /**
89
     * Returns full date. eg. October 8, 2014 21:19.
90
     *
91
     * @param \DateTime|string $datetime
92
     * @param string           $timezone
93
     * @param string           $fromFormat
94
     *
95
     * @return string
96
     */
97
    public function toFull($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
98
    {
99
        return $this->format('datetime', $datetime, $timezone, $fromFormat);
100
    }
101
102
    /**
103
     * Returns date and time concat eg 2014-08-02 5:00am.
104
     *
105
     * @param \DateTime|string $datetime
106
     * @param string           $timezone
107
     * @param string           $fromFormat
108
     *
109
     * @return string
110
     */
111
    public function toFullConcat($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
112
    {
113
        $this->helper->setDateTime($datetime, $fromFormat, $timezone);
114
115
        return $this->helper->toLocalString(
116
            $this->formats['date'].' '.$this->formats['time']
117
        );
118
    }
119
120
    /**
121
     * Returns short date format eg Sun, Oct 8.
122
     *
123
     * @param \DateTime|string $datetime
124
     * @param string           $timezone
125
     * @param string           $fromFormat
126
     *
127
     * @return string
128
     */
129
    public function toShort($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
130
    {
131
        return $this->format('short', $datetime, $timezone, $fromFormat);
132
    }
133
134
    /**
135
     * Returns date only e.g. 2014-08-09.
136
     *
137
     * @param \DateTime|string $datetime
138
     * @param string           $timezone
139
     * @param string           $fromFormat
140
     *
141
     * @return string
142
     */
143
    public function toDate($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
144
    {
145
        return $this->format('date', $datetime, $timezone, $fromFormat);
146
    }
147
148
    /**
149
     * Returns time only e.g. 21:19.
150
     *
151
     * @param \DateTime|string $datetime
152
     * @param string           $timezone
153
     * @param string           $fromFormat
154
     *
155
     * @return string
156
     */
157
    public function toTime($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s')
158
    {
159
        return $this->format('time', $datetime, $timezone, $fromFormat);
160
    }
161
162
    /**
163
     * Returns date/time like Today, 10:00 AM.
164
     *
165
     * @param        $datetime
166
     * @param string $timezone
167
     * @param string $fromFormat
168
     * @param bool   $forceDateForNonText If true, return as full date/time rather than "29 days ago"
169
     *
170
     * @return string
171
     */
172
    public function toText($datetime, $timezone = 'local', $fromFormat = 'Y-m-d H:i:s', $forceDateForNonText = false)
173
    {
174
        if (empty($datetime)) {
175
            return '';
176
        }
177
178
        $this->helper->setDateTime($datetime, $fromFormat, $timezone);
179
180
        $textDate = $this->helper->getTextDate();
181
        $dt       = $this->helper->getLocalDateTime();
182
183
        if ($textDate) {
184
            return $this->translator->trans('mautic.core.date.'.$textDate, ['%time%' => $dt->format($this->coreParametersHelper->get('date_format_timeonly'))]);
185
        } else {
186
            $interval = $this->helper->getDiff('now', null, true);
187
188
            if ($interval->invert && !$forceDateForNonText) {
189
                // In the past
190
                return $this->translator->trans('mautic.core.date.ago', ['%days%' => $interval->days]);
191
            } else {
192
                // In the future
193
                return $this->toFullConcat($datetime, $timezone, $fromFormat);
194
            }
195
        }
196
    }
197
198
    /**
199
     * Format DateInterval into humanly readable format.
200
     * Example: 55 minutes 49 seconds.
201
     * It doesn't return zero values like 0 years.
202
     *
203
     * @param DateInterval $range
204
     * @param string       $format
205
     *
206
     * @return string $formatedRange
207
     */
208
    public function formatRange($range, $format = null)
0 ignored issues
show
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

208
    public function formatRange($range, /** @scrutinizer ignore-unused */ $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
    {
210
        if ($range instanceof \DateInterval) {
211
            $formated  = [];
212
            $timeUnits = ['y' => 'year', 'm' => 'month', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second'];
213
214
            foreach ($timeUnits as $key => $unit) {
215
                if ($range->{$key}) {
216
                    $formated[] = $this->translator->transChoice(
217
                        'mautic.core.date.'.$unit,
218
                        $range->{$key},
219
                        ['%count%' => $range->{$key}]
220
                    );
221
                }
222
            }
223
224
            if (empty($formated)) {
225
                return $this->translator->trans('mautic.core.date.less.than.second');
226
            }
227
228
            return implode(' ', $formated);
229
        }
230
231
        return '';
232
    }
233
234
    /**
235
     * @return string
236
     */
237
    public function getFullFormat()
238
    {
239
        return $this->formats['datetime'];
240
    }
241
242
    /**
243
     * @return string
244
     */
245
    public function getDateFormat()
246
    {
247
        return $this->formats['date'];
248
    }
249
250
    /**
251
     * @return string
252
     */
253
    public function getTimeFormat()
254
    {
255
        return $this->formats['time'];
256
    }
257
258
    /**
259
     * @return string
260
     */
261
    public function getShortFormat()
262
    {
263
        return $this->formats['short'];
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function getName()
270
    {
271
        return 'date';
272
    }
273
}
274