1<?php
2/**
3 * Contains PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg class
4 */
5
6declare(strict_types=1);
7
8namespace PhpMyAdmin\Plugins\Schema\Svg;
9
10use PhpMyAdmin\Plugins\Schema\RelationStats;
11use function shuffle;
12use function sqrt;
13
14/**
15 * Relation preferences/statistics
16 *
17 * This class fetches the table master and foreign fields positions
18 * and helps in generating the Table references and then connects
19 * master table's master field to foreign table's foreign key
20 * in SVG XML document.
21 *
22 * @see     PMA_SVG::printElementLine
23 *
24 * @name    Relation_Stats_Svg
25 */
26class RelationStatsSvg extends RelationStats
27{
28    /**
29     * @param Svg    $diagram       The SVG diagram
30     * @param string $master_table  The master table name
31     * @param string $master_field  The relation field in the master table
32     * @param string $foreign_table The foreign table name
33     * @param string $foreign_field The relation field in the foreign table
34     */
35    public function __construct(
36        $diagram,
37        $master_table,
38        $master_field,
39        $foreign_table,
40        $foreign_field
41    ) {
42        $this->wTick = 10;
43        parent::__construct(
44            $diagram,
45            $master_table,
46            $master_field,
47            $foreign_table,
48            $foreign_field
49        );
50    }
51
52    /**
53     * draws relation links and arrows shows foreign key relations
54     *
55     * @see    PMA_SVG
56     *
57     * @param bool $showColor Whether to use one color per relation or not
58     *
59     * @return void
60     *
61     * @access public
62     */
63    public function relationDraw($showColor)
64    {
65        if ($showColor) {
66            $listOfColors = [
67                '#c00',
68                '#bbb',
69                '#333',
70                '#cb0',
71                '#0b0',
72                '#0bf',
73                '#b0b',
74            ];
75            shuffle($listOfColors);
76            $color = $listOfColors[0];
77        } else {
78            $color = '#333';
79        }
80
81        $this->diagram->printElementLine(
82            'line',
83            $this->xSrc,
84            $this->ySrc,
85            $this->xSrc + $this->srcDir * $this->wTick,
86            $this->ySrc,
87            'stroke:' . $color . ';stroke-width:1;'
88        );
89        $this->diagram->printElementLine(
90            'line',
91            $this->xDest + $this->destDir * $this->wTick,
92            $this->yDest,
93            $this->xDest,
94            $this->yDest,
95            'stroke:' . $color . ';stroke-width:1;'
96        );
97        $this->diagram->printElementLine(
98            'line',
99            $this->xSrc + $this->srcDir * $this->wTick,
100            $this->ySrc,
101            $this->xDest + $this->destDir * $this->wTick,
102            $this->yDest,
103            'stroke:' . $color . ';stroke-width:1;'
104        );
105        $root2 = 2 * sqrt(2);
106        $this->diagram->printElementLine(
107            'line',
108            $this->xSrc + $this->srcDir * $this->wTick * 0.75,
109            $this->ySrc,
110            $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
111            $this->ySrc + $this->wTick / $root2,
112            'stroke:' . $color . ';stroke-width:2;'
113        );
114        $this->diagram->printElementLine(
115            'line',
116            $this->xSrc + $this->srcDir * $this->wTick * 0.75,
117            $this->ySrc,
118            $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
119            $this->ySrc - $this->wTick / $root2,
120            'stroke:' . $color . ';stroke-width:2;'
121        );
122        $this->diagram->printElementLine(
123            'line',
124            $this->xDest + $this->destDir * $this->wTick / 2,
125            $this->yDest,
126            $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
127            $this->yDest + $this->wTick / $root2,
128            'stroke:' . $color . ';stroke-width:2;'
129        );
130        $this->diagram->printElementLine(
131            'line',
132            $this->xDest + $this->destDir * $this->wTick / 2,
133            $this->yDest,
134            $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
135            $this->yDest - $this->wTick / $root2,
136            'stroke:' . $color . ';stroke-width:2;'
137        );
138    }
139}
140