Tripal v1.0 (6.x-1.0)
custom_tables.php
Go to the documentation of this file.
00001 <?php
00002 
00019 function tripal_custom_table_view($table_id) {
00020 
00021   // get this custom_table details
00022   $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
00023   $custom_table = db_fetch_object(db_query($sql, $table_id));
00024 
00025   // create a table with each row containig stats for
00026   // an individual job in the results set.
00027   $return_url = url("admin/tripal/custom_tables/");
00028   $output .= "<p><a href=\"$return_url\">" . t("Return to list of custom tables") . "</a></p>";
00029   $output .= "<br />";
00030   $output .= "<p>Details for <b>$custom_table->table_name</b>:</p>";
00031   $output .= "<br />";
00032   $output .= "<table class=\"border-table\">";
00033   if ($custom_table->table_name) {
00034     $output .= "  <tr>".
00035     "    <th>Table Name</th>".
00036     "    <td>$custom_table->table_name</td>".
00037     "  </tr>";
00038   }
00039   if ($custom_table->schema) {
00040     $output .= "  <tr>".
00041     "    <th>Table Field Definitions</th>".
00042     "    <td><pre>" . var_export(unserialize($custom_table->schema),1) . "</pre></td>".
00043     "  </tr>";
00044   }
00045 
00046   // build the URLs using the url function so we can handle installations where
00047   // clean URLs are or are not used
00048   $delete_url = url("admin/tripal/custom_tables/action/delete/$custom_table->table_id");
00049   $edit_url = url("admin/tripal/custom_tables/edit/$custom_table->table_id");
00050   $output .= "<tr><th>Actions</th>".
00051             "<td>".
00052             "     <a href='$edit_url'>Edit</a>, ".
00053             "     <a href='$delete_url'>Delete</a></td></tr>";
00054   $output .= "</table>";
00055 
00056   return $output;
00057 }
00058 
00064 function tripal_custom_tables_list() {
00065   $header = array('', 'Table Name', 'Description');
00066   $rows = array();
00067   $custom_tables = db_query("SELECT * FROM {tripal_custom_tables} ORDER BY table_name");
00068 
00069   while ($custom_table = db_fetch_object($custom_tables)) {
00070  
00071     $rows[] = array(
00072       l(t('View'), "admin/tripal/custom_tables/view/$custom_table->table_id") ." | ".
00073       l(t('Edit'), "admin/tripal/custom_tables/edit/$custom_table->table_id") ." | ".
00074       $custom_table->table_name,
00075       $custom_table->comment,
00076       l(t('Delete'), "admin/tripal/custom_tables/action/delete/$custom_table->table_id"),
00077     );
00078   }
00079 
00080   $rows[] = array(
00081     'data' => array(
00082       array('data' => l(t('Create a new custom table.'), "admin/tripal/custom_tables/new"),
00083         'colspan' => 6),
00084     )
00085   );
00086 
00087   $page = theme('table', $header, $rows);
00088   return $page;
00089 }
00090 
00104 function tripal_custom_tables_form(&$form_state = NULL, $table_id = NULL) {
00105 
00106   if (!$table_id) {
00107     $action = 'Add';
00108   }
00109   else {
00110     $action = 'Edit';
00111   }
00112 
00113   // get this requested table
00114   if (strcmp($action, 'Edit')==0) {
00115     $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d ";
00116     $custom_table = db_fetch_object(db_query($sql, $table_id));
00117 
00118     // set the default values.  If there is a value set in the
00119     // form_state then let's use that, otherwise, we'll pull
00120     // the values from the database
00121     $default_schema = $form_state['values']['schema'];
00122     $default_force_drop = $form_state['values']['force_drop'];
00123 
00124     if (!$default_table_name) {
00125       $default_table = $custom_table->table_name;
00126     }
00127     if (!$default_schema) {
00128       $default_schema = var_export(unserialize($custom_table->schema),1);
00129       $default_schema = preg_replace('/=>\s+\n\s+array/','=> array', $default_schema);
00130     }
00131   }
00132 
00133   // Build the form
00134   $form['action'] = array(
00135     '#type' => 'value',
00136     '#value' => $action
00137   );
00138 
00139   $form['table_id'] = array(
00140     '#type' => 'value',
00141     '#value' => $table_id
00142   );
00143   
00144   $form['instructions']= array(
00145     '#type'          => 'markup',
00146     '#value'         => t('At times it is necessary to add a custom table to the Chado schema.  
00147        These are not offically sanctioned tables but may be necessary for local data requirements.  
00148        Avoid creating custom tables when possible as other GMOD tools may not recognize these tables
00149        nor the data in them.  Linker tables or property tables are often a good candidate for
00150        a custom table. For example a table to link stocks and libraries (e.g. library_stock) would be
00151        a good custom table. Try to model linker or propery tables after existing tables.  If the
00152        table already exists it will not be modified.  To force dropping and recreation of the table
00153        click the checkbox below.
00154     '),
00155   );
00156 
00157   $form['force_drop']= array(
00158     '#type'          => 'checkbox',
00159     '#title'         => t('Re-create table'),
00160     '#description'   => t('Check this box if your table already exists and you would like to drop it and recreate it.'),
00161     '#default_value' => $default_force_drop,
00162   );
00163   $form['schema']= array(
00164     '#type'          => 'textarea',
00165     '#title'         => t('Schema Array'),
00166     '#description'   => t('Please enter the Drupal Schema API compatible array that defines the table.'),
00167     '#required'      => FALSE,
00168     '#default_value' => $default_schema,
00169     '#rows'          => 25,
00170   );
00171 
00172   if ($action == 'Edit') {
00173     $value = 'Save';
00174   }
00175   if ($action == 'Add') {
00176     $value = 'Add';
00177   }
00178   $form['submit'] = array(
00179     '#type'         => 'submit',
00180     '#value'        => t($value),
00181     '#executes_submit_callback' => TRUE,
00182   );
00183   $form['#redirect'] = 'admin/tripal/custom_tables';
00184   
00185   $form['example']= array(
00186     '#type'          => 'markup',
00187     '#value'         => "<br>Example library_stock table: <pre>
00188 array (
00189   'table' => 'library_stock',
00190   'fields' => array (
00191     'library_stock_id' => array(
00192       'type' => serial,
00193       'not null' => TRUE,
00194     ),
00195     'library_id' => array(
00196       'type' => 'int',
00197       'not null' => TRUE,
00198     ),      
00199     'stock_id' => array(
00200       'type' => 'int',
00201       'not null' => TRUE,
00202     ),
00203   ),
00204   'primary key' => array(
00205     'library_stock_id'
00206   ),
00207   'unique keys' => array(
00208     'library_stock_c1' => array(
00209       'library_id',
00210       'stock_id'
00211     ),
00212   ),
00213   'foreign keys' => array(
00214     'library' => array(
00215       'table' => 'library',
00216       'columns' => array(
00217         'library_id' => 'library_id',
00218       ),
00219     ),
00220     'stock' => array(
00221       'table' => 'stock',
00222       'columns' => array(
00223         'stock_id' => 'stock_id',
00224       ),
00225     ),
00226   ),
00227 )
00228     </pre>",
00229   );
00230   
00231 
00232   return $form;
00233 }
00234 
00241 function tripal_custom_tables_form_validate($form, &$form_state) {
00242   $action = $form_state['values']['action'];
00243   $table_id = $form_state['values']['table_id'];
00244   $schema = $form_state['values']['schema'];
00245   $force_drop = $form_state['values']['force_drop'];
00246 
00247   if (!$schema) {
00248     form_set_error($form_state['values']['schema'],
00249       t('Schema array field is required.'));
00250   }
00251 
00252   // make sure the array is valid
00253   $schema_array = array();
00254   if ($schema) {
00255     $success = preg_match('/^\s*array/', $schema);
00256     if (!$success) {
00257       form_set_error($form_state['values']['schema'],
00258         t("The schema array should begin with the word 'array'."));
00259     }
00260     else {
00261       $success = eval("\$schema_array = $schema;");    
00262       if ($success === FALSE) {
00263         $error = error_get_last();
00264         form_set_error($form_state['values']['schema'],
00265           t("The schema array is improperly formatted. Parse Error : " . $error["message"]));
00266       }
00267       if (is_array($schema_array) and !array_key_exists('table', $schema_array)) {
00268         form_set_error($form_state['values']['schema'],
00269           t("The schema array must have key named 'table'"));
00270       }
00271       // check to see if the table name matches an existing table
00272       // if this is an add
00273       if ($action == 'Add') {
00274         $previous_db = tripal_db_set_active('chado');
00275         $exists = db_table_exists($schema_array['table']);
00276         tripal_db_set_active($previous_db);        
00277         if ($exists) {
00278           form_set_error($form_state['values']['schema'],
00279             t("The table name already exists, please choose a different name."));  
00280         }
00281       } 
00282       if ($action == 'Edit') {
00283         // see if the table name has changed. If so, then check to make sure
00284         // it doesn't already exists. We don't want to drop a table we didn't mean to
00285         $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
00286         $ct = db_fetch_object(db_query($sql, $table_id));
00287         if ($ct->table_name != $schema_array['table']) {
00288           $previous_db = tripal_db_set_active('chado');
00289           $exists = db_table_exists($schema_array['table']);
00290           tripal_db_set_active($previous_db);        
00291           if ($exists) {
00292             form_set_error($form_state['values']['schema'],
00293               t("The table name already exists, please choose a different name."));  
00294           }
00295         }
00296       }     
00297     }
00298   }
00299 }
00300 
00307 function tripal_custom_tables_form_submit($form, &$form_state) {
00308 
00309   $ret = array();
00310   $action = $form_state['values']['action'];
00311   $table_id = $form_state['values']['table_id'];
00312   $schema = $form_state['values']['schema'];
00313   $force_drop = $form_state['values']['force_drop'];
00314   
00315   $skip_creation = 1;
00316   if ($force_drop) {
00317      $skip_creation = 0;
00318   }
00319 
00320   // conver the schema into a PHP array
00321   $schema_arr = array();
00322   eval("\$schema_arr = $schema;");
00323   
00324 
00325   if (strcmp($action, 'Edit') == 0) {   
00326     tripal_core_edit_custom_table($table_id, $schema_arr['table'], $schema_arr, $skip_creation);
00327   }
00328   elseif (strcmp($action, 'Add') == 0) {
00329     tripal_core_create_custom_table($ret, $schema_arr['table'], $schema_arr, $skip_creation);
00330   }
00331   else {
00332     drupal_set_message(t("No action performed."));
00333   }
00334 
00335   return '';
00336 }
00349 function tripal_custom_tables_action($op, $table_id, $redirect = FALSE) {
00350   global $user;
00351 
00352   $args = array("$table_id");
00353   if (!$table_id) {
00354     return '';
00355   }
00356 
00357   // get this table details
00358   $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d";
00359   $custom_table = db_fetch_object(db_query($sql, $table_id));
00360 
00361   if ($op == 'delete') {
00362   
00363     // remove the entry from the tripal_custom tables table
00364     $sql = "DELETE FROM {tripal_custom_tables} ".
00365            "WHERE table_id = $table_id";
00366     db_query($sql);
00367     
00368     // drop the table from chado if it exists
00369     if (db_table_exists($custom_table->table_name)) {
00370       $success = chado_query("DROP TABLE %s", $custom_table->table_name);
00371       if($success){
00372         drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name)));
00373       }
00374     }
00375   }
00376 
00377   // Redirect the user
00378   if ($redirect) {
00379     drupal_goto("admin/tripal/custom_tables");
00380   }
00381 }
 All Classes Files Functions Variables