Updated documentation.
[openemr.git] / interface / main / myadmin / tbl_create.php
blob05077d666a198392d434e518f5fbde8802f6a35c
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Get some core libraries
8 */
9 require_once('./libraries/grab_globals.lib.php');
10 $js_to_run = 'functions.js';
11 require_once('./header.inc.php');
13 // Check parameters
15 require_once('./libraries/common.lib.php');
17 PMA_checkParameters(array('db', 'table'));
19 /**
20 * Defines the url to return to in case of error in a sql statement
22 $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table);
25 /**
26 * Selects the database to work with
28 PMA_mysql_select_db($db);
31 /**
32 * The form used to define the structure of the table has been submitted
34 $abort = false;
35 if (isset($submit)) {
36 $sql_query = $query_cpy = '';
38 // Transforms the radio button field_key into 3 arrays
39 $field_cnt = count($field_name);
40 for ($i = 0; $i < $field_cnt; ++$i) {
41 if (isset(${'field_key_' . $i})) {
42 if (${'field_key_' . $i} == 'primary_' . $i) {
43 $field_primary[] = $i;
45 if (${'field_key_' . $i} == 'index_' . $i) {
46 $field_index[] = $i;
48 if (${'field_key_' . $i} == 'unique_' . $i) {
49 $field_unique[] = $i;
51 } // end if
52 } // end for
53 // Builds the fields creation statements
54 for ($i = 0; $i < $field_cnt; $i++) {
55 if (empty($field_name[$i])) {
56 continue;
58 $query = PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
59 if ($field_length[$i] != '') {
60 $query .= '(' . $field_length[$i] . ')';
62 if ($field_attribute[$i] != '') {
63 $query .= ' ' . $field_attribute[$i];
64 } else if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($field_charset[$i])) {
65 $query .= ' CHARACTER SET ' . $field_charset[$i];
67 if ($field_default[$i] != '') {
68 if (strtoupper($field_default[$i]) == 'NULL') {
69 $query .= ' DEFAULT NULL';
70 } else {
71 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
74 if ($field_null[$i] != '') {
75 $query .= ' ' . $field_null[$i];
77 if ($field_extra[$i] != '') {
78 $query .= ' ' . $field_extra[$i];
80 $query .= ', ';
81 $sql_query .= $query;
82 $query_cpy .= "\n" . ' ' . $query;
83 } // end for
84 unset($field_cnt);
85 unset($query);
86 $sql_query = preg_replace('@, $@', '', $sql_query);
87 $query_cpy = preg_replace('@, $@', '', $query_cpy);
89 // Builds the primary keys statements
90 $primary = '';
91 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
92 for ($i = 0; $i < $primary_cnt; $i++) {
93 $j = $field_primary[$i];
94 if (!empty($field_name[$j])) {
95 $primary .= PMA_backquote($field_name[$j]) . ', ';
97 } // end for
98 unset($primary_cnt);
99 $primary = preg_replace('@, $@', '', $primary);
100 if (!empty($primary)) {
101 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
102 $query_cpy .= ',' . "\n" . ' PRIMARY KEY (' . $primary . ')';
104 unset($primary);
106 // Builds the indexes statements
107 $index = '';
108 $index_cnt = (isset($field_index) ? count($field_index) : 0);
109 for ($i = 0;$i < $index_cnt; $i++) {
110 $j = $field_index[$i];
111 if (!empty($field_name[$j])) {
112 $index .= PMA_backquote($field_name[$j]) . ', ';
114 } // end for
115 unset($index_cnt);
116 $index = preg_replace('@, $@', '', $index);
117 if (!empty($index)) {
118 $sql_query .= ', INDEX (' . $index . ')';
119 $query_cpy .= ',' . "\n" . ' INDEX (' . $index . ')';
121 unset($index);
123 // Builds the uniques statements
124 $unique = '';
125 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
126 for ($i = 0; $i < $unique_cnt; $i++) {
127 $j = $field_unique[$i];
128 if (!empty($field_name[$j])) {
129 $unique .= PMA_backquote($field_name[$j]) . ', ';
131 } // end for
132 unset($unique_cnt);
133 $unique = preg_replace('@, $@', '', $unique);
134 if (!empty($unique)) {
135 $sql_query .= ', UNIQUE (' . $unique . ')';
136 $query_cpy .= ',' . "\n" . ' UNIQUE (' . $unique . ')';
138 unset($unique);
140 // Builds the fulltextes statements
141 $fulltext = '';
142 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
143 for ($i = 0; $i < $fulltext_cnt; $i++) {
144 $j = $field_fulltext[$i];
145 if (!empty($field_name[$j])) {
146 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
148 } // end for
150 $fulltext = preg_replace('@, $@', '', $fulltext);
151 if (!empty($fulltext)) {
152 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
153 $query_cpy .= ',' . "\n" . ' FULLTEXT (' . $fulltext . ')';
155 unset($fulltext);
157 // Builds the 'create table' statement
158 $sql_query = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $sql_query . ')';
159 $query_cpy = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $query_cpy . "\n" . ')';
161 // Adds table type, character set and comments
162 if (!empty($tbl_type) && ($tbl_type != 'Default')) {
163 $sql_query .= ' TYPE = ' . $tbl_type;
164 $query_cpy .= "\n" . 'TYPE = ' . $tbl_type;
166 if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_charset)) {
167 $sql_query .= ' CHARACTER SET = ' . $tbl_charset;
168 $query_cpy .= "\n" . 'CHARACTER SET = ' . $tbl_charset;
171 if (!empty($comment)) {
172 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
173 $query_cpy .= "\n" . 'COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
176 // Executes the query
177 $error_create = false;
178 $result = PMA_mysql_query($sql_query) or $error_create = true;
180 if ($error_create == false) {
181 $sql_query = $query_cpy . ';';
182 unset($query_cpy);
183 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenCreated;
185 // garvin: If comments were sent, enable relation stuff
186 require_once('./libraries/relation.lib.php');
187 require_once('./libraries/transformations.lib.php');
189 $cfgRelation = PMA_getRelationsParam();
191 // garvin: Update comment table, if a comment was set.
192 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
193 foreach($field_comments AS $fieldindex => $fieldcomment) {
194 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment);
198 // garvin: Update comment table for mime types [MIME]
199 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
200 foreach($field_mimetype AS $fieldindex => $mimetype) {
201 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
205 require('./' . $cfg['DefaultTabTable']);
206 $abort = TRUE;
207 exit();
208 } else {
209 PMA_mysqlDie('', '', '', $err_url, FALSE);
210 // garvin: An error happened while inserting/updating a table definition.
211 // to prevent total loss of that data, we embed the form once again.
212 // The variable $regenerate will be used to restore data in tbl_properties.inc.php
213 $num_fields = $orig_num_fields;
214 $regenerate = true;
216 } // end do create table
219 * Displays the form used to define the structure of the table
221 if ($abort == FALSE) {
222 if (isset($num_fields)) {
223 $num_fields = intval($num_fields);
225 // No table name
226 if (!isset($table) || trim($table) == '') {
227 PMA_mysqlDie($strTableEmpty, '', '', $err_url);
229 // No valid number of fields
230 else if (empty($num_fields) || !is_int($num_fields)) {
231 PMA_mysqlDie($strFieldsEmpty, '', '', $err_url);
233 // Table name and number of fields are valid -> show the form
234 else {
235 $action = 'tbl_create.php';
236 require('./tbl_properties.inc.php');
237 // Diplays the footer
238 echo "\n";
239 require_once('./footer.inc.php');