xref: /webtrees/app/Statistics/Repository/MediaRepository.php (revision 09523927627d8824ee0799f8137d6de75b8b7921)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Repository;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Statistics\Google\ChartMedia;
25use Fisharebest\Webtrees\Statistics\Repository\Interfaces\MediaRepositoryInterface;
26use Fisharebest\Webtrees\Statistics\Service\ColorService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Database\Query\Expression;
29
30/**
31 * A repository providing methods for media type related statistics.
32 */
33class MediaRepository implements MediaRepositoryInterface
34{
35    private ColorService $color_service;
36
37    private Tree $tree;
38
39    /**
40     * Available media types.
41     */
42    private const MEDIA_TYPE_ALL         = 'all';
43    private const MEDIA_TYPE_AUDIO       = 'audio';
44    private const MEDIA_TYPE_BOOK        = 'book';
45    private const MEDIA_TYPE_CARD        = 'card';
46    private const MEDIA_TYPE_CERTIFICATE = 'certificate';
47    private const MEDIA_TYPE_COAT        = 'coat';
48    private const MEDIA_TYPE_DOCUMENT    = 'document';
49    private const MEDIA_TYPE_ELECTRONIC  = 'electronic';
50    private const MEDIA_TYPE_FICHE       = 'fiche';
51    private const MEDIA_TYPE_FILM        = 'film';
52    private const MEDIA_TYPE_MAGAZINE    = 'magazine';
53    private const MEDIA_TYPE_MANUSCRIPT  = 'manuscript';
54    private const MEDIA_TYPE_MAP         = 'map';
55    private const MEDIA_TYPE_NEWSPAPER   = 'newspaper';
56    private const MEDIA_TYPE_PAINTING    = 'painting';
57    private const MEDIA_TYPE_PHOTO       = 'photo';
58    private const MEDIA_TYPE_TOMBSTONE   = 'tombstone';
59    private const MEDIA_TYPE_VIDEO       = 'video';
60    private const MEDIA_TYPE_OTHER       = 'other';
61    private const MEDIA_TYPE_UNKNOWN     = '';
62
63    /**
64     * @param ColorService $color_service
65     * @param Tree         $tree
66     */
67    public function __construct(ColorService $color_service, Tree $tree)
68    {
69        $this->color_service = $color_service;
70        $this->tree          = $tree;
71    }
72
73    /**
74     * @param string $type
75     *
76     * @return string
77     */
78    public function totalMedia(string $type = self::MEDIA_TYPE_ALL): string
79    {
80        $query = DB::table('media_file')->where('m_file', '=', $this->tree->id());
81
82        if ($type !== self::MEDIA_TYPE_ALL) {
83            $query->where('source_media_type', '=', $type);
84        }
85
86        return I18N::number($query->count());
87    }
88
89    /**
90     * @return string
91     */
92    public function totalMediaAudio(): string
93    {
94        return $this->totalMedia(self::MEDIA_TYPE_AUDIO);
95    }
96
97    /**
98     * @return string
99     */
100    public function totalMediaBook(): string
101    {
102        return $this->totalMedia(self::MEDIA_TYPE_BOOK);
103    }
104
105    /**
106     * @return string
107     */
108    public function totalMediaCard(): string
109    {
110        return $this->totalMedia(self::MEDIA_TYPE_CARD);
111    }
112
113    /**
114     * @return string
115     */
116    public function totalMediaCertificate(): string
117    {
118        return $this->totalMedia(self::MEDIA_TYPE_CERTIFICATE);
119    }
120
121    /**
122     * @return string
123     */
124    public function totalMediaCoatOfArms(): string
125    {
126        return $this->totalMedia(self::MEDIA_TYPE_COAT);
127    }
128
129    /**
130     * @return string
131     */
132    public function totalMediaDocument(): string
133    {
134        return $this->totalMedia(self::MEDIA_TYPE_DOCUMENT);
135    }
136
137    /**
138     * @return string
139     */
140    public function totalMediaElectronic(): string
141    {
142        return $this->totalMedia(self::MEDIA_TYPE_ELECTRONIC);
143    }
144
145    /**
146     * @return string
147     */
148    public function totalMediaFiche(): string
149    {
150        return $this->totalMedia(self::MEDIA_TYPE_FICHE);
151    }
152
153    /**
154     * @return string
155     */
156    public function totalMediaFilm(): string
157    {
158        return $this->totalMedia(self::MEDIA_TYPE_FILM);
159    }
160
161    /**
162     * @return string
163     */
164    public function totalMediaMagazine(): string
165    {
166        return $this->totalMedia(self::MEDIA_TYPE_MAGAZINE);
167    }
168
169    /**
170     * @return string
171     */
172    public function totalMediaManuscript(): string
173    {
174        return $this->totalMedia(self::MEDIA_TYPE_MANUSCRIPT);
175    }
176
177    /**
178     * @return string
179     */
180    public function totalMediaMap(): string
181    {
182        return $this->totalMedia(self::MEDIA_TYPE_MAP);
183    }
184
185    /**
186     * @return string
187     */
188    public function totalMediaNewspaper(): string
189    {
190        return $this->totalMedia(self::MEDIA_TYPE_NEWSPAPER);
191    }
192
193    /**
194     * @return string
195     */
196    public function totalMediaPainting(): string
197    {
198        return $this->totalMedia(self::MEDIA_TYPE_PAINTING);
199    }
200
201    /**
202     * @return string
203     */
204    public function totalMediaPhoto(): string
205    {
206        return $this->totalMedia(self::MEDIA_TYPE_PHOTO);
207    }
208
209    /**
210     * @return string
211     */
212    public function totalMediaTombstone(): string
213    {
214        return $this->totalMedia(self::MEDIA_TYPE_TOMBSTONE);
215    }
216
217    /**
218     * @return string
219     */
220    public function totalMediaVideo(): string
221    {
222        return $this->totalMedia(self::MEDIA_TYPE_VIDEO);
223    }
224
225    /**
226     * @return string
227     */
228    public function totalMediaOther(): string
229    {
230        return $this->totalMedia(self::MEDIA_TYPE_OTHER);
231    }
232
233    /**
234     * @return string
235     */
236    public function totalMediaUnknown(): string
237    {
238        return $this->totalMedia(self::MEDIA_TYPE_UNKNOWN);
239    }
240
241    /**
242     * @param string|null $color_from
243     * @param string|null $color_to
244     *
245     * @return string
246     */
247    public function chartMedia(string $color_from = null, string $color_to = null): string
248    {
249        $media = DB::table('media_file')
250            ->where('m_file', '=', $this->tree->id())
251            ->groupBy('source_media_type')
252            ->pluck(new Expression('COUNT(*)'), 'source_media_type')
253            ->map(static fn (string $n): int => (int) $n)
254            ->all();
255
256        return (new ChartMedia($this->color_service))
257            ->chartMedia($media, $color_from, $color_to);
258    }
259}
260