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