xref: /webtrees/app/Statistics/Google/ChartMedia.php (revision 77a2010793e07b8d01d1a336fa5967181694729d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Google;
19
20use Fisharebest\Webtrees\GedcomTag;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Module\ModuleThemeInterface;
23use Fisharebest\Webtrees\Statistics\AbstractGoogle;
24use Fisharebest\Webtrees\Theme;
25
26/**
27 *
28 */
29class ChartMedia extends AbstractGoogle
30{
31    /**
32     * Create a chart of media types.
33     *
34     * @param int         $tot        The total number of media files
35     * @param array       $media      The list of media types to display
36     * @param string|null $size
37     * @param string|null $color_from
38     * @param string|null $color_to
39     *
40     * @return string
41     */
42    public function chartMedia(
43        int $tot,
44        array $media,
45        string $size       = null,
46        string $color_from = null,
47        string $color_to   = null
48    ): string {
49        $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values');
50        $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values');
51        $chart_x      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x');
52        $chart_y      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
53
54        $size       = $size ?? ($chart_x . 'x' . $chart_y);
55        $color_from = $color_from ?? $chart_color1;
56        $color_to   = $color_to ?? $chart_color2;
57        $sizes      = explode('x', $size);
58
59        // Beware divide by zero
60        if ($tot === 0) {
61            return I18N::translate('None');
62        }
63
64        // Build a table listing only the media types actually present in the GEDCOM
65        $mediaCounts = [];
66        $mediaTypes  = '';
67        $chart_title = '';
68
69        foreach ($media as $type => $count) {
70            $mediaCounts[] = intdiv(100 * $count, $tot);
71            $mediaTypes    .= GedcomTag::getFileFormTypeValue($type) . ' - ' . I18N::number($count) . '|';
72            $chart_title   .= GedcomTag::getFileFormTypeValue($type) . ' (' . $count . '), ';
73        }
74
75        $chart_title = substr($chart_title, 0, -2);
76        $chd         = $this->arrayToExtendedEncoding($mediaCounts);
77        $chl         = substr($mediaTypes, 0, -1);
78        $colors      = [$color_from, $color_to];
79
80        return view(
81            'statistics/other/chart-google',
82            [
83                'chart_title' => $chart_title,
84                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
85                'sizes'       => $sizes,
86            ]
87        );
88    }
89}
90