File: browse_foreigners.lib.php

package info (click to toggle)
phpmyadmin 4%3A4.2.12-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 33,240 kB
  • sloc: php: 133,253; sql: 510; sh: 240; makefile: 192; python: 187
file content (430 lines) | stat: -rw-r--r-- 12,344 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Contains functions used by browse_foreigners.php
 *
 * @package PhpMyAdmin
 */
if (! defined('PHPMYADMIN')) {
    exit;
}

/**
 * Function to get html for relational field selection
 *
 * @param string $db          current database
 * @param string $table       current table
 * @param string $field       field
 * @param array  $foreignData foreign column data
 * @param string $fieldkey    field key
 * @param array  $data        data
 *
 * @return string
 */
function PMA_getHtmlForRelationalFieldSelection($db, $table, $field, $foreignData,
    $fieldkey, $data
) {
    $gotopage = PMA_getHtmlForGotoPage($foreignData);
    $showall = PMA_getHtmlForShowAll($foreignData);

    $output = '<form action="browse_foreigners.php" method="post">'
        . '<fieldset>'
        . PMA_URL_getHiddenInputs($db, $table)
        . '<input type="hidden" name="field" value="' . htmlspecialchars($field)
        . '" />'
        . '<input type="hidden" name="fieldkey" value="'
        . (isset($fieldkey) ? htmlspecialchars($fieldkey) : '') . '" />';

    if (isset($_REQUEST['rownumber'])) {
        $output .= '<input type="hidden" name="rownumber" value="'
            . htmlspecialchars($_REQUEST['rownumber']) . '" />';
    }
    $output .= '<span class="formelement">'
        . '<label for="input_foreign_filter">' . __('Search:') . '</label>'
        . '<input type="text" name="foreign_filter" '
        . 'id="input_foreign_filter" value="'
        . (isset($_REQUEST['foreign_filter'])
        ? htmlspecialchars($_REQUEST['foreign_filter'])
        : '')
        . '" />'
        . '<input type="submit" name="submit_foreign_filter" value="'
        .  __('Go') . '" />'
        . '</span>'
        . '<span class="formelement">' . $gotopage . '</span>'
        . '<span class="formelement">' . $showall . '</span>'
        . '</fieldset>'
        . '</form>';

    $output .= '<table width="100%">';

    if (!is_array($foreignData['disp_row'])) {
        $output .= '</tbody>'
            . '</table>';

        return $output;
    }

    $header = '<tr>
        <th>' . __('Keyname') . '</th>
        <th>' . __('Description') . '</th>
        <td width="20%"></td>
        <th>' . __('Description') . '</th>
        <th>' . __('Keyname') . '</th>
    </tr>';

    $output .= '<thead>' . $header . '</thead>' . "\n"
        . '<tfoot>' . $header . '</tfoot>' . "\n"
        . '<tbody>' . "\n";

    $descriptions = array();
    $keys   = array();
    foreach ($foreignData['disp_row'] as $relrow) {
        if ($foreignData['foreign_display'] != false) {
            $descriptions[] = $relrow[$foreignData['foreign_display']];
        } else {
            $descriptions[] = '';
        }

        $keys[] = $relrow[$foreignData['foreign_field']];
    }

    asort($keys);

    $hcount = 0;
    $odd_row = true;
    $indexByDescription = 0;

    // whether the keyname corresponds to the selected value in the form
    $rightKeynameIsSelected = false;
    $leftKeynameIsSelected = false;

    foreach ($keys as $indexByKeyname => $value) {
        $hcount++;

        if ($GLOBALS['cfg']['RepeatCells'] > 0
            && $hcount > $GLOBALS['cfg']['RepeatCells']
        ) {
            $output .= $header;
            $hcount = 0;
            $odd_row = true;
        }

        // keynames and descriptions for the left section,
        // sorted by keynames
        $leftKeyname = $keys[$indexByKeyname];
        list(
            $leftDescription,
            $leftDescriptionTitle
        ) = PMA_getDescriptionAndTitle($descriptions[$indexByKeyname]);

        // keynames and descriptions for the right section,
        // sorted by descriptions
        $rightKeyname = $keys[$indexByDescription];
        list(
            $rightDescription,
            $rightDescriptionTitle
        ) = PMA_getDescriptionAndTitle($descriptions[$indexByDescription]);

        $indexByDescription++;

        if (! empty($data)) {
            $rightKeynameIsSelected = $rightKeyname == $data;
            $leftKeynameIsSelected = $leftKeyname == $data;
        }

        $output .= '<tr class="noclick ' . ($odd_row ? 'odd' : 'even') . '">';
        $odd_row = ! $odd_row;

        $output .= PMA_getHtmlForColumnElement(
            'class="nowrap"', $leftKeynameIsSelected,
            $leftKeyname, $leftDescription,
            $leftDescriptionTitle, $field
        );

        $output .= PMA_getHtmlForColumnElement(
            '', $leftKeynameIsSelected, $leftKeyname,
            $leftDescription, $leftDescriptionTitle, $field
        );

        $output .= '<td width="20%">'
            . '<img src="' . $GLOBALS['pmaThemeImage'] . 'spacer.png" alt=""'
            . ' width="1" height="1" /></td>';

        $output .= PMA_getHtmlForColumnElement(
            '', $rightKeynameIsSelected, $leftKeyname,
            $rightDescription, $rightDescriptionTitle, $field
        );

        $output .= PMA_getHtmlForColumnElement(
            'class="nowrap"', $rightKeynameIsSelected,
            $rightKeyname, $rightDescription,
            $rightDescriptionTitle, $field
        );
        $output .= '</tr>';
    } // end while
    $output .= '</tbody>'
        . '</table>';

    return $output;
}

