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

InsertEditRowTrait.php « traits « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a6c2ac2afef1ab82b876b2e271df4ae1cb89195b (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
<?php

/**
 * PHPPgAdmin 6.0.0
 */

namespace PHPPgAdmin\Traits;

/**
 * Common trait for exporting tables, views or materialized views.
 */
trait InsertEditRowTrait
{
    public $href = '';

    public $conf;

    public $misc;

    /**
     * Returns an array representing FKs definition for a table, sorted by fields
     * or by constraint.
     *
     * @param string $table The table to retrieve FK contraints from
     *
     * @return array{byconstr:array, byfield:array, code:string}|bool the array of FK definition:
     *
     * @example
     * ```
     * $fk = [
     *     'byconstr' => [
     *         'constrain id' => [
     *             'confrelid' => 'foreign relation oid',
     *             'f_schema'  => 'foreign schema name',
     *             'f_table'   => 'foreign table name',
     *             'pattnums'  => 'array of parent\'s fields nums',
     *             'pattnames' => 'array of parent\'s fields names',
     *             'fattnames' => 'array of foreign attributes names',
     *         ],
     *     ],
     *     'byfield'  => [
     *         'attribute num' => 'array (constraint id, ...)',
     *     ],
     *     'code'     => 'HTML/js code to include in the page for auto-completion',
     * ];
     * ```
     */
    public function getAutocompleteFKProperties($table)
    {
        $data = $this->misc->getDatabaseAccessor();

        $fksprops = [
            'byconstr' => [],
            'byfield' => [],
            'code' => '',
        ];

        $constrs = $data->getConstraintsWithFields($table);

        if (!$constrs->EOF) {
            //$conrelid = $constrs->fields['conrelid'];
            while (!$constrs->EOF) {
                if ('f' === $constrs->fields['contype']) {
                    if (!isset($fksprops['byconstr'][$constrs->fields['conid']])) {
                        $fksprops['byconstr'][$constrs->fields['conid']] = [
                            'confrelid' => $constrs->fields['confrelid'],
                            'f_table' => $constrs->fields['f_table'],
                            'f_schema' => $constrs->fields['f_schema'],
                            'pattnums' => [],
                            'pattnames' => [],
                            'fattnames' => [],
                        ];
                    }

                    $fksprops['byconstr'][$constrs->fields['conid']]['pattnums'][] = $constrs->fields['p_attnum'];
                    $fksprops['byconstr'][$constrs->fields['conid']]['pattnames'][] = $constrs->fields['p_field'];
                    $fksprops['byconstr'][$constrs->fields['conid']]['fattnames'][] = $constrs->fields['f_field'];

                    if (!isset($fksprops['byfield'][$constrs->fields['p_attnum']])) {
                        $fksprops['byfield'][$constrs->fields['p_attnum']] = [];
                    }

                    $fksprops['byfield'][$constrs->fields['p_attnum']][] = $constrs->fields['conid'];
                }
                $constrs->moveNext();
            }

            $fksprops['code'] = '<script type="text/javascript">' . \PHP_EOL;
            $fksprops['code'] .= "var constrs = {};\n";

            foreach ($fksprops['byconstr'] as $conid => $props) {
                $fksprops['code'] .= "constrs.constr_{$conid} = {\n";
                $fksprops['code'] .= 'pattnums: [' . \implode(',', $props['pattnums']) . "],\n";
                $fksprops['code'] .= "f_table:'" . \addslashes(\htmlentities($props['f_table'], \ENT_QUOTES, 'UTF-8')) . "',\n";
                $fksprops['code'] .= "f_schema:'" . \addslashes(\htmlentities($props['f_schema'], \ENT_QUOTES, 'UTF-8')) . "',\n";
                $_ = '';

                foreach ($props['pattnames'] as $n) {
                    $_ .= ",'" . \htmlentities($n, \ENT_QUOTES, 'UTF-8') . "'";
                }
                $fksprops['code'] .= 'pattnames: [' . \mb_substr($_, 1) . "],\n";

                $_ = '';

                foreach ($props['fattnames'] as $n) {
                    $_ .= ",'" . \htmlentities($n, \ENT_QUOTES, 'UTF-8') . "'";
                }

                $fksprops['code'] .= 'fattnames: [' . \mb_substr($_, 1) . "]\n";
                $fksprops['code'] .= "};\n";
            }

            $fksprops['code'] .= "var attrs = {};\n";

            foreach ($fksprops['byfield'] as $attnum => $cstrs) {
                $fksprops['code'] .= "attrs.attr_{$attnum} = [" . \implode(',', $fksprops['byfield'][$attnum]) . "];\n";
            }

            $fksprops['code'] .= "var table='" . \addslashes(\htmlentities($table, \ENT_QUOTES, 'UTF-8')) . "';";
            $fksprops['code'] .= "var server='" . \htmlentities($_REQUEST['server'], \ENT_QUOTES, 'UTF-8') . "';";
            $fksprops['code'] .= "var database='" . \addslashes(\htmlentities($_REQUEST['database'], \ENT_QUOTES, 'UTF-8')) . "';";
            $fksprops['code'] .= "var subfolder='" . self::SUBFOLDER . "';";
            $fksprops['code'] .= '</script>' . \PHP_EOL;

            $fksprops['code'] .= '<div id="fkbg"></div>';
            $fksprops['code'] .= '<div id="fklist"></div>';
            $fksprops['code'] .= '<script src="' . self::SUBFOLDER . '/assets/js/ac_insert_row.js" type="text/javascript"></script>';
        } else {
            /* we have no foreign keys on this table */
            return false;
        }

        return $fksprops;
    }

    private function _getFKProps()
    {
        if (('disable' !== $this->conf['autocomplete'])) {
            $fksprops = $this->getAutocompleteFKProperties($_REQUEST['table']);

            if (false !== $fksprops) {
                echo $fksprops['code'];
            }
        } else {
            $fksprops = false;
        }

        return $fksprops;
    }
}