Passed
Push — develop ( ff06b5...106cda )
by Felipe
04:40
created

InsertEditRowTrait::_getFKProps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.47
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
    public $conf;
16
    public $misc;
17
18
    private function _getFKProps()
19
    {
20
        if (('disable' != $this->conf['autocomplete'])) {
21
            $fksprops = $this->getAutocompleteFKProperties($_REQUEST['table']);
22
            if (false !== $fksprops) {
23
                echo $fksprops['code'];
24
            }
25
        } else {
26
            $fksprops = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $fksprops is dead and can be removed.
Loading history...
27
        }
28
    }
29
30
    /**
31
     * Returns an array representing FKs definition for a table, sorted by fields
32
     * or by constraint.
33
     *
34
     * @param string $table The table to retrieve FK contraints from
35
     *
36
     * @return array|bool the array of FK definition:
37
     *                    array(
38
     *                    'byconstr' => array(
39
     *                    constrain id => array(
40
     *                    confrelid => foreign relation oid
41
     *                    f_schema => foreign schema name
42
     *                    f_table => foreign table name
43
     *                    pattnums => array of parent's fields nums
44
     *                    pattnames => array of parent's fields names
45
     *                    fattnames => array of foreign attributes names
46
     *                    )
47
     *                    ),
48
     *                    'byfield' => array(
49
     *                    attribute num => array (constraint id, ...)
50
     *                    ),
51
     *                    'code' => HTML/js code to include in the page for auto-completion
52
     *                    )
53
     */
54
    public function getAutocompleteFKProperties($table)
55
    {
56
        $data = $this->misc->getDatabaseAccessor();
57
58
        $fksprops = [
59
            'byconstr' => [],
60
            'byfield'  => [],
61
            'code'     => '',
62
        ];
63
64
        $constrs = $data->getConstraintsWithFields($table);
65
66
        if (!$constrs->EOF) {
67
            //$conrelid = $constrs->fields['conrelid'];
68
            while (!$constrs->EOF) {
69
                if ($constrs->fields['contype'] == 'f') {
70
                    if (!isset($fksprops['byconstr'][$constrs->fields['conid']])) {
71
                        $fksprops['byconstr'][$constrs->fields['conid']] = [
72
                            'confrelid' => $constrs->fields['confrelid'],
73
                            'f_table'   => $constrs->fields['f_table'],
74
                            'f_schema'  => $constrs->fields['f_schema'],
75
                            'pattnums'  => [],
76
                            'pattnames' => [],
77
                            'fattnames' => [],
78
                        ];
79
                    }
80
81
                    $fksprops['byconstr'][$constrs->fields['conid']]['pattnums'][]  = $constrs->fields['p_attnum'];
82
                    $fksprops['byconstr'][$constrs->fields['conid']]['pattnames'][] = $constrs->fields['p_field'];
83
                    $fksprops['byconstr'][$constrs->fields['conid']]['fattnames'][] = $constrs->fields['f_field'];
84
85
                    if (!isset($fksprops['byfield'][$constrs->fields['p_attnum']])) {
86
                        $fksprops['byfield'][$constrs->fields['p_attnum']] = [];
87
                    }
88
89
                    $fksprops['byfield'][$constrs->fields['p_attnum']][] = $constrs->fields['conid'];
90
                }
91
                $constrs->moveNext();
92
            }
93
94
            $fksprops['code'] = "<script type=\"text/javascript\">\n";
95
            $fksprops['code'] .= "var constrs = {};\n";
96
            foreach ($fksprops['byconstr'] as $conid => $props) {
97
                $fksprops['code'] .= "constrs.constr_{$conid} = {\n";
98
                $fksprops['code'] .= 'pattnums: [' . implode(',', $props['pattnums']) . "],\n";
99
                $fksprops['code'] .= "f_table:'" . addslashes(htmlentities($props['f_table'], ENT_QUOTES, 'UTF-8')) . "',\n";
100
                $fksprops['code'] .= "f_schema:'" . addslashes(htmlentities($props['f_schema'], ENT_QUOTES, 'UTF-8')) . "',\n";
101
                $_ = '';
102
                foreach ($props['pattnames'] as $n) {
103
                    $_ .= ",'" . htmlentities($n, ENT_QUOTES, 'UTF-8') . "'";
104
                }
105
                $fksprops['code'] .= 'pattnames: [' . substr($_, 1) . "],\n";
106
107
                $_ = '';
108
                foreach ($props['fattnames'] as $n) {
109
                    $_ .= ",'" . htmlentities($n, ENT_QUOTES, 'UTF-8') . "'";
110
                }
111
112
                $fksprops['code'] .= 'fattnames: [' . substr($_, 1) . "]\n";
113
                $fksprops['code'] .= "};\n";
114
            }
115
116
            $fksprops['code'] .= "var attrs = {};\n";
117
            foreach ($fksprops['byfield'] as $attnum => $cstrs) {
118
                $fksprops['code'] .= "attrs.attr_{$attnum} = [" . implode(',', $fksprops['byfield'][$attnum]) . "];\n";
119
            }
120
121
            $fksprops['code'] .= "var table='" . addslashes(htmlentities($table, ENT_QUOTES, 'UTF-8')) . "';";
122
            $fksprops['code'] .= "var server='" . htmlentities($_REQUEST['server'], ENT_QUOTES, 'UTF-8') . "';";
123
            $fksprops['code'] .= "var database='" . addslashes(htmlentities($_REQUEST['database'], ENT_QUOTES, 'UTF-8')) . "';";
124
            $fksprops['code'] .= "var subfolder='" . SUBFOLDER . "';";
125
            $fksprops['code'] .= "</script>\n";
126
127
            $fksprops['code'] .= '<div id="fkbg"></div>';
128
            $fksprops['code'] .= '<div id="fklist"></div>';
129
            $fksprops['code'] .= '<script src="' . SUBFOLDER . '/assets/js/ac_insert_row.js" type="text/javascript"></script>';
130
        } else {
131
            /* we have no foreign keys on this table */
132
            return false;
133
        }
134
135
        return $fksprops;
136
    }
137
}
138