Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HTMLTableController.php « xhtml « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41391ab8a776c636ec5c0014a8c7906c4d75f9af (plain)
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
<?php

/**
 * PHPPgAdmin 6.0.0
 */

namespace PHPPgAdmin\XHtml;

use PHPPgAdmin\Decorators\Decorator;

/**
 * Class to render tables. Formerly part of Misc.php.
 */
class HTMLTableController extends HTMLController
{
    public $controller_name = 'HTMLTableController';

    protected $ma = [];

    protected $plugin_functions_parameters = [];

    protected $has_ma = false;

    protected $tabledata;

    protected $columns;

    protected $actions;

    protected $place;

    protected $nodata;

    protected $pre_fn;

    /**
     * Display a table of data.
     *
     * @param \ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted, as returned by $data->getDatabases() etc
     * @param array                                    $columns   An associative array of columns to be displayed:
     *                                                            $columns = array(
     *                                                            column_id => array(
     *                                                            'title' => Column heading,
     *                                                            'class' => The class to apply on the column cells,
     *                                                            'field' => Field name for $tabledata->fields[...],
     *                                                            'help'  => Help page for this column,
     *                                                            ), ...
     *                                                            );
     * @param array                                    $actions   Actions that can be performed on each object:
     *                                                            $actions = array(
     *                                                            * multi action support
     *                                                            * parameters are serialized for each entries and given in $_REQUEST['ma']
     *                                                            'multiactions' => array(
     *                                                            'keycols' => Associative array of (URL variable => field name), // fields included in the form
     *                                                            'url' => URL submission,
     *                                                            'default' => Default selected action in the form. If null, an empty action is added & selected
     *                                                            ),
     *                                                            * actions *
     *                                                            action_id => array(
     *                                                            'title' => Action heading,
     *                                                            'url'   => Static part of URL.  Often we rely
     *                                                            relative urls, usually the page itself (not '' !), or just a query string,
     *                                                            'vars'  => Associative array of (URL variable => field name),
     *                                                            'multiaction' => Name of the action to execute.
     *                                                            Add this action to the multi action form
     *                                                            ), ...
     *                                                            );
     * @param string                                   $place     Place where the $actions are displayed. Like 'display-browse',  where 'display'
     *                                                            is the entrypoint (/src/views/display) and 'browse' is the action used inside its controller (in this case, doBrowse).
     * @param string                                   $nodata    (optional) Message to display if data set is empty
     * @param callable                                 $pre_fn    (optional) callback closure for each row. It will be passed two params: $rowdata and $actions,
     *                                                            it may be used to derive new fields or modify actions.
     *                                                            It can return an array of actions specific to the row,  or if nothing is returned then the standard actions are used.
     *                                                            (see TblpropertiesController and ConstraintsController for examples)
     *                                                            The function must not must not store urls because     they are relative and won't work out of context.
     */
    public function initialize(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null): void
    {
        // Action buttons hook's place
        $this->plugin_functions_parameters = [
            'actionbuttons' => &$actions,
            'place' => $place,
        ];

        if ($this->has_ma = isset($actions['multiactions'])) {
            $this->ma = $actions['multiactions'];
        }
        unset($actions['multiactions']);

        $this->tabledata = $tabledata;
        $this->columns = $columns;
        $this->actions = $actions;
        $this->place = $place;
        $this->nodata = $nodata;
        $this->pre_fn = $pre_fn;
    }

    public function printTable($turn_into_datatable = true, $with_body = true)
    {
        if (0 >= $this->tabledata->recordCount()) {
            return "<p>{$this->nodata}</p>" . \PHP_EOL;
        }

        $tablehtml = '';
        // Remove the 'comment' column if they have been disabled
        if (!$this->conf['show_comments']) {
            unset($this->columns['comment']);
        }

        if (isset($this->columns['comment'])) {
            // Uncomment this for clipped comments.
            // TODO: This should be a user option.
            //$columns['comment']['params']['clip'] = true;
        }

        [$matop_html, $mabottom_html] = $this->_getMaHtml();

        $tablehtml .= $matop_html;

        $tablehtml .= '<table width="auto" class="' . ($turn_into_datatable ? 'will_be_datatable ' : ' ') . $this->place . '">' . \PHP_EOL;

        $tablehtml .= $this->getThead();

        $tablehtml .= $with_body ? $this->getTbody() : '';

        $tablehtml .= $this->getTfooter();

        $tablehtml .= '</table>' . \PHP_EOL;

        // Multi action table footer w/ options & [un]check'em all
        $tablehtml .= $mabottom_html;

        return $tablehtml;
    }

