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