home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / tbl_addfield.php < prev    next >
PHP Script  |  2008-06-23  |  9KB  |  237 lines

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