xref: /webtrees/app/Http/RequestHandlers/PendingChangesLogDownload.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
122e73debSGreg Roach<?php
222e73debSGreg Roach
322e73debSGreg Roach/**
422e73debSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
622e73debSGreg Roach * This program is free software: you can redistribute it and/or modify
722e73debSGreg Roach * it under the terms of the GNU General Public License as published by
822e73debSGreg Roach * the Free Software Foundation, either version 3 of the License, or
922e73debSGreg Roach * (at your option) any later version.
1022e73debSGreg Roach * This program is distributed in the hope that it will be useful,
1122e73debSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1222e73debSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1322e73debSGreg Roach * GNU General Public License for more details.
1422e73debSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1622e73debSGreg Roach */
1722e73debSGreg Roach
1822e73debSGreg Roachdeclare(strict_types=1);
1922e73debSGreg Roach
2022e73debSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2122e73debSGreg Roach
2222e73debSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2322e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService;
24*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2522e73debSGreg Roachuse Psr\Http\Message\ResponseInterface;
2622e73debSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2722e73debSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2822e73debSGreg Roach
2922e73debSGreg Roachuse function response;
3022e73debSGreg Roach
3122e73debSGreg Roach/**
3222e73debSGreg Roach * Download pending changes.
3322e73debSGreg Roach */
3422e73debSGreg Roachclass PendingChangesLogDownload implements RequestHandlerInterface
3522e73debSGreg Roach{
36c4943cffSGreg Roach    private PendingChangesService $pending_changes_service;
3722e73debSGreg Roach
3822e73debSGreg Roach    /**
3922e73debSGreg Roach     * @param PendingChangesService $pending_changes_service
4022e73debSGreg Roach     */
4122e73debSGreg Roach    public function __construct(PendingChangesService $pending_changes_service)
4222e73debSGreg Roach    {
4322e73debSGreg Roach        $this->pending_changes_service = $pending_changes_service;
4422e73debSGreg Roach    }
4522e73debSGreg Roach
4622e73debSGreg Roach    /**
4722e73debSGreg Roach     * @param ServerRequestInterface $request
4822e73debSGreg Roach     *
4922e73debSGreg Roach     * @return ResponseInterface
5022e73debSGreg Roach     */
5122e73debSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
5222e73debSGreg Roach    {
53*b55cbc6bSGreg Roach        $tree           = Validator::attributes($request)->tree();
5457bfa969SGreg Roach        $params         = $request->getQueryParams();
5557bfa969SGreg Roach        $params['tree'] = $tree->name();
5657bfa969SGreg Roach
5757bfa969SGreg Roach        $content = $this->pending_changes_service->changesQuery($params)
5822e73debSGreg Roach            ->get()
59f70bcff5SGreg Roach            ->map(static function (object $row): string {
6022e73debSGreg Roach                // Convert to CSV
6122e73debSGreg Roach                return implode(',', [
6222e73debSGreg Roach                    '"' . $row->change_time . '"',
6322e73debSGreg Roach                    '"' . $row->status . '"',
6422e73debSGreg Roach                    '"' . $row->xref . '"',
6522e73debSGreg Roach                    '"' . str_replace('"', '""', $row->old_gedcom) . '"',
6622e73debSGreg Roach                    '"' . str_replace('"', '""', $row->new_gedcom) . '"',
6722e73debSGreg Roach                    '"' . str_replace('"', '""', $row->user_name) . '"',
6822e73debSGreg Roach                    '"' . str_replace('"', '""', $row->gedcom_name) . '"',
6922e73debSGreg Roach                ]);
7022e73debSGreg Roach            })
7122e73debSGreg Roach            ->implode("\n");
7222e73debSGreg Roach
7322e73debSGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
74a0e7c429SGreg Roach            'Content-Type'        => 'text/csv; charset=UTF-8',
7522e73debSGreg Roach            'Content-Disposition' => 'attachment; filename="changes.csv"',
7622e73debSGreg Roach        ]);
7722e73debSGreg Roach    }
7822e73debSGreg Roach}
79