xref: /webtrees/app/Statistics/Repository/MediaRepository.php (revision d11be7027e34e3121be11cc025421873364403f9)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Repository;
218add1155SRico Sonntag
228add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
238add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartMedia;
248add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Repository\Interfaces\MediaRepositoryInterface;
25f78da678SGreg Roachuse Fisharebest\Webtrees\Statistics\Service\ColorService;
268add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
278add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
28e7bff7b8SGreg Roachuse Illuminate\Database\Query\Expression;
29e7bff7b8SGreg Roach
308add1155SRico Sonntag/**
318add1155SRico Sonntag * A repository providing methods for media type related statistics.
328add1155SRico Sonntag */
338add1155SRico Sonntagclass MediaRepository implements MediaRepositoryInterface
348add1155SRico Sonntag{
35f78da678SGreg Roach    private ColorService $color_service;
36f78da678SGreg Roach
374c78e066SGreg Roach    private Tree $tree;
388add1155SRico Sonntag
398add1155SRico Sonntag    /**
408add1155SRico Sonntag     * Available media types.
418add1155SRico Sonntag     */
428add1155SRico Sonntag    private const MEDIA_TYPE_ALL         = 'all';
438add1155SRico Sonntag    private const MEDIA_TYPE_AUDIO       = 'audio';
448add1155SRico Sonntag    private const MEDIA_TYPE_BOOK        = 'book';
458add1155SRico Sonntag    private const MEDIA_TYPE_CARD        = 'card';
468add1155SRico Sonntag    private const MEDIA_TYPE_CERTIFICATE = 'certificate';
478add1155SRico Sonntag    private const MEDIA_TYPE_COAT        = 'coat';
488add1155SRico Sonntag    private const MEDIA_TYPE_DOCUMENT    = 'document';
498add1155SRico Sonntag    private const MEDIA_TYPE_ELECTRONIC  = 'electronic';
508add1155SRico Sonntag    private const MEDIA_TYPE_FICHE       = 'fiche';
518add1155SRico Sonntag    private const MEDIA_TYPE_FILM        = 'film';
528add1155SRico Sonntag    private const MEDIA_TYPE_MAGAZINE    = 'magazine';
538add1155SRico Sonntag    private const MEDIA_TYPE_MANUSCRIPT  = 'manuscript';
548add1155SRico Sonntag    private const MEDIA_TYPE_MAP         = 'map';
558add1155SRico Sonntag    private const MEDIA_TYPE_NEWSPAPER   = 'newspaper';
568add1155SRico Sonntag    private const MEDIA_TYPE_PAINTING    = 'painting';
578add1155SRico Sonntag    private const MEDIA_TYPE_PHOTO       = 'photo';
588add1155SRico Sonntag    private const MEDIA_TYPE_TOMBSTONE   = 'tombstone';
598add1155SRico Sonntag    private const MEDIA_TYPE_VIDEO       = 'video';
608add1155SRico Sonntag    private const MEDIA_TYPE_OTHER       = 'other';
61e7bff7b8SGreg Roach    private const MEDIA_TYPE_UNKNOWN     = '';
628add1155SRico Sonntag
638add1155SRico Sonntag    /**
64f78da678SGreg Roach     * @param ColorService $color_service
658add1155SRico Sonntag     * @param Tree         $tree
668add1155SRico Sonntag     */
67f78da678SGreg Roach    public function __construct(ColorService $color_service, Tree $tree)
688add1155SRico Sonntag    {
69f78da678SGreg Roach        $this->color_service = $color_service;
708add1155SRico Sonntag        $this->tree          = $tree;
718add1155SRico Sonntag    }
728add1155SRico Sonntag
738add1155SRico Sonntag    /**
74e7bff7b8SGreg Roach     * @param string $type
75e7bff7b8SGreg Roach     *
760dcd9387SGreg Roach     * @return string
778add1155SRico Sonntag     */
78e7bff7b8SGreg Roach    public function totalMedia(string $type = self::MEDIA_TYPE_ALL): string
798add1155SRico Sonntag    {
80e7bff7b8SGreg Roach        $query = DB::table('media_file')->where('m_file', '=', $this->tree->id());
81e7bff7b8SGreg Roach
82e7bff7b8SGreg Roach        if ($type !== self::MEDIA_TYPE_ALL) {
83e7bff7b8SGreg Roach            $query->where('source_media_type', '=', $type);
84e7bff7b8SGreg Roach        }
85e7bff7b8SGreg Roach
86e7bff7b8SGreg Roach        return I18N::number($query->count());
878add1155SRico Sonntag    }
888add1155SRico Sonntag
898add1155SRico Sonntag    /**
900dcd9387SGreg Roach     * @return string
918add1155SRico Sonntag     */
928add1155SRico Sonntag    public function totalMediaAudio(): string
938add1155SRico Sonntag    {
94e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_AUDIO);
958add1155SRico Sonntag    }
968add1155SRico Sonntag
978add1155SRico Sonntag    /**
980dcd9387SGreg Roach     * @return string
998add1155SRico Sonntag     */
1008add1155SRico Sonntag    public function totalMediaBook(): string
1018add1155SRico Sonntag    {
102e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_BOOK);
1038add1155SRico Sonntag    }
1048add1155SRico Sonntag
1058add1155SRico Sonntag    /**
1060dcd9387SGreg Roach     * @return string
1078add1155SRico Sonntag     */
1088add1155SRico Sonntag    public function totalMediaCard(): string
1098add1155SRico Sonntag    {
110e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_CARD);
1118add1155SRico Sonntag    }
1128add1155SRico Sonntag
1138add1155SRico Sonntag    /**
1140dcd9387SGreg Roach     * @return string
1158add1155SRico Sonntag     */
1168add1155SRico Sonntag    public function totalMediaCertificate(): string
1178add1155SRico Sonntag    {
118e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_CERTIFICATE);
1198add1155SRico Sonntag    }
1208add1155SRico Sonntag
1218add1155SRico Sonntag    /**
1220dcd9387SGreg Roach     * @return string
1238add1155SRico Sonntag     */
1248add1155SRico Sonntag    public function totalMediaCoatOfArms(): string
1258add1155SRico Sonntag    {
126e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_COAT);
1278add1155SRico Sonntag    }
1288add1155SRico Sonntag
1298add1155SRico Sonntag    /**
1300dcd9387SGreg Roach     * @return string
1318add1155SRico Sonntag     */
1328add1155SRico Sonntag    public function totalMediaDocument(): string
1338add1155SRico Sonntag    {
134e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_DOCUMENT);
1358add1155SRico Sonntag    }
1368add1155SRico Sonntag
1378add1155SRico Sonntag    /**
1380dcd9387SGreg Roach     * @return string
1398add1155SRico Sonntag     */
1408add1155SRico Sonntag    public function totalMediaElectronic(): string
1418add1155SRico Sonntag    {
142e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_ELECTRONIC);
1438add1155SRico Sonntag    }
1448add1155SRico Sonntag
1458add1155SRico Sonntag    /**
1460dcd9387SGreg Roach     * @return string
1478add1155SRico Sonntag     */
1488add1155SRico Sonntag    public function totalMediaFiche(): string
1498add1155SRico Sonntag    {
150e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_FICHE);
1518add1155SRico Sonntag    }
1528add1155SRico Sonntag
1538add1155SRico Sonntag    /**
1540dcd9387SGreg Roach     * @return string
1558add1155SRico Sonntag     */
1568add1155SRico Sonntag    public function totalMediaFilm(): string
1578add1155SRico Sonntag    {
158e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_FILM);
1598add1155SRico Sonntag    }
1608add1155SRico Sonntag
1618add1155SRico Sonntag    /**
1620dcd9387SGreg Roach     * @return string
1638add1155SRico Sonntag     */
1648add1155SRico Sonntag    public function totalMediaMagazine(): string
1658add1155SRico Sonntag    {
166e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_MAGAZINE);
1678add1155SRico Sonntag    }
1688add1155SRico Sonntag
1698add1155SRico Sonntag    /**
1700dcd9387SGreg Roach     * @return string
1718add1155SRico Sonntag     */
1728add1155SRico Sonntag    public function totalMediaManuscript(): string
1738add1155SRico Sonntag    {
174e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_MANUSCRIPT);
1758add1155SRico Sonntag    }
1768add1155SRico Sonntag
1778add1155SRico Sonntag    /**
1780dcd9387SGreg Roach     * @return string
1798add1155SRico Sonntag     */
1808add1155SRico Sonntag    public function totalMediaMap(): string
1818add1155SRico Sonntag    {
182e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_MAP);
1838add1155SRico Sonntag    }
1848add1155SRico Sonntag
1858add1155SRico Sonntag    /**
1860dcd9387SGreg Roach     * @return string
1878add1155SRico Sonntag     */
1888add1155SRico Sonntag    public function totalMediaNewspaper(): string
1898add1155SRico Sonntag    {
190e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_NEWSPAPER);
1918add1155SRico Sonntag    }
1928add1155SRico Sonntag
1938add1155SRico Sonntag    /**
1940dcd9387SGreg Roach     * @return string
1958add1155SRico Sonntag     */
1968add1155SRico Sonntag    public function totalMediaPainting(): string
1978add1155SRico Sonntag    {
198e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_PAINTING);
1998add1155SRico Sonntag    }
2008add1155SRico Sonntag
2018add1155SRico Sonntag    /**
2020dcd9387SGreg Roach     * @return string
2038add1155SRico Sonntag     */
2048add1155SRico Sonntag    public function totalMediaPhoto(): string
2058add1155SRico Sonntag    {
206e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_PHOTO);
2078add1155SRico Sonntag    }
2088add1155SRico Sonntag
2098add1155SRico Sonntag    /**
2100dcd9387SGreg Roach     * @return string
2118add1155SRico Sonntag     */
2128add1155SRico Sonntag    public function totalMediaTombstone(): string
2138add1155SRico Sonntag    {
214e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_TOMBSTONE);
2158add1155SRico Sonntag    }
2168add1155SRico Sonntag
2178add1155SRico Sonntag    /**
2180dcd9387SGreg Roach     * @return string
2198add1155SRico Sonntag     */
2208add1155SRico Sonntag    public function totalMediaVideo(): string
2218add1155SRico Sonntag    {
222e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_VIDEO);
2238add1155SRico Sonntag    }
2248add1155SRico Sonntag
2258add1155SRico Sonntag    /**
2260dcd9387SGreg Roach     * @return string
2278add1155SRico Sonntag     */
2288add1155SRico Sonntag    public function totalMediaOther(): string
2298add1155SRico Sonntag    {
230e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_OTHER);
2318add1155SRico Sonntag    }
2328add1155SRico Sonntag
2338add1155SRico Sonntag    /**
2340dcd9387SGreg Roach     * @return string
2358add1155SRico Sonntag     */
2368add1155SRico Sonntag    public function totalMediaUnknown(): string
2378add1155SRico Sonntag    {
238e7bff7b8SGreg Roach        return $this->totalMedia(self::MEDIA_TYPE_UNKNOWN);
2398add1155SRico Sonntag    }
2408add1155SRico Sonntag
2418add1155SRico Sonntag    /**
2420dcd9387SGreg Roach     * @param string|null $color_from
2430dcd9387SGreg Roach     * @param string|null $color_to
2440dcd9387SGreg Roach     *
2450dcd9387SGreg Roach     * @return string
2468add1155SRico Sonntag     */
24788de55fdSRico Sonntag    public function chartMedia(string $color_from = null, string $color_to = null): string
2488add1155SRico Sonntag    {
249d1c4161eSGreg Roach        $media = DB::table('media_file')
250d1c4161eSGreg Roach            ->where('m_file', '=', $this->tree->id())
251d1c4161eSGreg Roach            ->groupBy('source_media_type')
252d1c4161eSGreg Roach            ->pluck(new Expression('COUNT(*)'), 'source_media_type')
253d1c4161eSGreg Roach            ->map(static fn (string $n): int => (int) $n)
254d1c4161eSGreg Roach            ->all();
2558add1155SRico Sonntag
256f78da678SGreg Roach        return (new ChartMedia($this->color_service))
25788de55fdSRico Sonntag            ->chartMedia($media, $color_from, $color_to);
2588add1155SRico Sonntag    }
2598add1155SRico Sonntag}
260