Tripal v1.0 (6.x-1.0)
|
00001 <?php 00002 00018 function tripal_bulk_loader_modify_template_base_form($form_state = NULL, $mode) { 00019 $form = array(); 00020 00021 // get template id from path and rebuild form 00022 if ($_GET['template_id']) { 00023 if (preg_match('/^\d+$/', $_GET['template_id'])) { 00024 $form_state['storage']['template_id'] = $_GET['template_id']; 00025 } 00026 00027 $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d"; 00028 $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id'])); 00029 $form_state['storage']['template'] = unserialize($result->template_array); 00030 $form_state['storage']['template_name'] = $result->name; 00031 00032 $form_state['storage']['record2priority'] = array(); 00033 foreach ($form_state['storage']['template'] as $priority => $record_array) { 00034 if (!is_array($record_array)) { 00035 continue; } 00036 $form_state['storage']['record2priority'][$record_array['record_id']] = $priority; 00037 } 00038 } 00039 00040 $form['mode'] = array( 00041 '#type' => 'hidden', 00042 '#value' => $mode, 00043 ); 00044 00045 if ($form_state['storage']['template_id']) { 00046 $form['template_name'] = array( 00047 '#type' => 'item', 00048 '#title' => 'Template', 00049 '#value' => $form_state['storage']['template_name'], 00050 '#weight' => 1, 00051 ); 00052 } 00053 else { 00054 if (preg_match('/create/', $mode)) { 00055 $form['new_template_name'] = array( 00056 '#type' => 'textfield', 00057 '#title' => 'Template Name', 00058 '#weight' => 1, 00059 ); 00060 } 00061 elseif (preg_match('/edit/', $mode)) { 00062 $sql = "SELECT * FROM {tripal_bulk_loader_template}"; 00063 $resource = db_query($sql); 00064 $templates = array(); 00065 $templates[''] = 'Select a Template'; 00066 while ($r = db_fetch_object($resource)) { 00067 $templates[$r->template_id] = $r->name; 00068 } 00069 00070 $form['template_id'] = array( 00071 '#title' => t('Template'), 00072 '#description' => t('Please select the template you would like to edit.'), 00073 '#type' => 'select', 00074 '#options' => $templates, 00075 '#default_value' => $form_state['storage']['template_id'], 00076 '#weight' => 0, 00077 '#required' => TRUE, 00078 '#weight' => 1, 00079 ); 00080 } 00081 } 00082 00083 $form['records'] = array( 00084 '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden', 00085 '#title' => t('Current Records'), 00086 '#collapsible' => TRUE, 00087 '#weight' => 2, 00088 ); 00089 00090 $form['records']['description'] = array( 00091 '#type' => 'item', 00092 '#value' => 'Records will be inserted into the chado database in the order listed below. To ' 00093 .'change this order: <ul><li>Drag the rows into the correct order <br/>(If you don\'t have javascript enabled then enter ' 00094 .'the numbers 1 and up in the Order textboxes to indicate the correct order).</li></ul>', 00095 ); 00096 00097 $form['records']['records-data'] = array( 00098 '#tree' => TRUE, 00099 ); 00100 00101 $form['records']['no_records'] = array( 00102 '#type' => 'hidden', 00103 '#value' => TRUE, 00104 ); 00105 00106 $form['records']['submit-new_record'] = array( 00107 '#type' => 'submit', 00108 '#value' => 'New Record/Field', 00109 ); 00110 00111 $form['records']['submit-reorder'] = array( 00112 '#type' => 'submit', 00113 '#value' => 'Save Order', 00114 ); 00115 00116 $form['fields'] = array( 00117 '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden', 00118 '#title' => t('Current Fields'), 00119 '#collapsible' => TRUE, 00120 '#weight' => 3, 00121 ); 00122 00123 $form['fields']['fields-data'] = array( 00124 '#tree' => TRUE, 00125 ); 00126 00127 if ($form_state['storage']['template']) { 00128 00129 // List Current Fields ------------------------------------------------------------- 00130 $i=1; 00131 foreach ($form_state['storage']['template'] as $priority => $table_array) { 00132 if (!is_array($table_array)) { 00133 continue; } 00134 00135 $form['records']['no_records']['#value'] = FALSE; 00136 00137 $mode_value = ''; 00138 if ($table_array['optional']) { 00139 $mode_value .= 'optional '; 00140 } 00141 00142 // for backwards compatibility we want to convert insert_unique to be 'insert' 00143 // and optional to 'insert' 00144 if (strcmp($table_array['mode'], 'insert_unique')==0) { 00145 $mode_value .= 'insert or select if duplicate'; 00146 } 00147 elseif (strcmp($table_array['mode'], 'optional')==0) { 00148 $mode_value .= 'optional insert'; 00149 } 00150 elseif (strcmp($table_array['mode'], 'insert_once')==0) { 00151 $mode_value .= 'insert once'; 00152 } 00153 elseif (strcmp($table_array['mode'], 'select_once')==0) { 00154 $mode_value .= 'select once'; 00155 } 00156 elseif ($table_array['mode']) { 00157 $mode_value .= $table_array['mode']; 00158 } 00159 else { 00160 $mode_value .= 'insert'; 00161 } 00162 00163 // add in the select if duplicate 00164 if ($table_array['select_if_duplicate']) { 00165 $mode_value .= ' or select if duplicate'; 00166 } 00167 if ($table_array['select_optional']) { 00168 $mode_value .= ' (no fail)'; 00169 } 00170 if ($table_array['update_if_duplicate']) { 00171 $mode_value .= ' or update if duplicate'; 00172 } 00173 00174 00175 // add in the disabled 00176 if ($table_array['disable']) { 00177 $mode_value .= '. <font color="Red">DISABLED</font>'; 00178 } 00179 00180 $form['records']['records-data'][$priority] = array( 00181 'title' => array( 00182 '#type' => 'markup', 00183 '#value' => filter_xss($priority . ". " . $table_array['record_id']), 00184 '#prefix' => "<a name=\"record_$priority\"></a>", 00185 '#suffix' => "<p><a href=\"#fields_$priority\"> View Fields </a></p>", 00186 ), 00187 'chado_table' => array( 00188 '#type' => 'markup', 00189 '#value' => filter_xss($table_array['table']), 00190 ), 00191 'mode' => array( 00192 '#type' => 'item', 00193 '#value' => $mode_value, 00194 ), 00195 'new_priority' => array( 00196 '#type' => 'select', 00197 '#options' => range(1, sizeof($form_state['storage']['template'])), 00198 '#default_value' => $priority, 00199 ), 00200 'old_priority' => array( 00201 '#type' => 'hidden', 00202 '#value' => $priority, 00203 ), 00204 'id' => array( 00205 '#type' => 'hidden', 00206 '#value' => $priority, 00207 ), 00208 'submit-edit_record' => array( 00209 '#type' => 'submit', 00210 '#name' => ($priority !== 0) ? (string)$priority : 'zero', 00211 '#value' => 'Edit Record', 00212 ), 00213 'submit-delete_record' => array( 00214 '#type' => 'submit', 00215 '#name' => ($priority !== 0) ? (string)$priority : 'zero', 00216 '#value' => 'Delete Record', 00217 ), 00218 'submit-add_field' => array( 00219 '#type' => 'submit', 00220 '#name' => ($priority !== 0) ? (string)$priority : 'zero', 00221 '#value' => 'Add Field', 00222 ), 00223 'submit-duplicate_record' => array( 00224 '#type' => 'submit', 00225 '#name' => ($priority !== 0) ? (string)$priority : 'zero', 00226 '#value' => 'Duplicate Record' 00227 ), 00228 ); 00229 00230 foreach ($table_array['fields'] as $field_index => $field) { 00231 $fk_value = ''; 00232 if ($field['foreign key']) { 00233 if ($field['foreign field']) { 00234 $fk_value = $field['foreign key'] . " (" . $field['foreign field'] . ")"; 00235 } 00236 else { 00237 // for backwards compatibility we need to get the FK relationship to find 00238 // out what field we're joining on. For templates created using a 00239 // previous version this information isn't stored in the template 00240 // so we need to get it. 00241 $fk_priority = $form_state['storage']['record2priority'][$field['foreign key']]; 00242 $fk_table = $form_state['storage']['template'][$fk_priority]['table']; 00243 $tbl_description = tripal_core_get_chado_table_schema($table_array['table']); 00244 foreach ($tbl_description['foreign keys'] as $key_table => $key_array) { 00245 foreach ($key_array['columns'] as $left_field => $right_field) { 00246 if ($key_table == $fk_table and $left_field == $field['field']) { 00247 $fk_value = $field['foreign key'] . " ($right_field)"; 00248 } 00249 } 00250 } 00251 } 00252 } 00253 00254 $form['fields']['fields-data'][$i] = array( 00255 'record_id' => array( 00256 '#type' => 'item', 00257 '#value' => $table_array['record_id'], 00258 '#prefix' => "<a name=\"fields_$priority\"></a>", 00259 '#suffix' => "<p><a href=\"#record_$priority\"> View Record </a></p>", 00260 ), 00261 'priority_hidden' => array( 00262 '#type' => 'hidden', 00263 '#value' => $priority, 00264 ), 00265 'field_name' => array( 00266 '#type' => 'item', 00267 '#value' => $field['title'], 00268 ), 00269 'chado_table_name' => array( 00270 '#type' => 'item', 00271 '#value' => $table_array['table'], 00272 ), 00273 'chado_table_hidden' => array( 00274 '#type' => 'hidden', 00275 '#value' => $table_array['table'], 00276 ), 00277 'chado_field_name' => array( 00278 '#type' => 'item', 00279 '#value' => $field['field'], 00280 ), 00281 'sheet_name' => array( 00282 '#type' => 'item', 00283 '#value' => $field['spreadsheet sheet'], 00284 ), 00285 'column_num' => array( 00286 '#type' => 'item', 00287 '#value' => $field['spreadsheet column'], 00288 ), 00289 'constant_value' => array( 00290 '#type' => 'item', 00291 '#value' => $field['constant value'], 00292 ), 00293 'field_index' => array( 00294 '#type' => 'hidden', 00295 '#value' => $field_index 00296 ), 00297 'foreign_record_id' => array( 00298 '#type' => 'item', 00299 '#value' => $fk_value, 00300 ), 00301 'edit_submit' => array( 00302 '#type' => 'submit', 00303 '#name' => (string)$i, 00304 '#value' => "Edit Field", 00305 ), 00306 'delete_submit' => array( 00307 '#type' => 'submit', 00308 '#name' => (string)$i, 00309 '#value' => "Delete Field", 00310 ), 00311 ); 00312 00313 $i++; 00314 } 00315 } 00316 $form['fields']['total_fields'] = array( 00317 '#type' => 'item', 00318 '#value' => $i, 00319 ); 00320 00321 } 00322 00323 if ($form['records']['no_records']['#value']) { 00324 $form['records']['description'] = array( 00325 '#type' => 'item', 00326 '#value' => 'There are currently no records.', 00327 ); 00328 unset($form['records']['submit-reorder']); 00329 00330 $form['fields']['description'] = array( 00331 '#type' => 'item', 00332 '#value' => 'There are currently no fields.', 00333 ); 00334 00335 } 00336 00337 $mode_title = (preg_match('/create/', $mode)) ? 'Create Template' : 'Edit Template'; 00338 $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title; 00339 $form['submit'] = array( 00340 '#type' => 'submit', 00341 '#value' => $value, 00342 '#weight' => 4, 00343 ); 00344 00345 return $form; 00346 } 00347 00351 function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state) { 00352 00353 $form_state['rebuild'] = TRUE; 00354 if ($form_state['storage']['template_id']) { 00355 $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d"; 00356 $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id'])); 00357 $form_state['storage']['template'] = unserialize($result->template_array); 00358 } 00359 00360 $op = $form_state['values'][ $form_state['clicked_button']['#name'] ]; 00361 00362 // part of fix for 1st button set not working on records list (edit record, duplicate record, add field) 00363 if ($form_state['clicked_button']['#name'] === 'zero') { 00364 $form_state['clicked_button']['#name'] = '0'; 00365 } 00366 00367 switch ($op) { 00368 // Initialize after template is chosen ---------------------------------------- 00369 case 'Edit Template': 00370 $form_state['storage']['template_id'] = $form_state['values']['template_id']; 00371 00372 $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d"; 00373 $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id'])); 00374 $form_state['storage']['template'] = unserialize($result->template_array); 00375 $form_state['storage']['template_name'] = $result->name; 00376 00377 $form_state['storage']['record2priority'] = array(); 00378 foreach ($form_state['storage']['template'] as $priority => $record_array) { 00379 if (!is_array($record_array)) { 00380 continue; 00381 } 00382 $form_state['storage']['record2priority'][$record_array['record_id']] = $priority; 00383 } 00384 break; 00385 00386 case 'Create Template': 00387 $record = array( 00388 'name' => $form_state['values']['new_template_name'], 00389 'template_array' => array(), 00390 ); 00391 drupal_write_record('tripal_bulk_loader_template', $record); 00392 $form_state['storage']['template_id'] = $record['template_id']; 00393 $form_state['storage']['template_name'] = $record['name']; 00394 $form_state['storage']['template'] = array(); 00395 break; 00396 00397 // Save Reordered Records ----------------------------------------------------- 00398 case 'Save Order': 00399 $new_template = $form_state['storage']['template']; 00400 // unset old elements 00401 $form_state['storage']['record2priority'] = array(); 00402 foreach ($new_template as $priority => $record_array) { 00403 if (preg_match('/\d+/', $priority)) { 00404 unset($new_template[$priority]); 00405 } 00406 } 00407 //set elements in new order 00408 foreach ($form_state['values']['records-data'] as $item) { 00409 $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']]; 00410 $record_name = $new_template[$item['new_priority']]['record_id']; 00411 $form_state['storage']['record2priority'][$record_name] = $item['new_priority']; 00412 } 00413 ksort($new_template); 00414 $form_state['storage']['template'] = $new_template; 00415 break; 00416 00417 case 'New Record/Field': 00418 $query = array( 00419 'template_id' => $form_state['storage']['template_id'], 00420 'record_id' => 'NEW', 00421 ); 00422 drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query); 00423 break; 00424 00425 case 'Edit Record': 00426 $query = array( 00427 'template_id' => $form_state['storage']['template_id'], 00428 'record_id' => $form_state['clicked_button']['#name'], 00429 ); 00430 drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_record', $query); 00431 break; 00432 00433 case 'Delete Record': 00434 $form_state['storage']['record2priority'] = array(); 00435 $new_template = tripal_bulk_loader_delete_record($form_state['clicked_button']['#name'], $form_state['storage']['template']); 00436 if (!empty($new_template)) { 00437 $form_state['storage']['template'] = $new_template; 00438 } 00439 break; 00440 00441 case 'Add Field': 00442 $query = array( 00443 'template_id' => $form_state['storage']['template_id'], 00444 'record_id' => $form_state['clicked_button']['#name'], 00445 ); 00446 drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query); 00447 break; 00448 00449 case 'Duplicate Record': 00450 // original record (one to be duplicated) 00451 $orig_priority = $form_state['clicked_button']['#name']; 00452 $record = $form_state['storage']['template'][ $orig_priority ]; 00453 00454 // new record 00455 $new_priority = sizeof($form_state['storage']['template']) + 1; 00456 $record['record_id'] = $record['record_id'] . '_' . date('YzHi'); 00457 $form_state['storage']['template'][ $new_priority ] = $record; 00458 break; 00459 00460 case 'Edit Field': 00461 $field_data_index = $form_state['clicked_button']['#name']; 00462 $query = array( 00463 'template_id' => $form_state['storage']['template_id'], 00464 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'], 00465 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'], 00466 ); 00467 drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query); 00468 break; 00469 00470 case 'Delete Field': 00471 // Delete Field 00472 $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']]; 00473 $priority = $field_data['priority_hidden']; 00474 $field_key = $field_data['field_index']; 00475 $new_template = tripal_bulk_loader_delete_field($priority, $field_key, $form_state['storage']['template']); 00476 if (!empty($new_template)) { 00477 $form_state['storage']['template'] = $new_template; 00478 } 00479 drupal_set_message(t('Deleted Field from Template.')); 00480 break; 00481 } //end of switch 00482 00483 // Save Template 00484 $record = array( 00485 'template_id' => $form_state['storage']['template_id'], 00486 'template_array' => serialize($form_state['storage']['template']) 00487 ); 00488 drupal_write_record('tripal_bulk_loader_template', $record, array('template_id')); 00489 drupal_set_message(t('Template Saved.')); 00490 00491 } 00492 00502 function tripal_bulk_loader_delete_template_base_form() { 00503 $form = array(); 00504 00505 $sql = "SELECT * FROM {tripal_bulk_loader_template}"; 00506 $resource = db_query($sql); 00507 $templates = array(); 00508 $templates[''] = 'Select a Template'; 00509 while ($r = db_fetch_object($resource)) { 00510 $templates[$r->template_id] = $r->name; 00511 } 00512 $form['template_name'] = array( 00513 '#title' => t('Template'), 00514 '#description' => t('Please select the template you would like to delete.'), 00515 '#type' => 'select', 00516 '#options' => $templates, 00517 '#weight' => 0, 00518 '#required' => TRUE, 00519 ); 00520 00521 $form['submit'] = array( 00522 '#type' => 'submit', 00523 '#value' => 'Delete Template', 00524 ); 00525 00526 return $form; 00527 } 00528 00537 function tripal_bulk_loader_delete_template_base_form_submit($form, &$form_state) { 00538 $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d"; 00539 db_query($sql, $form_state['values']['template_name']); 00540 } 00541 00563 function tripal_bulk_loader_import_export_template_form($form_state = NULL, $mode) { 00564 $form = array(); 00565 00566 $form['mode'] = array( 00567 '#type' => 'hidden', 00568 '#value' => $mode, 00569 ); 00570 00571 if (preg_match('/import/', $mode)) { 00572 $form['new_template_name'] = array( 00573 '#type' => 'textfield', 00574 '#title' => 'Template Name', 00575 '#weight' => 1, 00576 ); 00577 } 00578 elseif (preg_match('/export/', $mode)) { 00579 $sql = "SELECT * FROM {tripal_bulk_loader_template}"; 00580 $resource = db_query($sql); 00581 $templates = array(); 00582 $templates[''] = 'Select a Template'; 00583 while ($r = db_fetch_object($resource)) { 00584 $templates[$r->template_id] = $r->name; 00585 } 00586 00587 $form['template_id'] = array( 00588 '#title' => t('Template'), 00589 '#description' => t('Please select the template you would like to edit.'), 00590 '#type' => 'select', 00591 '#options' => $templates, 00592 '#default_value' => $form_state['storage']['template_id'], 00593 '#weight' => 0, 00594 '#required' => TRUE, 00595 ); 00596 } 00597 00598 $form['template_array'] = array( 00599 '#type' => 'textarea', 00600 '#title' => 'Template Array', 00601 '#default_value' => $form_state['storage']['template_array'], 00602 '#description' => t('Use this serialized array for import.'), 00603 '#rows' => 15, 00604 '#weight' => 5, 00605 00606 ); 00607 00608 $form['submit'] = array( 00609 '#type' => 'submit', 00610 '#value' => 'Submit', 00611 '#weight' => 10, 00612 ); 00613 00614 return $form; 00615 } 00616 00625 function tripal_bulk_loader_import_export_template_form_submit($form, &$form_state) { 00626 switch ($form_state['values']['mode']) { 00627 case 'export': 00628 $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id'])); 00629 //$form_state['storage']['template_array'] = $record->template_array; 00630 $t = var_export(unserialize($record->template_array), TRUE); 00631 $t = preg_replace("/\n\s+array/", "array", $t); // move array( to previous line 00632 $t = preg_replace("/true/", "TRUE", $t); // upper case true 00633 $t = preg_replace("/false/", "FALSE", $t); // upper case false 00634 $t = preg_replace("/array\(/", "array (", $t); // put a space between array and paren 00635 00636 $form_state['storage']['template_array'] = $t; 00637 $form_state['storage']['template_id'] = $form_state['values']['template_id']; 00638 break; 00639 case 'import': 00640 // get the template array, and convert from text into an array. 00641 $t = array(); 00642 eval("\$t = " . $form_state['values']['template_array'] . ";"); 00643 $record = array( 00644 'name' => $form_state['values']['new_template_name'], 00645 'template_array' => serialize($t), 00646 ); 00647 drupal_write_record('tripal_bulk_loader_template', $record); 00648 if ($record->template_id) { 00649 drupal_set_message(t('Successfully imported Tripal Bulk Loader Template.')); 00650 } 00651 break; 00652 } 00653 } 00654 00673 function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) { 00674 $form['#cache'] = TRUE; // Make sure the form is cached. 00675 00676 // get template id from path 00677 $template_id = ($_GET['template_id'] !== NULL) ? $_GET['template_id'] : $form_state['values']['template_id']; 00678 00679 // if there is no template supplied don't return rest of form 00680 if (!$template_id) { 00681 return $form; 00682 } 00683 00684 // Pre-process values/defaults --------------------------- 00685 00686 // If this is the first load of the form (no form state) we need to initialize some variables 00687 if (!$form_state['storage']['template']) { 00688 $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d"; 00689 $template = db_fetch_object(db_query($sql, $template_id)); 00690 $form_state['storage']['template_array'] = unserialize($template->template_array); 00691 $form_state['storage']['template'] = $template; 00692 00693 $form_state['storage']['record2priority'] = array(); 00694 foreach ($form_state['storage']['template_array'] as $priority => $record_array) { 00695 if (!is_array($record_array)) { 00696 continue; 00697 } 00698 $form_state['storage']['record2priority'][$record_array['record_id']] = $priority; 00699 } 00700 00701 $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"]; 00702 } 00703 else { 00704 $template = $form_state['storage']['template']; 00705 } 00706 00707 // get the record_id from the path 00708 if ($_GET['record_id'] !== NULL) { 00709 $form_state['values']['field_group'] = $_GET['record_id']; 00710 $form_state['storage']['original_priority'] = $_GET['record_id']; 00711 } 00712 00713 00714 // Tables and default table 00715 $tables = tripal_core_get_chado_tables(TRUE); 00716 if ($form_state['values']['chado_table']) { 00717 $table = $form_state['values']['chado_table']; 00718 } 00719 else { 00720 $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table']; 00721 } 00722 00723 // get the default mode 00724 $mode = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['mode']; 00725 if (!$mode) { 00726 $mode = 'insert'; 00727 } 00728 00729 // get default for the select optional 00730 $select_optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_optional']; 00731 if (!isset($select_optional)) { 00732 $select_optional = 0; 00733 } 00734 00735 // get default for the select if duplicate 00736 $select_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_if_duplicate']; 00737 if (!isset($select_if_duplicate)) { 00738 $select_if_duplicate = 1; 00739 } 00740 00741 // get default for the update if duplicate 00742 $update_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['update_if_duplicate']; 00743 if (!isset($update_if_duplicate)) { 00744 $update_if_duplicate = 0; 00745 } 00746 00747 // get default for the select if duplicate 00748 $optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['optional']; 00749 if (!isset($optional)) { 00750 $optional = 0; 00751 } 00752 00753 // get the default for disabling the record 00754 $disable = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['disable']; 00755 00756 // this is just for backwards compatibility. the insert_unique mode type is no longer available 00757 if (strcmp($mode, 'insert_unique')==0) { 00758 $mode = 'insert'; 00759 $select_if_duplicate = 1; 00760 } 00761 00762 // this is just for backwards compatibility. the insert_unique mode type is no longer available 00763 if (strcmp($mode, 'optional')==0) { 00764 $mode = 'insert'; 00765 $optional = 1; 00766 } 00767 00768 //dpm($form_state, 'form state'); 00769 00770 // Form Proper ------------------------------------------- 00771 $form['template_name'] = array( 00772 '#type' => 'item', 00773 '#title' => 'Template', 00774 '#value' => $template->name, 00775 ); 00776 00777 $form['template_id'] = array( 00778 '#type' => 'hidden', 00779 '#value' => $template_id, 00780 ); 00781 00782 $form['edit_record'] = array( 00783 '#type' => 'fieldset', 00784 ); 00785 00786 // check template array for records then add one more 00787 if (!$form_state['storage']['record2priority']) { 00788 $groups = array(); 00789 } 00790 else { 00791 $groups = array_flip($form_state['storage']['record2priority']); 00792 } 00793 $priority_default = $form_state['values']['field_group']; 00794 $form['edit_record']['field_group'] = array( 00795 '#type' => 'select', 00796 '#title' => 'Record', 00797 '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.', 00798 '#options' => $groups, 00799 '#default_value' => $priority_default, 00800 '#required' => TRUE, 00801 ); 00802 00803 $form['edit_record']['record_name'] = array( 00804 '#type' => 'textfield', 00805 '#title' => 'Unique Record Name', 00806 '#default_value' => $groups[$priority_default], 00807 ); 00808 00809 $form['edit_record']['chado_table'] = array( 00810 '#type' => 'select', 00811 '#title' => t('Chado Table'), 00812 '#description' => 'This changes the chado table for all fields in this record.', 00813 '#options' => $tables, 00814 '#default_value' => $table, 00815 ); 00816 00817 $form['edit_record']['mode'] = array( 00818 '#type' => 'radios', 00819 '#title' => 'Action to take when Loading Record', 00820 '#options' => array( 00821 'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record', 00822 'select_once' => 'SELECT ONCE: Select the record only once for each constant set.', 00823 'insert' => 'INSERT: Insert the record', 00824 'insert_once' => 'INSERT ONCE: Record will be inserted once for each constant set. If the record is the same across multiple constant sets, make sure to select "SELECT if duplicate" as well.', 00825 ), 00826 '#default_value' => $mode 00827 ); 00828 00829 $form['edit_record']['select_options'] = array( 00830 '#type' => 'markup', 00831 '#value' => t('Additional Select Options:'), 00832 ); 00833 $form['edit_record']['select_optional'] = array( 00834 '#type' => 'checkbox', 00835 '#title' => t('Continue if no record exists or too many exist.'), 00836 '#description' => t('By default if a select does not find a match the loader will fail, or if it finds too many matches it will fail. Check here to allow the loader to continue when no match is found. In either case no value is passed on.'), 00837 '#default_value' => $select_optional 00838 ); 00839 00840 $form['edit_record']['insert_options'] = array( 00841 '#type' => 'markup', 00842 '#prefix' => '<br>', 00843 '#value' => t('Additional Insert Options:'), 00844 ); 00845 00846 $form['edit_record']['select_if_duplicate'] = array( 00847 '#type' => 'checkbox', 00848 '#title' => t('SELECT if duplicate (no insert)'), 00849 '#description' => t('If this is not the first time this record has been added then perform a select rather than an insert.'), 00850 '#default_value' => $select_if_duplicate 00851 ); 00852 00853 $form['edit_record']['update_if_duplicate'] = array( 00854 '#type' => 'checkbox', 00855 '#title' => t('UPDATE if duplicate (no insert)'), 00856 '#description' => t('If this is not the first time this record has been added then perform an update rather than an insert.'), 00857 '#default_value' => $update_if_duplicate 00858 ); 00859 00860 $form['edit_record']['optional'] = array( 00861 '#type' => 'checkbox', 00862 '#title' => t('Optional'), 00863 '#description' => t('The insert, update or select will only be performed only if all required data are present'), 00864 '#default_value' => $optional 00865 ); 00866 $form['edit_record']['disable'] = array( 00867 '#type' => 'checkbox', 00868 '#title' => t('Disable this record'), 00869 '#description' => t("Check this box to ignore this record (not perform select or insert) during template loading. Uncheck to re-enable the record"), 00870 '#default_value' => $disable, 00871 ); 00872 00873 $form['edit_record']['submit-edit_record'] = array( 00874 '#type' => 'submit', 00875 '#value' => 'Edit Record' 00876 ); 00877 00878 $form['edit_record']['submit-cancel'] = array( 00879 '#type' => 'submit', 00880 '#value' => 'Cancel' 00881 ); 00882 00883 return $form; 00884 } 00885 00886 function tripal_bulk_loader_edit_template_record_form_validate($form, $form_state) { 00887 00888 // Don't worry about validation when Cancel button is clicked 00889 if ($form_state['clicked_button']['#value'] == 'Edit Record') { 00890 $is_unique = tripal_bulk_loader_is_record_name_unique( 00891 $form_state['values']['record_name'], 00892 $form_state['values']['template_id'], 00893 $form_state['storage']['template_array'], 00894 $form_state['values']['field_group'] 00895 ); 00896 if (!$is_unique) { 00897 form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique."); 00898 } 00899 } 00900 00901 } 00902 00911 function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) { 00912 //dpm($form_state, 'form state -start submit'); 00913 00914 if (!$form_state['ahah_submission']) { 00915 if ($form_state['values']['op'] == 'Edit Record') { 00916 00917 $template = $form_state['storage']['template_array']; 00918 $original_record_name = $template[ $form_state['storage']['original_priority'] ]['record_id']; 00919 00920 // Edit Record 00921 $record = $template[ $form_state['storage']['original_priority'] ]; 00922 $record['record_id'] = $form_state['values']['record_name']; 00923 $record['mode'] = $form_state['values']['mode']; 00924 $record['table'] = $form_state['values']['chado_table']; 00925 $record['select_if_duplicate'] = $form_state['values']['select_if_duplicate']; 00926 $record['update_if_duplicate'] = $form_state['values']['update_if_duplicate']; 00927 $record['select_optional'] = $form_state['values']['select_optional']; 00928 $record['disable'] = $form_state['values']['disable']; 00929 $record['optional'] = $form_state['values']['optional']; 00930 00931 if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) { 00932 $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']); 00933 $template[ $form_state['values']['field_group'] ] = $record; 00934 unset($template[ $form_state['storage']['original_priority'] ]); 00935 } 00936 else { 00937 $template[ $form_state['storage']['original_priority'] ] = $record; 00938 } 00939 00940 // Go through all records and update this record name where it was used for a foreign key 00941 // Foreach field in each record that is of type: foreign key 00942 foreach ($template as $priority => $record) { 00943 foreach ($record['fields'] as $field_index => $field) { 00944 if ($field['type'] === 'foreign key') { 00945 // Check if this points to the old record name and if so, update it 00946 if ($field['foreign key'] === $original_record_name) { 00947 $template[$priority]['fields'][$field_index]['foreign key'] = $form_state['values']['record_name']; 00948 } 00949 } 00950 } 00951 } 00952 00953 // Save Template 00954 $form_state['storage']['template']->template_array = serialize($template); 00955 $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id')); 00956 00957 if ($success) { 00958 drupal_set_message(t('Successfully Updated Template Record')); 00959 drupal_set_message(t('Template Saved.')); 00960 00961 $path = explode('?', $form_state['storage']['referring URL']); 00962 parse_str($path[1], $query); 00963 $query['template_id'] = $form_state['storage']['template']->template_id; 00964 drupal_goto($path[0], $query); 00965 } 00966 else { 00967 drupal_set_message(t('Unable to Save Template!'), 'error'); 00968 watchdog('T_bulk_loader', 00969 'Unable to save bulk loader template: %template', 00970 array('%template' => print_r($form_state['storage']['template'], TRUE)), 00971 WATCHDOG_ERROR 00972 ); 00973 } 00974 } 00975 elseif ($form_state['values']['op'] == 'Cancel') { 00976 $path = explode('?', $form_state['storage']['referring URL']); 00977 parse_str($path[1], $query); 00978 $query['template_id'] = $form_state['storage']['template']->template_id; 00979 //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']); 00980 drupal_goto($path[0], $query); 00981 } 00982 } 00983 00984 } 00985 01004 function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) { 01005 $form = array(); 01006 $form['#cache'] = TRUE; // Make sure the form is cached. 01007 01008 // get template id from path 01009 $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id']; 01010 01011 // if there is no template supplied don't return rest of form 01012 if (!$template_id) { 01013 return $form; 01014 } 01015 01016 // Pre-set Variables needed for form proper------------------------------------------ 01017 01018 // If this is the first load of the form (no form state) we need to initialize some variables 01019 if (!$form_state['storage']['template']) { 01020 $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d"; 01021 $template = db_fetch_object(db_query($sql, $template_id)); 01022 $form_state['storage']['template_array'] = unserialize($template->template_array); 01023 $form_state['storage']['template'] = $template; 01024 01025 $form_state['storage']['record2priority'] = array(); 01026 foreach ($form_state['storage']['template_array'] as $priority => $record_array) { 01027 if (!is_array($record_array)) { 01028 continue; 01029 } 01030 $form_state['storage']['record2priority'][$record_array['record_id']] = $priority; 01031 } 01032 01033 $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"]; 01034 } 01035 else { 01036 $template = $form_state['storage']['template']; 01037 } 01038 01039 $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field'; 01040 01041 // Tables and default table 01042 $tables = tripal_core_get_chado_tables(TRUE); 01043 if ($form_state['values']) { 01044 if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) { 01045 $table = $form_state['values']['chado_table']; 01046 } 01047 elseif ($form_state['values']['record_name']) { 01048 $record_name = $form_state['values']['record_name']; 01049 $priority = $form_state['storage']['record2priority'][$record_name]; 01050 $table = $form_state['storage']['template_array'][$priority]['table']; 01051 } 01052 else { 01053 $priority = $form_state['values']['field_group']; 01054 $table = $form_state['storage']['template_array'][$priority]['table']; 01055 } 01056 } 01057 if (!$table) { 01058 $table = reset($tables); 01059 } 01060 01061 // get the record_id from the path 01062 if ($_GET['record_id'] !== NULL) { 01063 $form_state['values']['field_group'] = $_GET['record_id']; 01064 if (preg_match('/\d+/', $_GET['record_id'])) { 01065 $priority = $form_state['values']['field_group']; 01066 $table = $form_state['storage']['template_array'][$priority]['table']; 01067 } 01068 } 01069 01070 $show_all = $form_state['values']['show_all_records']; 01071 01072 $table_description = tripal_core_get_chado_table_schema($table); 01073 01074 // Fields and foreign key / referral mappings 01075 // build the fields array 01076 $chado_fields = array(); 01077 foreach ($table_description['fields'] as $field_name => $field_array) { 01078 $chado_fields[$field_name] = $field_name; 01079 } 01080 01081 $fk_options = array(); 01082 $fk_options['NULL'] = 'None'; 01083 if ($field_type == 'foreign key' and !$show_all) { 01084 01085 $foreign_field2table = array(); 01086 foreach ($table_description['foreign keys'] as $key_table => $key_array) { 01087 foreach ($key_array['columns'] as $left_field => $right_field) { 01088 //$chado_fields[$left_field] = $left_field; 01089 $foreign_field2table[$left_field] = $key_table; 01090 } 01091 } 01092 // reset($chado_fields); 01093 01094 // set default field 01095 if (empty($chado_fields)) { 01096 $field = NULL; 01097 } 01098 elseif ($chado_fields[$form_state['values']['chado_field']]) { 01099 $field = $form_state['values']['chado_field']; 01100 } 01101 else { 01102 $field = current($chado_fields); 01103 } 01104 01105 // Foreign key options 01106 $foreign_table = $foreign_field2table[$field]; 01107 if ($foreign_table) { 01108 foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) { 01109 if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) { 01110 $fk_options[$record_name] = $record_name; 01111 } 01112 } 01113 } 01114 } 01115 // the user wants to see all of the records 01116 elseif ($field_type == 'foreign key' and $show_all) { 01117 foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) { 01118 $fk_options[$record_name] = $record_name; 01119 } 01120 } 01121 01122 // build the list of referrer table fields. This list is used 01123 // for the referring table field drop down. It has two groups 01124 // one for foreign keys and another for additional fields. 01125 $ref_chado_fields = array(); 01126 if ($field_type == 'foreign key') { 01127 $fk_rec = $form_state['values']['foreign_record']; 01128 if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') { 01129 01130 // first add in the foreign key field in the referring table 01131 // that corresponds to this field (if one exists). 01132 $fk_priority = $form_state['storage']['record2priority'][$fk_rec]; 01133 $fk_table = $form_state['storage']['template_array'][$fk_priority]['table']; 01134 foreach ($table_description['foreign keys'] as $key_table => $key_array) { 01135 foreach ($key_array['columns'] as $left_field => $right_field) { 01136 if ($key_table == $fk_table and $left_field == $field) { 01137 $ref_chado_fields['Foreign Key'][$right_field] = $right_field; 01138 } 01139 } 01140 } 01141 // now add in the remaining fields of the referring record's table. 01142 $fk_description = tripal_core_get_chado_table_schema($fk_table); 01143 foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) { 01144 // don't include the FK field it's included above 01145 if (in_array('Foreign Key', $ref_chado_fields)) { 01146 if (in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) { 01147 continue; 01148 } 01149 } 01150 $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name; 01151 } 01152 } 01153 } 01154 01155 // Ensure other field type fields are reset/cleared except for those of the current type 01156 // ie: if the type is changed from spreadsheet column to constant, the spreadsheet column 01157 // fields should be cleared since they don't apply to a constant 01158 switch ($field_type) { 01159 case 'table field': 01160 // clear constant 01161 $form_state['values']['constant_value'] = NULL; 01162 // clear foreign key 01163 // clears by default :) 01164 break; 01165 case 'constant': 01166 // clear spreadsheet column 01167 $form_state['values']['column_number'] = NULL; 01168 $form_state['values']['column_exposed'] = NULL; 01169 $template_field['exposed'] = NULL; 01170 $form_state['values']['column_exposed_desc'] = NULL; 01171 $template_field['exposed_description'] = NULL; 01172 // clear foreign key 01173 // clears by default :) 01174 break; 01175 case 'foreign key': 01176 // clear constant 01177 $form_state['values']['constant_value'] = NULL; 01178 // clear spreadsheet column 01179 $form_state['values']['column_number'] = NULL; 01180 $form_state['values']['column_exposed'] = NULL; 01181 $template_field['exposed'] = NULL; 01182 $form_state['values']['column_exposed_desc'] = NULL; 01183 $template_field['exposed_description'] = NULL; 01184 break; 01185 } 01186 01187 // Start of Form Proper------------------------------------------------------------- 01188 01189 $form['template_name'] = array( 01190 '#type' => 'item', 01191 '#title' => 'Template', 01192 '#value' => $template->name, 01193 ); 01194 01195 $form['template_id'] = array( 01196 '#type' => 'hidden', 01197 '#value' => $template_id, 01198 ); 01199 01200 $form['add_fields'] = array( 01201 '#type' => 'fieldset', 01202 '#prefix' => '<div id="tripal_bulk_loader_template-add_field">', 01203 '#suffix' => '</div>', 01204 ); 01205 01206 $form['add_fields']['field_type'] = array( 01207 '#type' => 'radios', 01208 '#title' => t('Type of Field'), 01209 '#options' => array( 01210 'table field' => t('Data: A Field which maps to a column in the supplied file.'), 01211 'constant' => t('Constant: Field which remains Constant throughout the file'), 01212 'foreign key' => t('Record Referral: Fields which map to a record in another table'), 01213 ), 01214 '#required' => TRUE, 01215 '#default_value' => $field_type, 01216 '#ahah' => array( 01217 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah', 01218 'wrapper' => 'tripal_bulk_loader_template-add_field', 01219 'effect' => 'fade' 01220 ), 01221 ); 01222 01223 // check template array for records then add one more 01224 if (!$form_state['storage']['record2priority']) { 01225 $groups = array(); 01226 } 01227 else { 01228 $groups = array_flip($form_state['storage']['record2priority']); 01229 } 01230 $groups['NONE'] = 'Select a Record'; 01231 $groups['NEW'] = 'New Record'; 01232 $form['add_fields']['field_group'] = array( 01233 '#type' => 'select', 01234 '#title' => 'Record', 01235 '#description' => 'This is used to group a set of fields together allowing ' 01236 .'multiple records to be inserted into the same table per line of the file', 01237 '#options' => $groups, 01238 '#default_value' => (preg_match('/\w+.*/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE', 01239 '#ahah' => array( 01240 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah', 01241 'wrapper' => 'tripal_bulk_loader_template-add_field', 01242 'effect' => 'fade' 01243 ), 01244 '#required' => TRUE, 01245 ); 01246 01247 $form['add_fields']['record_name'] = array( 01248 '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden', 01249 '#title' => 'Unique Record Name', 01250 '#prefix' => '<div id="tripal_bulk_loader_template-add_record">', 01251 '#suffix' => '</div>', 01252 '#default_value' => $form_state['values']['record_name'], 01253 ); 01254 01255 $form['add_fields']['field_title'] = array( 01256 '#type' => 'textfield', 01257 '#title' => t('Human-readable Title for Field'), 01258 '#default_value' => $form_state['values']['field_title'], 01259 ); 01260 01261 01262 // Chado Field 01263 $form['add_fields']['chado'] = array( 01264 '#type' => 'fieldset', 01265 '#title' => t('Chado Field/Column Details'), 01266 '#description' => t('Specify the Table/Field in chado that this field maps to.'), 01267 ); 01268 01269 $form['add_fields']['chado']['chado_table'] = array( 01270 '#type' => 'select', 01271 '#title' => t('Chado Table'), 01272 '#options' => $tables, 01273 '#default_value' => $table, 01274 '#ahah' => array( 01275 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah', 01276 'wrapper' => 'tripal_bulk_loader_template-add_field', 01277 'effect' => 'fade' 01278 ), 01279 ); 01280 01281 $form['add_fields']['chado']['chado_field'] = array( 01282 '#type' => 'select', 01283 '#title' => t('Chado Field/Column'), 01284 '#options' => $chado_fields, 01285 '#default_value' => $form_state['values']['chado_field'], 01286 '#ahah' => array( 01287 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah', 01288 'wrapper' => 'tripal_bulk_loader_template-add_field', 01289 'effect' => 'fade' 01290 ), 01291 ); 01292 01293 // loading file data column 01294 $form['add_fields']['columns'] = array( 01295 '#type' => 'fieldset', 01296 '#title' => t('Data File Column'), 01297 '#collapsible' => TRUE, 01298 '#collapsed' => ($field_type == 'table field')? FALSE : TRUE, 01299 ); 01300 01311 $form['add_fields']['columns']['column_number'] = array( 01312 '#type' => 'textfield', 01313 '#title' => t('Column'), 01314 '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'), 01315 '#size' => 5, 01316 '#default_value' => $form_state['values']['column_number'], 01317 ); 01318 01319 $form['add_fields']['columns']['column_exposed'] = array( 01320 '#type' => 'checkbox', 01321 '#title' => t('Allow Column to be set for each Bulk Loading Job'), 01322 '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'), 01323 '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'], 01324 ); 01325 01326 $form['add_fields']['columns']['column_exposed_desc'] = array( 01327 '#type' => 'textfield', 01328 '#title' => t('Description for exposed field on bulk loading job'), 01329 '#description' => t('This description should tell the user what column should be entered here.'), 01330 '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'], 01331 ); 01332 01333 // Global Value 01334 $form['add_fields']['constant'] = array( 01335 '#type' => 'fieldset', 01336 '#title' => t('Constant'), 01337 '#collapsible' => TRUE, 01338 '#collapsed' => ($field_type == 'constant')? FALSE : TRUE, 01339 ); 01340 01341 $form['add_fields']['constant']['constant_value'] = array( 01342 '#type' => 'textfield', 01343 '#title' => t('Constant Value'), 01344 '#description' => t('Specify the value you wish this field to have regardless of data file value.'), 01345 '#default_value' => $form_state['values']['constant_value'] 01346 ); 01347 01348 $form['add_fields']['constant']['constant_exposed'] = array( 01349 '#type' => 'checkbox', 01350 '#title' => t('Allow Constant to be set for each Bulk Loading Job'), 01351 '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.') 01352 ); 01353 01354 $form['add_fields']['constant']['constant_validate'] = array( 01355 '#type' => 'checkbox', 01356 '#title' => t('Ensure value is in table'), 01357 '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'), 01358 ); 01359 01360 // Foreign Key / Referrer 01361 $form['add_fields']['foreign_key'] = array( 01362 '#type' => 'fieldset', 01363 '#title' => 'Record Referral', 01364 '#collapsible' => TRUE, 01365 '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE, 01366 ); 01367 01368 $form['add_fields']['foreign_key']['show_all_records'] = array( 01369 '#type' => 'checkbox', 01370 '#title' => 'Refer to any record', 01371 '#description' => t('By default, the bulk loader will only allow referral to records in a foreign key relationship. To allow referral to any previous record, check this box'), 01372 '#default_value' => $show_all, 01373 '#ahah' => array( 01374 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah', 01375 'wrapper' => 'tripal_bulk_loader_template-add_field', 01376 'effect' => 'fade' 01377 ), 01378 ); 01379 01380 $form['add_fields']['foreign_key']['foreign_record'] = array( 01381 '#type' => 'select', 01382 '#title' => 'Record to refer to', 01383 '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.', 01384 '#options' => $fk_options, 01385 '#ahah' => array( 01386 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah', 01387 'wrapper' => 'tripal_bulk_loader_template-add_field', 01388 'effect' => 'fade' 01389 ), 01390 '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'], 01391 ); 01392 01393 $form['add_fields']['foreign_key']['foreign_field'] = array( 01394 '#type' => 'select', 01395 '#title' => 'Field to refer to', 01396 '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.', 01397 '#options' => $ref_chado_fields, 01398 '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign_field'], 01399 ); 01400 01401 01402 $form['add_fields']['additional'] = array( 01403 '#type' => 'fieldset', 01404 '#title' => 'Additional Options', 01405 ); 01406 01407 $form['add_fields']['additional']['required'] = array( 01408 '#type' => 'checkbox', 01409 '#title' => 'Make this field required', 01410 '#default_value' => (!empty($form_state['values']['required'])) ? $form_state['values']['required'] : $template_field['required'], 01411 ); 01412 01413 $form['add_fields']['additional']['regex_transform'] = array( 01414 '#type' => 'fieldset', 01415 '#title' => 'Transform Data File Value Rules', 01416 '#collapsible' => TRUE, 01417 '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE, 01418 ); 01419 01420 $form['add_fields']['additional']['regex_transform']['regex_description'] = array( 01421 '#type' => 'item', 01422 '#value' => 'A transformation rule allows you to transform the original value ' 01423 .'(usually from a user submitted data file) into the form you would like it stored ' 01424 .'in the chado database. Each rule consists of a match pattern (a php regular expression ' 01425 .'which determines which replacement patterns are applied and captures regions of the ' 01426 .'original value) and a replacement pattern (a string which may contain capture references ' 01427 .'that describes what the new value should be). Each rule is applied to the result of the ' 01428 .'previous rule.' 01429 ); 01430 01431 $form['add_fields']['additional']['regex_transform']['regex-data'] = array( 01432 '#tree' => TRUE, 01433 ); 01434 if (!$form_state['storage']['regex']['pattern']) { 01435 $form_state['storage']['regex']['pattern'] = array(); 01436 } 01437 foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) { 01438 $data_element = array( 01439 'pattern' => array( 01440 '#type' => 'item', 01441 '#value' => $pattern, 01442 ), 01443 'replace' => array( 01444 '#type' => 'item', 01445 '#value' => $form_state['storage']['regex']['replace'][$index], 01446 ), 01447 'old_index' => array( 01448 '#type' => 'hidden', 01449 '#value' => $index, 01450 ), 01451 'new_index' => array( 01452 '#type' => 'select', 01453 '#options' => range(0, sizeof($form_state['storage']['regex']['pattern'])-1), 01454 '#default_value' => $index, 01455 ), 01456 'id' => array( 01457 '#type' => 'hidden', 01458 '#value' => $index, 01459 ), 01460 'submit-delete' => array( 01461 '#type' => 'submit', 01462 '#value' => 'Delete Transformation', 01463 '#name' => $index, 01464 ), 01465 ); 01466 $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element; 01467 } 01468 01469 $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array( 01470 '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden', 01471 '#value' => 'Save Transformation Rule Order' 01472 ); 01473 01474 $form['add_fields']['additional']['regex_transform']['new_regex'] = array( 01475 '#type' => 'fieldset', 01476 '#title' => 'Add a new Transformation Rule', 01477 ); 01478 01479 $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array( 01480 '#type' => 'textfield', 01481 '#title' => 'Match Pattern', 01482 '#description' => 'You can use standard php regular expressions in this field to specify a ' 01483 .'pattern. Only if this pattern matches the value in the data file does the replacement ' 01484 .'pattern get applied to the value. To capture a section of your value for use in the ' 01485 .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match ' 01486 .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. ' 01487 .' To match and capture any value use <i>.*</i>', 01488 ); 01489 01490 $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array( 01491 '#type' => 'textfield', 01492 '#title' => 'Replacement Pattern', 01493 '#description' => 'This pattern should contain the text you want to replace the match pattern ' 01494 .'mentioned above. It can include references of the form \n where n is the number of the ' 01495 .'capture in the match pattern. For example, \1 will be replaced with the text matched in your ' 01496 .'first set of round brackets.', 01497 ); 01498 01499 if ($field_type == 'table field') { 01500 $tab = '        '; 01501 $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>' 01502 .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. ' 01503 .'This allows you to substitute other data file values into the current field. For example, ' 01504 .'if you had the following line:<br />' 01505 . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355' 01506 .'<br /> and your current field is for column #1 and you\'re inserting into the chado field ' 01507 .'feature.uniquename then you might want to add in the data to ensure your uniquename is ' 01508 .'unique. The Match Pattern is (.*) to select all the first column and the Replacement ' 01509 .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>'; 01510 } 01511 01512 $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array( 01513 '#type' => 'submit', 01514 '#value' => 'Add Transformation', 01515 ); 01516 01517 $form['add_fields']['additional']['regex_transform']['test_regex'] = array( 01518 '#type' => 'fieldset', 01519 '#title' => 'Test Transformation Rules', 01520 ); 01521 01522 $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array( 01523 '#type' => 'textfield', 01524 '#title' => 'Test Value', 01525 '#description' => 'This should be a value that you expect the above transformation rules ' 01526 .'to be applied to.', 01527 '#default_value' => $form_state['storage']['test_regex_test'], 01528 ); 01529 01530 $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array( 01531 '#type' => 'textfield', 01532 '#title' => 'Test Result', 01533 '#description' => 'This is the value that would be saved to the database after the above transformation ' 01534 .'riles were applied to the Test Value.', 01535 '#default_value' => $form_state['storage']['test_regex_result'], 01536 ); 01537 01538 $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array( 01539 '#type' => 'submit', 01540 '#value' => 'Test Transformation Rules' 01541 ); 01542 01543 $form['add_fields']['submit-add_field'] = array( 01544 '#type' => 'submit', 01545 '#value' => 'Save Changes' 01546 ); 01547 01548 $form['add_fields']['submit-cancel'] = array( 01549 '#type' => 'submit', 01550 '#value' => 'Cancel' 01551 ); 01552 01553 return $form; 01554 } 01555 01556 function tripal_bulk_loader_add_template_field_form_validate($form, $form_state) { 01557 01558 // Don't worry about validation when Cancel button is clicked 01559 if ($form_state['clicked_button']['#value'] == 'Save Changes') { 01560 $is_unique = tripal_bulk_loader_is_record_name_unique( 01561 $form_state['values']['record_name'], 01562 $form_state['values']['template_id'], 01563 $form_state['storage']['template_array'] 01564 ); 01565 $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE; 01566 if ((!$is_unique) AND $new_record) { 01567 form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique."); 01568 } 01569 } 01570 01571 } 01572 01581 function tripal_bulk_loader_add_template_field_form_submit($form, &$form_state) { 01582 01583 $op = $form_state['values'][ $form_state['clicked_button']['#name'] ]; 01584 01585 if (!$form_state['ahah_submission']) { 01586 if ($op == 'Save Changes') { 01587 01588 $template = $form_state['storage']['template_array']; 01589 01590 // If new record 01591 if (preg_match('/NEW/', $form_state['values']['field_group'])) { 01592 $record_name = $form_state['values']['record_name']; 01593 $priority = sizeof($form_state['storage']['template_array']) + 1; 01594 $record2priority[$record_name] = $priority; 01595 $template[$priority]['table'] = $form_state['values']['chado_table']; 01596 $template[$priority]['record_id'] = $record_name; 01597 01598 } 01599 else { 01600 $priority = $form_state['values']['field_group']; 01601 $record_name = $record2priority[$priority]; 01602 } 01603 01604 // Add field to template array 01605 if ($form_state['values']['field_type'] == 'table field') { 01606 $field = array( 01607 'type' => 'table field', 01608 'title' => $form_state['values']['field_title'], 01609 'field' => $form_state['values']['chado_field'], 01610 'required' => $form_state['values']['required'], 01611 //'allowed values' => empty by default, 01612 'spreadsheet column' => $form_state['values']['column_number'], 01613 'exposed' => $form_state['values']['column_exposed'], 01614 'exposed_description' => $form_state['values']['column_exposed_desc'], 01615 ); 01616 } 01617 elseif ($form_state['values']['field_type'] == 'constant') { 01618 $field = array( 01619 'type' => 'constant', 01620 'title' => $form_state['values']['field_title'], 01621 'field' => $form_state['values']['chado_field'], 01622 'required' => $form_state['values']['required'], 01623 //'allowed values' => empty by default, 01624 'constant value' => $form_state['values']['constant_value'], 01625 'exposed' => $form_state['values']['constant_exposed'], 01626 'exposed_validate' => $form_state['values']['constant_validate'], 01627 ); 01628 } 01629 elseif ($form_state['values']['field_type'] == 'foreign key') { 01630 $field = array( 01631 'type' => 'foreign key', 01632 'title' => $form_state['values']['field_title'], 01633 'field' => $form_state['values']['chado_field'], 01634 'show_all_records' => $form_state['values']['show_all_records'], 01635 'foreign key' => $form_state['values']['foreign_record'], 01636 'foreign field' => $form_state['values']['foreign_field'], 01637 'required' => $form_state['values']['required'], 01638 ); 01639 } 01640 01641 // Deal with any additional options 01642 if ($form_state['storage']['regex']) { 01643 $field['regex'] = $form_state['storage']['regex']; 01644 } 01645 01646 // Save Template 01647 $template[$priority]['fields'][] = $field; 01648 $form_state['storage']['template']->template_array = serialize($template); 01649 $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id')); 01650 01651 if ($success) { 01652 drupal_set_message(t('Successfully Added Field to Template')); 01653 drupal_set_message(t('Template Saved.')); 01654 01655 $path = explode('?', $form_state['storage']['referring URL']); 01656 parse_str($path[1], $query); 01657 $query['template_id'] = $form_state['storage']['template']->template_id; 01658 drupal_goto($path[0], $query); 01659 } 01660 else { 01661 drupal_set_message(t('Unable to Save Template!'), 'error'); 01662 watchdog('T_bulk_loader', 01663 'Unable to save bulk loader template: %template', 01664 array('%template' => print_r($form_state['storage']['template'], TRUE)), 01665 WATCHDOG_ERROR 01666 ); 01667 } 01668 } 01669 elseif ($op == 'Cancel') { 01670 $path = explode('?', $form_state['storage']['referring URL']); 01671 parse_str($path[1], $query); 01672 $query['template_id'] = $form_state['storage']['template']->template_id; 01673 drupal_goto($path[0], $query); 01674 } 01675 elseif ($op == 'Add Transformation') { 01676 01677 // Add transformation rule to original field 01678 $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/'; 01679 $form_state['storage']['regex']['replace'][] = $form_state['values']['replace']; 01680 drupal_set_message(t('Successfully Added Transformation Rule')); 01681 01682 } 01683 elseif ($op == 'Save Transformation Rule Order') { 01684 01685 // Generate new regex array 01686 $new_regex = array(); 01687 $old_regex = $form_state['storage']['regex']; 01688 foreach ($form_state['values']['regex-data'] as $key => $element) { 01689 $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ]; 01690 $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ]; 01691 } 01692 01693 // sort new regex arrays 01694 ksort($new_regex['pattern']); 01695 ksort($new_regex['replace']); 01696 01697 $form_state['storage']['regex'] = $new_regex; 01698 } 01699 elseif ($op == 'Delete Transformation') { 01700 01701 // Unset regex rule 01702 $index = $form_state['clicked_button']['#name']; 01703 unset($form_state['storage']['regex']['pattern'][$index]); 01704 unset($form_state['storage']['regex']['replace'][$index]); 01705 01706 } 01707 elseif ($op == 'Test Transformation Rules') { 01708 01709 $patterns = $form_state['storage']['regex']['pattern']; 01710 $replaces = $form_state['storage']['regex']['replace']; 01711 $test_string = $form_state['values']['test_string']; 01712 $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string); 01713 $form_state['storage']['test_regex_test'] = $test_string; 01714 } 01715 } 01716 01717 } 01718 01735 function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) { 01736 $form = array(); 01737 $form['#cache'] = TRUE; // Make sure the form is cached. 01738 01739 // get template id from path 01740 $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id']; 01741 01742 // if there is no template supplied don't return rest of form 01743 if (!$template_id) { 01744 return $form; 01745 } 01746 01747 // Pre-set Variables needed for form proper------------------------------------------ 01748 01749 // If this is the first load of the form (no form state) we need to initialize some variables 01750 if (!$form_state['storage']['template']) { 01751 $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d"; 01752 $template = db_fetch_object(db_query($sql, $template_id)); 01753 $form_state['storage']['template_array'] = unserialize($template->template_array); 01754 $form_state['storage']['template'] = $template; 01755 01756 $form_state['storage']['record2priority'] = array(); 01757 foreach ($form_state['storage']['template_array'] as $priority => $record_array) { 01758 if (!is_array($record_array)) { 01759 continue; 01760 } 01761 $form_state['storage']['record2priority'][$record_array['record_id']] = $priority; 01762 } 01763 01764 $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"]; 01765 } 01766 else { 01767 $template = $form_state['storage']['template']; 01768 } 01769 01770 // get the field from the path 01771 if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) { 01772 $priority = $_GET['record_id']; 01773 $field_index = $_GET['field_index']; 01774 $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index]; 01775 $form_state['storage']['original_field'] = $template_field; 01776 $form_state['storage']['original_field']['priority'] = $priority; 01777 $form_state['storage']['original_field']['field_index'] = $field_index; 01778 } 01779 // get field from the form_state 01780 elseif (!empty($form_state['storage']['original_field']['priority']) && !empty($form_state['storage']['original_field']['field_index'])) { 01781 $priority = $form_state['storage']['original_field']['priority']; 01782 $field_index = $form_state['storage']['original_field']['field_index']; 01783 $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index]; 01784 } 01785 01786 $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type']; 01787 01788 // Get the tables array and default table 01789 $tables = tripal_core_get_chado_tables(TRUE); 01790 if ($form_state['values']) { 01791 $table = $form_state['values']['chado_table']; 01792 } 01793 else { 01794 $table = $form_state['storage']['template_array'][$priority]['table']; 01795 } 01796 01797 $show_all = ($form_state['values']['show_all_records'] ? $form_state['values']['show_all_records'] : $template_field['show_all_records']); 01798 $table_description = tripal_core_get_chado_table_schema($table); 01799 01800 // Fields and foreign key / referral mappings 01801 // build the fields array 01802 $chado_fields = array(); 01803 foreach ($table_description['fields'] as $field_name => $field_array) { 01804 $chado_fields[$field_name] = $field_name; 01805 } 01806 01807 $ref_chado_fields = array(); 01808 $fk_options = array(); 01809 $fk_options['NULL'] = 'None'; 01810 if ($field_type == 'foreign key' and !$show_all) { 01811 01812 $foreign_field2table = array(); 01813 foreach ($table_description['foreign keys'] as $key_table => $key_array) { 01814 foreach ($key_array['columns'] as $left_field => $right_field) { 01815 //$chado_fields[$left_field] = $left_field; 01816 $foreign_field2table[$left_field] = $key_table; 01817 } 01818 } 01819 // reset($chado_fields); 01820 01821 // set default field 01822 if (empty($chado_fields)) { 01823 $field = NULL; 01824 } 01825 elseif ($chado_fields[$form_state['values']['chado_field']]) { 01826 $field = $form_state['values']['chado_field']; 01827 } 01828 elseif ($template_field['field']) { 01829 $field = $template_field['field']; 01830 } 01831 else { 01832 $field = current($chado_fields); 01833 } 01834 01835 // Foreign key options 01836 $foreign_table = $foreign_field2table[$field]; 01837 if ($foreign_table) { 01838 foreach ($form_state['storage']['record2priority'] as $record_name_ => $priority_ ) { 01839 if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority_]['table'])) { 01840 $fk_options[$record_name_] = $record_name_; 01841 } 01842 } 01843 } 01844 } 01845 // the user wants to see all of the records 01846 elseif ($field_type == 'foreign key' and $show_all) { 01847 foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) { 01848 $fk_options[$record_name] = $record_name; 01849 } 01850 } 01851 01852 // build the list of referrer table fields 01853 if ($field_type == 'foreign key') { 01854 $fk_rec = $form_state['values']['foreign_record'] ? $form_state['values']['foreign_record'] : $template_field['foreign key']; 01855 if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') { 01856 01857 // first add in the foreign keys 01858 $fk_priority = $form_state['storage']['record2priority'][$fk_rec]; 01859 $fk_table = $form_state['storage']['template_array'][$fk_priority]['table']; 01860 foreach ($table_description['foreign keys'] as $key_table => $key_array) { 01861 foreach ($key_array['columns'] as $left_field => $right_field) { 01862 if ($key_table == $fk_table and $left_field == $field) { 01863 $ref_chado_fields['Foreign Key'][$right_field] = $right_field; 01864 } 01865 } 01866 } 01867 $fk_description = tripal_core_get_chado_table_schema($fk_table); 01868 foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) { 01869 // don't include the FK field it's included above 01870 if (in_array('Foreign Key', $ref_chado_fields) and 01871 in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) { 01872 continue; 01873 } 01874 $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name; 01875 } 01876 } 01877 } 01878 01879 // Ensure other field type fields are reset/cleared except for those of the current type 01880 // ie: if the type is changed from spreadsheet column to constant, the spreadsheet column 01881 // fields should be cleared since they don't apply to a constant 01882 switch ($field_type) { 01883 case 'table field': 01884 // clear constant 01885 $form_state['values']['constant_value'] = NULL; 01886 $template_field['constant value'] = NULL; 01887 $form_state['values']['constant_exposed'] = NULL; 01888 $template_field['exposed'] = NULL; 01889 $form_state['values']['constant_validate'] = NULL; 01890 $template_field['exposed_validate'] = NULL; 01891 // clear foreign key 01892 // clears by default :) 01893 break; 01894 case 'constant': 01895 // clear spreadsheet column 01896 $form_state['values']['column_number'] = NULL; 01897 $form_state['values']['column_exposed'] = NULL; 01898 $template_field['exposed'] = NULL; 01899 $form_state['values']['column_exposed_desc'] = NULL; 01900 $template_field['exposed_description'] = NULL; 01901 // clear foreign key 01902 // clears by default :) 01903 break; 01904 case 'foreign key': 01905 // clear constant 01906 $form_state['values']['constant_value'] = NULL; 01907 $template_field['constant value'] = NULL; 01908 $form_state['values']['constant_exposed'] = NULL; 01909 $template_field['exposed'] = NULL; 01910 $form_state['values']['constant_validate'] = NULL; 01911 $template_field['exposed_validate'] = NULL; 01912 // clear spreadsheet column 01913 $form_state['values']['column_number'] = NULL; 01914 $form_state['values']['column_exposed'] = NULL; 01915 $template_field['exposed'] = NULL; 01916 $form_state['values']['column_exposed_desc'] = NULL; 01917 $template_field['exposed_description'] = NULL; 01918 break; 01919 } 01920 01921 // Start of Form Proper-------------------------------------------------------------- 01922 01923 $form['template_name'] = array( 01924 '#type' => 'item', 01925 '#title' => 'Template', 01926 '#value' => $template->name, 01927 ); 01928 01929 $form['template_id'] = array( 01930 '#type' => 'hidden', 01931 '#value' => $template_id, 01932 ); 01933 01934 $form['edit_fields'] = array( 01935 '#type' => 'fieldset', 01936 '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">', 01937 '#suffix' => '</div>', 01938 ); 01939 01940 $form['edit_fields']['field_type'] = array( 01941 '#type' => 'radios', 01942 '#title' => t('Type of Field'), 01943 '#options' => array( 01944 'table field' => t('Data Field: Fields which maps to a data file column'), 01945 'constant' => t('Constant: Field which remains Constant throughout the data file'), 01946 'foreign key' => t('Record Referral: Fields which map to a record in another table'), 01947 ), 01948 '#required' => TRUE, 01949 '#default_value' => $field_type, 01950 '#ahah' => array( 01951 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah', 01952 'wrapper' => 'tripal_bulk_loader_template-edit_field', 01953 'effect' => 'fade' 01954 ), 01955 ); 01956 01957 // check template array for records then edit one more 01958 if (!$form_state['storage']['record2priority']) { 01959 $groups = array(); 01960 } 01961 else { 01962 $groups = array_flip($form_state['storage']['record2priority']); 01963 } 01964 $groups['NONE'] = 'Select a Record'; 01965 $groups['NEW'] = 'New Record'; 01966 $form['edit_fields']['field_group'] = array( 01967 '#type' => 'select', 01968 '#title' => 'Record', 01969 '#description' => 'This is used to group a set of fields together allowing ' 01970 .'multiple records to be inserted into the same table per line of the data file', 01971 '#options' => $groups, 01972 '#default_value' => (preg_match('/(\d+|\w+)/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority, 01973 '#ahah' => array( 01974 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah', 01975 'wrapper' => 'tripal_bulk_loader_template-edit_field', 01976 'effect' => 'fade' 01977 ), 01978 '#required' => TRUE, 01979 ); 01980 01981 $form['edit_fields']['record_name'] = array( 01982 '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden', 01983 '#title' => 'Unique Record Name', 01984 '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">', 01985 '#suffix' => '</div>', 01986 '#default_value' => $form_state['values']['record_name'], 01987 ); 01988 01989 $form['edit_fields']['field_title'] = array( 01990 '#type' => 'textfield', 01991 '#title' => t('Human-readable Title for Field'), 01992 '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'], 01993 ); 01994 01995 // Chado Field 01996 $form['edit_fields']['chado'] = array( 01997 '#type' => 'fieldset', 01998 '#title' => t('Chado Field/Column Details'), 01999 '#description' => t('Specify the Table/Field in chado that this field maps to.'), 02000 ); 02001 02002 $form['edit_fields']['chado']['chado_table'] = array( 02003 '#type' => 'select', 02004 '#title' => t('Chado Table'), 02005 '#options' => $tables, 02006 '#default_value' => $table, 02007 '#ahah' => array( 02008 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah', 02009 'wrapper' => 'tripal_bulk_loader_template-edit_field', 02010 'effect' => 'fade' 02011 ), 02012 ); 02013 02014 $form['edit_fields']['chado']['chado_field'] = array( 02015 '#type' => 'select', 02016 '#title' => t('Chado Field/Column'), 02017 '#options' => $chado_fields, 02018 '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'], 02019 '#ahah' => array( 02020 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah', 02021 'wrapper' => 'tripal_bulk_loader_template-edit_field', 02022 'effect' => 'fade' 02023 ), 02024 ); 02025 02026 // data file column 02027 $form['edit_fields']['columns'] = array( 02028 '#type' => 'fieldset', 02029 '#title' => t('Data File Column'), 02030 '#collapsible' => TRUE, 02031 '#collapsed' => ($field_type == 'table field')? FALSE : TRUE, 02032 ); 02033 02044 $form['edit_fields']['columns']['column_number'] = array( 02045 '#type' => 'textfield', 02046 '#title' => t('Column'), 02047 '#description' => t('Specify the column in the data file that this field maps to where the first column is 1.'), 02048 '#size' => 5, 02049 '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'], 02050 ); 02051 02052 $form['edit_fields']['columns']['column_exposed'] = array( 02053 '#type' => 'checkbox', 02054 '#title' => t('Allow Column to be set for each Bulk Loading Job'), 02055 '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'), 02056 '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'], 02057 ); 02058 02059 $form['edit_fields']['columns']['column_exposed_desc'] = array( 02060 '#type' => 'textfield', 02061 '#title' => t('Description for exposed field on bulk loading job'), 02062 '#description' => t('This description should tell the user what column should be entered here.'), 02063 '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'], 02064 ); 02065 02066 // Global Value 02067 $form['edit_fields']['constant'] = array( 02068 '#type' => 'fieldset', 02069 '#title' => t('Constant'), 02070 '#collapsible' => TRUE, 02071 '#collapsed' => ($field_type == 'constant')? FALSE : TRUE, 02072 ); 02073 02074 $form['edit_fields']['constant']['constant_value'] = array( 02075 '#type' => 'textfield', 02076 '#title' => t('Constant Value'), 02077 '#description' => t('Specify the value you wish this field to have regardless of data file value.'), 02078 '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'], 02079 ); 02080 02081 $form['edit_fields']['constant']['constant_exposed'] = array( 02082 '#type' => 'checkbox', 02083 '#title' => t('Allow Constant to be set for each Bulk Loading Job'), 02084 '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'), 02085 '#default_value' => ($form_state['values']['constant_exposed']) ? $form_state['values']['constant_exposed'] : $template_field['exposed'], 02086 ); 02087 02088 $form['edit_fields']['constant']['constant_validate'] = array( 02089 '#type' => 'checkbox', 02090 '#title' => t('Ensure value is in table'), 02091 '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'), 02092 '#default_value' => ($form_state['values']['constant_validate']) ? $form_state['values']['constant_validate'] : $template_field['exposed_validate'], 02093 ); 02094 02095 // Foreign Key / Referrer 02096 $form['edit_fields']['foreign_key'] = array( 02097 '#type' => 'fieldset', 02098 '#title' => 'Record Referral', 02099 '#collapsible' => TRUE, 02100 '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE, 02101 ); 02102 02103 $form['edit_fields']['foreign_key']['show_all_records'] = array( 02104 '#type' => 'checkbox', 02105 '#title' => 'Refer to any record', 02106 '#description' => t('By default, the bulk loader will only allow referral to records in a foreign key relationship. To allow referral to any previous record, check this box'), 02107 '#default_value' => $show_all, 02108 '#ahah' => array( 02109 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah', 02110 'wrapper' => 'tripal_bulk_loader_template-add_field', 02111 'effect' => 'fade' 02112 ), 02113 ); 02114 02115 $form['edit_fields']['foreign_key']['foreign_record'] = array( 02116 '#type' => 'select', 02117 '#title' => 'Record to refer to', 02118 '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.', 02119 '#options' => $fk_options, 02120 '#ahah' => array( 02121 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah', 02122 'wrapper' => 'tripal_bulk_loader_template-add_field', 02123 'effect' => 'fade' 02124 ), 02125 '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'], 02126 ); 02127 02128 $form['edit_fields']['foreign_key']['foreign_field'] = array( 02129 '#type' => 'select', 02130 '#title' => 'Field to refer to', 02131 '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.', 02132 '#options' => $ref_chado_fields, 02133 '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign field'], 02134 ); 02135 02136 02137 02138 $form['edit_fields']['additional'] = array( 02139 '#type' => 'fieldset', 02140 '#title' => 'Additional Options', 02141 ); 02142 02143 $form['edit_fields']['additional']['required'] = array( 02144 '#type' => 'checkbox', 02145 '#title' => 'Make this field required', 02146 '#default_value' => (!empty($form_state['values']['required'])) ? $form_state['values']['required'] : $template_field['required'], 02147 ); 02148 02149 $form['edit_fields']['additional']['regex_transform'] = array( 02150 '#type' => 'fieldset', 02151 '#title' => 'Transform Data File Value Rules', 02152 '#collapsible' => TRUE, 02153 '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE, 02154 ); 02155 02156 $transformation_msg = '<p>A transformation rule allows you to transform the original value ' 02157 .'(usually from a user submitted data file) into the form you would like it stored ' 02158 .'in the chado database. Each rule consists of a match pattern (a php regular expression ' 02159 .'which determines which replacement patterns are applied and captures regions of the ' 02160 .'original value) and a replacement pattern (a string which may contain capture references ' 02161 .'that describes what the new value should be). Each rule is applied to the result of the ' 02162 .'previous rule.<p>'; 02163 $form['edit_fields']['additional']['regex_transform']['regex_description'] = array( 02164 '#type' => 'item', 02165 '#value' => $transformation_msg, 02166 ); 02167 02168 $form['edit_fields']['additional']['regex_transform']['regex-data'] = array( 02169 '#tree' => TRUE, 02170 ); 02171 02172 if (!is_array($template_field['regex']['pattern'])) { 02173 $template_field['regex']['pattern'] = array(); 02174 } 02175 $key_options = array(); 02176 foreach ($template_field['regex']['pattern'] as $k => $v) { 02177 $key_options[$k] = $k; 02178 } 02179 foreach ($template_field['regex']['pattern'] as $index => $pattern) { 02180 $data_element = array( 02181 'pattern' => array( 02182 '#type' => 'item', 02183 '#value' => $pattern, 02184 ), 02185 'replace' => array( 02186 '#type' => 'item', 02187 '#value' => $template_field['regex']['replace'][$index], 02188 ), 02189 'old_index' => array( 02190 '#type' => 'hidden', 02191 '#value' => $index, 02192 ), 02193 'new_index' => array( 02194 '#type' => 'select', 02195 '#options' => $key_options, 02196 '#default_value' => $index, 02197 ), 02198 'id' => array( 02199 '#type' => 'hidden', 02200 '#value' => $index, 02201 ), 02202 'submit-delete' => array( 02203 '#type' => 'submit', 02204 '#value' => 'Delete Transformation', 02205 '#name' => $index, 02206 ), 02207 ); 02208 $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element; 02209 } 02210 02211 $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array( 02212 '#type' => 'submit', 02213 '#value' => 'Save Transformation Rule Order' 02214 ); 02215 02216 $form['edit_fields']['additional']['regex_transform']['new_regex'] = array( 02217 '#type' => 'fieldset', 02218 '#title' => 'Add a new Transformation Rule', 02219 ); 02220 02221 $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array( 02222 '#type' => 'textfield', 02223 '#title' => 'Match Pattern', 02224 '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a ' 02225 .'pattern. Only if this pattern matches the value in the data file does the replacement ' 02226 .'pattern get applied to the value. To capture a section of your value for use in the ' 02227 .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match ' 02228 .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. ' 02229 .' To match and capture any value use <i>.*</i>', 02230 ); 02231 02232 $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array( 02233 '#type' => 'textfield', 02234 '#title' => 'Replacement Pattern', 02235 '#description' => '<p>This pattern should contain the text you want to replace the match pattern ' 02236 .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the ' 02237 .'capture in the match pattern. For example, \1 will be replaced with the text matched in your ' 02238 .'first set of round brackets.</p>', 02239 ); 02240 02241 if ($field_type == 'table field') { 02242 $tab = '        '; 02243 $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>' 02244 .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. ' 02245 .'This allows you to substitute other data file values into the current field. For example, ' 02246 .'if you had the following line:<br />' 02247 . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355' 02248 .'<br /> and your current field is for column #1 and you\'re inserting into the chado field ' 02249 .'feature.uniquename then you might want to add in the data to ensure your uniquename is ' 02250 .'unique. The Match Pattern is (.*) to select all the first column and the Replacement ' 02251 .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>'; 02252 } 02253 02254 $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array( 02255 '#type' => 'submit', 02256 '#value' => 'Add Transformation', 02257 ); 02258 02259 $form['edit_fields']['additional']['regex_transform']['test_regex'] = array( 02260 '#type' => 'fieldset', 02261 '#title' => 'Test Transformation Rules', 02262 ); 02263 02264 $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array( 02265 '#type' => 'textfield', 02266 '#title' => 'Test Value', 02267 '#description' => 'This should be a value that you expect the above transformation rules ' 02268 .'to be applied to.', 02269 '#default_value' => $form_state['storage']['test_regex_test'], 02270 ); 02271 02272 $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array( 02273 '#type' => 'textfield', 02274 '#title' => 'Test Result', 02275 '#description' => 'This is the value that would be saved to the database after the above transformation ' 02276 .'riles were applied to the Test Value.', 02277 '#default_value' => $form_state['storage']['test_regex_result'], 02278 ); 02279 02280 $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array( 02281 '#type' => 'submit', 02282 '#value' => 'Test Transformation Rules' 02283 ); 02284 02285 $form['edit_fields']['submit-edit_field'] = array( 02286 '#type' => 'submit', 02287 '#value' => 'Save Changes' 02288 ); 02289 02290 $form['edit_fields']['submit-cancel'] = array( 02291 '#type' => 'submit', 02292 '#value' => 'Cancel' 02293 ); 02294 02295 return $form; 02296 } 02297 02298 function tripal_bulk_loader_edit_template_field_form_validate($form, $form_state) { 02299 02300 // Don't worry about validation when Cancel button is clicked 02301 if ($form_state['clicked_button']['#value'] == 'Save Changes') { 02302 $is_unique = tripal_bulk_loader_is_record_name_unique( 02303 $form_state['values']['record_name'], 02304 $form_state['values']['template_id'], 02305 $form_state['storage']['template_array'] 02306 ); 02307 $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE; 02308 if ((!$is_unique) AND $new_record) { 02309 form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique."); 02310 } 02311 } 02312 02313 } 02314 02323 function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state) { 02324 02325 $op = $form_state['values'][ $form_state['clicked_button']['#name'] ]; 02326 02327 //Clear Test 02328 $form_state['storage']['test_regex_result'] = NULL; 02329 $form_state['storage']['test_regex_test'] = NULL; 02330 02331 if (!$form_state['ahah_submission']) { 02332 if ($op == 'Save Changes') { 02333 02334 // If new record 02335 if (preg_match('/NEW/', $form_state['values']['field_group'])) { 02336 // add new record 02337 $record_name = $form_state['values']['record_name']; 02338 $priority = sizeof($form_state['storage']['template_array']) + 1; 02339 $old_priority = $form_state['storage']['original_field']['priority']; 02340 $field_index = $form_state['storage']['original_field']['field_index']; 02341 $form_state['storage']['record2priority'][$record_name] = $priority; 02342 $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table']; 02343 $form_state['storage']['template_array'][$priority]['record_id'] = $record_name; 02344 02345 } 02346 else { 02347 $priority = $form_state['values']['field_group']; 02348 $old_priority = $form_state['storage']['original_field']['priority']; 02349 $field_index = $form_state['storage']['original_field']['field_index']; 02350 $record_name = $form_state['storage']['record2priority'][$priority]; 02351 } 02352 02353 $field = $form_state['storage']['original_field']; 02354 if ($form_state['values']['field_type'] == 'table field') { 02355 $field['type'] = 'table field'; 02356 $field['title'] = $form_state['values']['field_title']; 02357 $field['field'] = $form_state['values']['chado_field']; 02358 $field['required'] = $form_state['values']['required']; 02359 //$field['allowed values'] = empty by default; 02360 $field['spreadsheet column'] = $form_state['values']['column_number']; 02361 $field['exposed'] = $form_state['values']['column_exposed']; 02362 $field['exposed_description'] = $form_state['values']['column_exposed_desc']; 02363 } 02364 elseif ($form_state['values']['field_type'] == 'constant') { 02365 $field['type'] = 'constant'; 02366 $field['title'] = $form_state['values']['field_title']; 02367 $field['field'] = $form_state['values']['chado_field']; 02368 $field['required'] = $form_state['values']['required']; 02369 //$field['allowed values'] = empty by default; 02370 $field['constant value'] = $form_state['values']['constant_value']; 02371 $field['exposed_validate'] = $form_state['values']['constant_validate']; 02372 $field['exposed'] = $form_state['values']['constant_exposed']; 02373 } 02374 elseif ($form_state['values']['field_type'] == 'foreign key') { 02375 $field['type'] = 'foreign key'; 02376 $field['title'] = $form_state['values']['field_title']; 02377 $field['field'] = $form_state['values']['chado_field']; 02378 $field['show_all_records'] = $form_state['values']['show_all_records']; 02379 $field['foreign key'] = $form_state['values']['foreign_record']; 02380 $field['foreign field'] = $form_state['values']['foreign_field']; 02381 02382 $field['required'] = $form_state['values']['required']; 02383 } 02384 02385 // Deal with any additional options 02386 02387 // if the record has changed... 02388 $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table']; 02389 if ($old_priority != $priority) { 02390 $form_state['storage']['template_array'][$priority]['fields'][] = $field; 02391 unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]); 02392 02393 // if there are no fields left delete the old record 02394 if (!$form_state['storage']['template_array'][$old_priority]['fields']) { 02395 unset($form_state['storage']['template_array'][$old_priority]); 02396 } 02397 } 02398 else { 02399 $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field; 02400 } 02401 02402 // Save Template 02403 $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']); 02404 $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id')); 02405 02406 if ($success) { 02407 drupal_set_message(t('Successfully Updated Field')); 02408 drupal_set_message(t('Template Saved.')); 02409 02410 $path = explode('?', $form_state['storage']['referring URL']); 02411 parse_str($path[1], $query); 02412 $query['template_id'] = $form_state['storage']['template']->template_id; 02413 drupal_goto($path[0], $query); 02414 } 02415 else { 02416 drupal_set_message(t('Unable to Save Template!'), 'error'); 02417 watchdog('T_bulk_loader', 02418 'Unable to save bulk loader template: %template', 02419 array('%template' => print_r($form_state['storage']['template'], TRUE)), 02420 WATCHDOG_ERROR 02421 ); 02422 } 02423 02424 } 02425 elseif ($op == 'Cancel') { 02426 02427 $path = explode('?', $form_state['storage']['referring URL']); 02428 parse_str($path[1], $query); 02429 $query['template_id'] = $form_state['storage']['template']->template_id; 02430 drupal_goto($path[0], $query); 02431 02432 } 02433 elseif ($op == 'Add Transformation') { 02434 02435 // Add transformation rule to original field 02436 $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/'; 02437 $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace']; 02438 02439 // Add original field back into template 02440 $priority = $form_state['storage']['original_field']['priority']; 02441 $field_index = $form_state['storage']['original_field']['field_index']; 02442 $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field']; 02443 02444 } 02445 elseif ($op == 'Save Transformation Rule Order') { 02446 02447 // Generate new regex array 02448 $new_regex = array(); 02449 $old_regex = $form_state['storage']['original_field']['regex']; 02450 foreach ($form_state['values']['regex-data'] as $key => $element) { 02451 $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ]; 02452 $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ]; 02453 } 02454 02455 // sort new regex arrays 02456 ksort($new_regex['pattern']); 02457 ksort($new_regex['replace']); 02458 02459 // Add back to original field 02460 $form_state['storage']['original_field']['regex'] = $new_regex; 02461 $priority = $form_state['storage']['original_field']['priority']; 02462 $field_index = $form_state['storage']['original_field']['field_index']; 02463 $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field']; 02464 02483 } 02484 elseif ($op == 'Delete Transformation') { 02485 02486 // Unset regex rule 02487 $index = $form_state['clicked_button']['#name']; 02488 unset($form_state['storage']['original_field']['regex']['pattern'][$index]); 02489 unset($form_state['storage']['original_field']['regex']['replace'][$index]); 02490 02491 $priority = $form_state['storage']['original_field']['priority']; 02492 $field_index = $form_state['storage']['original_field']['field_index']; 02493 $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field']; 02494 02514 } 02515 elseif ($op == 'Test Transformation Rules') { 02516 02517 $patterns = $form_state['storage']['original_field']['regex']['pattern']; 02518 $replaces = $form_state['storage']['original_field']['regex']['replace']; 02519 $test_string = $form_state['values']['test_string']; 02520 $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string); 02521 $form_state['storage']['test_regex_test'] = $test_string; 02522 } 02523 } 02524 02525 } 02526 02538 function tripal_bulk_loader_add_field_ahah() { 02539 02540 $form_state = array('storage' => NULL, 'submitted' => FALSE); 02541 $form_build_id = filter_xss($_POST['form_build_id']); 02542 $form = form_get_cache($form_build_id, $form_state); 02543 $args = $form['#parameters']; 02544 $form_id = array_shift($args); 02545 $form_state['post'] = $form['#post'] = $_POST; 02546 02547 // Enable the submit/validate handlers to determine whether AHAH-submittted. 02548 $form_state['ahah_submission'] = TRUE; 02549 02550 $form['#programmed'] = $form['#redirect'] = FALSE; 02551 drupal_process_form($form_id, $form, $form_state); 02552 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); 02553 02554 $form_element = $form['add_fields']; 02555 // Remove the wrapper so we don't double it up. 02556 //unset($form_element['#prefix'], $form_element['#suffix']); 02557 02558 $output = theme('status_messages'); 02559 $output .= drupal_render($form_element); 02560 02561 // Final rendering callback. 02562 print drupal_json(array('status' => TRUE, 'data' => $output)); 02563 exit(); 02564 02565 } 02566 02573 function tripal_bulk_loader_edit_field_ahah() { 02574 02575 $form_state = array('storage' => NULL, 'submitted' => FALSE); 02576 $form_build_id = filter_xss($_POST['form_build_id']); 02577 $form = form_get_cache($form_build_id, $form_state); 02578 $args = $form['#parameters']; 02579 $form_id = array_shift($args); 02580 $form_state['post'] = $form['#post'] = $_POST; 02581 02582 // Enable the submit/validate handlers to determine whether AHAH-submittted. 02583 $form_state['ahah_submission'] = TRUE; 02584 02585 $form['#programmed'] = $form['#redirect'] = FALSE; 02586 drupal_process_form($form_id, $form, $form_state); 02587 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); 02588 02589 $form_element = $form['edit_fields']; 02590 // Remove the wrapper so we don't double it up. 02591 unset($form_element['#prefix'], $form_element['#suffix']); 02592 02593 $output = theme('status_messages'); 02594 $output .= drupal_render($form_element); 02595 02596 // Final rendering callback. 02597 print drupal_json(array('status' => TRUE, 'data' => $output)); 02598 exit(); 02599 02600 }