Skip to content

Commit 51f9bf9

Browse files
committed
fix: deprecation notice in volumes report
Fix deprecation notice in volumes report for users using PHP 8.2 and above. Replace unmaintained pear/date_humandiff dependency by nesbot/carbon. Use datetime_format_short from the configuration if the user has set it. Resolve #237
1 parent e110875 commit 51f9bf9

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

application/Controller/VolumesController.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
use App\Libs\Config;
2525
use App\Validator\VolumesRequestValidator;
26+
use Carbon\Carbon;
2627
use Core\Db\CDBQuery;
2728
use Core\Db\DBPagination;
2829
use Core\Utils\CUtils;
2930
use App\Table\VolumeTable;
3031
use App\Table\PoolTable;
31-
use Date_HumanDiff;
3232
use Exception;
3333
use Psr\Http\Message\ServerRequestInterface as Request;
3434
use GuzzleHttp\Psr7\Response;
@@ -178,21 +178,25 @@ public function index(Request $request, Response $response): Response
178178
'where' => $where ]);
179179

180180
foreach ($pagination->paginate($this->volumeTable, $sqlQuery, $countquery, $params) as $volume) {
181-
// Calculate volume expiration
182-
// If volume have already been used
181+
/**
182+
* Calculate volume expiration only if volume has already been written
183+
*/
183184
if ($volume['lastwritten'] != "0000-00-00 00:00:00" && !is_null($volume['lastwritten'])) {
184-
// Calculate expiration date only if volume status is Full or Used
185+
/**
186+
* Calculate expiration date only if volume status is Full or Used
187+
*/
185188
if ($volume['volstatus'] == 'Full' || $volume['volstatus'] == 'Used') {
186-
$dh = new Date_HumanDiff();
187-
$dateTimeFormatShort = explode(' ', $this->config->get('datetime_format', 'Y-m-d H:i:s'));
188-
$volume['expire'] = date(
189-
$dateTimeFormatShort[0],
190-
strtotime($volume['lastwritten']) + $volume['volretention']
191-
);
192-
$volume['expire'] = $dh->get(
193-
strtotime($volume['lastwritten']) + $volume['volretention'],
194-
time()
195-
) . ' (' . $volume['expire'] . ')';
189+
if ($this->config->has('datetime_format_short')) {
190+
$dateTimeFormatShort = $this->config->get('datetime_format_short');
191+
} else {
192+
$dateTimeFormatShort = explode(' ', $this->config->get('datetime_format', 'Y-m-d H:i:s'));
193+
$dateTimeFormatShort = $dateTimeFormatShort[0];
194+
}
195+
196+
$volumeExpiration = Carbon::parse($volume['lastwritten'])->addSeconds($volume['volretention']);
197+
$volume['expire'] =
198+
$volumeExpiration->format($dateTimeFormatShort) .
199+
' (in ' . (int) Carbon::now()->diffInDays($volumeExpiration) . ' day(s) )';
196200
} else {
197201
$volume['expire'] = 'n/a';
198202
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"novus/nvd3": "@stable",
4949
"components/font-awesome": "6.*",
5050
"vlucas/valitron": "^1.4",
51-
"pear/date_humandiff": "^0.5.0",
5251
"vlucas/phpdotenv": "^5.5",
5352
"php-di/php-di": "^7.0",
5453
"twbs/bootstrap": "5.2.3",
@@ -59,7 +58,8 @@
5958
"symfony/translation": "^5.4",
6059
"symfony/console": "6.0.19",
6160
"slim/csrf": "^1.3",
62-
"symfony/twig-bridge": "^5.4"
61+
"symfony/twig-bridge": "^5.4",
62+
"nesbot/carbon": "^3.8"
6363
},
6464
"require-dev": {
6565
"phpmd/phpmd": "@stable",

0 commit comments

Comments
 (0)