    public function getThead()
    {
        $columns = $this->columns;
        $actions = $this->actions;

        $thead_html = '<thead><tr>' . \PHP_EOL;

        // Display column headings
        if ($this->has_ma) {
            $thead_html .= '<th></th>';
        }

        foreach ($columns as $column_id => $column) {
            // Handle cases where no class has been passed

            $class = (isset($column['class']) && '' !== $column['class']) ? $column['class'] : '';

            switch ($column_id) {
                case 'actions':
                    if (0 < \count($actions)) {
                        $thead_html .= '<th class="data" >' . $column['title'] . '</th>' . \PHP_EOL;
                    }

                    break;

                default:
                    $thead_html .= '<th class="data' . $class . '">';

                    if (isset($column['help'])) {
                        $thead_html .= $this->view->printHelp($column['title'], $column['help'], false);
                    } else {
                        $thead_html .= $column['title'];
                    }

                    $thead_html .= '</th>' . \PHP_EOL;

                    break;
            }
        }
        $thead_html .= '</tr></thead>' . \PHP_EOL;

        return $thead_html;
    }

    public function getTfooter()
    {
        $columns = $this->columns;
        $actions = $this->actions;

        $tfoot_html = '<tfoot><tr>' . \PHP_EOL;

        // Display column headings
        if ($this->has_ma) {
            $tfoot_html .= '<td></td>';
        }

        foreach ($columns as $column_id => $column) {
            // Handle cases where no class has been passed

            $class = (isset($column['class']) && '' !== $column['class']) ? $column['class'] : '';

            if ('actions' !== $column_id || 0 < \count($actions)) {
                $tfoot_html .= "<td class=\"data{$class}\"></td>" . \PHP_EOL;
            }
        }
        $tfoot_html .= '</tr></tfoot>' . \PHP_EOL;

        return $tfoot_html;
    }

    private function _getMaHtml()
    {
        $matop_html = '';
        $ma_bottomhtml = '';
        $lang = $this->lang;

        if ($this->has_ma) {
            $matop_html .= '<script src="' . \containerInstance()->subFolder . '/assets/js/multiactionform.js" type="text/javascript"></script>' . \PHP_EOL;
            $matop_html .= \sprintf('<form id="multi_form" action="%s" method="post" enctype="multipart/form-data">%s', $this->ma['url'], \PHP_EOL);
            $this->coalesceArr($this->ma, 'vars', []);

            foreach ($this->ma['vars'] as $k => $v) {
                $matop_html .= \sprintf('<input type="hidden" name="%s" value="%s" />', $k, $v);
            }

            // if default is not set or doesn't exist, set it to null
            if (!isset($this->ma['default']) || !isset($this->actions[$this->ma['default']])) {
                $this->ma['default'] = null;
            }

            $ma_bottomhtml .= '<br />' . \PHP_EOL;
            $ma_bottomhtml .= '<table>' . \PHP_EOL;
            $ma_bottomhtml .= '<tr>' . \PHP_EOL;
            $ma_bottomhtml .= "<th class=\"data\" style=\"text-align: left\" colspan=\"3\">{$lang['stractionsonmultiplelines']}</th>" . \PHP_EOL;
            $ma_bottomhtml .= '</tr>' . \PHP_EOL;
            $ma_bottomhtml .= '<tr class="row1">' . \PHP_EOL;
            $ma_bottomhtml .= '<td>';
            $ma_bottomhtml .= "<a href=\"#\" onclick=\"javascript:checkAll(true);\">{$lang['strselectall']}</a> / ";
            $ma_bottomhtml .= "<a href=\"#\" onclick=\"javascript:checkAll(false);\">{$lang['strunselectall']}</a></td>" . \PHP_EOL;
            $ma_bottomhtml .= '<td>&nbsp;--->&nbsp;</td>' . \PHP_EOL;
            $ma_bottomhtml .= '<td>' . \PHP_EOL;
            $ma_bottomhtml .= "\t<select name=\"action\">" . \PHP_EOL;

            if (null === $this->ma['default']) {
                $ma_bottomhtml .= "\t\t<option value=\"\">--</option>" . \PHP_EOL;
            }

            foreach ($this->actions as $k => $a) {
                if (isset($a['multiaction'])) {
                    $selected = $this->ma['default'] === $k ? ' selected="selected" ' : '';
                    $ma_bottomhtml .= "\t\t";
                    $ma_bottomhtml .= '<option value="' . $a['multiaction'] . '" ' . $selected . ' rel="' . $k . '">' . $a['content'] . '</option>';
                    $ma_bottomhtml .= \PHP_EOL;
                }
            }

            $ma_bottomhtml .= "\t</select>" . \PHP_EOL;
            $ma_bottomhtml .= "<input type=\"submit\" value=\"{$lang['strexecute']}\" />" . \PHP_EOL;
            $ma_bottomhtml .= $this->getForm();
            $ma_bottomhtml .= '</td>' . \PHP_EOL;
            $ma_bottomhtml .= '</tr>' . \PHP_EOL;
            $ma_bottomhtml .= '</table>' . \PHP_EOL;
            $ma_bottomhtml .= '</form>';
        }

        return [$matop_html, $ma_bottomhtml];
    }