/**
 * Get the description (possibly truncated) and the title
 *
 * @param string $description the keyname's description
 *
 * @return array the new description and title
 */
function PMA_getDescriptionAndTitle($description)
{
    $pmaString = $GLOBALS['PMA_String'];
    $limitChars = $GLOBALS['cfg']['LimitChars'];
    if ($pmaString->strlen($description) <= $limitChars) {
        $description = htmlspecialchars(
            $description
        );
        $descriptionTitle = '';
    } else {
        $descriptionTitle = htmlspecialchars(
            $description
        );
        $description = htmlspecialchars(
            $pmaString->substr(
                $description, 0, $limitChars
            )
            . '...'
        );
    }
    return array($description, $descriptionTitle);
}

/**
 * Function to get html for each column element
 *
 * @param string $cssClass    class="nowrap" or ''
 * @param bool   $isSelected  whether current equals form's value
 * @param string $keyname     current key
 * @param string $description current value
 * @param string $title       current title
 * @param string $field       field
 *
 * @return string
 */
function PMA_getHtmlForColumnElement($cssClass, $isSelected, $keyname,
    $description, $title, $field
) {
    $output = '<td ' . $cssClass . '>'
        . ($isSelected ? '<strong>' : '')
        . '<a href="#" title="' . __('Use this value')
        . ($title != ''
            ? ': ' . $title
            : '')
        . '" onclick="formupdate(\'' . md5($field) . '\', \''
        . PMA_jsFormat($keyname, false)
        . '\'); return false;">';
    if ($cssClass !== '') {
        $output .= htmlspecialchars($keyname);
    } else {
        $output .= $description;
    }

    $output .=  '</a>' . ($isSelected ? '</strong>' : '') . '</td>';

    return $output;
}

/**
 * Function to get javascript code to handle display selection for relational
 * field values
 *
 * @return string
 */
