home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / tbl_create.php < prev    next >
Encoding:
PHP Script  |  2004-12-26  |  8.5 KB  |  249 lines

  1. <?php
  2. /* $Id: tbl_create.php,v 2.12 2004/12/26 22:47:40 lem9 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.  
  15. require_once('./libraries/common.lib.php');
  16.  
  17. PMA_checkParameters(array('db', 'table'));
  18.  
  19. /**
  20.  * Defines the url to return to in case of error in a sql statement
  21.  */
  22. $err_url = $cfg['DefaultTabTable'] . '?' . PMA_generate_common_url($db, $table);
  23.  
  24.  
  25. /**
  26.  * Selects the database to work with
  27.  */
  28. PMA_DBI_select_db($db);
  29.  
  30.  
  31. /**
  32.  * The form used to define the structure of the table has been submitted
  33.  */
  34. $abort = FALSE;
  35. if (isset($submit_num_fields)) {
  36.     $regenerate = TRUE;
  37.     $num_fields = $orig_num_fields + $added_fields;
  38. } else if (isset($do_save_data)) {
  39.     $sql_query = $query_cpy = '';
  40.  
  41.     // Transforms the radio button field_key into 3 arrays
  42.     $field_cnt = count($field_name);
  43.     for ($i = 0; $i < $field_cnt; ++$i) {
  44.         if (isset(${'field_key_' . $i})) {
  45.             if (${'field_key_' . $i} == 'primary_' . $i) {
  46.                 $field_primary[] = $i;
  47.             }
  48.             if (${'field_key_' . $i} == 'index_' . $i) {
  49.                 $field_index[]   = $i;
  50.             }
  51.             if (${'field_key_' . $i} == 'unique_' . $i) {
  52.                 $field_unique[]  = $i;
  53.             }
  54.         } // end if
  55.     } // end for
  56.     // Builds the fields creation statements
  57.     for ($i = 0; $i < $field_cnt; $i++) {
  58.         // '0' is also empty for php :-(
  59.         if (empty($field_name[$i]) && $field_name[$i] != '0') {
  60.             continue;
  61.         }
  62.         $query = PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
  63.         if ($field_length[$i] != ''
  64.             && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) {
  65.             $query .= '(' . $field_length[$i] . ')';
  66.         }
  67.         if ($field_attribute[$i] != '') {
  68.             $query .= ' ' . $field_attribute[$i];
  69.         } else if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($field_collation[$i])) {
  70.             $query .= PMA_generateCharsetQueryPart($field_collation[$i]);
  71.         }
  72.         if ($field_default[$i] != '') {
  73.             if (strtoupper($field_default[$i]) == 'NULL') {
  74.                 $query .= ' DEFAULT NULL';
  75.             } else {
  76.                 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
  77.             }
  78.         }
  79.         if ($field_null[$i] != '') {
  80.             $query .= ' ' . $field_null[$i];
  81.         }
  82.         if ($field_extra[$i] != '') {
  83.             $query .= ' ' . $field_extra[$i];
  84.         }
  85.         $query .= ', ';
  86.         $sql_query .= $query;
  87.         $query_cpy .= "\n" . '  ' . $query;
  88.     } // end for
  89.     unset($field_cnt);
  90.     unset($query);
  91.     $sql_query = preg_replace('@, $@', '', $sql_query);
  92.     $query_cpy = preg_replace('@, $@', '', $query_cpy);
  93.  
  94.     // Builds the primary keys statements
  95.     $primary     = '';
  96.     $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
  97.     for ($i = 0; $i < $primary_cnt; $i++) {
  98.         $j = $field_primary[$i];
  99.         if (!empty($field_name[$j])) {
  100.             $primary .= PMA_backquote($field_name[$j]) . ', ';
  101.         }
  102.     } // end for
  103.     unset($primary_cnt);
  104.     $primary = preg_replace('@, $@', '', $primary);
  105.     if (!empty($primary)) {
  106.         $sql_query .= ', PRIMARY KEY (' . $primary . ')';
  107.         $query_cpy .= ',' . "\n" . '  PRIMARY KEY (' . $primary . ')';
  108.     }
  109.     unset($primary);
  110.  
  111.     // Builds the indexes statements
  112.     $index     = '';
  113.     $index_cnt = (isset($field_index) ? count($field_index) : 0);
  114.     for ($i = 0;$i < $index_cnt; $i++) {
  115.         $j = $field_index[$i];
  116.         if (!empty($field_name[$j])) {
  117.             $index .= PMA_backquote($field_name[$j]) . ', ';
  118.         }
  119.     } // end for
  120.     unset($index_cnt);
  121.     $index = preg_replace('@, $@', '', $index);
  122.     if (!empty($index)) {
  123.         $sql_query .= ', INDEX (' . $index . ')';
  124.         $query_cpy .= ',' . "\n" . '  INDEX (' . $index . ')';
  125.     }
  126.     unset($index);
  127.  
  128.     // Builds the uniques statements
  129.     $unique     = '';
  130.     $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
  131.     for ($i = 0; $i < $unique_cnt; $i++) {
  132.         $j = $field_unique[$i];
  133.         if (!empty($field_name[$j])) {
  134.            $unique .= PMA_backquote($field_name[$j]) . ', ';
  135.         }
  136.     } // end for
  137.     unset($unique_cnt);
  138.     $unique = preg_replace('@, $@', '', $unique);
  139.     if (!empty($unique)) {
  140.         $sql_query .= ', UNIQUE (' . $unique . ')';
  141.         $query_cpy .= ',' . "\n" . '  UNIQUE (' . $unique . ')';
  142.     }
  143.     unset($unique);
  144.  
  145.     // Builds the fulltextes statements
  146.     $fulltext     = '';
  147.     $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
  148.     for ($i = 0; $i < $fulltext_cnt; $i++) {
  149.         $j = $field_fulltext[$i];
  150.         if (!empty($field_name[$j])) {
  151.            $fulltext .= PMA_backquote($field_name[$j]) . ', ';
  152.         }
  153.     } // end for
  154.  
  155.     $fulltext = preg_replace('@, $@', '', $fulltext);
  156.     if (!empty($fulltext)) {
  157.         $sql_query .= ', FULLTEXT (' . $fulltext . ')';
  158.         $query_cpy .= ',' . "\n" . '  FULLTEXT (' . $fulltext . ')';
  159.     }
  160.     unset($fulltext);
  161.  
  162.     // Builds the 'create table' statement
  163.     $sql_query      = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $sql_query . ')';
  164.     $query_cpy      = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $query_cpy . "\n" . ')';
  165.  
  166.     // Adds table type, character set and comments
  167.     if (!empty($tbl_type) && ($tbl_type != 'Default')) {
  168.         $sql_query .= ' TYPE = ' . $tbl_type;
  169.         $query_cpy .= "\n" . 'TYPE = ' . $tbl_type;
  170.     }
  171.     if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_collation)) {
  172.         $sql_query .= PMA_generateCharsetQueryPart($tbl_collation);
  173.         $query_cpy .= "\n" . PMA_generateCharsetQueryPart($tbl_collation);
  174.     }
  175.  
  176.     if (!empty($comment)) {
  177.         $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
  178.         $query_cpy .= "\n" . 'COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
  179.     }
  180.  
  181.     // Executes the query
  182.     $error_create = FALSE;
  183.     $result    = PMA_DBI_try_query($sql_query) or $error_create = TRUE;
  184.  
  185.     if ($error_create == FALSE) {
  186.         $sql_query = $query_cpy . ';';
  187.         unset($query_cpy);
  188.         $message   = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenCreated;
  189.  
  190.         // garvin: If comments were sent, enable relation stuff
  191.         require_once('./libraries/relation.lib.php');
  192.         require_once('./libraries/transformations.lib.php');
  193.  
  194.         $cfgRelation = PMA_getRelationsParam();
  195.  
  196.         // garvin: Update comment table, if a comment was set.
  197.         if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
  198.             foreach ($field_comments AS $fieldindex => $fieldcomment) {
  199.                 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment);
  200.             }
  201.         }
  202.  
  203.         // garvin: Update comment table for mime types [MIME]
  204.         if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
  205.             foreach ($field_mimetype AS $fieldindex => $mimetype) {
  206.                 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
  207.             }
  208.         }
  209.  
  210.         require('./' . $cfg['DefaultTabTable']);
  211.         $abort = TRUE;
  212.         exit();
  213.     } else {
  214.         PMA_mysqlDie('', '', '', $err_url, FALSE);
  215.         // garvin: An error happened while inserting/updating a table definition.
  216.         // to prevent total loss of that data, we embed the form once again.
  217.         // The variable $regenerate will be used to restore data in tbl_properties.inc.php
  218.         $num_fields = $orig_num_fields;
  219.         $regenerate = TRUE;
  220.     }
  221. } // end do create table
  222.  
  223. /**
  224.  * Displays the form used to define the structure of the table
  225.  */
  226. if ($abort == FALSE) {
  227.     if (isset($num_fields)) {
  228.         $num_fields = intval($num_fields);
  229.     }
  230.     // No table name
  231.     if (!isset($table) || trim($table) == '') {
  232.         PMA_mysqlDie($strTableEmpty, '', '', $err_url);
  233.     }
  234.     // No valid number of fields
  235.     else if (empty($num_fields) || !is_int($num_fields)) {
  236.         PMA_mysqlDie($strFieldsEmpty, '', '', $err_url);
  237.     }
  238.     // Table name and number of fields are valid -> show the form
  239.     else {
  240.         $action = 'tbl_create.php';
  241.         require('./tbl_properties.inc.php');
  242.         // Diplays the footer
  243.         echo "\n";
  244.         require_once('./footer.inc.php');
  245.    }
  246. }
  247.  
  248. ?>
  249.