summaryrefslogtreecommitdiff
path: root/app/Statistics/Repository/MediaRepository.php
blob: 54c5569674d1bbf02fafbb287f2eb60789667404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2023 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Statistics\Repository;

use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Statistics\Google\ChartMedia;
use Fisharebest\Webtrees\Statistics\Repository\Interfaces\MediaRepositoryInterface;
use Fisharebest\Webtrees\Statistics\Service\ColorService;
use Fisharebest\Webtrees\Tree;
use Illuminate\Database\Query\Expression;

/**
 * A repository providing methods for media type related statistics.
 */
class MediaRepository implements MediaRepositoryInterface
{
    private ColorService $color_service;

    private Tree $tree;

    /**
     * Available media types.
     */
    private const MEDIA_TYPE_ALL         = 'all';
    private const MEDIA_TYPE_AUDIO       = 'audio';
    private const MEDIA_TYPE_BOOK        = 'book';
    private const MEDIA_TYPE_CARD        = 'card';
    private const MEDIA_TYPE_CERTIFICATE = 'certificate';
    private const MEDIA_TYPE_COAT        = 'coat';
    private const MEDIA_TYPE_DOCUMENT    = 'document';
    private const MEDIA_TYPE_ELECTRONIC  = 'electronic';
    private const MEDIA_TYPE_FICHE       = 'fiche';
    private const MEDIA_TYPE_FILM        = 'film';
    private const MEDIA_TYPE_MAGAZINE    = 'magazine';
    private const MEDIA_TYPE_MANUSCRIPT  = 'manuscript';
    private const MEDIA_TYPE_MAP         = 'map';
    private const MEDIA_TYPE_NEWSPAPER   = 'newspaper';
    private const MEDIA_TYPE_PAINTING    = 'painting';
    private const MEDIA_TYPE_PHOTO       = 'photo';
    private const MEDIA_TYPE_TOMBSTONE   = 'tombstone';
    private const MEDIA_TYPE_VIDEO       = 'video';
    private const MEDIA_TYPE_OTHER       = 'other';
    private const MEDIA_TYPE_UNKNOWN     = '';

    /**
     * @param ColorService $color_service
     * @param Tree         $tree
     */
    public function __construct(ColorService $color_service, Tree $tree)
    {
        $this->color_service = $color_service;
        $this->tree          = $tree;
    }

    /**
     * @param string $type
     *
     * @return string
     */
    public function totalMedia(string $type = self::MEDIA_TYPE_ALL): string
    {
        $query = DB::table('media_file')->where('m_file', '=', $this->tree->id());

        if ($type !== self::MEDIA_TYPE_ALL) {
            $query->where('source_media_type', '=', $type);
        }

        return I18N::number($query->count());
    }

    /**
     * @return string
     */
    public function totalMediaAudio(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_AUDIO);
    }

    /**
     * @return string
     */
    public function totalMediaBook(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_BOOK);
    }

    /**
     * @return string
     */
    public function totalMediaCard(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_CARD);
    }

    /**
     * @return string
     */
    public function totalMediaCertificate(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_CERTIFICATE);
    }

    /**
     * @return string
     */
    public function totalMediaCoatOfArms(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_COAT);
    }

    /**
     * @return string
     */
    public function totalMediaDocument(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_DOCUMENT);
    }

    /**
     * @return string
     */
    public function totalMediaElectronic(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_ELECTRONIC);
    }

    /**
     * @return string
     */
    public function totalMediaFiche(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_FICHE);
    }

    /**
     * @return string
     */
    public function totalMediaFilm(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_FILM);
    }

    /**
     * @return string
     */
    public function totalMediaMagazine(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_MAGAZINE);
    }

    /**
     * @return string
     */
    public function totalMediaManuscript(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_MANUSCRIPT);
    }

    /**
     * @return string
     */
    public function totalMediaMap(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_MAP);
    }

    /**
     * @return string
     */
    public function totalMediaNewspaper(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_NEWSPAPER);
    }

    /**
     * @return string
     */
    public function totalMediaPainting(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_PAINTING);
    }

    /**
     * @return string
     */
    public function totalMediaPhoto(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_PHOTO);
    }

    /**
     * @return string
     */
    public function totalMediaTombstone(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_TOMBSTONE);
    }

    /**
     * @return string
     */
    public function totalMediaVideo(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_VIDEO);
    }

    /**
     * @return string
     */
    public function totalMediaOther(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_OTHER);
    }

    /**
     * @return string
     */
    public function totalMediaUnknown(): string
    {
        return $this->totalMedia(self::MEDIA_TYPE_UNKNOWN);
    }

    /**
     * @param string|null $color_from
     * @param string|null $color_to
     *
     * @return string
     */
    public function chartMedia(string $color_from = null, string $color_to = null): string
    {
        $media = DB::table('media_file')
            ->where('m_file', '=', $this->tree->id())
            ->groupBy('source_media_type')
            ->pluck(new Expression('COUNT(*)'), 'source_media_type')
            ->map(static fn (string $n): int => (int) $n)
            ->all();

        return (new ChartMedia($this->color_service))
            ->chartMedia($media, $color_from, $color_to);
    }
}