function PMA_getJsScriptToHandleSelectRelationalFields()
{
    $element_name = PMA_getElementName();
    $fieldkey = PMA_getFieldKey();
    $error = PMA_getJsError();
    $code = <<<EOC
self.focus();
function formupdate(fieldmd5, key) {
    var \$inline = window.opener.jQuery('.browse_foreign_clicked');
    if (\$inline.length != 0) {
        \$inline.removeClass('browse_foreign_clicked')
            // for grid editing,
            // puts new value in the previous element which is
            // a span with class curr_value, and trigger .change()
            .prev('.curr_value').text(key).change();
        // for zoom-search editing, puts new value in the previous
        // element which is an input field
        \$inline.prev('input[type=text]').val(key);
        self.close();
        return false;
    }

    if (opener && opener.document && opener.document.insertForm) {
        var field = 'fields';
        var field_null = 'fields_null';

        $element_name

        var element_name_alt = field + '[$fieldkey]';

        if (opener.document.insertForm.elements[element_name]) {
            // Edit/Insert form
            opener.document.insertForm.elements[element_name].value = key;
            if (opener.document.insertForm.elements[null_name]) {
                opener.document.insertForm.elements[null_name].checked = false;
            }
            self.close();
            return false;
        } else if (opener.document.insertForm.elements[element_name_alt]) {
            // Search form
            opener.document.insertForm.elements[element_name_alt].value = key;
            self.close();
            return false;
        }
    }

    alert('$error');
}
EOC;

    return $code;
}

/**
 * Function to get formatted error message for javascript
 *
 * @return string
 */
function PMA_getJsError()
{
    return PMA_jsFormat(
        __(
            'The target browser window could not be updated. '
            . 'Maybe you have closed the parent window, or '
            . 'your browser\'s security settings are '
            . 'configured to block cross-window updates.'
        )
    );
}

/**
 * Function to get the field key
 *
 * @return string
 */
function PMA_getFieldKey()
{
    if (! isset($_REQUEST['fieldkey']) || ! is_numeric($_REQUEST['fieldkey'])) {
        $fieldkey = 0;
    } else {
        $fieldkey = $_REQUEST['fieldkey'];
    }

    return $fieldkey;
}

/**
 * Function to get the element name
 *
 * @return string
 */
function PMA_getElementName()
{
    // When coming from Table/Zoom search
    if (isset($_REQUEST['fromsearch'])) {
        // In table or zoom search, input fields are named "criteriaValues"
        $element_name = " var field = 'criteriaValues';\n";
    } else {
        // In insert/edit, input fields are named "fields"
        $element_name = " var field = 'fields';\n";
    }

    if (isset($_REQUEST['rownumber'])) {
        $element_name  .= "        var element_name = field + '[multi_edit]["
            . htmlspecialchars($_REQUEST['rownumber']) . "][' + fieldmd5 + ']';\n"
            . "        var null_name = field_null + '[multi_edit]["
            . htmlspecialchars($_REQUEST['rownumber']) . "][' + fieldmd5 + ']';\n";
    } else {
        $element_name .= "var element_name = field + '[]'";
    }

    return $element_name;
}

/**
 * Function to get html for show all case
 *
 * @param array $foreignData foreign data
 *
 * @return string
 */
function PMA_getHtmlForShowAll($foreignData)
{
    $showall = '';
    if (is_array($foreignData['disp_row'])) {
        if ($GLOBALS['cfg']['ShowAll']
            && ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows'])
        ) {
            $showall = '<input type="submit" name="foreign_navig" value="'
                     . __('Show all') . '" />';
        }
    }

    return $showall;
}

/**
 * Function to get html for the goto page option
 *
 * @param array $foreignData foreign data
 *
 * @return string
 */
function PMA_getHtmlForGotoPage($foreignData)
{
    $gotopage = '';
    isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0;
    if (!is_array($foreignData['disp_row'])) {
        return $gotopage;
    }

    $session_max_rows = $GLOBALS['cfg']['MaxRows'];
    $pageNow = @floor($pos / $session_max_rows) + 1;
    $nbTotalPage = @ceil($foreignData['the_total'] / $session_max_rows);

    if ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) {
        $gotopage = PMA_Util::pageselector(
            'pos',
            $session_max_rows,
            $pageNow,
            $nbTotalPage,
            200,
            5,
            5,
            20,
            10,
            __('Page number:')
        );
    }

    return $gotopage;
}

/**
 * Function to get foreign limit
 *
 * @param string $foreign_navig foreign navigation
 *
 * @return string
 */
function PMA_getForeignLimit($foreign_navig)
{
    if (isset($foreign_navig) && $foreign_navig == __('Show all')) {
        return null;
    }
    isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0;
    return 'LIMIT ' . $pos . ', ' . $GLOBALS['cfg']['MaxRows'] . ' ';
}
?>