xref: /webtrees/app/Module/FanChartModule.php (revision 9b5537c37ce2b6d3b5c1a28c56f73d7ff9b9355c)
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;
27389266c0SGreg Roachuse Symfony\Component\HttpFoundation\Request;
28389266c0SGreg Roachuse Symfony\Component\HttpFoundation\Response;
29168ff6f3Sric2016
30168ff6f3Sric2016/**
31168ff6f3Sric2016 * Class FanChartModule
32168ff6f3Sric2016 */
3349a243cbSGreg Roachclass FanChartModule extends AbstractModule implements ModuleInterface, 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 */
107e6562982SGreg Roach        return I18N::translate('Fan chart of %s', $individual->getFullName());
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
115389266c0SGreg Roach     * @param ChartService $chart_service
116389266c0SGreg Roach     *
117389266c0SGreg Roach     * @return Response
118389266c0SGreg Roach     */
119389266c0SGreg Roach    public function getChartAction(Request $request, Tree $tree, ChartService $chart_service): Response
120389266c0SGreg Roach    {
121*9b5537c3SGreg Roach        $ajax       = (bool) $request->get('ajax');
122389266c0SGreg Roach        $xref       = $request->get('xref', '');
123389266c0SGreg Roach        $individual = Individual::getInstance($xref, $tree);
124389266c0SGreg Roach
125389266c0SGreg Roach        Auth::checkIndividualAccess($individual);
126389266c0SGreg Roach
127389266c0SGreg Roach        $chart_style = (int) $request->get('chart_style', self::DEFAULT_STYLE);
128389266c0SGreg Roach        $fan_width   = (int) $request->get('fan_width', self::DEFAULT_WIDTH);
129389266c0SGreg Roach        $generations = (int) $request->get('generations', self::DEFAULT_GENERATIONS);
130389266c0SGreg Roach
131389266c0SGreg Roach        $fan_width = min($fan_width, self::MAXIMUM_WIDTH);
132389266c0SGreg Roach        $fan_width = max($fan_width, self::MINIMUM_WIDTH);
133389266c0SGreg Roach
134389266c0SGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
135389266c0SGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
136389266c0SGreg Roach
137*9b5537c3SGreg Roach        if ($ajax) {
138389266c0SGreg Roach            return $this->chart($individual, $chart_style, $fan_width, $generations, $chart_service);
139389266c0SGreg Roach        }
140389266c0SGreg Roach
141389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
142*9b5537c3SGreg Roach            'ajax'        => true,
143389266c0SGreg Roach            'chart_style' => $chart_style,
144389266c0SGreg Roach            'fan_width'   => $fan_width,
145389266c0SGreg Roach            'generations' => $generations,
146389266c0SGreg Roach        ]);
147389266c0SGreg Roach
148*9b5537c3SGreg Roach        return $this->viewResponse('modules/fanchart/page', [
149389266c0SGreg Roach            'ajax_url'            => $ajax_url,
150389266c0SGreg Roach            'chart_style'         => $chart_style,
151389266c0SGreg Roach            'chart_styles'        => $this->chartStyles(),
152389266c0SGreg Roach            'fan_width'           => $fan_width,
153389266c0SGreg Roach            'generations'         => $generations,
154389266c0SGreg Roach            'individual'          => $individual,
155389266c0SGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
156389266c0SGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
157389266c0SGreg Roach            'maximum_width'       => self::MAXIMUM_WIDTH,
158389266c0SGreg Roach            'minimum_width'       => self::MINIMUM_WIDTH,
15926684e68SGreg Roach            'module_name'         => $this->name(),
160389266c0SGreg Roach            'title'               => $this->chartTitle($individual),
161389266c0SGreg Roach        ]);
162389266c0SGreg Roach    }
163389266c0SGreg Roach
164389266c0SGreg Roach    /**
165389266c0SGreg Roach     * Generate both the HTML and PNG components of the fan chart
166e6562982SGreg Roach     *
167e6562982SGreg Roach     * @param Individual   $individual
168389266c0SGreg Roach     * @param int          $chart_style
169389266c0SGreg Roach     * @param int          $fan_width
170389266c0SGreg Roach     * @param int          $generations
171389266c0SGreg Roach     * @param ChartService $chart_service
172e6562982SGreg Roach     *
173389266c0SGreg Roach     * @return Response
174e6562982SGreg Roach     */
175389266c0SGreg Roach    protected function chart(Individual $individual, int $chart_style, int $fan_width, int $generations, ChartService $chart_service): Response
176e6562982SGreg Roach    {
177389266c0SGreg Roach        $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations);
178389266c0SGreg Roach
179389266c0SGreg Roach        $gen  = $generations - 1;
180389266c0SGreg Roach        $sosa = 2 ** ($generations) - 1;
181389266c0SGreg Roach
182389266c0SGreg Roach        // fan size
183389266c0SGreg Roach        $fanw = 640 * $fan_width / 100;
184389266c0SGreg Roach        $cx   = $fanw / 2 - 1; // center x
185389266c0SGreg Roach        $cy   = $cx; // center y
186389266c0SGreg Roach        $rx   = $fanw - 1;
187389266c0SGreg Roach        $rw   = $fanw / ($gen + 1);
188389266c0SGreg Roach        $fanh = $fanw; // fan height
189389266c0SGreg Roach        if ($chart_style === self::STYLE_HALF_CIRCLE) {
190389266c0SGreg Roach            $fanh = $fanh * ($gen + 1) / ($gen * 2);
191389266c0SGreg Roach        }
192389266c0SGreg Roach        if ($chart_style === self::STYLE_THREE_QUARTER_CIRCLE) {
193389266c0SGreg Roach            $fanh = $fanh * 0.86;
194389266c0SGreg Roach        }
195389266c0SGreg Roach        $scale = $fanw / 640;
196389266c0SGreg Roach
197389266c0SGreg Roach        // Create the image
198389266c0SGreg Roach        $image = imagecreate((int) $fanw, (int) $fanh);
199389266c0SGreg Roach
200389266c0SGreg Roach        // Create colors
201389266c0SGreg Roach        $transparent = imagecolorallocate($image, 0, 0, 0);
202389266c0SGreg Roach        imagecolortransparent($image, $transparent);
203389266c0SGreg Roach
204389266c0SGreg Roach        $theme = Theme::theme();
205389266c0SGreg Roach
206389266c0SGreg Roach        $foreground = $this->imageColor($image, $theme->parameter('chart-font-color'));
207389266c0SGreg Roach
208389266c0SGreg Roach        $backgrounds = [
209389266c0SGreg Roach            'M' => $this->imageColor($image, $theme->parameter('chart-background-m')),
210389266c0SGreg Roach            'F' => $this->imageColor($image, $theme->parameter('chart-background-f')),
211389266c0SGreg Roach            'U' => $this->imageColor($image, $theme->parameter('chart-background-u')),
212389266c0SGreg Roach        ];
213389266c0SGreg Roach
214389266c0SGreg Roach
215389266c0SGreg Roach        imagefilledrectangle($image, 0, 0, (int) $fanw, (int) $fanh, $transparent);
216389266c0SGreg Roach
217389266c0SGreg Roach        $fandeg = 90 * $chart_style;
218389266c0SGreg Roach
219389266c0SGreg Roach        // Popup menus for each ancestor
220389266c0SGreg Roach        $html = '';
221389266c0SGreg Roach
222389266c0SGreg Roach        // Areas for the imagemap
223389266c0SGreg Roach        $areas = '';
224389266c0SGreg Roach
225389266c0SGreg Roach        // loop to create fan cells
226389266c0SGreg Roach        while ($gen >= 0) {
227389266c0SGreg Roach            // clean current generation area
228389266c0SGreg Roach            $deg2 = 360 + ($fandeg - 180) / 2;
229389266c0SGreg Roach            $deg1 = $deg2 - $fandeg;
230389266c0SGreg Roach            imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $backgrounds['U'], IMG_ARC_PIE);
231389266c0SGreg Roach            $rx -= 3;
232389266c0SGreg Roach
233389266c0SGreg Roach            // calculate new angle
234389266c0SGreg Roach            $p2    = 2 ** $gen;
235389266c0SGreg Roach            $angle = $fandeg / $p2;
236389266c0SGreg Roach            $deg2  = 360 + ($fandeg - 180) / 2;
237389266c0SGreg Roach            $deg1  = $deg2 - $angle;
238389266c0SGreg Roach            // special case for rootid cell
239389266c0SGreg Roach            if ($gen == 0) {
240389266c0SGreg Roach                $deg1 = 90;
241389266c0SGreg Roach                $deg2 = 360 + $deg1;
242389266c0SGreg Roach            }
243389266c0SGreg Roach
244389266c0SGreg Roach            // draw each cell
245389266c0SGreg Roach            while ($sosa >= $p2) {
246389266c0SGreg Roach                if ($ancestors->has($sosa)) {
247389266c0SGreg Roach                    $person = $ancestors->get($sosa);
248389266c0SGreg Roach                    $name    = $person->getFullName();
249389266c0SGreg Roach                    $addname = $person->getAddName();
250389266c0SGreg Roach
251389266c0SGreg Roach                    $text = I18N::reverseText($name);
252389266c0SGreg Roach                    if ($addname) {
253389266c0SGreg Roach                        $text .= "\n" . I18N::reverseText($addname);
254389266c0SGreg Roach                    }
255389266c0SGreg Roach
256389266c0SGreg Roach                    $text .= "\n" . I18N::reverseText($person->getLifeSpan());
257389266c0SGreg Roach
258389266c0SGreg Roach                    $background = $backgrounds[$person->getSex()];
259389266c0SGreg Roach
260389266c0SGreg Roach                    imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $background, IMG_ARC_PIE);
261389266c0SGreg Roach
262389266c0SGreg Roach                    // split and center text by lines
263389266c0SGreg Roach                    $wmax = (int) ($angle * 7 / 7 * $scale);
264389266c0SGreg Roach                    $wmax = min($wmax, 35 * $scale);
265389266c0SGreg Roach                    if ($gen == 0) {
266389266c0SGreg Roach                        $wmax = min($wmax, 17 * $scale);
267389266c0SGreg Roach                    }
268389266c0SGreg Roach                    $text = $this->splitAlignText($text, (int) $wmax);
269389266c0SGreg Roach
270389266c0SGreg Roach                    // text angle
271389266c0SGreg Roach                    $tangle = 270 - ($deg1 + $angle / 2);
272389266c0SGreg Roach                    if ($gen == 0) {
273389266c0SGreg Roach                        $tangle = 0;
274389266c0SGreg Roach                    }
275389266c0SGreg Roach
276389266c0SGreg Roach                    // calculate text position
277389266c0SGreg Roach                    $deg = $deg1 + 0.44;
278389266c0SGreg Roach                    if ($deg2 - $deg1 > 40) {
279389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 11;
280389266c0SGreg Roach                    }
281389266c0SGreg Roach                    if ($deg2 - $deg1 > 80) {
282389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 7;
283389266c0SGreg Roach                    }
284389266c0SGreg Roach                    if ($deg2 - $deg1 > 140) {
285389266c0SGreg Roach                        $deg = $deg1 + ($deg2 - $deg1) / 4;
286389266c0SGreg Roach                    }
287389266c0SGreg Roach                    if ($gen == 0) {
288389266c0SGreg Roach                        $deg = 180;
289389266c0SGreg Roach                    }
290389266c0SGreg Roach                    $rad = deg2rad($deg);
291389266c0SGreg Roach                    $mr  = ($rx - $rw / 4) / 2;
292389266c0SGreg Roach                    if ($gen > 0 && $deg2 - $deg1 > 80) {
293389266c0SGreg Roach                        $mr = $rx / 2;
294389266c0SGreg Roach                    }
295389266c0SGreg Roach                    $tx = $cx + $mr * cos($rad);
296389266c0SGreg Roach                    $ty = $cy + $mr * sin($rad);
297389266c0SGreg Roach                    if ($sosa == 1) {
298389266c0SGreg Roach                        $ty -= $mr / 2;
299389266c0SGreg Roach                    }
300389266c0SGreg Roach
301389266c0SGreg Roach                    // print text
302389266c0SGreg Roach                    imagettftext(
303389266c0SGreg Roach                        $image,
304389266c0SGreg Roach                        7,
305389266c0SGreg Roach                        $tangle,
306389266c0SGreg Roach                        (int) $tx,
307389266c0SGreg Roach                        (int) $ty,
308389266c0SGreg Roach                        $foreground,
309389266c0SGreg Roach                        WT_ROOT . 'resources/fonts/DejaVuSans.ttf',
310389266c0SGreg Roach                        $text
311389266c0SGreg Roach                    );
312389266c0SGreg Roach
313389266c0SGreg Roach                    $areas .= '<area shape="poly" coords="';
314389266c0SGreg Roach                    // plot upper points
315389266c0SGreg Roach                    $mr  = $rx / 2;
316389266c0SGreg Roach                    $deg = $deg1;
317389266c0SGreg Roach                    while ($deg <= $deg2) {
318389266c0SGreg Roach                        $rad   = deg2rad($deg);
319389266c0SGreg Roach                        $tx    = round($cx + $mr * cos($rad));
320389266c0SGreg Roach                        $ty    = round($cy + $mr * sin($rad));
321389266c0SGreg Roach                        $areas .= "$tx,$ty,";
322389266c0SGreg Roach                        $deg   += ($deg2 - $deg1) / 6;
323389266c0SGreg Roach                    }
324389266c0SGreg Roach                    // plot lower points
325389266c0SGreg Roach                    $mr  = ($rx - $rw) / 2;
326389266c0SGreg Roach                    $deg = $deg2;
327389266c0SGreg Roach                    while ($deg >= $deg1) {
328389266c0SGreg Roach                        $rad   = deg2rad($deg);
329389266c0SGreg Roach                        $tx    = round($cx + $mr * cos($rad));
330389266c0SGreg Roach                        $ty    = round($cy + $mr * sin($rad));
331389266c0SGreg Roach                        $areas .= "$tx,$ty,";
332389266c0SGreg Roach                        $deg   -= ($deg2 - $deg1) / 6;
333389266c0SGreg Roach                    }
334389266c0SGreg Roach                    // join first point
335389266c0SGreg Roach                    $mr    = $rx / 2;
336389266c0SGreg Roach                    $deg   = $deg1;
337389266c0SGreg Roach                    $rad   = deg2rad($deg);
338389266c0SGreg Roach                    $tx    = round($cx + $mr * cos($rad));
339389266c0SGreg Roach                    $ty    = round($cy + $mr * sin($rad));
340389266c0SGreg Roach                    $areas .= "$tx,$ty";
341389266c0SGreg Roach                    // add action url
342389266c0SGreg Roach                    $areas .= '" href="#' . $person->xref() . '"';
343389266c0SGreg Roach                    $html  .= '<div id="' . $person->xref() . '" class="fan_chart_menu">';
344389266c0SGreg Roach                    $html  .= '<div class="person_box"><div class="details1">';
345389266c0SGreg Roach                    $html  .= '<a href="' . e($person->url()) . '" class="name1">' . $name;
346389266c0SGreg Roach                    if ($addname) {
347389266c0SGreg Roach                        $html .= $addname;
348389266c0SGreg Roach                    }
349389266c0SGreg Roach                    $html .= '</a>';
350389266c0SGreg Roach                    $html .= '<ul class="charts">';
351389266c0SGreg Roach                    foreach ($theme->individualBoxMenu($person) as $menu) {
352389266c0SGreg Roach                        $html .= $menu->getMenuAsList();
353389266c0SGreg Roach                    }
354389266c0SGreg Roach                    $html  .= '</ul>';
355389266c0SGreg Roach                    $html  .= '</div></div>';
356389266c0SGreg Roach                    $html  .= '</div>';
357389266c0SGreg Roach                    $areas .= ' alt="' . strip_tags($person->getFullName()) . '" title="' . strip_tags($person->getFullName()) . '">';
358389266c0SGreg Roach                }
359389266c0SGreg Roach                $deg1 -= $angle;
360389266c0SGreg Roach                $deg2 -= $angle;
361389266c0SGreg Roach                $sosa--;
362389266c0SGreg Roach            }
363389266c0SGreg Roach            $rx -= $rw;
364389266c0SGreg Roach            $gen--;
365389266c0SGreg Roach        }
366389266c0SGreg Roach
367389266c0SGreg Roach        ob_start();
368389266c0SGreg Roach        imagepng($image);
369389266c0SGreg Roach        imagedestroy($image);
370389266c0SGreg Roach        $png = ob_get_clean();
371389266c0SGreg Roach
372389266c0SGreg Roach        return new Response(view('modules/fanchart/chart', [
373389266c0SGreg Roach            'fanh'  => $fanh,
374389266c0SGreg Roach            'fanw'  => $fanw,
375389266c0SGreg Roach            'html'  => $html,
376389266c0SGreg Roach            'areas' => $areas,
377389266c0SGreg Roach            'png'   => $png,
378389266c0SGreg Roach            'title' => $this->chartTitle($individual),
379389266c0SGreg Roach        ]));
380389266c0SGreg Roach    }
381389266c0SGreg Roach
382389266c0SGreg Roach    /**
383389266c0SGreg Roach     * split and center text by lines
384389266c0SGreg Roach     *
385389266c0SGreg Roach     * @param string $data   input string
386389266c0SGreg Roach     * @param int    $maxlen max length of each line
387389266c0SGreg Roach     *
388389266c0SGreg Roach     * @return string $text output string
389389266c0SGreg Roach     */
390389266c0SGreg Roach    protected function splitAlignText(string $data, int $maxlen): string
391389266c0SGreg Roach    {
392389266c0SGreg Roach        $RTLOrd = [
393389266c0SGreg Roach            215,
394389266c0SGreg Roach            216,
395389266c0SGreg Roach            217,
396389266c0SGreg Roach            218,
397389266c0SGreg Roach            219,
398389266c0SGreg Roach        ];
399389266c0SGreg Roach
400389266c0SGreg Roach        $lines = explode("\n", $data);
401389266c0SGreg Roach        // more than 1 line : recursive calls
402389266c0SGreg Roach        if (count($lines) > 1) {
403389266c0SGreg Roach            $text = '';
404389266c0SGreg Roach            foreach ($lines as $line) {
405389266c0SGreg Roach                $text .= $this->splitAlignText($line, $maxlen) . "\n";
406389266c0SGreg Roach            }
407389266c0SGreg Roach
408389266c0SGreg Roach            return $text;
409389266c0SGreg Roach        }
410389266c0SGreg Roach        // process current line word by word
411389266c0SGreg Roach        $split = explode(' ', $data);
412389266c0SGreg Roach        $text  = '';
413389266c0SGreg Roach        $line  = '';
414389266c0SGreg Roach
415389266c0SGreg Roach        // do not split hebrew line
416389266c0SGreg Roach        $found = false;
417389266c0SGreg Roach        foreach ($RTLOrd as $ord) {
418389266c0SGreg Roach            if (strpos($data, chr($ord)) !== false) {
419389266c0SGreg Roach                $found = true;
420389266c0SGreg Roach            }
421389266c0SGreg Roach        }
422389266c0SGreg Roach        if ($found) {
423389266c0SGreg Roach            $line = $data;
424389266c0SGreg Roach        } else {
425389266c0SGreg Roach            foreach ($split as $word) {
426389266c0SGreg Roach                $len  = strlen($line);
427389266c0SGreg Roach                $wlen = strlen($word);
428389266c0SGreg Roach                if (($len + $wlen) < $maxlen) {
429389266c0SGreg Roach                    if (!empty($line)) {
430389266c0SGreg Roach                        $line .= ' ';
431389266c0SGreg Roach                    }
432389266c0SGreg Roach                    $line .= "$word";
433389266c0SGreg Roach                } else {
434389266c0SGreg Roach                    $p = max(0, (int) (($maxlen - $len) / 2));
435389266c0SGreg Roach                    if (!empty($line)) {
436389266c0SGreg Roach                        $line = str_repeat(' ', $p) . $line; // center alignment using spaces
437389266c0SGreg Roach                        $text .= $line . "\n";
438389266c0SGreg Roach                    }
439389266c0SGreg Roach                    $line = $word;
440389266c0SGreg Roach                }
441389266c0SGreg Roach            }
442389266c0SGreg Roach        }
443389266c0SGreg Roach        // last line
444389266c0SGreg Roach        if (!empty($line)) {
445389266c0SGreg Roach            $len = strlen($line);
446389266c0SGreg Roach            if (in_array(ord($line{0}), $RTLOrd)) {
447389266c0SGreg Roach                $len /= 2;
448389266c0SGreg Roach            }
449389266c0SGreg Roach            $p    = max(0, (int) (($maxlen - $len) / 2));
450389266c0SGreg Roach            $line = str_repeat(' ', $p) . $line; // center alignment using spaces
451389266c0SGreg Roach            $text .= $line;
452389266c0SGreg Roach        }
453389266c0SGreg Roach
454389266c0SGreg Roach        return $text;
455389266c0SGreg Roach    }
456389266c0SGreg Roach
457389266c0SGreg Roach    /**
458389266c0SGreg Roach     * Convert a CSS color into a GD color.
459389266c0SGreg Roach     *
460389266c0SGreg Roach     * @param resource $image
461389266c0SGreg Roach     * @param string   $css_color
462389266c0SGreg Roach     *
463389266c0SGreg Roach     * @return int
464389266c0SGreg Roach     */
465389266c0SGreg Roach    protected function imageColor($image, string $css_color): int
466389266c0SGreg Roach    {
467389266c0SGreg Roach        return imagecolorallocate(
468389266c0SGreg Roach            $image,
469389266c0SGreg Roach            (int) hexdec(substr($css_color, 0, 2)),
470389266c0SGreg Roach            (int) hexdec(substr($css_color, 2, 2)),
471389266c0SGreg Roach            (int) hexdec(substr($css_color, 4, 2))
472389266c0SGreg Roach        );
473389266c0SGreg Roach    }
474389266c0SGreg Roach
475389266c0SGreg Roach    /**
476389266c0SGreg Roach     * This chart can display its output in a number of styles
477389266c0SGreg Roach     *
478389266c0SGreg Roach     * @return array
479389266c0SGreg Roach     */
480389266c0SGreg Roach    protected function chartStyles(): array
481389266c0SGreg Roach    {
482389266c0SGreg Roach        return [
483389266c0SGreg Roach            /* I18N: layout option for the fan chart */
484389266c0SGreg Roach            self::STYLE_HALF_CIRCLE          => I18N::translate('half circle'),
485389266c0SGreg Roach            /* I18N: layout option for the fan chart */
486389266c0SGreg Roach            self::STYLE_THREE_QUARTER_CIRCLE => I18N::translate('three-quarter circle'),
487389266c0SGreg Roach            /* I18N: layout option for the fan chart */
488389266c0SGreg Roach            self::STYLE_FULL_CIRCLE          => I18N::translate('full circle'),
489389266c0SGreg Roach        ];
490e6562982SGreg Roach    }
491168ff6f3Sric2016}
492