Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / classes / BrowseForeigners.php
blobcc5216866a336a52ab9f6c1eed4da540def6b188
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Contains functions used by browse_foreigners.php
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 namespace PhpMyAdmin;
12 /**
13 * PhpMyAdmin\BrowseForeigners class
15 * @package PhpMyAdmin
17 class BrowseForeigners
19 private $limitChars;
20 private $maxRows;
21 private $repeatCells;
22 private $showAll;
23 private $themeImage;
25 /**
26 * @var Template
28 public $template;
30 /**
31 * Constructor
33 * @param int $limitChars Maximum number of characters to show
34 * @param int $maxRows Number of rows to display
35 * @param int $repeatCells Repeat the headers every X cells, or 0 to deactivate
36 * @param boolean $showAll Shows the 'Show all' button or not
37 * @param string $themeImage Theme image path
38 * @param Template $template Template object
40 public function __construct(
41 int $limitChars,
42 int $maxRows,
43 int $repeatCells,
44 bool $showAll,
45 string $themeImage,
46 Template $template
47 ) {
48 $this->limitChars = $limitChars;
49 $this->maxRows = $maxRows;
50 $this->repeatCells = $repeatCells;
51 $this->showAll = $showAll;
52 $this->themeImage = $themeImage;
53 $this->template = $template;
56 /**
57 * Function to get html for one relational key
59 * @param integer $horizontal_count the current horizontal count
60 * @param string $header table header
61 * @param array $keys all the keys
62 * @param integer $indexByKeyname index by keyname
63 * @param array $descriptions descriptions
64 * @param integer $indexByDescription index by description
65 * @param string $current_value current value on the edit form
67 * @return array the generated html
69 private function getHtmlForOneKey(
70 int $horizontal_count,
71 string $header,
72 array $keys,
73 int $indexByKeyname,
74 array $descriptions,
75 int $indexByDescription,
76 string $current_value
77 ): array {
78 $horizontal_count++;
79 $output = '';
81 // whether the key name corresponds to the selected value in the form
82 $rightKeynameIsSelected = false;
83 $leftKeynameIsSelected = false;
85 if ($this->repeatCells > 0 && $horizontal_count > $this->repeatCells) {
86 $output .= $header;
87 $horizontal_count = 0;
90 // key names and descriptions for the left section,
91 // sorted by key names
92 $leftKeyname = $keys[$indexByKeyname];
93 list(
94 $leftDescription,
95 $leftDescriptionTitle
96 ) = $this->getDescriptionAndTitle($descriptions[$indexByKeyname]);
98 // key names and descriptions for the right section,
99 // sorted by descriptions
100 $rightKeyname = $keys[$indexByDescription];
101 list(
102 $rightDescription,
103 $rightDescriptionTitle
104 ) = $this->getDescriptionAndTitle($descriptions[$indexByDescription]);
106 $indexByDescription++;
108 if (! empty($current_value)) {
109 $rightKeynameIsSelected = $rightKeyname == $current_value;
110 $leftKeynameIsSelected = $leftKeyname == $current_value;
113 $output .= '<tr class="noclick">';
115 $output .= $this->template->render('table/browse_foreigners/column_element', [
116 'keyname' => $leftKeyname,
117 'description' => $leftDescription,
118 'title' => $leftDescriptionTitle,
119 'is_selected' => $leftKeynameIsSelected,
120 'nowrap' => true,
122 $output .= $this->template->render('table/browse_foreigners/column_element', [
123 'keyname' => $leftKeyname,
124 'description' => $leftDescription,
125 'title' => $leftDescriptionTitle,
126 'is_selected' => $leftKeynameIsSelected,
127 'nowrap' => false,
130 $output .= '<td width="20%">'
131 . '<img src="' . $this->themeImage . 'spacer.png" alt=""'
132 . ' width="1" height="1"></td>';
134 $output .= $this->template->render('table/browse_foreigners/column_element', [
135 'keyname' => $rightKeyname,
136 'description' => $rightDescription,
137 'title' => $rightDescriptionTitle,
138 'is_selected' => $rightKeynameIsSelected,
139 'nowrap' => false,
141 $output .= $this->template->render('table/browse_foreigners/column_element', [
142 'keyname' => $rightKeyname,
143 'description' => $rightDescription,
144 'title' => $rightDescriptionTitle,
145 'is_selected' => $rightKeynameIsSelected,
146 'nowrap' => true,
149 $output .= '</tr>';
151 return [
152 $output,
153 $horizontal_count,
154 $indexByDescription,
159 * Function to get html for relational field selection
161 * @param string $db current database
162 * @param string $table current table
163 * @param string $field field
164 * @param array $foreignData foreign column data
165 * @param string|null $fieldkey field key
166 * @param string $current_value current columns's value
168 * @return string
170 public function getHtmlForRelationalFieldSelection(
171 string $db,
172 string $table,
173 string $field,
174 array $foreignData,
175 ?string $fieldkey,
176 string $current_value
177 ): string {
178 $gotopage = $this->getHtmlForGotoPage($foreignData);
179 $foreignShowAll = $this->template->render('table/browse_foreigners/show_all', [
180 'foreign_data' => $foreignData,
181 'show_all' => $this->showAll,
182 'max_rows' => $this->maxRows,
185 $output = '<form class="ajax" '
186 . 'id="browse_foreign_form" name="browse_foreign_from" '
187 . 'action="browse_foreigners.php" method="post">'
188 . '<fieldset>'
189 . Url::getHiddenInputs($db, $table)
190 . '<input type="hidden" name="field" value="' . htmlspecialchars($field)
191 . '">'
192 . '<input type="hidden" name="fieldkey" value="'
193 . (isset($fieldkey) ? htmlspecialchars($fieldkey) : '') . '">';
195 if (isset($_POST['rownumber'])) {
196 $output .= '<input type="hidden" name="rownumber" value="'
197 . htmlspecialchars((string) $_POST['rownumber']) . '">';
199 $filter_value = (isset($_POST['foreign_filter'])
200 ? htmlspecialchars($_POST['foreign_filter'])
201 : '');
202 $output .= '<span class="formelement">'
203 . '<label for="input_foreign_filter">' . __('Search:') . '</label>'
204 . '<input type="text" name="foreign_filter" '
205 . 'id="input_foreign_filter" '
206 . 'value="' . $filter_value . '" data-old="' . $filter_value . '" '
207 . '>'
208 . '<input class="btn btn-primary" type="submit" name="submit_foreign_filter" value="'
209 . __('Go') . '">'
210 . '</span>'
211 . '<span class="formelement">' . $gotopage . '</span>'
212 . '<span class="formelement">' . $foreignShowAll . '</span>'
213 . '</fieldset>'
214 . '</form>';
216 $output .= '<table width="100%" id="browse_foreign_table">';
218 if (! is_array($foreignData['disp_row'])) {
219 $output .= '</tbody>'
220 . '</table>';
222 return $output;
225 $header = '<tr>
226 <th>' . __('Keyname') . '</th>
227 <th>' . __('Description') . '</th>
228 <td width="20%"></td>
229 <th>' . __('Description') . '</th>
230 <th>' . __('Keyname') . '</th>
231 </tr>';
233 $output .= '<thead>' . $header . '</thead>' . "\n"
234 . '<tfoot>' . $header . '</tfoot>' . "\n"
235 . '<tbody>' . "\n";
237 $descriptions = [];
238 $keys = [];
239 foreach ($foreignData['disp_row'] as $relrow) {
240 if ($foreignData['foreign_display'] != false) {
241 $descriptions[] = $relrow[$foreignData['foreign_display']];
242 } else {
243 $descriptions[] = '';
246 $keys[] = $relrow[$foreignData['foreign_field']];
249 asort($keys);
251 $horizontal_count = 0;
252 $indexByDescription = 0;
254 foreach ($keys as $indexByKeyname => $value) {
255 list(
256 $html,
257 $horizontal_count,
258 $indexByDescription
259 ) = $this->getHtmlForOneKey(
260 $horizontal_count,
261 $header,
262 $keys,
263 $indexByKeyname,
264 $descriptions,
265 $indexByDescription,
266 $current_value
268 $output .= $html;
271 $output .= '</tbody>'
272 . '</table>';
274 return $output;
278 * Get the description (possibly truncated) and the title
280 * @param string $description the key name's description
282 * @return array the new description and title
284 private function getDescriptionAndTitle(string $description): array
286 if (mb_strlen($description) <= $this->limitChars) {
287 $description = htmlspecialchars(
288 $description
290 $descriptionTitle = '';
291 } else {
292 $descriptionTitle = htmlspecialchars(
293 $description
295 $description = htmlspecialchars(
296 mb_substr(
297 $description,
299 $this->limitChars
301 . '...'
304 return [
305 $description,
306 $descriptionTitle,
311 * Function to get html for the goto page option
313 * @param array|null $foreignData foreign data
315 * @return string
317 private function getHtmlForGotoPage(?array $foreignData): string
319 $gotopage = '';
320 isset($_POST['pos']) ? $pos = $_POST['pos'] : $pos = 0;
321 if (! is_array($foreignData['disp_row'])) {
322 return $gotopage;
325 $pageNow = @floor($pos / $this->maxRows) + 1;
326 $nbTotalPage = @ceil($foreignData['the_total'] / $this->maxRows);
328 if ($foreignData['the_total'] > $this->maxRows) {
329 $gotopage = Util::pageselector(
330 'pos',
331 $this->maxRows,
332 $pageNow,
333 $nbTotalPage,
334 200,
339 __('Page number:')
343 return $gotopage;
347 * Function to get foreign limit
349 * @param string|null $foreignShowAll foreign navigation
351 * @return string
353 public function getForeignLimit(?string $foreignShowAll): ?string
355 if (isset($foreignShowAll) && $foreignShowAll == __('Show all')) {
356 return null;
358 isset($_POST['pos']) ? $pos = $_POST['pos'] : $pos = 0;
359 return 'LIMIT ' . $pos . ', ' . $this->maxRows . ' ';