-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathabraflexi-weekdigest.php
More file actions
119 lines (96 loc) · 3.56 KB
/
abraflexi-weekdigest.php
File metadata and controls
119 lines (96 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
/**
* This file is part of the AbraFlexi-Digest package
*
* https://github.com/VitexSoftware/AbraFlexi-Digest/
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AbraFlexi\Digest;
\define('EASE_APPNAME', 'AbraFlexi 📆 WeekDigest');
require_once 'init.php';
$start = new \DateTime();
$start->modify('-1 week');
$end = new \DateTime();
$period = new \DatePeriod($start, new \DateInterval('P1D'), $end);
try {
$fmt = datefmt_create(
'cs_CZ',
\IntlDateFormatter::SHORT,
\IntlDateFormatter::NONE,
'Europe/Prague',
\IntlDateFormatter::GREGORIAN,
);
} catch (\ValueError $e) {
$fmt = false;
}
// Check if datefmt_create failed and create fallback
if ($fmt === false) {
try {
$fmt = datefmt_create(
'en_US',
\IntlDateFormatter::SHORT,
\IntlDateFormatter::NONE,
'UTC',
\IntlDateFormatter::GREGORIAN,
);
} catch (\ValueError $e) {
// If even the fallback fails, we'll handle it later
$fmt = false;
}
}
// Create IntlDateFormatter with proper error handling (procedural API)
$locale = \Ease\Locale::$localeUsed ?? 'en_US';
try {
$formatter = \datefmt_create($locale, \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, 'Europe/Prague');
} catch (\ValueError $e) {
$formatter = false;
}
// If creation failed, try with a fallback locale
if ($formatter === false) {
try {
$formatter = \datefmt_create('en_US', \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, 'Europe/Prague');
} catch (\ValueError $e) {
$formatter = false;
}
}
$period = new \DatePeriod($start, new \DateInterval('P1D'), $end);
$myCompany = new \AbraFlexi\Company(\Ease\Shared::cfg('ABRAFLEXI_COMPANY'));
$myCompanyName = $myCompany->getDataValue('nazev');
$subject = sprintf(_('AbraFlexi %s 📆 Weekly digest'), $myCompanyName);
$digestor = new Digestor($subject);
// Helper to validate formatter objects and avoid "unconstructed" fatals
$isFormatterValid = static function ($fmt): bool {
if (!$fmt instanceof \IntlDateFormatter) {
return false;
}
// When the formatter is not properly constructed, error code is not zero
$code = \datefmt_get_error_code($fmt);
return \function_exists('intl_is_failure') ? !\intl_is_failure($code) : ($code === \U_ZERO_ERROR);
};
// Format dates with fallback if formatter failed
if ($formatter !== false && $isFormatterValid($formatter)) {
$startFormatted = \datefmt_format($formatter, $period->getStartDate()->getTimestamp());
$endFormatted = \datefmt_format($formatter, $period->getEndDate()->getTimestamp());
if ($startFormatted === false) {
$startFormatted = $period->getStartDate()->format('Y-m-d');
}
if ($endFormatted === false) {
$endFormatted = $period->getEndDate()->format('Y-m-d');
}
} else {
$startFormatted = $period->getStartDate()->format('Y-m-d');
$endFormatted = $period->getEndDate()->format('Y-m-d');
}
$digestor->addItem(new \Ease\Html\DivTag(sprintf(
_('from %s to %s'),
$startFormatted,
$endFormatted,
)));
\Ease\Functions::loadClassesInNamespace('AbraFlexi\Digest\Modules');
\Ease\Functions::loadClassesInNamespace('AbraFlexi\Digest\Modules\Weekly');
$digestor->dig($period, array_merge(\Ease\Functions::classesInNamespace('AbraFlexi\Digest\Modules', true), \Ease\Functions::classesInNamespace('AbraFlexi\Digest\Modules\Weekly', true)));