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