xref: /webtrees/app/Module/FanChartModule.php (revision 8136679edbff8170fb92bd6e8dd8df8eb645bb1a)
1168ff6f3Sric2016<?php
2168ff6f3Sric2016/**
3168ff6f3Sric2016 * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
6168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
7168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
8168ff6f3Sric2016 * (at your option) any later version.
9168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
10168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12168ff6f3Sric2016 * GNU General Public License for more details.
13168ff6f3Sric2016 * You should have received a copy of the GNU General Public License
14168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15168ff6f3Sric2016 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
18168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
19168ff6f3Sric2016
20389266c0SGreg Roachuse Fisharebest\Webtrees\Auth;
21168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
22168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
236664b4a3SGreg Roachuse Fisharebest\Webtrees\Menu;
24389266c0SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
25389266c0SGreg Roachuse Fisharebest\Webtrees\Theme;
26389266c0SGreg Roachuse Fisharebest\Webtrees\Tree;
279867b2f0SGreg Roachuse Fisharebest\Webtrees\User;
28389266c0SGreg Roachuse Symfony\Component\HttpFoundation\Request;
29389266c0SGreg Roachuse Symfony\Component\HttpFoundation\Response;
30168ff6f3Sric2016
31168ff6f3Sric2016/**
32168ff6f3Sric2016 * Class FanChartModule
33168ff6f3Sric2016 */
3437eb8894SGreg Roachclass FanChartModule extends AbstractModule implements ModuleChartInterface
35c1010edaSGreg Roach{
3649a243cbSGreg Roach    use ModuleChartTrait;
3749a243cbSGreg Roach
38389266c0SGreg Roach    // Chart styles
39389266c0SGreg Roach    private const STYLE_HALF_CIRCLE          = 2;
40389266c0SGreg Roach    private const STYLE_THREE_QUARTER_CIRCLE = 3;
41389266c0SGreg Roach    private const STYLE_FULL_CIRCLE          = 4;
42389266c0SGreg Roach
43389266c0SGreg Roach    // Limits
44389266c0SGreg Roach    private const MINIMUM_GENERATIONS = 2;
45389266c0SGreg Roach    private const MAXIMUM_GENERATIONS = 9;
46389266c0SGreg Roach    private const MINIMUM_WIDTH       = 50;
47389266c0SGreg Roach    private const MAXIMUM_WIDTH       = 500;
48389266c0SGreg Roach
49389266c0SGreg Roach    // Defaults
50389266c0SGreg Roach    private const DEFAULT_STYLE       = self::STYLE_THREE_QUARTER_CIRCLE;
51389266c0SGreg Roach    private const DEFAULT_GENERATIONS = 4;
52389266c0SGreg Roach    private const DEFAULT_WIDTH       = 100;
53389266c0SGreg Roach
54168ff6f3Sric2016    /**
55168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
56168ff6f3Sric2016     *
57168ff6f3Sric2016     * @return string
58168ff6f3Sric2016     */
5949a243cbSGreg Roach    public function title(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
62bbb76c12SGreg Roach        return I18N::translate('Fan chart');
63168ff6f3Sric2016    }
64168ff6f3Sric2016
65168ff6f3Sric2016    /**
66168ff6f3Sric2016     * A sentence describing what this module does.
67168ff6f3Sric2016     *
68168ff6f3Sric2016     * @return string
69168ff6f3Sric2016     */
7049a243cbSGreg Roach    public function description(): string
71c1010edaSGreg Roach    {
72bbb76c12SGreg Roach        /* I18N: Description of the “Fan Chart” module */
73bbb76c12SGreg Roach        return I18N::translate('A fan chart of an individual’s ancestors.');
74168ff6f3Sric2016    }
75168ff6f3Sric2016
76168ff6f3Sric2016    /**
77377a2979SGreg Roach     * CSS class for the URL.
78377a2979SGreg Roach     *
79377a2979SGreg Roach     * @return string
80377a2979SGreg Roach     */
81377a2979SGreg Roach    public function chartMenuClass(): string
82377a2979SGreg Roach    {
83377a2979SGreg Roach        return 'menu-chart-fanchart';
84377a2979SGreg Roach    }
85377a2979SGreg Roach
86377a2979SGreg Roach    /**
874eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
884eb71cfaSGreg Roach     *
8960bc3e3fSGreg Roach     * @param Individual $individual
9060bc3e3fSGreg Roach     *
914eb71cfaSGreg Roach     * @return Menu|null
924eb71cfaSGreg Roach     */
93377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
94c1010edaSGreg Roach    {
95e6562982SGreg Roach        return $this->chartMenu($individual);
96e6562982SGreg Roach    }
97e6562982SGreg Roach
98e6562982SGreg Roach    /**
99e6562982SGreg Roach     * The title for a specific instance of this chart.
100e6562982SGreg Roach     *
101e6562982SGreg Roach     * @param Individual $individual
102e6562982SGreg Roach     *
103e6562982SGreg Roach     * @return string
104e6562982SGreg Roach     */
105e6562982SGreg Roach    public function chartTitle(Individual $individual): string
106e6562982SGreg Roach    {
107389266c0SGreg Roach        /* I18N: http://en.wikipedia.org/wiki/Family_tree#Fan_chart - %s is an individual’s name */
108e6562982SGreg Roach        return I18N::translate('Fan chart of %s', $individual->getFullName());
109e6562982SGreg Roach    }
110e6562982SGreg Roach
111e6562982SGreg Roach    /**
112389266c0SGreg Roach     * A form to request the chart parameters.
113389266c0SGreg Roach     *
114389266c0SGreg Roach     * @param Request      $request
115389266c0SGreg Roach     * @param Tree         $tree
1169867b2f0SGreg Roach     * @param User         $user
117389266c0SGreg Roach     * @param ChartService $chart_service
118389266c0SGreg Roach     *
119389266c0SGreg Roach     * @return Response
120389266c0SGreg Roach     */
1219867b2f0SGreg Roach    public function getChartAction(Request $request, Tree $tree, User $user, ChartService $chart_service): Response
122389266c0SGreg Roach    {
1239b5537c3SGreg Roach        $ajax       = (bool) $request->get('ajax');
124389266c0SGreg Roach        $xref       = $request->get('xref', '');
125389266c0SGreg Roach        $individual = Individual::getInstance($xref, $tree);
126389266c0SGreg Roach
127389266c0SGreg Roach        Auth::checkIndividualAccess($individual);
1289867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
129389266c0SGreg Roach
130389266c0SGreg Roach        $chart_style = (int) $request->get('chart_style', self::DEFAULT_STYLE);
131389266c0SGreg Roach        $fan_width   = (int) $request->get('fan_width', self::DEFAULT_WIDTH);
132389266c0SGreg Roach        $generations = (int) $request->get('generations', self::DEFAULT_GENERATIONS);
133389266c0SGreg Roach
134389266c0SGreg Roach        $fan_width = min($fan_width, self::MAXIMUM_WIDTH);
135389266c0SGreg Roach        $fan_width = max($fan_width, self::MINIMUM_WIDTH);
136389266c0SGreg Roach
137389266c0SGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
138389266c0SGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
139389266c0SGreg Roach
1409b5537c3SGreg Roach        if ($ajax) {
141389266c0SGreg Roach            return $this->chart($individual, $chart_style, $fan_width, $generations, $chart_service);
142389266c0SGreg Roach        }
143389266c0SGreg Roach
144389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1459b5537c3SGreg Roach            'ajax'        => true,
146389266c0SGreg Roach            'chart_style' => $chart_style,
147389266c0SGreg Roach            'fan_width'   => $fan_width,
148389266c0SGreg Roach            'generations' => $generations,
149389266c0SGreg Roach        ]);
150389266c0SGreg Roach
1519b5537c3SGreg Roach        return $this->viewResponse('modules/fanchart/page', [
152389266c0SGreg Roach            'ajax_url'            => $ajax_url,
153389266c0SGreg Roach            'chart_style'         => $chart_style,
154389266c0SGreg Roach            'chart_styles'        => $this->chartStyles(),
155389266c0SGreg Roach            'fan_width'           => $fan_width,
156389266c0SGreg Roach            'generations'         => $generations,
157389266c0SGreg Roach            'individual'          => $individual,
158389266c0SGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
159389266c0SGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
160389266c0SGreg Roach            'maximum_width'       => self::MAXIMUM_WIDTH,
161389266c0SGreg Roach            'minimum_width'       => self::MINIMUM_WIDTH,
16226684e68SGreg Roach            'module_name'         => $this->name(),
163389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
164389266c0SGreg Roach        ]);
165389266c0SGreg Roach    }
166389266c0SGreg Roach
167389266c0SGreg Roach    /**
168389266c0SGreg Roach     * Generate both the HTML and PNG components of the fan chart
169e6562982SGreg Roach     *
170e6562982SGreg Roach     * @param Individual   $individual
171389266c0SGreg Roach     * @param int          $chart_style
172389266c0SGreg Roach     * @param int          $fan_width
173389266c0SGreg Roach     * @param int          $generations
174389266c0SGreg Roach     * @param ChartService $chart_service
175e6562982SGreg Roach     *
176389266c0SGreg Roach     * @return Response
177e6562982SGreg Roach     */
178389266c0SGreg Roach    protected function chart(Individual $individual, int $chart_style, int $fan_width, int $generations, ChartService $chart_service): Response
179e6562982SGreg Roach    {
180389266c0SGreg Roach        $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations);
181389266c0SGreg Roach
182389266c0SGreg Roach        $gen  = $generations - 1;
183389266c0SGreg Roach        $sosa = 2 ** ($generations) - 1;
184389266c0SGreg Roach
185389266c0SGreg Roach        // fan size
186389266c0SGreg Roach        $fanw = 640 * $fan_width / 100;
187389266c0SGreg Roach        $cx   = $fanw / 2 - 1; // center x
188389266c0SGreg Roach        $cy   = $cx; // center y
189389266c0SGreg Roach        $rx   = $fanw - 1;
190389266c0SGreg Roach        $rw   = $fanw / ($gen + 1);
191389266c0SGreg Roach        $fanh = $fanw; // fan height
192389266c0SGreg Roach        if ($chart_style === self::STYLE_HALF_CIRCLE) {
193389266c0SGreg Roach            $fanh = $fanh * ($gen + 1) / ($gen * 2);
194389266c0SGreg Roach        }
195389266c0SGreg Roach        if ($chart_style === self::STYLE_THREE_QUARTER_CIRCLE) {
196389266c0SGreg Roach            $fanh = $fanh * 0.86;
197389266c0SGreg Roach        }
198389266c0SGreg Roach        $scale = $fanw / 640;
199389266c0SGreg Roach
200389266c0SGreg Roach        // Create the image
201389266c0SGreg Roach        $image = imagecreate((int) $fanw, (int) $fanh);
202389266c0SGreg Roach
203389266c0SGreg Roach        // Create colors
204389266c0SGreg Roach        $transparent = imagecolorallocate($image, 0, 0, 0);
205389266c0SGreg Roach        imagecolortransparent($image, $transparent);
206389266c0SGreg Roach
207*8136679eSGreg Roach        $theme = app()->make(ModuleThemeInterface::class);
208389266c0SGreg Roach
209389266c0SGreg Roach        $foreground = $this->imageColor($image, $theme->parameter('chart-font-color'));
210389266c0SGreg Roach
211389266c0SGreg Roach        $backgrounds = [
212389266c0SGreg Roach            'M' => $this->imageColor($image, $theme->parameter('chart-background-m')),
213389266c0SGreg Roach            'F' => $this->imageColor($image, $theme->parameter('chart-background-f')),
214389266c0SGreg Roach            'U' => $this->imageColor($image, $theme->parameter('chart-background-u')),
215389266c0SGreg Roach        ];
216389266c0SGreg Roach
217389266c0SGreg Roach
218389266c0SGreg Roach        imagefilledrectangle($image, 0, 0, (int) $fanw, (int) $fanh, $transparent);
219389266c0SGreg Roach
220389266c0SGreg Roach        $fandeg = 90 * $chart_style;
221389266c0SGreg Roach
222389266c0SGreg Roach        // Popup menus for each ancestor
223389266c0SGreg Roach        $html = '';
224389266c0SGreg Roach
225389266c0SGreg Roach        // Areas for the imagemap
226389266c0SGreg Roach        $areas = '';
227389266c0SGreg Roach
228389266c0SGreg Roach        // loop to create fan cells
229389266c0SGreg Roach        while ($gen >= 0) {
230389266c0SGreg Roach            // clean current generation area
231389266c0SGreg Roach            $deg2 = 360 + ($fandeg - 180) / 2;
232389266c0SGreg Roach            $deg1 = $deg2 - $fandeg;
233389266c0SGreg Roach            imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $backgrounds['U'], IMG_ARC_PIE);
234389266c0SGreg Roach            $rx -= 3;
235389266c0SGreg Roach
236389266c0SGreg Roach            // calculate new angle
237389266c0SGreg Roach            $p2    = 2 ** $gen;
238389266c0SGreg Roach            $angle = $fandeg / $p2;
239389266c0SGreg Roach            $deg2  = 360 + ($fandeg - 180) / 2;
240389266c0SGreg Roach            $deg1  = $deg2 - $angle;
241389266c0SGreg Roach            // special case for rootid cell
242389266c0SGreg Roach            if ($gen == 0) {
243389266c0SGreg Roach                $deg1 = 90;
244389266c0SGreg Roach                $deg2 = 360 + $deg1;
245389266c0SGreg Roach            }
246389266c0SGreg Roach
247389266c0SGreg Roach            // draw each cell
248389266c0SGreg Roach            while ($sosa >= $p2) {
249389266c0SGreg Roach                if ($ancestors->has($sosa)) {
250389266c0SGreg Roach                    $person = $ancestors->get($sosa);
251389266c0SGreg Roach                    $name    = $person->getFullName();
252389266c0SGreg Roach                    $addname = $person->getAddName();
253389266c0SGreg Roach
254389266c0SGreg Roach                    $text = I18N::reverseText($name);
255389266c0SGreg Roach                    if ($addname) {
256389266c0SGreg Roach                        $text .= "\n" . I18N::reverseText($addname);
257389266c0SGreg Roach                    }
258389266c0SGreg Roach
259389266c0SGreg Roach                    $text .= "\n" . I18N::reverseText($person->getLifeSpan());
260389266c0SGreg Roach
261389266c0SGreg Roach                    $background = $backgrounds[$person->getSex()];
262389266c0SGreg Roach
263389266c0SGreg Roach                    imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $background, IMG_ARC_PIE);
264389266c0SGreg Roach
265389266c0SGreg Roach                    // split and center text by lines
266389266c0SGreg Roach                    $wmax = (int) ($angle * 7 / 7 * $scale);
267389266c0SGreg Roach                    $wmax = min($wmax, 35 * $scale);
268389266c0SGreg Roach                    if ($gen == 0) {
269389266c0SGreg Roach                        $wmax = min($wmax, 17 * $scale);
270389266c0SGreg Roach                    }
271389266c0SGreg Roach                    $text = $this->splitAlignText($text, (int) $wmax);
272389266c0SGreg Roach
273389266c0SGreg Roach                    // text angle
274389266c0SGreg Roach                    $tangle = 270 - ($deg1 + $angle / 2);
275389266c0SGreg Roach                    if ($gen == 0) {
276389266c0SGreg Roach                        $tangle = 0;
277389266c0SGreg Roach                    }
278389266c0SGreg Roach
279389266c0SGreg Roach                    // calculate text position
280389266c0SGreg Roach                    $deg = $deg1 + 0.44;
281389266c0SGreg Roach                    if ($deg2 - $deg1 > 40) {
282389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 11;
283389266c0SGreg Roach                    }
284389266c0SGreg Roach                    if ($deg2 - $deg1 > 80) {
285389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 7;
286389266c0SGreg Roach                    }
287389266c0SGreg Roach                    if ($deg2 - $deg1 > 140) {
288389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 4;
289389266c0SGreg Roach                    }
290389266c0SGreg Roach                    if ($gen == 0) {
291389266c0SGreg Roach                        $deg = 180;
292389266c0SGreg Roach                    }
293389266c0SGreg Roach                    $rad = deg2rad($deg);
294389266c0SGreg Roach                    $mr  = ($rx - $rw / 4) / 2;
295389266c0SGreg Roach                    if ($gen > 0 && $deg2 - $deg1 > 80) {
296389266c0SGreg Roach                        $mr = $rx / 2;
297389266c0SGreg Roach                    }
298389266c0SGreg Roach                    $tx = $cx + $mr * cos($rad);
299389266c0SGreg Roach                    $ty = $cy + $mr * sin($rad);
300389266c0SGreg Roach                    if ($sosa == 1) {
301389266c0SGreg Roach                        $ty -= $mr / 2;
302389266c0SGreg Roach                    }
303389266c0SGreg Roach
304389266c0SGreg Roach                    // print text
305389266c0SGreg Roach                    imagettftext(
306389266c0SGreg Roach                        $image,
307389266c0SGreg Roach                        7,
308389266c0SGreg Roach                        $tangle,
309389266c0SGreg Roach                        (int) $tx,
310389266c0SGreg Roach                        (int) $ty,
311389266c0SGreg Roach                        $foreground,
312389266c0SGreg Roach                        WT_ROOT . 'resources/fonts/DejaVuSans.ttf',
313389266c0SGreg Roach                        $text
314389266c0SGreg Roach                    );
315389266c0SGreg Roach
316389266c0SGreg Roach                    $areas .= '<area shape="poly" coords="';
317389266c0SGreg Roach                    // plot upper points
318389266c0SGreg Roach                    $mr  = $rx / 2;
319389266c0SGreg Roach                    $deg = $deg1;
320389266c0SGreg Roach                    while ($deg <= $deg2) {
321389266c0SGreg Roach                        $rad   = deg2rad($deg);
322389266c0SGreg Roach                        $tx    = round($cx + $mr * cos($rad));
323389266c0SGreg Roach                        $ty    = round($cy + $mr * sin($rad));
324389266c0SGreg Roach                        $areas .= "$tx,$ty,";
325389266c0SGreg Roach                        $deg   += ($deg2 - $deg1) / 6;
326389266c0SGreg Roach                    }
327389266c0SGreg Roach                    // plot lower points
328389266c0SGreg Roach                    $mr  = ($rx - $rw) / 2;
329389266c0SGreg Roach                    $deg = $deg2;
330389266c0SGreg Roach                    while ($deg >= $deg1) {
331389266c0SGreg Roach                        $rad   = deg2rad($deg);
332389266c0SGreg Roach                        $tx    = round($cx + $mr * cos($rad));
333389266c0SGreg Roach                        $ty    = round($cy + $mr * sin($rad));
334389266c0SGreg Roach                        $areas .= "$tx,$ty,";
335389266c0SGreg Roach                        $deg   -= ($deg2 - $deg1) / 6;
336389266c0SGreg Roach                    }
337389266c0SGreg Roach                    // join first point
338389266c0SGreg Roach                    $mr    = $rx / 2;
339389266c0SGreg Roach                    $deg   = $deg1;
340389266c0SGreg Roach                    $rad   = deg2rad($deg);
341389266c0SGreg Roach                    $tx    = round($cx + $mr * cos($rad));
342389266c0SGreg Roach                    $ty    = round($cy + $mr * sin($rad));
343389266c0SGreg Roach                    $areas .= "$tx,$ty";
344389266c0SGreg Roach                    // add action url
345389266c0SGreg Roach                    $areas .= '" href="#' . $person->xref() . '"';
346389266c0SGreg Roach                    $html  .= '<div id="' . $person->xref() . '" class="fan_chart_menu">';
347389266c0SGreg Roach                    $html  .= '<div class="person_box"><div class="details1">';
348389266c0SGreg Roach                    $html  .= '<a href="' . e($person->url()) . '" class="name1">' . $name;
349389266c0SGreg Roach                    if ($addname) {
350389266c0SGreg Roach                        $html .= $addname;
351389266c0SGreg Roach                    }
352389266c0SGreg Roach                    $html .= '</a>';
353389266c0SGreg Roach                    $html .= '<ul class="charts">';
354389266c0SGreg Roach                    foreach ($theme->individualBoxMenu($person) as $menu) {
355389266c0SGreg Roach                        $html .= $menu->getMenuAsList();
356389266c0SGreg Roach                    }
357389266c0SGreg Roach                    $html  .= '</ul>';
358389266c0SGreg Roach                    $html  .= '</div></div>';
359389266c0SGreg Roach                    $html  .= '</div>';
360389266c0SGreg Roach                    $areas .= ' alt="' . strip_tags($person->getFullName()) . '" title="' . strip_tags($person->getFullName()) . '">';
361389266c0SGreg Roach                }
362389266c0SGreg Roach                $deg1 -= $angle;
363389266c0SGreg Roach                $deg2 -= $angle;
364389266c0SGreg Roach                $sosa--;
365389266c0SGreg Roach            }
366389266c0SGreg Roach            $rx -= $rw;
367389266c0SGreg Roach            $gen--;
368389266c0SGreg Roach        }
369389266c0SGreg Roach
370389266c0SGreg Roach        ob_start();
371389266c0SGreg Roach        imagepng($image);
372389266c0SGreg Roach        imagedestroy($image);
373389266c0SGreg Roach        $png = ob_get_clean();
374389266c0SGreg Roach
375389266c0SGreg Roach        return new Response(view('modules/fanchart/chart', [
376389266c0SGreg Roach            'fanh'  => $fanh,
377389266c0SGreg Roach            'fanw'  => $fanw,
378389266c0SGreg Roach            'html'  => $html,
379389266c0SGreg Roach            'areas' => $areas,
380389266c0SGreg Roach            'png'   => $png,
381389266c0SGreg Roach            'title' => $this->chartTitle($individual),
382389266c0SGreg Roach        ]));
383389266c0SGreg Roach    }
384389266c0SGreg Roach
385389266c0SGreg Roach    /**
386389266c0SGreg Roach     * split and center text by lines
387389266c0SGreg Roach     *
388389266c0SGreg Roach     * @param string $data   input string
389389266c0SGreg Roach     * @param int    $maxlen max length of each line
390389266c0SGreg Roach     *
391389266c0SGreg Roach     * @return string $text output string
392389266c0SGreg Roach     */
393389266c0SGreg Roach    protected function splitAlignText(string $data, int $maxlen): string
394389266c0SGreg Roach    {
395389266c0SGreg Roach        $RTLOrd = [
396389266c0SGreg Roach            215,
397389266c0SGreg Roach            216,
398389266c0SGreg Roach            217,
399389266c0SGreg Roach            218,
400389266c0SGreg Roach            219,
401389266c0SGreg Roach        ];
402389266c0SGreg Roach
403389266c0SGreg Roach        $lines = explode("\n", $data);
404389266c0SGreg Roach        // more than 1 line : recursive calls
405389266c0SGreg Roach        if (count($lines) > 1) {
406389266c0SGreg Roach            $text = '';
407389266c0SGreg Roach            foreach ($lines as $line) {
408389266c0SGreg Roach                $text .= $this->splitAlignText($line, $maxlen) . "\n";
409389266c0SGreg Roach            }
410389266c0SGreg Roach
411389266c0SGreg Roach            return $text;
412389266c0SGreg Roach        }
413389266c0SGreg Roach        // process current line word by word
414389266c0SGreg Roach        $split = explode(' ', $data);
415389266c0SGreg Roach        $text  = '';
416389266c0SGreg Roach        $line  = '';
417389266c0SGreg Roach
418389266c0SGreg Roach        // do not split hebrew line
419389266c0SGreg Roach        $found = false;
420389266c0SGreg Roach        foreach ($RTLOrd as $ord) {
421389266c0SGreg Roach            if (strpos($data, chr($ord)) !== false) {
422389266c0SGreg Roach                $found = true;
423389266c0SGreg Roach            }
424389266c0SGreg Roach        }
425389266c0SGreg Roach        if ($found) {
426389266c0SGreg Roach            $line = $data;
427389266c0SGreg Roach        } else {
428389266c0SGreg Roach            foreach ($split as $word) {
429389266c0SGreg Roach                $len  = strlen($line);
430389266c0SGreg Roach                $wlen = strlen($word);
431389266c0SGreg Roach                if (($len + $wlen) < $maxlen) {
432389266c0SGreg Roach                    if (!empty($line)) {
433389266c0SGreg Roach                        $line .= ' ';
434389266c0SGreg Roach                    }
435389266c0SGreg Roach                    $line .= "$word";
436389266c0SGreg Roach                } else {
437389266c0SGreg Roach                    $p = max(0, (int) (($maxlen - $len) / 2));
438389266c0SGreg Roach                    if (!empty($line)) {
439389266c0SGreg Roach                        $line = str_repeat(' ', $p) . $line; // center alignment using spaces
440389266c0SGreg Roach                        $text .= $line . "\n";
441389266c0SGreg Roach                    }
442389266c0SGreg Roach                    $line = $word;
443389266c0SGreg Roach                }
444389266c0SGreg Roach            }
445389266c0SGreg Roach        }
446389266c0SGreg Roach        // last line
447389266c0SGreg Roach        if (!empty($line)) {
448389266c0SGreg Roach            $len = strlen($line);
449389266c0SGreg Roach            if (in_array(ord($line{0}), $RTLOrd)) {
450389266c0SGreg Roach                $len /= 2;
451389266c0SGreg Roach            }
452389266c0SGreg Roach            $p    = max(0, (int) (($maxlen - $len) / 2));
453389266c0SGreg Roach            $line = str_repeat(' ', $p) . $line; // center alignment using spaces
454389266c0SGreg Roach            $text .= $line;
455389266c0SGreg Roach        }
456389266c0SGreg Roach
457389266c0SGreg Roach        return $text;
458389266c0SGreg Roach    }
459389266c0SGreg Roach
460389266c0SGreg Roach    /**
461389266c0SGreg Roach     * Convert a CSS color into a GD color.
462389266c0SGreg Roach     *
463389266c0SGreg Roach     * @param resource $image
464389266c0SGreg Roach     * @param string   $css_color
465389266c0SGreg Roach     *
466389266c0SGreg Roach     * @return int
467389266c0SGreg Roach     */
468389266c0SGreg Roach    protected function imageColor($image, string $css_color): int
469389266c0SGreg Roach    {
470389266c0SGreg Roach        return imagecolorallocate(
471389266c0SGreg Roach            $image,
472389266c0SGreg Roach            (int) hexdec(substr($css_color, 0, 2)),
473389266c0SGreg Roach            (int) hexdec(substr($css_color, 2, 2)),
474389266c0SGreg Roach            (int) hexdec(substr($css_color, 4, 2))
475389266c0SGreg Roach        );
476389266c0SGreg Roach    }
477389266c0SGreg Roach
478389266c0SGreg Roach    /**
479389266c0SGreg Roach     * This chart can display its output in a number of styles
480389266c0SGreg Roach     *
481389266c0SGreg Roach     * @return array
482389266c0SGreg Roach     */
483389266c0SGreg Roach    protected function chartStyles(): array
484389266c0SGreg Roach    {
485389266c0SGreg Roach        return [
486389266c0SGreg Roach            /* I18N: layout option for the fan chart */
487389266c0SGreg Roach            self::STYLE_HALF_CIRCLE          => I18N::translate('half circle'),
488389266c0SGreg Roach            /* I18N: layout option for the fan chart */
489389266c0SGreg Roach            self::STYLE_THREE_QUARTER_CIRCLE => I18N::translate('three-quarter circle'),
490389266c0SGreg Roach            /* I18N: layout option for the fan chart */
491389266c0SGreg Roach            self::STYLE_FULL_CIRCLE          => I18N::translate('full circle'),
492389266c0SGreg Roach        ];
493e6562982SGreg Roach    }
494168ff6f3Sric2016}
495