    private function getTbody()
    {
        $columns = $this->columns;
        $actions = $this->actions;
        $tabledata = $this->tabledata;
        $pre_fn = $this->pre_fn;

        // Display table rows
        $i = 0;
        $tbody_html = '<tbody>';

        while (!$tabledata->EOF) {
            $id = ($i % 2) + 1;

            unset($alt_actions);

            if (null !== $pre_fn) {
                $alt_actions = $pre_fn($tabledata, $actions);
            }

            if (!isset($alt_actions)) {
                $alt_actions = &$actions;
            }

            $tbody_html .= \sprintf('<tr class="data%s">', $id) . \PHP_EOL;

            if ($this->has_ma) {
                $a = [];

                foreach ($this->ma['keycols'] as $k => $v) {
                    $a[$k] = $tabledata->fields[$v];
                }
                $tbody_html .= \sprintf('<td><input type="checkbox" name="ma[]" value="%s"/></td>', \htmlentities(\serialize($a), \ENT_COMPAT, 'UTF-8')) . \PHP_EOL;
            }

            foreach ($columns as $column_id => $column) {
                // Apply default values for missing parameters
                if (isset($column['url']) && !isset($column['vars'])) {
                    $column['vars'] = [];
                }
                $class = (isset($column['class']) && '' !== $column['class']) ? $column['class'] : '';

                switch ($column_id) {
                    case 'actions':
                        $tbody_html .= "<td class=\"opbutton{$id} {$class}\">";

                        foreach ($alt_actions as $action) {
                            if (isset($action['disable']) && true === $action['disable']) {
                                continue;
                            }
                            $action['fields'] = $tabledata->fields;
                            $tbody_html .= $this->printLink($action, false, __METHOD__);
                        }
                        $tbody_html .= '</td>' . \PHP_EOL;

                        break;
                    case 'comment':
                        $tbody_html .= "<td class='comment_cell'>";
                        $tbody_html .= \htmlentities(Decorator::get_sanitized_value($column['field'], $tabledata->fields));
                        $tbody_html .= '</td>';

                        break;

                    default:
                        $tbody_html .= '<td class="' . $class . '">';
                        $val = Decorator::get_sanitized_value($column['field'], $tabledata->fields);

                        if (null !== $val) {
                            if (isset($column['url'])) {
                                $tbody_html .= "<a href=\"{$column['url']}";
                                $tbody_html .= $this->printUrlVars($column['vars'], $tabledata->fields, false);
                                $tbody_html .= '">';
                            }
                            $type = $column['type'] ?? null;
                            $params = $column['params'] ?? [];
                            $tbody_html .= $this->misc->printVal($val, $type, $params);

                            if (isset($column['url'])) {
                                $tbody_html .= '</a>';
                            }
                        }

                        $tbody_html .= '</td>' . \PHP_EOL;

                        break;
                }
            }
            $tbody_html .= '</tr>' . \PHP_EOL;

            $tabledata->moveNext();
            ++$i;
        }

        $tbody_html .= '</tbody>';

        return $tbody_html;
    }

    private function getForm()
    {
        if (!$this->form) {
            $this->form = $this->view->setForm();
        }

        return $this->form;
    }

    private function printUrlVars(&$vars, &$fields, bool $do_print = true)
    {
        $url_vars_html = '';

        foreach ($vars as $var => $varfield) {
            $url_vars_html .= "{$var}=" . \urlencode($fields[$varfield]) . '&amp;';
        }

        if ($do_print) {
            echo $url_vars_html;
        } else {
            return $url_vars_html;
        }
    }
}