InsertEditRowTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 65
c 2
b 0
f 0
dl 0
loc 137
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getAutocompleteFKProperties() 0 86 10
A _getFKProps() 0 13 3
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
/**
10
 * Common trait for exporting tables, views or materialized views.
11
 */
12
trait InsertEditRowTrait
13
{
14
    public $href = '';
15
16
    public $conf;
17
18
    public $misc;
19
20
    /**
21
     * Returns an array representing FKs definition for a table, sorted by fields
22
     * or by constraint.
23
     *
24
     * @param string $table The table to retrieve FK contraints from
25
     *
26
     * @return array{byconstr:array, byfield:array, code:string}|bool the array of FK definition:
27
     *
28
     * @example
29
     * ```
30
     * $fk = [
31
     *     'byconstr' => [
32
     *         'constrain id' => [
33
     *             'confrelid' => 'foreign relation oid',
34
     *             'f_schema'  => 'foreign schema name',
35
     *             'f_table'   => 'foreign table name',
36
     *             'pattnums'  => 'array of parent\'s fields nums',
37
     *             'pattnames' => 'array of parent\'s fields names',
38
     *             'fattnames' => 'array of foreign attributes names',
39
     *         ],
40
     *     ],
41
     *     'byfield'  => [
42
     *         'attribute num' => 'array (constraint id, ...)',
43
     *     ],
44
     *     'code'     => 'HTML/js code to include in the page for auto-completion',
45
     * ];
46
     * ```
47
     */
48
    public function getAutocompleteFKProperties($table)
49
    {
50
        $data = $this->misc->getDatabaseAccessor();
51
52
        $fksprops = [
53
            'byconstr' => [],
54
            'byfield' => [],
55
            'code' => '',
56
        ];
57
58
        $constrs = $data->getConstraintsWithFields($table);
59
60
        if (!$constrs->EOF) {
61
            //$conrelid = $constrs->fields['conrelid'];
62
            while (!$constrs->EOF) {
63
                if ('f' === $constrs->fields['contype']) {
64
                    if (!isset($fksprops['byconstr'][$constrs->fields['conid']])) {
65
                        $fksprops['byconstr'][$constrs->fields['conid']] = [
66
                            'confrelid' => $constrs->fields['confrelid'],
67
                            'f_table' => $constrs->fields['f_table'],
68
                            'f_schema' => $constrs->fields['f_schema'],
69
                            'pattnums' => [],
70
                            'pattnames' => [],
71
                            'fattnames' => [],
72
                        ];
73
                    }
74
75
                    $fksprops['byconstr'][$constrs->fields['conid']]['pattnums'][] = $constrs->fields['p_attnum'];
76
                    $fksprops['byconstr'][$constrs->fields['conid']]['pattnames'][] = $constrs->fields['p_field'];
77
                    $fksprops['byconstr'][$constrs->fields['conid']]['fattnames'][] = $constrs->fields['f_field'];
78
79
                    if (!isset($fksprops['byfield'][$constrs->fields['p_attnum']])) {
80
                        $fksprops['byfield'][$constrs->fields['p_attnum']] = [];
81
                    }
82
83
                    $fksprops['byfield'][$constrs->fields['p_attnum']][] = $constrs->fields['conid'];
84
                }
85
                $constrs->moveNext();
86
            }
87
88
            $fksprops['code'] = '<script type="text/javascript">' . \PHP_EOL;
89
            $fksprops['code'] .= "var constrs = {};\n";
90
91
            foreach ($fksprops['byconstr'] as $conid => $props) {
92
                $fksprops['code'] .= "constrs.constr_{$conid} = {\n";
93
                $fksprops['code'] .= 'pattnums: [' . \implode(',', $props['pattnums']) . "],\n";
94
                $fksprops['code'] .= "f_table:'" . \addslashes(\htmlentities($props['f_table'], \ENT_QUOTES, 'UTF-8')) . "',\n";
95
                $fksprops['code'] .= "f_schema:'" . \addslashes(\htmlentities($props['f_schema'], \ENT_QUOTES, 'UTF-8')) . "',\n";
96
                $_ = '';
97
98
                foreach ($props['pattnames'] as $n) {
99
                    $_ .= ",'" . \htmlentities($n, \ENT_QUOTES, 'UTF-8') . "'";
100
                }
101
                $fksprops['code'] .= 'pattnames: [' . \mb_substr($_, 1) . "],\n";
102
103
                $_ = '';
104
105
                foreach ($props['fattnames'] as $n) {
106
                    $_ .= ",'" . \htmlentities($n, \ENT_QUOTES, 'UTF-8') . "'";
107
                }
108
109
                $fksprops['code'] .= 'fattnames: [' . \mb_substr($_, 1) . "]\n";
110
                $fksprops['code'] .= "};\n";
111
            }
112
113
            $fksprops['code'] .= "var attrs = {};\n";
114
115
            foreach ($fksprops['byfield'] as $attnum => $cstrs) {
116
                $fksprops['code'] .= "attrs.attr_{$attnum} = [" . \implode(',', $fksprops['byfield'][$attnum]) . "];\n";
117
            }
118
119
            $fksprops['code'] .= "var table='" . \addslashes(\htmlentities($table, \ENT_QUOTES, 'UTF-8')) . "';";
120
            $fksprops['code'] .= "var server='" . \htmlentities($_REQUEST['server'], \ENT_QUOTES, 'UTF-8') . "';";
121
            $fksprops['code'] .= "var database='" . \addslashes(\htmlentities($_REQUEST['database'], \ENT_QUOTES, 'UTF-8')) . "';";
122
            $fksprops['code'] .= "var subfolder='" . \containerInstance()->subFolder . "';";
123
            $fksprops['code'] .= '</script>' . \PHP_EOL;
124
125
            $fksprops['code'] .= '<div id="fkbg"></div>';
126
            $fksprops['code'] .= '<div id="fklist"></div>';
127
            $fksprops['code'] .= '<script src="' . \containerInstance()->subFolder . '/assets/js/ac_insert_row.js" type="text/javascript"></script>';
128
        } else {
129
            /* we have no foreign keys on this table */
130
            return false;
131
        }
132
133
        return $fksprops;
134
    }
135
136
    private function _getFKProps()
137
    {
138
        if (('disable' !== $this->conf['autocomplete'])) {
139
            $fksprops = $this->getAutocompleteFKProperties($_REQUEST['table']);
140
141
            if (false !== $fksprops) {
0 ignored issues
show
introduced by
The condition false !== $fksprops is always false.
Loading history...
142
                echo $fksprops['code'];
143
            }
144
        } else {
145
            $fksprops = false;
146
        }
147
148
        return $fksprops;
149
    }
150
}
151