1<?php
2/* vim: set expandtab sw=4 ts=4 sts=4: */
3/**
4 * Contains PhpMyAdmin\Plugins\Schema\Svg\TableStatsSvg class
5 *
6 * @package PhpMyAdmin
7 */
8namespace PhpMyAdmin\Plugins\Schema\Svg;
9
10use PhpMyAdmin\Font;
11use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
12use PhpMyAdmin\Plugins\Schema\TableStats;
13
14/**
15 * Table preferences/statistics
16 *
17 * This class preserves the table co-ordinates,fields
18 * and helps in drawing/generating the Tables in SVG XML document.
19 *
20 * @package PhpMyAdmin
21 * @name    Table_Stats_Svg
22 * @see     PMA_SVG
23 */
24class TableStatsSvg extends TableStats
25{
26    /**
27     * Defines properties
28     */
29    public $height;
30    public $currentCell = 0;
31
32    /**
33     * The "PhpMyAdmin\Plugins\Schema\Svg\TableStatsSvg" constructor
34     *
35     * @param object  $diagram          The current SVG image document
36     * @param string  $db               The database name
37     * @param string  $tableName        The table name
38     * @param string  $font             Font face
39     * @param integer $fontSize         The font size
40     * @param integer $pageNumber       Page number
41     * @param integer &$same_wide_width The max. width among tables
42     * @param boolean $showKeys         Whether to display keys or not
43     * @param boolean $tableDimension   Whether to display table position or not
44     * @param boolean $offline          Whether the coordinates are sent
45     *
46     *
47     * @see PMA_SVG, Table_Stats_Svg::Table_Stats_setWidth,
48     *       PhpMyAdmin\Plugins\Schema\Svg\TableStatsSvg::Table_Stats_setHeight
49     */
50    public function __construct(
51        $diagram,
52        $db,
53        $tableName,
54        $font,
55        $fontSize,
56        $pageNumber,
57        &$same_wide_width,
58        $showKeys = false,
59        $tableDimension = false,
60        $offline = false
61    ) {
62        parent::__construct(
63            $diagram,
64            $db,
65            $pageNumber,
66            $tableName,
67            $showKeys,
68            $tableDimension,
69            $offline
70        );
71
72        // height and width
73        $this->_setHeightTable($fontSize);
74        // setWidth must me after setHeight, because title
75        // can include table height which changes table width
76        $this->_setWidthTable($font, $fontSize);
77        if ($same_wide_width < $this->width) {
78            $same_wide_width = $this->width;
79        }
80    }
81
82    /**
83     * Displays an error when the table cannot be found.
84     *
85     * @return void
86     */
87    protected function showMissingTableError()
88    {
89        ExportRelationSchema::dieSchema(
90            $this->pageNumber,
91            "SVG",
92            sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
93        );
94    }
95
96    /**
97     * Sets the width of the table
98     *
99     * @param string  $font     The font size
100     * @param integer $fontSize The font size
101     *
102     * @return void
103     * @access private
104     *
105     * @see    PMA_SVG
106     */
107    private function _setWidthTable($font, $fontSize)
108    {
109        foreach ($this->fields as $field) {
110            $this->width = max(
111                $this->width,
112                Font::getStringWidth($field, $font, $fontSize)
113            );
114        }
115        $this->width += Font::getStringWidth('  ', $font, $fontSize);
116
117        /*
118         * it is unknown what value must be added, because
119         * table title is affected by the table width value
120         */
121        while ($this->width
122            < Font::getStringWidth($this->getTitle(), $font, $fontSize)
123        ) {
124            $this->width += 7;
125        }
126    }
127
128    /**
129     * Sets the height of the table
130     *
131     * @param integer $fontSize font size
132     *
133     * @return void
134     */
135    private function _setHeightTable($fontSize)
136    {
137        $this->heightCell = $fontSize + 4;
138        $this->height = (count($this->fields) + 1) * $this->heightCell;
139    }
140
141    /**
142     * draw the table
143     *
144     * @param boolean $showColor Whether to display color
145     *
146     * @access public
147     * @return void
148     *
149     * @see    PMA_SVG,PMA_SVG::printElement
150     */
151    public function tableDraw($showColor)
152    {
153        $this->diagram->printElement(
154            'rect',
155            $this->x,
156            $this->y,
157            $this->width,
158            $this->heightCell,
159            null,
160            'fill:#007;stroke:black;'
161        );
162        $this->diagram->printElement(
163            'text',
164            $this->x + 5,
165            $this->y + 14,
166            $this->width,
167            $this->heightCell,
168            $this->getTitle(),
169            'fill:#fff;'
170        );
171        foreach ($this->fields as $field) {
172            $this->currentCell += $this->heightCell;
173            $fillColor = 'none';
174            if ($showColor) {
175                if (in_array($field, $this->primary)) {
176                    $fillColor = '#aea';
177                }
178                if ($field == $this->displayfield) {
179                    $fillColor = 'none';
180                }
181            }
182            $this->diagram->printElement(
183                'rect',
184                $this->x,
185                $this->y + $this->currentCell,
186                $this->width,
187                $this->heightCell,
188                null,
189                'fill:' . $fillColor . ';stroke:black;'
190            );
191            $this->diagram->printElement(
192                'text',
193                $this->x + 5,
194                $this->y + 14 + $this->currentCell,
195                $this->width,
196                $this->heightCell,
197                $field,
198                'fill:black;'
199            );
200        }
201    }
202}
203