home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / tbl_addfield.php < prev    next >
Encoding:
PHP Script  |  2003-11-26  |  9.1 KB  |  237 lines

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