xref: /webtrees/app/Module/FanChartModule.php (revision cab242e7d7a773b0a6dab130048696e26fd6612c)
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;
21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
22168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
23168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
246664b4a3SGreg Roachuse Fisharebest\Webtrees\Menu;
25389266c0SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
26389266c0SGreg Roachuse Fisharebest\Webtrees\Tree;
27389266c0SGreg Roachuse Symfony\Component\HttpFoundation\Request;
28389266c0SGreg Roachuse Symfony\Component\HttpFoundation\Response;
29168ff6f3Sric2016
30168ff6f3Sric2016/**
31168ff6f3Sric2016 * Class FanChartModule
32168ff6f3Sric2016 */
3337eb8894SGreg Roachclass FanChartModule extends AbstractModule implements ModuleChartInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleChartTrait;
3649a243cbSGreg Roach
37389266c0SGreg Roach    // Chart styles
38389266c0SGreg Roach    private const STYLE_HALF_CIRCLE          = 2;
39389266c0SGreg Roach    private const STYLE_THREE_QUARTER_CIRCLE = 3;
40389266c0SGreg Roach    private const STYLE_FULL_CIRCLE          = 4;
41389266c0SGreg Roach
42389266c0SGreg Roach    // Limits
43389266c0SGreg Roach    private const MINIMUM_GENERATIONS = 2;
44389266c0SGreg Roach    private const MAXIMUM_GENERATIONS = 9;
45389266c0SGreg Roach    private const MINIMUM_WIDTH       = 50;
46389266c0SGreg Roach    private const MAXIMUM_WIDTH       = 500;
47389266c0SGreg Roach
48389266c0SGreg Roach    // Defaults
49389266c0SGreg Roach    private const DEFAULT_STYLE       = self::STYLE_THREE_QUARTER_CIRCLE;
50389266c0SGreg Roach    private const DEFAULT_GENERATIONS = 4;
51389266c0SGreg Roach    private const DEFAULT_WIDTH       = 100;
52389266c0SGreg Roach
53168ff6f3Sric2016    /**
54168ff6f3Sric2016     * How should this module be labelled on tabs, menus, etc.?
55168ff6f3Sric2016     *
56168ff6f3Sric2016     * @return string
57168ff6f3Sric2016     */
5849a243cbSGreg Roach    public function title(): string
59c1010edaSGreg Roach    {
60bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
61bbb76c12SGreg Roach        return I18N::translate('Fan chart');
62168ff6f3Sric2016    }
63168ff6f3Sric2016
64168ff6f3Sric2016    /**
65168ff6f3Sric2016     * A sentence describing what this module does.
66168ff6f3Sric2016     *
67168ff6f3Sric2016     * @return string
68168ff6f3Sric2016     */
6949a243cbSGreg Roach    public function description(): string
70c1010edaSGreg Roach    {
71bbb76c12SGreg Roach        /* I18N: Description of the “Fan Chart” module */
72bbb76c12SGreg Roach        return I18N::translate('A fan chart of an individual’s ancestors.');
73168ff6f3Sric2016    }
74168ff6f3Sric2016
75168ff6f3Sric2016    /**
76377a2979SGreg Roach     * CSS class for the URL.
77377a2979SGreg Roach     *
78377a2979SGreg Roach     * @return string
79377a2979SGreg Roach     */
80377a2979SGreg Roach    public function chartMenuClass(): string
81377a2979SGreg Roach    {
82377a2979SGreg Roach        return 'menu-chart-fanchart';
83377a2979SGreg Roach    }
84377a2979SGreg Roach
85377a2979SGreg Roach    /**
864eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
874eb71cfaSGreg Roach     *
8860bc3e3fSGreg Roach     * @param Individual $individual
8960bc3e3fSGreg Roach     *
904eb71cfaSGreg Roach     * @return Menu|null
914eb71cfaSGreg Roach     */
92377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
93c1010edaSGreg Roach    {
94e6562982SGreg Roach        return $this->chartMenu($individual);
95e6562982SGreg Roach    }
96e6562982SGreg Roach
97e6562982SGreg Roach    /**
98e6562982SGreg Roach     * The title for a specific instance of this chart.
99e6562982SGreg Roach     *
100e6562982SGreg Roach     * @param Individual $individual
101e6562982SGreg Roach     *
102e6562982SGreg Roach     * @return string
103e6562982SGreg Roach     */
104e6562982SGreg Roach    public function chartTitle(Individual $individual): string
105e6562982SGreg Roach    {
106389266c0SGreg Roach        /* I18N: http://en.wikipedia.org/wiki/Family_tree#Fan_chart - %s is an individual’s name */
10739ca88baSGreg Roach        return I18N::translate('Fan chart of %s', $individual->fullName());
108e6562982SGreg Roach    }
109e6562982SGreg Roach
110e6562982SGreg Roach    /**
111389266c0SGreg Roach     * A form to request the chart parameters.
112389266c0SGreg Roach     *
113389266c0SGreg Roach     * @param Request       $request
114389266c0SGreg Roach     * @param Tree          $tree
115e5a6b4d4SGreg Roach     * @param UserInterface $user
116389266c0SGreg Roach     * @param ChartService  $chart_service
117389266c0SGreg Roach     *
118389266c0SGreg Roach     * @return Response
119389266c0SGreg Roach     */
120e5a6b4d4SGreg Roach    public function getChartAction(Request $request, Tree $tree, UserInterface $user, ChartService $chart_service): Response
121389266c0SGreg Roach    {
1229b5537c3SGreg Roach        $ajax       = (bool) $request->get('ajax');
123389266c0SGreg Roach        $xref       = $request->get('xref', '');
124389266c0SGreg Roach        $individual = Individual::getInstance($xref, $tree);
125389266c0SGreg Roach
126389266c0SGreg Roach        Auth::checkIndividualAccess($individual);
1279867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
128389266c0SGreg Roach
129389266c0SGreg Roach        $chart_style = (int) $request->get('chart_style', self::DEFAULT_STYLE);
130389266c0SGreg Roach        $fan_width   = (int) $request->get('fan_width', self::DEFAULT_WIDTH);
131389266c0SGreg Roach        $generations = (int) $request->get('generations', self::DEFAULT_GENERATIONS);
132389266c0SGreg Roach
133389266c0SGreg Roach        $fan_width = min($fan_width, self::MAXIMUM_WIDTH);
134389266c0SGreg Roach        $fan_width = max($fan_width, self::MINIMUM_WIDTH);
135389266c0SGreg Roach
136389266c0SGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
137389266c0SGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
138389266c0SGreg Roach
1399b5537c3SGreg Roach        if ($ajax) {
140389266c0SGreg Roach            return $this->chart($individual, $chart_style, $fan_width, $generations, $chart_service);
141389266c0SGreg Roach        }
142389266c0SGreg Roach
143389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1449b5537c3SGreg Roach            'ajax'        => true,
145389266c0SGreg Roach            'chart_style' => $chart_style,
146389266c0SGreg Roach            'fan_width'   => $fan_width,
147389266c0SGreg Roach            'generations' => $generations,
148389266c0SGreg Roach        ]);
149389266c0SGreg Roach
1509b5537c3SGreg Roach        return $this->viewResponse('modules/fanchart/page', [
151389266c0SGreg Roach            'ajax_url'            => $ajax_url,
152389266c0SGreg Roach            'chart_style'         => $chart_style,
153389266c0SGreg Roach            'chart_styles'        => $this->chartStyles(),
154389266c0SGreg Roach            'fan_width'           => $fan_width,
155389266c0SGreg Roach            'generations'         => $generations,
156389266c0SGreg Roach            'individual'          => $individual,
157389266c0SGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
158389266c0SGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
159389266c0SGreg Roach            'maximum_width'       => self::MAXIMUM_WIDTH,
160389266c0SGreg Roach            'minimum_width'       => self::MINIMUM_WIDTH,
16126684e68SGreg Roach            'module_name'         => $this->name(),
162389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
163389266c0SGreg Roach        ]);
164389266c0SGreg Roach    }
165389266c0SGreg Roach
166389266c0SGreg Roach    /**
167389266c0SGreg Roach     * Generate both the HTML and PNG components of the fan chart
168e6562982SGreg Roach     *
169e6562982SGreg Roach     * @param Individual   $individual
170389266c0SGreg Roach     * @param int          $chart_style
171389266c0SGreg Roach     * @param int          $fan_width
172389266c0SGreg Roach     * @param int          $generations
173389266c0SGreg Roach     * @param ChartService $chart_service
174e6562982SGreg Roach     *
175389266c0SGreg Roach     * @return Response
176e6562982SGreg Roach     */
177389266c0SGreg Roach    protected function chart(Individual $individual, int $chart_style, int $fan_width, int $generations, ChartService $chart_service): Response
178e6562982SGreg Roach    {
179389266c0SGreg Roach        $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations);
180389266c0SGreg Roach
181389266c0SGreg Roach        $gen  = $generations - 1;
182389266c0SGreg Roach        $sosa = 2 ** ($generations) - 1;
183389266c0SGreg Roach
184389266c0SGreg Roach        // fan size
185389266c0SGreg Roach        $fanw = 640 * $fan_width / 100;
186389266c0SGreg Roach        $cx   = $fanw / 2 - 1; // center x
187389266c0SGreg Roach        $cy   = $cx; // center y
188389266c0SGreg Roach        $rx   = $fanw - 1;
189389266c0SGreg Roach        $rw   = $fanw / ($gen + 1);
190389266c0SGreg Roach        $fanh = $fanw; // fan height
191389266c0SGreg Roach        if ($chart_style === self::STYLE_HALF_CIRCLE) {
192389266c0SGreg Roach            $fanh = $fanh * ($gen + 1) / ($gen * 2);
193389266c0SGreg Roach        }
194389266c0SGreg Roach        if ($chart_style === self::STYLE_THREE_QUARTER_CIRCLE) {
195389266c0SGreg Roach            $fanh = $fanh * 0.86;
196389266c0SGreg Roach        }
197389266c0SGreg Roach        $scale = $fanw / 640;
198389266c0SGreg Roach
199389266c0SGreg Roach        // Create the image
200389266c0SGreg Roach        $image = imagecreate((int) $fanw, (int) $fanh);
201389266c0SGreg Roach
202389266c0SGreg Roach        // Create colors
203389266c0SGreg Roach        $transparent = imagecolorallocate($image, 0, 0, 0);
204389266c0SGreg Roach        imagecolortransparent($image, $transparent);
205389266c0SGreg Roach
206*cab242e7SGreg Roach        $theme = app(ModuleThemeInterface::class);
207389266c0SGreg Roach
208389266c0SGreg Roach        $foreground = $this->imageColor($image, $theme->parameter('chart-font-color'));
209389266c0SGreg Roach
210389266c0SGreg Roach        $backgrounds = [
211389266c0SGreg Roach            'M' => $this->imageColor($image, $theme->parameter('chart-background-m')),
212389266c0SGreg Roach            'F' => $this->imageColor($image, $theme->parameter('chart-background-f')),
213389266c0SGreg Roach            'U' => $this->imageColor($image, $theme->parameter('chart-background-u')),
214389266c0SGreg Roach        ];
215389266c0SGreg Roach
216389266c0SGreg Roach        imagefilledrectangle($image, 0, 0, (int) $fanw, (int) $fanh, $transparent);
217389266c0SGreg Roach
218389266c0SGreg Roach        $fandeg = 90 * $chart_style;
219389266c0SGreg Roach
220389266c0SGreg Roach        // Popup menus for each ancestor
221389266c0SGreg Roach        $html = '';
222389266c0SGreg Roach
223389266c0SGreg Roach        // Areas for the imagemap
224389266c0SGreg Roach        $areas = '';
225389266c0SGreg Roach
226389266c0SGreg Roach        // loop to create fan cells
227389266c0SGreg Roach        while ($gen >= 0) {
228389266c0SGreg Roach            // clean current generation area
229389266c0SGreg Roach            $deg2 = 360 + ($fandeg - 180) / 2;
230389266c0SGreg Roach            $deg1 = $deg2 - $fandeg;
231389266c0SGreg Roach            imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $backgrounds['U'], IMG_ARC_PIE);
232389266c0SGreg Roach            $rx -= 3;
233389266c0SGreg Roach
234389266c0SGreg Roach            // calculate new angle
235389266c0SGreg Roach            $p2    = 2 ** $gen;
236389266c0SGreg Roach            $angle = $fandeg / $p2;
237389266c0SGreg Roach            $deg2  = 360 + ($fandeg - 180) / 2;
238389266c0SGreg Roach            $deg1  = $deg2 - $angle;
239389266c0SGreg Roach            // special case for rootid cell
240389266c0SGreg Roach            if ($gen == 0) {
241389266c0SGreg Roach                $deg1 = 90;
242389266c0SGreg Roach                $deg2 = 360 + $deg1;
243389266c0SGreg Roach            }
244389266c0SGreg Roach
245389266c0SGreg Roach            // draw each cell
246389266c0SGreg Roach            while ($sosa >= $p2) {
247389266c0SGreg Roach                if ($ancestors->has($sosa)) {
248389266c0SGreg Roach                    $person  = $ancestors->get($sosa);
24939ca88baSGreg Roach                    $name    = $person->fullName();
25039ca88baSGreg Roach                    $addname = $person->alternateName();
251389266c0SGreg Roach
252389266c0SGreg Roach                    $text = I18N::reverseText($name);
253389266c0SGreg Roach                    if ($addname) {
254389266c0SGreg Roach                        $text .= "\n" . I18N::reverseText($addname);
255389266c0SGreg Roach                    }
256389266c0SGreg Roach
257389266c0SGreg Roach                    $text .= "\n" . I18N::reverseText($person->getLifeSpan());
258389266c0SGreg Roach
25939ca88baSGreg Roach                    $background = $backgrounds[$person->sex()];
260389266c0SGreg Roach
261389266c0SGreg Roach                    imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $background, IMG_ARC_PIE);
262389266c0SGreg Roach
263389266c0SGreg Roach                    // split and center text by lines
264389266c0SGreg Roach                    $wmax = (int) ($angle * 7 / 7 * $scale);
265389266c0SGreg Roach                    $wmax = min($wmax, 35 * $scale);
266389266c0SGreg Roach                    if ($gen == 0) {
267389266c0SGreg Roach                        $wmax = min($wmax, 17 * $scale);
268389266c0SGreg Roach                    }
269389266c0SGreg Roach                    $text = $this->splitAlignText($text, (int) $wmax);
270389266c0SGreg Roach
271389266c0SGreg Roach                    // text angle
272389266c0SGreg Roach                    $tangle = 270 - ($deg1 + $angle / 2);
273389266c0SGreg Roach                    if ($gen == 0) {
274389266c0SGreg Roach                        $tangle = 0;
275389266c0SGreg Roach                    }
276389266c0SGreg Roach
277389266c0SGreg Roach                    // calculate text position
278389266c0SGreg Roach                    $deg = $deg1 + 0.44;
279389266c0SGreg Roach                    if ($deg2 - $deg1 > 40) {
280389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 11;
281389266c0SGreg Roach                    }
282389266c0SGreg Roach                    if ($deg2 - $deg1 > 80) {
283389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 7;
284389266c0SGreg Roach                    }
285389266c0SGreg Roach                    if ($deg2 - $deg1 > 140) {
286389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 4;
287389266c0SGreg Roach                    }
288389266c0SGreg Roach                    if ($gen == 0) {
289389266c0SGreg Roach                        $deg = 180;
290389266c0SGreg Roach                    }
291389266c0SGreg Roach                    $rad = deg2rad($deg);
292389266c0SGreg Roach                    $mr  = ($rx - $rw / 4) / 2;
293389266c0SGreg Roach                    if ($gen > 0 && $deg2 - $deg1 > 80) {
294389266c0SGreg Roach                        $mr = $rx / 2;
295389266c0SGreg Roach                    }
296389266c0SGreg Roach                    $tx = $cx + $mr * cos($rad);
297389266c0SGreg Roach                    $ty = $cy + $mr * sin($rad);
298389266c0SGreg Roach                    if ($sosa == 1) {
299389266c0SGreg Roach                        $ty -= $mr / 2;
300389266c0SGreg Roach                    }
301389266c0SGreg Roach
302389266c0SGreg Roach                    // print text
303389266c0SGreg Roach                    imagettftext(
304389266c0SGreg Roach                        $image,
305389266c0SGreg Roach                        7,
306389266c0SGreg Roach                        $tangle,
307389266c0SGreg Roach                        (int) $tx,
308389266c0SGreg Roach                        (int) $ty,
309389266c0SGreg Roach                        $foreground,
310389266c0SGreg Roach                        WT_ROOT . 'resources/fonts/DejaVuSans.ttf',
311389266c0SGreg Roach                        $text
312389266c0SGreg Roach                    );
313389266c0SGreg Roach
314389266c0SGreg Roach                    $areas .= '<area shape="poly" coords="';
315389266c0SGreg Roach                    // plot upper points
316389266c0SGreg Roach                    $mr  = $rx / 2;
317389266c0SGreg Roach                    $deg = $deg1;
318389266c0SGreg Roach                    while ($deg <= $deg2) {
319389266c0SGreg Roach                        $rad   = deg2rad($deg);
320389266c0SGreg Roach                        $tx    = round($cx + $mr * cos($rad));
321389266c0SGreg Roach                        $ty    = round($cy + $mr * sin($rad));
322389266c0SGreg Roach                        $areas .= "$tx,$ty,";
323389266c0SGreg Roach                        $deg   += ($deg2 - $deg1) / 6;
324389266c0SGreg Roach                    }
325389266c0SGreg Roach                    // plot lower points
326389266c0SGreg Roach                    $mr  = ($rx - $rw) / 2;
327389266c0SGreg Roach                    $deg = $deg2;
328389266c0SGreg Roach                    while ($deg >= $deg1) {
329389266c0SGreg Roach                        $rad   = deg2rad($deg);
330389266c0SGreg Roach                        $tx    = round($cx + $mr * cos($rad));
331389266c0SGreg Roach                        $ty    = round($cy + $mr * sin($rad));
332389266c0SGreg Roach                        $areas .= "$tx,$ty,";
333389266c0SGreg Roach                        $deg   -= ($deg2 - $deg1) / 6;
334389266c0SGreg Roach                    }
335389266c0SGreg Roach                    // join first point
336389266c0SGreg Roach                    $mr    = $rx / 2;
337389266c0SGreg Roach                    $deg   = $deg1;
338389266c0SGreg Roach                    $rad   = deg2rad($deg);
339389266c0SGreg Roach                    $tx    = round($cx + $mr * cos($rad));
340389266c0SGreg Roach                    $ty    = round($cy + $mr * sin($rad));
341389266c0SGreg Roach                    $areas .= "$tx,$ty";
342389266c0SGreg Roach                    // add action url
343389266c0SGreg Roach                    $areas .= '" href="#' . $person->xref() . '"';
344389266c0SGreg Roach                    $html  .= '<div id="' . $person->xref() . '" class="fan_chart_menu">';
345389266c0SGreg Roach                    $html  .= '<div class="person_box"><div class="details1">';
346389266c0SGreg Roach                    $html  .= '<a href="' . e($person->url()) . '" class="name1">' . $name;
347389266c0SGreg Roach                    if ($addname) {
348389266c0SGreg Roach                        $html .= $addname;
349389266c0SGreg Roach                    }
350389266c0SGreg Roach                    $html .= '</a>';
351389266c0SGreg Roach                    $html .= '<ul class="charts">';
352389266c0SGreg Roach                    foreach ($theme->individualBoxMenu($person) as $menu) {
353389266c0SGreg Roach                        $html .= $menu->getMenuAsList();
354389266c0SGreg Roach                    }
355389266c0SGreg Roach                    $html  .= '</ul>';
356389266c0SGreg Roach                    $html  .= '</div></div>';
357389266c0SGreg Roach                    $html  .= '</div>';
35839ca88baSGreg Roach                    $areas .= ' alt="' . strip_tags($person->fullName()) . '" title="' . strip_tags($person->fullName()) . '">';
359389266c0SGreg Roach                }
360389266c0SGreg Roach                $deg1 -= $angle;
361389266c0SGreg Roach                $deg2 -= $angle;
362389266c0SGreg Roach                $sosa--;
363389266c0SGreg Roach            }
364389266c0SGreg Roach            $rx -= $rw;
365389266c0SGreg Roach            $gen--;
366389266c0SGreg Roach        }
367389266c0SGreg Roach
368389266c0SGreg Roach        ob_start();
369389266c0SGreg Roach        imagepng($image);
370389266c0SGreg Roach        imagedestroy($image);
371389266c0SGreg Roach        $png = ob_get_clean();
372389266c0SGreg Roach
373389266c0SGreg Roach        return new Response(view('modules/fanchart/chart', [
374389266c0SGreg Roach            'fanh'  => $fanh,
375389266c0SGreg Roach            'fanw'  => $fanw,
376389266c0SGreg Roach            'html'  => $html,
377389266c0SGreg Roach            'areas' => $areas,
378389266c0SGreg Roach            'png'   => $png,
379389266c0SGreg Roach            'title' => $this->chartTitle($individual),
380389266c0SGreg Roach        ]));
381389266c0SGreg Roach    }
382389266c0SGreg Roach
383389266c0SGreg Roach    /**
384389266c0SGreg Roach     * split and center text by lines
385389266c0SGreg Roach     *
386389266c0SGreg Roach     * @param string $data   input string
387389266c0SGreg Roach     * @param int    $maxlen max length of each line
388389266c0SGreg Roach     *
389389266c0SGreg Roach     * @return string $text output string
390389266c0SGreg Roach     */
391389266c0SGreg Roach    protected function splitAlignText(string $data, int $maxlen): string
392389266c0SGreg Roach    {
393389266c0SGreg Roach        $RTLOrd = [
394389266c0SGreg Roach            215,
395389266c0SGreg Roach            216,
396389266c0SGreg Roach            217,
397389266c0SGreg Roach            218,
398389266c0SGreg Roach            219,
399389266c0SGreg Roach        ];
400389266c0SGreg Roach
401389266c0SGreg Roach        $lines = explode("\n", $data);
402389266c0SGreg Roach        // more than 1 line : recursive calls
403389266c0SGreg Roach        if (count($lines) > 1) {
404389266c0SGreg Roach            $text = '';
405389266c0SGreg Roach            foreach ($lines as $line) {
406389266c0SGreg Roach                $text .= $this->splitAlignText($line, $maxlen) . "\n";
407389266c0SGreg Roach            }
408389266c0SGreg Roach
409389266c0SGreg Roach            return $text;
410389266c0SGreg Roach        }
411389266c0SGreg Roach        // process current line word by word
412389266c0SGreg Roach        $split = explode(' ', $data);
413389266c0SGreg Roach        $text  = '';
414389266c0SGreg Roach        $line  = '';
415389266c0SGreg Roach
416389266c0SGreg Roach        // do not split hebrew line
417389266c0SGreg Roach        $found = false;
418389266c0SGreg Roach        foreach ($RTLOrd as $ord) {
419389266c0SGreg Roach            if (strpos($data, chr($ord)) !== false) {
420389266c0SGreg Roach                $found = true;
421389266c0SGreg Roach            }
422389266c0SGreg Roach        }
423389266c0SGreg Roach        if ($found) {
424389266c0SGreg Roach            $line = $data;
425389266c0SGreg Roach        } else {
426389266c0SGreg Roach            foreach ($split as $word) {
427389266c0SGreg Roach                $len  = strlen($line);
428389266c0SGreg Roach                $wlen = strlen($word);
429389266c0SGreg Roach                if (($len + $wlen) < $maxlen) {
430389266c0SGreg Roach                    if (!empty($line)) {
431389266c0SGreg Roach                        $line .= ' ';
432389266c0SGreg Roach                    }
433389266c0SGreg Roach                    $line .= "$word";
434389266c0SGreg Roach                } else {
435389266c0SGreg Roach                    $p = max(0, (int) (($maxlen - $len) / 2));
436389266c0SGreg Roach                    if (!empty($line)) {
437389266c0SGreg Roach                        $line = str_repeat(' ', $p) . $line; // center alignment using spaces
438389266c0SGreg Roach                        $text .= $line . "\n";
439389266c0SGreg Roach                    }
440389266c0SGreg Roach                    $line = $word;
441389266c0SGreg Roach                }
442389266c0SGreg Roach            }
443389266c0SGreg Roach        }
444389266c0SGreg Roach        // last line
445389266c0SGreg Roach        if (!empty($line)) {
446389266c0SGreg Roach            $len = strlen($line);
447389266c0SGreg Roach            if (in_array(ord($line{0}), $RTLOrd)) {
448389266c0SGreg Roach                $len /= 2;
449389266c0SGreg Roach            }
450389266c0SGreg Roach            $p    = max(0, (int) (($maxlen - $len) / 2));
451389266c0SGreg Roach            $line = str_repeat(' ', $p) . $line; // center alignment using spaces
452389266c0SGreg Roach            $text .= $line;
453389266c0SGreg Roach        }
454389266c0SGreg Roach
455389266c0SGreg Roach        return $text;
456389266c0SGreg Roach    }
457389266c0SGreg Roach
458389266c0SGreg Roach    /**
459389266c0SGreg Roach     * Convert a CSS color into a GD color.
460389266c0SGreg Roach     *
461389266c0SGreg Roach     * @param resource $image
462389266c0SGreg Roach     * @param string   $css_color
463389266c0SGreg Roach     *
464389266c0SGreg Roach     * @return int
465389266c0SGreg Roach     */
466389266c0SGreg Roach    protected function imageColor($image, string $css_color): int
467389266c0SGreg Roach    {
468389266c0SGreg Roach        return imagecolorallocate(
469389266c0SGreg Roach            $image,
470389266c0SGreg Roach            (int) hexdec(substr($css_color, 0, 2)),
471389266c0SGreg Roach            (int) hexdec(substr($css_color, 2, 2)),
472389266c0SGreg Roach            (int) hexdec(substr($css_color, 4, 2))
473389266c0SGreg Roach        );
474389266c0SGreg Roach    }
475389266c0SGreg Roach
476389266c0SGreg Roach    /**
477389266c0SGreg Roach     * This chart can display its output in a number of styles
478389266c0SGreg Roach     *
479389266c0SGreg Roach     * @return array
480389266c0SGreg Roach     */
481389266c0SGreg Roach    protected function chartStyles(): array
482389266c0SGreg Roach    {
483389266c0SGreg Roach        return [
484389266c0SGreg Roach            /* I18N: layout option for the fan chart */
485389266c0SGreg Roach            self::STYLE_HALF_CIRCLE          => I18N::translate('half circle'),
486389266c0SGreg Roach            /* I18N: layout option for the fan chart */
487389266c0SGreg Roach            self::STYLE_THREE_QUARTER_CIRCLE => I18N::translate('three-quarter circle'),
488389266c0SGreg Roach            /* I18N: layout option for the fan chart */
489389266c0SGreg Roach            self::STYLE_FULL_CIRCLE          => I18N::translate('full circle'),
490389266c0SGreg Roach        ];
491e6562982SGreg Roach    }
492168ff6f3Sric2016}
493