PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/tbl_addfield.php

https://github.com/md-tech/openemr
PHP | 236 lines | 165 code | 26 blank | 45 comment | 61 complexity | 746e470e962e6ac6fe5aa20d88260f67 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @version $Id$
  6. */
  7. /**
  8. * Get some core libraries
  9. */
  10. require_once './libraries/common.inc.php';
  11. require_once './libraries/Table.class.php';
  12. $js_to_run = 'functions.js';
  13. require_once './libraries/header.inc.php';
  14. // Check parameters
  15. PMA_checkParameters(array('db', 'table'));
  16. /**
  17. * Defines the url to return to in case of error in a sql statement
  18. */
  19. $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
  20. /**
  21. * The form used to define the field to add has been submitted
  22. */
  23. $abort = false;
  24. if (isset($submit_num_fields)) {
  25. if (isset($orig_after_field)) {
  26. $after_field = $orig_after_field;
  27. }
  28. if (isset($orig_field_where)) {
  29. $field_where = $orig_field_where;
  30. }
  31. $num_fields = $orig_num_fields + $added_fields;
  32. $regenerate = TRUE;
  33. } elseif (isset($do_save_data)) {
  34. $query = '';
  35. // Transforms the radio button field_key into 3 arrays
  36. $field_cnt = count($field_name);
  37. for ($i = 0; $i < $field_cnt; ++$i) {
  38. if (isset(${'field_key_' . $i})) {
  39. if (${'field_key_' . $i} == 'primary_' . $i) {
  40. $field_primary[] = $i;
  41. }
  42. if (${'field_key_' . $i} == 'index_' . $i) {
  43. $field_index[] = $i;
  44. }
  45. if (${'field_key_' . $i} == 'unique_' . $i) {
  46. $field_unique[] = $i;
  47. }
  48. } // end if
  49. } // end for
  50. // Builds the field creation statement and alters the table
  51. for ($i = 0; $i < $field_cnt; ++$i) {
  52. // '0' is also empty for php :-(
  53. if (empty($field_name[$i]) && $field_name[$i] != '0') {
  54. continue;
  55. }
  56. $query .= PMA_Table::generateFieldSpec($field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$i]) ? $field_collation[$i] : '', $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_primary, $i);
  57. if ($field_where != 'last') {
  58. // Only the first field can be added somewhere other than at the end
  59. if ($i == 0) {
  60. if ($field_where == 'first') {
  61. $query .= ' FIRST';
  62. } else {
  63. $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
  64. }
  65. } else {
  66. $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
  67. }
  68. }
  69. $query .= ', ADD ';
  70. } // end for
  71. $query = preg_replace('@, ADD $@', '', $query);
  72. // To allow replication, we first select the db to use and then run queries
  73. // on this db.
  74. PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
  75. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
  76. $error_create = FALSE;
  77. PMA_DBI_try_query($sql_query) or $error_create = TRUE;
  78. if ($error_create == false) {
  79. $sql_query_cpy = $sql_query . ';';
  80. // Builds the primary keys statements and updates the table
  81. $primary = '';
  82. if (isset($field_primary)) {
  83. $primary_cnt = count($field_primary);
  84. for ($i = 0; $i < $primary_cnt; $i++) {
  85. $j = $field_primary[$i];
  86. if (isset($field_name[$j]) && strlen($field_name[$j])) {
  87. $primary .= PMA_backquote($field_name[$j]) . ', ';
  88. }
  89. } // end for
  90. $primary = preg_replace('@, $@', '', $primary);
  91. if (strlen($primary)) {
  92. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ');';
  93. $result = PMA_DBI_query($sql_query);
  94. $sql_query_cpy .= "\n" . $sql_query . ';';
  95. }
  96. } // end if
  97. // Builds the indexes statements and updates the table
  98. $index = '';
  99. if (isset($field_index)) {
  100. $index_cnt = count($field_index);
  101. for ($i = 0; $i < $index_cnt; $i++) {
  102. $j = $field_index[$i];
  103. if (isset($field_name[$j]) && strlen($field_name[$j])) {
  104. $index .= PMA_backquote($field_name[$j]) . ', ';
  105. }
  106. } // end for
  107. $index = preg_replace('@, $@', '', $index);
  108. if (strlen($index)) {
  109. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
  110. $result = PMA_DBI_query($sql_query);
  111. $sql_query_cpy .= "\n" . $sql_query . ';';
  112. }
  113. } // end if
  114. // Builds the uniques statements and updates the table
  115. $unique = '';
  116. if (isset($field_unique)) {
  117. $unique_cnt = count($field_unique);
  118. for ($i = 0; $i < $unique_cnt; $i++) {
  119. $j = $field_unique[$i];
  120. if (isset($field_name[$j]) && strlen($field_name[$j])) {
  121. $unique .= PMA_backquote($field_name[$j]) . ', ';
  122. }
  123. } // end for
  124. $unique = preg_replace('@, $@', '', $unique);
  125. if (strlen($unique)) {
  126. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
  127. $result = PMA_DBI_query($sql_query);
  128. $sql_query_cpy .= "\n" . $sql_query . ';';
  129. }
  130. } // end if
  131. // Builds the fulltext statements and updates the table
  132. $fulltext = '';
  133. if (isset($field_fulltext)) {
  134. $fulltext_cnt = count($field_fulltext);
  135. for ($i = 0; $i < $fulltext_cnt; $i++) {
  136. $j = $field_fulltext[$i];
  137. $fulltext .= PMA_backquote($field_name[$j]) . ', ';
  138. } // end for
  139. $fulltext = preg_replace('@, $@', '', $fulltext);
  140. if (strlen($fulltext)) {
  141. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
  142. $result = PMA_DBI_query($sql_query);
  143. $sql_query_cpy .= "\n" . $sql_query . ';';
  144. }
  145. } // end if
  146. // garvin: If comments were sent, enable relation stuff
  147. require_once './libraries/relation.lib.php';
  148. require_once './libraries/transformations.lib.php';
  149. $cfgRelation = PMA_getRelationsParam();
  150. // garvin: Update comment table, if a comment was set.
  151. if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
  152. foreach ($field_comments AS $fieldindex => $fieldcomment) {
  153. if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
  154. PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
  155. }
  156. }
  157. }
  158. // garvin: Update comment table for mime types [MIME]
  159. if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
  160. foreach ($field_mimetype AS $fieldindex => $mimetype) {
  161. if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
  162. PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
  163. }
  164. }
  165. }
  166. // Go back to the structure sub-page
  167. $sql_query = $sql_query_cpy;
  168. unset($sql_query_cpy);
  169. $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
  170. $active_page = 'tbl_structure.php';
  171. require './tbl_structure.php';
  172. } else {
  173. PMA_mysqlDie('', '', '', $err_url, FALSE);
  174. // garvin: An error happened while inserting/updating a table definition.
  175. // to prevent total loss of that data, we embed the form once again.
  176. // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
  177. $num_fields = $orig_num_fields;
  178. if (isset($orig_after_field)) {
  179. $after_field = $orig_after_field;
  180. }
  181. if (isset($orig_field_where)) {
  182. $field_where = $orig_field_where;
  183. }
  184. $regenerate = true;
  185. }
  186. } // end do alter table
  187. /**
  188. * Displays the form used to define the new field
  189. */
  190. if ($abort == FALSE) {
  191. /**
  192. * Gets tables informations
  193. */
  194. require_once './libraries/tbl_common.php';
  195. require_once './libraries/tbl_info.inc.php';
  196. /**
  197. * Displays top menu links
  198. */
  199. $active_page = 'tbl_structure.php';
  200. require_once './libraries/tbl_links.inc.php';
  201. /**
  202. * Display the form
  203. */
  204. $action = 'tbl_addfield.php';
  205. require_once './libraries/tbl_properties.inc.php';
  206. // Diplays the footer
  207. echo "\n";
  208. require_once './libraries/footer.inc.php';
  209. }
  210. ?>