home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / tbl_addfield.php < prev    next >
Encoding:
PHP Script  |  2005-02-03  |  9.8 KB  |  261 lines

  1. <?php
  2. /* $Id: tbl_addfield.php,v 2.11.2.1 2005/02/03 19:56:54 rabus Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Get some core libraries
  7.  */
  8. require_once('./libraries/grab_globals.lib.php');
  9. $js_to_run = 'functions.js';
  10. require_once('./header.inc.php');
  11.  
  12. // Check parameters
  13. PMA_checkParameters(array('db', 'table'));
  14.  
  15.  
  16. /**
  17.  * Defines the url to return to in case of error in a sql statement
  18.  */
  19. $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table);
  20.  
  21. /**
  22.  * The form used to define the field to add has been submitted
  23.  */
  24. $abort = false;
  25. if (isset($submit_num_fields)) {
  26.     if (isset($orig_after_field)) {
  27.         $after_field = $orig_after_field;
  28.     }
  29.     if (isset($orig_field_where)) {
  30.         $field_where = $orig_field_where;
  31.     }
  32.     $num_fields = $orig_num_fields + $added_fields;
  33.     $regenerate = TRUE;
  34. } else if (isset($do_save_data)) {
  35.     $query = '';
  36.  
  37.     // Transforms the radio button field_key into 3 arrays
  38.     $field_cnt = count($field_name);
  39.     for ($i = 0; $i < $field_cnt; ++$i) {
  40.         if (isset(${'field_key_' . $i})) {
  41.             if (${'field_key_' . $i} == 'primary_' . $i) {
  42.                 $field_primary[] = $i;
  43.             }
  44.             if (${'field_key_' . $i} == 'index_' . $i) {
  45.                 $field_index[]   = $i;
  46.             }
  47.             if (${'field_key_' . $i} == 'unique_' . $i) {
  48.                 $field_unique[]  = $i;
  49.             }
  50.         } // end if
  51.     } // end for
  52.     // Builds the field creation statement and alters the table
  53.     for ($i = 0; $i < $field_cnt; ++$i) {
  54.         if (empty($field_name[$i])) {
  55.             continue;
  56.         }
  57.  
  58.         $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
  59.         if ($field_length[$i] != ''
  60.             && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) {
  61.             $query .= '(' . $field_length[$i] . ')';
  62.         }
  63.         if ($field_attribute[$i] != '') {
  64.             $query .= ' ' . $field_attribute[$i];
  65.         } else if (PMA_MYSQL_INT_VERSION >= 40100 && isset($field_collation[$i]) && $field_collation[$i] != '') {
  66.             list($tmp_charset) = explode('_', $field_collation[$i]);
  67.             $query .= ' CHARACTER SET ' . $tmp_charset . ' COLLATE ' . $field_collation[$i];
  68.             unset($tmp_charset);
  69.         }
  70.         if ($field_default[$i] != '') {
  71.             if (strtoupper($field_default[$i]) == 'NULL') {
  72.                 $query .= ' DEFAULT NULL';
  73.             } else {
  74.                 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
  75.             }
  76.         }
  77.         if ($field_null[$i] != '') {
  78.             $query .= ' ' . $field_null[$i];
  79.         }
  80.         if ($field_extra[$i] != '') {
  81.             $query .= ' ' . $field_extra[$i];
  82.             // An auto_increment field must be use as a primary key
  83.             if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
  84.                 $primary_cnt = count($field_primary);
  85.                 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
  86.                     // void
  87.                 } // end for
  88.                 if ($field_primary[$j] == $i) {
  89.                     $query .= ' PRIMARY KEY';
  90.                     unset($field_primary[$j]);
  91.                 } // end if
  92.             } // end if (auto_increment)
  93.         }
  94.  
  95.         if ($field_where != 'last') {
  96.             // Only the first field can be added somewhere other than at the end
  97.             if ($i == 0) {
  98.                 if ($field_where == 'first') {
  99.                     $query .= ' FIRST';
  100.                 } else {
  101.                     $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
  102.                 }
  103.             } else {
  104.                 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
  105.             }
  106.         }
  107.         $query .= ', ADD ';
  108.     } // end for
  109.     $query = preg_replace('@, ADD $@', '', $query);
  110.  
  111.     // To allow replication, we first select the db to use and then run queries
  112.     // on this db.
  113.     PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
  114.     $sql_query     = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
  115.     $error_create = FALSE;
  116.     PMA_DBI_try_query($sql_query) or $error_create = TRUE;
  117.  
  118.     if ($error_create == false) {
  119.  
  120.         $sql_query_cpy = $sql_query . ';';
  121.  
  122.         // Builds the primary keys statements and updates the table
  123.         $primary = '';
  124.         if (isset($field_primary)) {
  125.             $primary_cnt = count($field_primary);
  126.             for ($i = 0; $i < $primary_cnt; $i++) {
  127.                 $j       = $field_primary[$i];
  128.                 if (!empty($field_name[$j])) {
  129.                     $primary .= PMA_backquote($field_name[$j]) . ', ';
  130.                 }
  131.             } // end for
  132.             $primary     = preg_replace('@, $@', '', $primary);
  133.             if (!empty($primary)) {
  134.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ');';
  135.                 $result         = PMA_DBI_query($sql_query);
  136.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  137.             }
  138.         } // end if
  139.  
  140.         // Builds the indexes statements and updates the table
  141.         $index = '';
  142.         if (isset($field_index)) {
  143.             $index_cnt = count($field_index);
  144.             for ($i = 0; $i < $index_cnt; $i++) {
  145.                 $j     = $field_index[$i];
  146.                 if (!empty($field_name[$j])) {
  147.                     $index .= PMA_backquote($field_name[$j]) . ', ';
  148.                 }
  149.             } // end for
  150.             $index     = preg_replace('@, $@', '', $index);
  151.             if (!empty($index)) {
  152.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
  153.                 $result         = PMA_DBI_query($sql_query);
  154.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  155.             }
  156.         } // end if
  157.  
  158.         // Builds the uniques statements and updates the table
  159.         $unique = '';
  160.         if (isset($field_unique)) {
  161.             $unique_cnt = count($field_unique);
  162.             for ($i = 0; $i < $unique_cnt; $i++) {
  163.                 $j      = $field_unique[$i];
  164.                 if (!empty($field_name[$j])) {
  165.                     $unique .= PMA_backquote($field_name[$j]) . ', ';
  166.                 }
  167.             } // end for
  168.             $unique = preg_replace('@, $@', '', $unique);
  169.             if (!empty($unique)) {
  170.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
  171.                 $result         = PMA_DBI_query($sql_query);
  172.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  173.             }
  174.         } // end if
  175.  
  176.  
  177.         // Builds the fulltext statements and updates the table
  178.         $fulltext = '';
  179.         if (isset($field_fulltext)) {
  180.             $fulltext_cnt = count($field_fulltext);
  181.             for ($i = 0; $i < $fulltext_cnt; $i++) {
  182.                 $j        = $field_fulltext[$i];
  183.                 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
  184.             } // end for
  185.             $fulltext = preg_replace('@, $@', '', $fulltext);
  186.             if (!empty($fulltext)) {
  187.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
  188.                 $result         = PMA_DBI_query($sql_query);
  189.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  190.             }
  191.         } // end if
  192.  
  193.         // garvin: If comments were sent, enable relation stuff
  194.         require_once('./libraries/relation.lib.php');
  195.         require_once('./libraries/transformations.lib.php');
  196.  
  197.         $cfgRelation = PMA_getRelationsParam();
  198.  
  199.         // garvin: Update comment table, if a comment was set.
  200.         if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
  201.             foreach ($field_comments AS $fieldindex => $fieldcomment) {
  202.                 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment);
  203.             }
  204.         }
  205.  
  206.         // garvin: Update comment table for mime types [MIME]
  207.         if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
  208.             foreach ($field_mimetype AS $fieldindex => $mimetype) {
  209.                 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
  210.             }
  211.         }
  212.  
  213.         // Go back to the structure sub-page
  214.         $sql_query = $sql_query_cpy;
  215.         unset($sql_query_cpy);
  216.         $message   = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
  217.         $active_page = 'tbl_properties_structure.php';
  218.         require('./tbl_properties_structure.php');
  219.     } else {
  220.         PMA_mysqlDie('', '', '', $err_url, FALSE);
  221.         // garvin: An error happened while inserting/updating a table definition.
  222.         // to prevent total loss of that data, we embed the form once again.
  223.         // The variable $regenerate will be used to restore data in tbl_properties.inc.php
  224.         $num_fields = $orig_num_fields;
  225.         if (isset($orig_after_field)) {
  226.             $after_field = $orig_after_field;
  227.         }
  228.         if (isset($orig_field_where)) {
  229.             $field_where = $orig_field_where;
  230.         }
  231.         $regenerate = true;
  232.     }
  233. } // end do alter table
  234.  
  235. /**
  236.  * Displays the form used to define the new field
  237.  */
  238. if ($abort == FALSE) {
  239.     /**
  240.      * Gets tables informations
  241.      */
  242.     require('./tbl_properties_common.php');
  243.     require('./tbl_properties_table_info.php');
  244.     /**
  245.      * Displays top menu links
  246.      */
  247.     $active_page = 'tbl_properties_structure.php';
  248.     require('./tbl_properties_links.php');
  249.     /**
  250.      * Display the form
  251.      */
  252.     $action = 'tbl_addfield.php';
  253.     require('./tbl_properties.inc.php');
  254.  
  255.     // Diplays the footer
  256.     echo "\n";
  257.     require_once('./footer.inc.php');
  258. }
  259.  
  260. ?>
  261.