home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / tbl_addfield.php < prev    next >
Encoding:
PHP Script  |  2005-01-07  |  9.6 KB  |  259 lines

  1. <?php
  2. /* $Id: tbl_addfield.php,v 2.11 2005/01/07 14:33:20 lem9 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_charset[$i]) && $field_charset[$i] != '') {
  66.             $query .= ' CHARACTER SET ' . $field_charset[$i];
  67.         }
  68.         if ($field_default[$i] != '') {
  69.             if (strtoupper($field_default[$i]) == 'NULL') {
  70.                 $query .= ' DEFAULT NULL';
  71.             } else {
  72.                 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
  73.             }
  74.         }
  75.         if ($field_null[$i] != '') {
  76.             $query .= ' ' . $field_null[$i];
  77.         }
  78.         if ($field_extra[$i] != '') {
  79.             $query .= ' ' . $field_extra[$i];
  80.             // An auto_increment field must be use as a primary key
  81.             if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
  82.                 $primary_cnt = count($field_primary);
  83.                 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
  84.                     // void
  85.                 } // end for
  86.                 if ($field_primary[$j] == $i) {
  87.                     $query .= ' PRIMARY KEY';
  88.                     unset($field_primary[$j]);
  89.                 } // end if
  90.             } // end if (auto_increment)
  91.         }
  92.  
  93.         if ($field_where != 'last') {
  94.             // Only the first field can be added somewhere other than at the end
  95.             if ($i == 0) {
  96.                 if ($field_where == 'first') {
  97.                     $query .= ' FIRST';
  98.                 } else {
  99.                     $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
  100.                 }
  101.             } else {
  102.                 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
  103.             }
  104.         }
  105.         $query .= ', ADD ';
  106.     } // end for
  107.     $query = preg_replace('@, ADD $@', '', $query);
  108.  
  109.     // To allow replication, we first select the db to use and then run queries
  110.     // on this db.
  111.     PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
  112.     $sql_query     = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
  113.     $error_create = FALSE;
  114.     PMA_DBI_try_query($sql_query) or $error_create = TRUE;
  115.  
  116.     if ($error_create == false) {
  117.  
  118.         $sql_query_cpy = $sql_query . ';';
  119.  
  120.         // Builds the primary keys statements and updates the table
  121.         $primary = '';
  122.         if (isset($field_primary)) {
  123.             $primary_cnt = count($field_primary);
  124.             for ($i = 0; $i < $primary_cnt; $i++) {
  125.                 $j       = $field_primary[$i];
  126.                 if (!empty($field_name[$j])) {
  127.                     $primary .= PMA_backquote($field_name[$j]) . ', ';
  128.                 }
  129.             } // end for
  130.             $primary     = preg_replace('@, $@', '', $primary);
  131.             if (!empty($primary)) {
  132.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ');';
  133.                 $result         = PMA_DBI_query($sql_query);
  134.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  135.             }
  136.         } // end if
  137.  
  138.         // Builds the indexes statements and updates the table
  139.         $index = '';
  140.         if (isset($field_index)) {
  141.             $index_cnt = count($field_index);
  142.             for ($i = 0; $i < $index_cnt; $i++) {
  143.                 $j     = $field_index[$i];
  144.                 if (!empty($field_name[$j])) {
  145.                     $index .= PMA_backquote($field_name[$j]) . ', ';
  146.                 }
  147.             } // end for
  148.             $index     = preg_replace('@, $@', '', $index);
  149.             if (!empty($index)) {
  150.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
  151.                 $result         = PMA_DBI_query($sql_query);
  152.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  153.             }
  154.         } // end if
  155.  
  156.         // Builds the uniques statements and updates the table
  157.         $unique = '';
  158.         if (isset($field_unique)) {
  159.             $unique_cnt = count($field_unique);
  160.             for ($i = 0; $i < $unique_cnt; $i++) {
  161.                 $j      = $field_unique[$i];
  162.                 if (!empty($field_name[$j])) {
  163.                     $unique .= PMA_backquote($field_name[$j]) . ', ';
  164.                 }
  165.             } // end for
  166.             $unique = preg_replace('@, $@', '', $unique);
  167.             if (!empty($unique)) {
  168.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
  169.                 $result         = PMA_DBI_query($sql_query);
  170.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  171.             }
  172.         } // end if
  173.  
  174.  
  175.         // Builds the fulltext statements and updates the table
  176.         $fulltext = '';
  177.         if (isset($field_fulltext)) {
  178.             $fulltext_cnt = count($field_fulltext);
  179.             for ($i = 0; $i < $fulltext_cnt; $i++) {
  180.                 $j        = $field_fulltext[$i];
  181.                 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
  182.             } // end for
  183.             $fulltext = preg_replace('@, $@', '', $fulltext);
  184.             if (!empty($fulltext)) {
  185.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
  186.                 $result         = PMA_DBI_query($sql_query);
  187.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  188.             }
  189.         } // end if
  190.  
  191.         // garvin: If comments were sent, enable relation stuff
  192.         require_once('./libraries/relation.lib.php');
  193.         require_once('./libraries/transformations.lib.php');
  194.  
  195.         $cfgRelation = PMA_getRelationsParam();
  196.  
  197.         // garvin: Update comment table, if a comment was set.
  198.         if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
  199.             foreach ($field_comments AS $fieldindex => $fieldcomment) {
  200.                 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment);
  201.             }
  202.         }
  203.  
  204.         // garvin: Update comment table for mime types [MIME]
  205.         if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
  206.             foreach ($field_mimetype AS $fieldindex => $mimetype) {
  207.                 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
  208.             }
  209.         }
  210.  
  211.         // Go back to the structure sub-page
  212.         $sql_query = $sql_query_cpy;
  213.         unset($sql_query_cpy);
  214.         $message   = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
  215.         $active_page = 'tbl_properties_structure.php';
  216.         require('./tbl_properties_structure.php');
  217.     } else {
  218.         PMA_mysqlDie('', '', '', $err_url, FALSE);
  219.         // garvin: An error happened while inserting/updating a table definition.
  220.         // to prevent total loss of that data, we embed the form once again.
  221.         // The variable $regenerate will be used to restore data in tbl_properties.inc.php
  222.         $num_fields = $orig_num_fields;
  223.         if (isset($orig_after_field)) {
  224.             $after_field = $orig_after_field;
  225.         }
  226.         if (isset($orig_field_where)) {
  227.             $field_where = $orig_field_where;
  228.         }
  229.         $regenerate = true;
  230.     }
  231. } // end do alter table
  232.  
  233. /**
  234.  * Displays the form used to define the new field
  235.  */
  236. if ($abort == FALSE) {
  237.     /**
  238.      * Gets tables informations
  239.      */
  240.     require('./tbl_properties_common.php');
  241.     require('./tbl_properties_table_info.php');
  242.     /**
  243.      * Displays top menu links
  244.      */
  245.     $active_page = 'tbl_properties_structure.php';
  246.     require('./tbl_properties_links.php');
  247.     /**
  248.      * Display the form
  249.      */
  250.     $action = 'tbl_addfield.php';
  251.     require('./tbl_properties.inc.php');
  252.  
  253.     // Diplays the footer
  254.     echo "\n";
  255.     require_once('./footer.inc.php');
  256. }
  257.  
  258. ?>
  259.