xref: /webtrees/app/Statistics/Repository/GedcomRepository.php (revision 29a51599e39417638370fe4c2c4d77201726c9ff)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Repository;
21
22use Fisharebest\Webtrees\Date;
23use Fisharebest\Webtrees\Fact;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\Statistics\Repository\Interfaces\GedcomRepositoryInterface;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Database\Capsule\Manager as DB;
28use Illuminate\Database\Query\Builder;
29
30/**
31 * A repository providing methods for GEDCOM related statistics.
32 */
33class GedcomRepository implements GedcomRepositoryInterface
34{
35    /**
36     * @var Tree
37     */
38    private $tree;
39
40    /**
41     * Constructor.
42     *
43     * @param Tree $tree
44     */
45    public function __construct(Tree $tree)
46    {
47        $this->tree = $tree;
48    }
49
50    /**
51     * Get information from the GEDCOM's HEAD record.
52     *
53     * @return string[]
54     */
55    private function gedcomHead(): array
56    {
57        $title   = '';
58        $version = '';
59        $source  = '';
60
61        $head = GedcomRecord::getInstance('HEAD', $this->tree);
62
63        if ($head instanceof GedcomRecord) {
64            $sour = $head->facts(['SOUR'])->first();
65
66            if ($sour instanceof Fact) {
67                $source  = $sour->value();
68                $title   = $sour->attribute('NAME');
69                $version = $sour->attribute('VERS');
70            }
71        }
72
73        return [
74            $title,
75            $version,
76            $source,
77        ];
78    }
79
80    /**
81     * @inheritDoc
82     */
83    public function gedcomFilename(): string
84    {
85        return $this->tree->name();
86    }
87
88    /**
89     * @inheritDoc
90     */
91    public function gedcomId(): int
92    {
93        return $this->tree->id();
94    }
95
96    /**
97     * @inheritDoc
98     */
99    public function gedcomTitle(): string
100    {
101        return e($this->tree->title());
102    }
103
104    /**
105     * @inheritDoc
106     */
107    public function gedcomCreatedSoftware(): string
108    {
109        return $this->gedcomHead()[0];
110    }
111
112    /**
113     * @inheritDoc
114     */
115    public function gedcomCreatedVersion(): string
116    {
117        $head = $this->gedcomHead();
118
119        if ($head === null) {
120            return '';
121        }
122
123        // Fix broken version string in Family Tree Maker
124        if (strpos($head[1], 'Family Tree Maker ') !== false) {
125            $p       = strpos($head[1], '(') + 1;
126            $p2      = strpos($head[1], ')');
127            $head[1] = substr($head[1], $p, $p2 - $p);
128        }
129
130        // Fix EasyTree version
131        if ($head[2] === 'EasyTree') {
132            $head[1] = substr($head[1], 1);
133        }
134
135        return $head[1];
136    }
137
138    /**
139     * @inheritDoc
140     */
141    public function gedcomDate(): string
142    {
143        $head = GedcomRecord::getInstance('HEAD', $this->tree);
144
145        if ($head instanceof GedcomRecord) {
146            $fact = $head->facts(['DATE'])->first();
147
148            if ($fact instanceof Fact) {
149                return (new Date($fact->value()))->display();
150            }
151        }
152
153        return '';
154    }
155
156    /**
157     * @inheritDoc
158     */
159    public function gedcomUpdated(): string
160    {
161        $row = DB::table('dates')
162            ->select(['d_year', 'd_month', 'd_day'])
163            ->where('d_julianday1', '=', function (Builder $query): void {
164                $query->selectRaw('MAX(d_julianday1)')
165                    ->from('dates')
166                    ->where('d_file', '=', $this->tree->id())
167                    ->where('d_fact', '=', 'CHAN');
168            })
169            ->first();
170
171        if ($row) {
172            $date = new Date("{$row->d_day} {$row->d_month} {$row->d_year}");
173            return $date->display();
174        }
175
176        return $this->gedcomDate();
177    }
178
179    /**
180     * @inheritDoc
181     */
182    public function gedcomRootId(): string
183    {
184        return $this->tree->getPreference('PEDIGREE_ROOT_ID');
185    }
186}
187