12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600 |
- <?php
- function tripal_bulk_loader_modify_template_base_form($form_state = NULL, $mode) {
- $form = array();
-
- if ($_GET['template_id']) {
- if (preg_match('/^\d+$/', $_GET['template_id'])) {
- $form_state['storage']['template_id'] = $_GET['template_id'];
- }
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
- $form_state['storage']['template'] = unserialize($result->template_array);
- $form_state['storage']['template_name'] = $result->name;
- $form_state['storage']['record2priority'] = array();
- foreach ($form_state['storage']['template'] as $priority => $record_array) {
- if (!is_array($record_array)) {
- continue; }
- $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
- }
- }
- $form['mode'] = array(
- '#type' => 'hidden',
- '#value' => $mode,
- );
- if ($form_state['storage']['template_id']) {
- $form['template_name'] = array(
- '#type' => 'item',
- '#title' => 'Template',
- '#value' => $form_state['storage']['template_name'],
- '#weight' => 1,
- );
- }
- else {
- if (preg_match('/create/', $mode)) {
- $form['new_template_name'] = array(
- '#type' => 'textfield',
- '#title' => 'Template Name',
- '#weight' => 1,
- );
- }
- elseif (preg_match('/edit/', $mode)) {
- $sql = "SELECT * FROM {tripal_bulk_loader_template}";
- $resource = db_query($sql);
- $templates = array();
- $templates[''] = 'Select a Template';
- while ($r = db_fetch_object($resource)) {
- $templates[$r->template_id] = $r->name;
- }
- $form['template_id'] = array(
- '#title' => t('Template'),
- '#description' => t('Please select the template you would like to edit.'),
- '#type' => 'select',
- '#options' => $templates,
- '#default_value' => $form_state['storage']['template_id'],
- '#weight' => 0,
- '#required' => TRUE,
- '#weight' => 1,
- );
- }
- }
- $form['records'] = array(
- '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
- '#title' => t('Current Records'),
- '#collapsible' => TRUE,
- '#weight' => 2,
- );
- $form['records']['description'] = array(
- '#type' => 'item',
- '#value' => 'Records will be inserted into the chado database in the order listed below. To '
- .'change this order: <ul><li>Drag the rows into the correct order <br/>(If you don\'t have javascript enabled then enter '
- .'the numbers 1 and up in the Order textboxes to indicate the correct order).</li></ul>',
- );
- $form['records']['records-data'] = array(
- '#tree' => TRUE,
- );
- $form['records']['no_records'] = array(
- '#type' => 'hidden',
- '#value' => TRUE,
- );
- $form['records']['submit-new_record'] = array(
- '#type' => 'submit',
- '#value' => 'New Record/Field',
- );
- $form['records']['submit-reorder'] = array(
- '#type' => 'submit',
- '#value' => 'Save Order',
- );
- $form['fields'] = array(
- '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
- '#title' => t('Current Fields'),
- '#collapsible' => TRUE,
- '#weight' => 3,
- );
- $form['fields']['fields-data'] = array(
- '#tree' => TRUE,
- );
- if ($form_state['storage']['template']) {
-
- $i=1;
- foreach ($form_state['storage']['template'] as $priority => $table_array) {
- if (!is_array($table_array)) {
- continue; }
- $form['records']['no_records']['#value'] = FALSE;
- $mode_value = '';
- if ($table_array['optional']) {
- $mode_value .= 'optional ';
- }
-
-
- if (strcmp($table_array['mode'], 'insert_unique')==0) {
- $mode_value .= 'insert or select if duplicate';
- }
- elseif (strcmp($table_array['mode'], 'optional')==0) {
- $mode_value .= 'optional insert';
- }
- elseif (strcmp($table_array['mode'], 'insert_once')==0) {
- $mode_value .= 'insert once';
- }
- elseif (strcmp($table_array['mode'], 'select_once')==0) {
- $mode_value .= 'select once';
- }
- elseif ($table_array['mode']) {
- $mode_value .= $table_array['mode'];
- }
- else {
- $mode_value .= 'insert';
- }
-
- if ($table_array['select_if_duplicate']) {
- $mode_value .= ' or select if duplicate';
- }
- if ($table_array['select_optional']) {
- $mode_value .= ' (no fail)';
- }
- if ($table_array['update_if_duplicate']) {
- $mode_value .= ' or update if duplicate';
- }
-
- if ($table_array['disable']) {
- $mode_value .= '. <font color="Red">DISABLED</font>';
- }
- $form['records']['records-data'][$priority] = array(
- 'title' => array(
- '#type' => 'markup',
- '#value' => filter_xss($priority . ". " . $table_array['record_id']),
- '#prefix' => "<a name=\"record_$priority\"></a>",
- '#suffix' => "<p><a href=\"#fields_$priority\"> View Fields </a></p>",
- ),
- 'chado_table' => array(
- '#type' => 'markup',
- '#value' => filter_xss($table_array['table']),
- ),
- 'mode' => array(
- '#type' => 'item',
- '#value' => $mode_value,
- ),
- 'new_priority' => array(
- '#type' => 'select',
- '#options' => range(1, sizeof($form_state['storage']['template'])),
- '#default_value' => $priority,
- ),
- 'old_priority' => array(
- '#type' => 'hidden',
- '#value' => $priority,
- ),
- 'id' => array(
- '#type' => 'hidden',
- '#value' => $priority,
- ),
- 'submit-edit_record' => array(
- '#type' => 'submit',
- '#name' => ($priority !== 0) ? (string)$priority : 'zero',
- '#value' => 'Edit Record',
- ),
- 'submit-delete_record' => array(
- '#type' => 'submit',
- '#name' => ($priority !== 0) ? (string)$priority : 'zero',
- '#value' => 'Delete Record',
- ),
- 'submit-add_field' => array(
- '#type' => 'submit',
- '#name' => ($priority !== 0) ? (string)$priority : 'zero',
- '#value' => 'Add Field',
- ),
- 'submit-duplicate_record' => array(
- '#type' => 'submit',
- '#name' => ($priority !== 0) ? (string)$priority : 'zero',
- '#value' => 'Duplicate Record'
- ),
- );
- foreach ($table_array['fields'] as $field_index => $field) {
- $fk_value = '';
- if ($field['foreign key']) {
- if ($field['foreign field']) {
- $fk_value = $field['foreign key'] . " (" . $field['foreign field'] . ")";
- }
- else {
-
-
-
-
- $fk_priority = $form_state['storage']['record2priority'][$field['foreign key']];
- $fk_table = $form_state['storage']['template'][$fk_priority]['table'];
- $tbl_description = tripal_core_get_chado_table_schema($table_array['table']);
- foreach ($tbl_description['foreign keys'] as $key_table => $key_array) {
- foreach ($key_array['columns'] as $left_field => $right_field) {
- if ($key_table == $fk_table and $left_field == $field['field']) {
- $fk_value = $field['foreign key'] . " ($right_field)";
- }
- }
- }
- }
- }
- $form['fields']['fields-data'][$i] = array(
- 'record_id' => array(
- '#type' => 'item',
- '#value' => $table_array['record_id'],
- '#prefix' => "<a name=\"fields_$priority\"></a>",
- '#suffix' => "<p><a href=\"#record_$priority\"> View Record </a></p>",
- ),
- 'priority_hidden' => array(
- '#type' => 'hidden',
- '#value' => $priority,
- ),
- 'field_name' => array(
- '#type' => 'item',
- '#value' => $field['title'],
- ),
- 'chado_table_name' => array(
- '#type' => 'item',
- '#value' => $table_array['table'],
- ),
- 'chado_table_hidden' => array(
- '#type' => 'hidden',
- '#value' => $table_array['table'],
- ),
- 'chado_field_name' => array(
- '#type' => 'item',
- '#value' => $field['field'],
- ),
- 'sheet_name' => array(
- '#type' => 'item',
- '#value' => $field['spreadsheet sheet'],
- ),
- 'column_num' => array(
- '#type' => 'item',
- '#value' => $field['spreadsheet column'],
- ),
- 'constant_value' => array(
- '#type' => 'item',
- '#value' => $field['constant value'],
- ),
- 'field_index' => array(
- '#type' => 'hidden',
- '#value' => $field_index
- ),
- 'foreign_record_id' => array(
- '#type' => 'item',
- '#value' => $fk_value,
- ),
- 'edit_submit' => array(
- '#type' => 'submit',
- '#name' => (string)$i,
- '#value' => "Edit Field",
- ),
- 'delete_submit' => array(
- '#type' => 'submit',
- '#name' => (string)$i,
- '#value' => "Delete Field",
- ),
- );
- $i++;
- }
- }
- $form['fields']['total_fields'] = array(
- '#type' => 'item',
- '#value' => $i,
- );
- }
- if ($form['records']['no_records']['#value']) {
- $form['records']['description'] = array(
- '#type' => 'item',
- '#value' => 'There are currently no records.',
- );
- unset($form['records']['submit-reorder']);
- $form['fields']['description'] = array(
- '#type' => 'item',
- '#value' => 'There are currently no fields.',
- );
- }
- $mode_title = (preg_match('/create/', $mode)) ? 'Create Template' : 'Edit Template';
- $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => $value,
- '#weight' => 4,
- );
- return $form;
- }
- function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state) {
- $form_state['rebuild'] = TRUE;
- if ($form_state['storage']['template_id']) {
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
- $form_state['storage']['template'] = unserialize($result->template_array);
- }
- $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
-
- if ($form_state['clicked_button']['#name'] === 'zero') {
- $form_state['clicked_button']['#name'] = '0';
- }
- switch ($op) {
-
- case 'Edit Template':
- $form_state['storage']['template_id'] = $form_state['values']['template_id'];
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
- $form_state['storage']['template'] = unserialize($result->template_array);
- $form_state['storage']['template_name'] = $result->name;
- $form_state['storage']['record2priority'] = array();
- foreach ($form_state['storage']['template'] as $priority => $record_array) {
- if (!is_array($record_array)) {
- continue;
- }
- $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
- }
- break;
- case 'Create Template':
- $record = array(
- 'name' => $form_state['values']['new_template_name'],
- 'template_array' => array(),
- );
- drupal_write_record('tripal_bulk_loader_template', $record);
- $form_state['storage']['template_id'] = $record['template_id'];
- $form_state['storage']['template_name'] = $record['name'];
- $form_state['storage']['template'] = array();
- break;
-
- case 'Save Order':
- $new_template = $form_state['storage']['template'];
-
- $form_state['storage']['record2priority'] = array();
- foreach ($new_template as $priority => $record_array) {
- if (preg_match('/\d+/', $priority)) {
- unset($new_template[$priority]);
- }
- }
-
- foreach ($form_state['values']['records-data'] as $item) {
- $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
- $record_name = $new_template[$item['new_priority']]['record_id'];
- $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
- }
- ksort($new_template);
- $form_state['storage']['template'] = $new_template;
- break;
- case 'New Record/Field':
- $query = array(
- 'template_id' => $form_state['storage']['template_id'],
- 'record_id' => 'NEW',
- );
- drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
- break;
- case 'Edit Record':
- $query = array(
- 'template_id' => $form_state['storage']['template_id'],
- 'record_id' => $form_state['clicked_button']['#name'],
- );
- drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_record', $query);
- break;
- case 'Delete Record':
- $form_state['storage']['record2priority'] = array();
- $new_template = tripal_bulk_loader_delete_record($form_state['clicked_button']['#name'], $form_state['storage']['template']);
- if (!empty($new_template)) {
- $form_state['storage']['template'] = $new_template;
- }
- break;
- case 'Add Field':
- $query = array(
- 'template_id' => $form_state['storage']['template_id'],
- 'record_id' => $form_state['clicked_button']['#name'],
- );
- drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
- break;
- case 'Duplicate Record':
-
- $orig_priority = $form_state['clicked_button']['#name'];
- $record = $form_state['storage']['template'][ $orig_priority ];
-
- $new_priority = sizeof($form_state['storage']['template']) + 1;
- $record['record_id'] = $record['record_id'] . '_' . date('YzHi');
- $form_state['storage']['template'][ $new_priority ] = $record;
- break;
- case 'Edit Field':
- $field_data_index = $form_state['clicked_button']['#name'];
- $query = array(
- 'template_id' => $form_state['storage']['template_id'],
- 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'],
- 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'],
- );
- drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query);
- break;
- case 'Delete Field':
-
- $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']];
- $priority = $field_data['priority_hidden'];
- $field_key = $field_data['field_index'];
- $new_template = tripal_bulk_loader_delete_field($priority, $field_key, $form_state['storage']['template']);
- if (!empty($new_template)) {
- $form_state['storage']['template'] = $new_template;
- }
- drupal_set_message(t('Deleted Field from Template.'));
- break;
- }
-
- $record = array(
- 'template_id' => $form_state['storage']['template_id'],
- 'template_array' => serialize($form_state['storage']['template'])
- );
- drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
- drupal_set_message(t('Template Saved.'));
- }
- function tripal_bulk_loader_delete_template_base_form() {
- $form = array();
- $sql = "SELECT * FROM {tripal_bulk_loader_template}";
- $resource = db_query($sql);
- $templates = array();
- $templates[''] = 'Select a Template';
- while ($r = db_fetch_object($resource)) {
- $templates[$r->template_id] = $r->name;
- }
- $form['template_name'] = array(
- '#title' => t('Template'),
- '#description' => t('Please select the template you would like to delete.'),
- '#type' => 'select',
- '#options' => $templates,
- '#weight' => 0,
- '#required' => TRUE,
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Delete Template',
- );
- return $form;
- }
- function tripal_bulk_loader_delete_template_base_form_submit($form, &$form_state) {
- $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- db_query($sql, $form_state['values']['template_name']);
- }
- function tripal_bulk_loader_import_export_template_form($form_state = NULL, $mode) {
- $form = array();
- $form['mode'] = array(
- '#type' => 'hidden',
- '#value' => $mode,
- );
- if (preg_match('/import/', $mode)) {
- $form['new_template_name'] = array(
- '#type' => 'textfield',
- '#title' => 'Template Name',
- '#weight' => 1,
- );
- }
- elseif (preg_match('/export/', $mode)) {
- $sql = "SELECT * FROM {tripal_bulk_loader_template}";
- $resource = db_query($sql);
- $templates = array();
- $templates[''] = 'Select a Template';
- while ($r = db_fetch_object($resource)) {
- $templates[$r->template_id] = $r->name;
- }
- $form['template_id'] = array(
- '#title' => t('Template'),
- '#description' => t('Please select the template you would like to edit.'),
- '#type' => 'select',
- '#options' => $templates,
- '#default_value' => $form_state['storage']['template_id'],
- '#weight' => 0,
- '#required' => TRUE,
- );
- }
- $form['template_array'] = array(
- '#type' => 'textarea',
- '#title' => 'Template Array',
- '#default_value' => $form_state['storage']['template_array'],
- '#description' => t('Use this serialized array for import.'),
- '#rows' => 15,
- '#weight' => 5,
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Submit',
- '#weight' => 10,
- );
- return $form;
- }
- function tripal_bulk_loader_import_export_template_form_submit($form, &$form_state) {
- switch ($form_state['values']['mode']) {
- case 'export':
- $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
-
- $t = var_export(unserialize($record->template_array), TRUE);
- $t = preg_replace("/\n\s+array/", "array", $t);
- $t = preg_replace("/true/", "TRUE", $t);
- $t = preg_replace("/false/", "FALSE", $t);
- $t = preg_replace("/array\(/", "array (", $t);
- $form_state['storage']['template_array'] = $t;
- $form_state['storage']['template_id'] = $form_state['values']['template_id'];
- break;
- case 'import':
-
- $t = array();
- eval("\$t = " . $form_state['values']['template_array'] . ";");
- $record = array(
- 'name' => $form_state['values']['new_template_name'],
- 'template_array' => serialize($t),
- );
- drupal_write_record('tripal_bulk_loader_template', $record);
- if ($record->template_id) {
- drupal_set_message(t('Successfully imported Tripal Bulk Loader Template.'));
- }
- break;
- }
- }
- function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) {
- $form['#cache'] = TRUE;
-
- $template_id = ($_GET['template_id'] !== NULL) ? $_GET['template_id'] : $form_state['values']['template_id'];
-
- if (!$template_id) {
- return $form;
- }
-
-
- if (!$form_state['storage']['template']) {
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $template = db_fetch_object(db_query($sql, $template_id));
- $form_state['storage']['template_array'] = unserialize($template->template_array);
- $form_state['storage']['template'] = $template;
- $form_state['storage']['record2priority'] = array();
- foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
- if (!is_array($record_array)) {
- continue;
- }
- $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
- }
- $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
- }
- else {
- $template = $form_state['storage']['template'];
- }
-
- if ($_GET['record_id'] !== NULL) {
- $form_state['values']['field_group'] = $_GET['record_id'];
- $form_state['storage']['original_priority'] = $_GET['record_id'];
- }
-
- $tables = tripal_core_get_chado_tables(TRUE);
- if ($form_state['values']['chado_table']) {
- $table = $form_state['values']['chado_table'];
- }
- else {
- $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
- }
-
- $mode = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['mode'];
- if (!$mode) {
- $mode = 'insert';
- }
-
- $select_optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_optional'];
- if (!isset($select_optional)) {
- $select_optional = 0;
- }
-
- $select_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_if_duplicate'];
- if (!isset($select_if_duplicate)) {
- $select_if_duplicate = 1;
- }
-
- $update_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['update_if_duplicate'];
- if (!isset($update_if_duplicate)) {
- $update_if_duplicate = 0;
- }
-
- $optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['optional'];
- if (!isset($optional)) {
- $optional = 0;
- }
-
- $disable = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['disable'];
-
- if (strcmp($mode, 'insert_unique')==0) {
- $mode = 'insert';
- $select_if_duplicate = 1;
- }
-
- if (strcmp($mode, 'optional')==0) {
- $mode = 'insert';
- $optional = 1;
- }
-
-
- $form['template_name'] = array(
- '#type' => 'item',
- '#title' => 'Template',
- '#value' => $template->name,
- );
- $form['template_id'] = array(
- '#type' => 'hidden',
- '#value' => $template_id,
- );
- $form['edit_record'] = array(
- '#type' => 'fieldset',
- );
-
- if (!$form_state['storage']['record2priority']) {
- $groups = array();
- }
- else {
- $groups = array_flip($form_state['storage']['record2priority']);
- }
- $priority_default = $form_state['values']['field_group'];
- $form['edit_record']['field_group'] = array(
- '#type' => 'select',
- '#title' => 'Record',
- '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
- '#options' => $groups,
- '#default_value' => $priority_default,
- '#required' => TRUE,
- );
- $form['edit_record']['record_name'] = array(
- '#type' => 'textfield',
- '#title' => 'Unique Record Name',
- '#default_value' => $groups[$priority_default],
- );
- $form['edit_record']['chado_table'] = array(
- '#type' => 'select',
- '#title' => t('Chado Table'),
- '#description' => 'This changes the chado table for all fields in this record.',
- '#options' => $tables,
- '#default_value' => $table,
- );
- $form['edit_record']['mode'] = array(
- '#type' => 'radios',
- '#title' => 'Action to take when Loading Record',
- '#options' => array(
- 'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
- 'select_once' => 'SELECT ONCE: Select the record only once for each constant set.',
- 'insert' => 'INSERT: Insert the record',
- '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.',
- ),
- '#default_value' => $mode
- );
- $form['edit_record']['select_options'] = array(
- '#type' => 'markup',
- '#value' => t('Additional Select Options:'),
- );
- $form['edit_record']['select_optional'] = array(
- '#type' => 'checkbox',
- '#title' => t('Continue if no record exists or too many exist.'),
- '#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.'),
- '#default_value' => $select_optional
- );
- $form['edit_record']['insert_options'] = array(
- '#type' => 'markup',
- '#prefix' => '<br>',
- '#value' => t('Additional Insert Options:'),
- );
- $form['edit_record']['select_if_duplicate'] = array(
- '#type' => 'checkbox',
- '#title' => t('SELECT if duplicate (no insert)'),
- '#description' => t('If this is not the first time this record has been added then perform a select rather than an insert.'),
- '#default_value' => $select_if_duplicate
- );
- $form['edit_record']['update_if_duplicate'] = array(
- '#type' => 'checkbox',
- '#title' => t('UPDATE if duplicate (no insert)'),
- '#description' => t('If this is not the first time this record has been added then perform an update rather than an insert.'),
- '#default_value' => $update_if_duplicate
- );
- $form['edit_record']['optional'] = array(
- '#type' => 'checkbox',
- '#title' => t('Optional'),
- '#description' => t('The insert, update or select will only be performed only if all required data are present'),
- '#default_value' => $optional
- );
- $form['edit_record']['disable'] = array(
- '#type' => 'checkbox',
- '#title' => t('Disable this record'),
- '#description' => t("Check this box to ignore this record (not perform select or insert) during template loading. Uncheck to re-enable the record"),
- '#default_value' => $disable,
- );
- $form['edit_record']['submit-edit_record'] = array(
- '#type' => 'submit',
- '#value' => 'Edit Record'
- );
- $form['edit_record']['submit-cancel'] = array(
- '#type' => 'submit',
- '#value' => 'Cancel'
- );
- return $form;
- }
- function tripal_bulk_loader_edit_template_record_form_validate($form, $form_state) {
-
- if ($form_state['clicked_button']['#value'] == 'Edit Record') {
- $is_unique = tripal_bulk_loader_is_record_name_unique(
- $form_state['values']['record_name'],
- $form_state['values']['template_id'],
- $form_state['storage']['template_array'],
- $form_state['values']['field_group']
- );
- if (!$is_unique) {
- form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
- }
- }
- }
- function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) {
-
- if (!$form_state['ahah_submission']) {
- if ($form_state['values']['op'] == 'Edit Record') {
- $template = $form_state['storage']['template_array'];
- $original_record_name = $template[ $form_state['storage']['original_priority'] ]['record_id'];
-
- $record = $template[ $form_state['storage']['original_priority'] ];
- $record['record_id'] = $form_state['values']['record_name'];
- $record['mode'] = $form_state['values']['mode'];
- $record['table'] = $form_state['values']['chado_table'];
- $record['select_if_duplicate'] = $form_state['values']['select_if_duplicate'];
- $record['update_if_duplicate'] = $form_state['values']['update_if_duplicate'];
- $record['select_optional'] = $form_state['values']['select_optional'];
- $record['disable'] = $form_state['values']['disable'];
- $record['optional'] = $form_state['values']['optional'];
- if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
- $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
- $template[ $form_state['values']['field_group'] ] = $record;
- unset($template[ $form_state['storage']['original_priority'] ]);
- }
- else {
- $template[ $form_state['storage']['original_priority'] ] = $record;
- }
-
-
- foreach ($template as $priority => $record) {
- foreach ($record['fields'] as $field_index => $field) {
- if ($field['type'] === 'foreign key') {
-
- if ($field['foreign key'] === $original_record_name) {
- $template[$priority]['fields'][$field_index]['foreign key'] = $form_state['values']['record_name'];
- }
- }
- }
- }
-
- $form_state['storage']['template']->template_array = serialize($template);
- $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
- if ($success) {
- drupal_set_message(t('Successfully Updated Template Record'));
- drupal_set_message(t('Template Saved.'));
- $path = explode('?', $form_state['storage']['referring URL']);
- parse_str($path[1], $query);
- $query['template_id'] = $form_state['storage']['template']->template_id;
- drupal_goto($path[0], $query);
- }
- else {
- drupal_set_message(t('Unable to Save Template!'), 'error');
- watchdog('T_bulk_loader',
- 'Unable to save bulk loader template: %template',
- array('%template' => print_r($form_state['storage']['template'], TRUE)),
- WATCHDOG_ERROR
- );
- }
- }
- elseif ($form_state['values']['op'] == 'Cancel') {
- $path = explode('?', $form_state['storage']['referring URL']);
- parse_str($path[1], $query);
- $query['template_id'] = $form_state['storage']['template']->template_id;
-
- drupal_goto($path[0], $query);
- }
- }
- }
- function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
- $form = array();
- $form['#cache'] = TRUE;
-
- $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
-
- if (!$template_id) {
- return $form;
- }
-
-
- if (!$form_state['storage']['template']) {
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $template = db_fetch_object(db_query($sql, $template_id));
- $form_state['storage']['template_array'] = unserialize($template->template_array);
- $form_state['storage']['template'] = $template;
- $form_state['storage']['record2priority'] = array();
- foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
- if (!is_array($record_array)) {
- continue;
- }
- $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
- }
- $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
- }
- else {
- $template = $form_state['storage']['template'];
- }
- $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
-
- $tables = tripal_core_get_chado_tables(TRUE);
- if ($form_state['values']) {
- if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
- $table = $form_state['values']['chado_table'];
- }
- elseif ($form_state['values']['record_name']) {
- $record_name = $form_state['values']['record_name'];
- $priority = $form_state['storage']['record2priority'][$record_name];
- $table = $form_state['storage']['template_array'][$priority]['table'];
- }
- else {
- $priority = $form_state['values']['field_group'];
- $table = $form_state['storage']['template_array'][$priority]['table'];
- }
- }
- if (!$table) {
- $table = reset($tables);
- }
-
- if ($_GET['record_id'] !== NULL) {
- $form_state['values']['field_group'] = $_GET['record_id'];
- if (preg_match('/\d+/', $_GET['record_id'])) {
- $priority = $form_state['values']['field_group'];
- $table = $form_state['storage']['template_array'][$priority]['table'];
- }
- }
- $show_all = $form_state['values']['show_all_records'];
- $table_description = tripal_core_get_chado_table_schema($table);
-
-
- $chado_fields = array();
- foreach ($table_description['fields'] as $field_name => $field_array) {
- $chado_fields[$field_name] = $field_name;
- }
- $fk_options = array();
- $fk_options['NULL'] = 'None';
- if ($field_type == 'foreign key' and !$show_all) {
- $foreign_field2table = array();
- foreach ($table_description['foreign keys'] as $key_table => $key_array) {
- foreach ($key_array['columns'] as $left_field => $right_field) {
-
- $foreign_field2table[$left_field] = $key_table;
- }
- }
-
- if (empty($chado_fields)) {
- $field = NULL;
- }
- elseif ($chado_fields[$form_state['values']['chado_field']]) {
- $field = $form_state['values']['chado_field'];
- }
- else {
- $field = current($chado_fields);
- }
-
- $foreign_table = $foreign_field2table[$field];
- if ($foreign_table) {
- foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
- if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
- $fk_options[$record_name] = $record_name;
- }
- }
- }
- }
-
- elseif ($field_type == 'foreign key' and $show_all) {
- foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
- $fk_options[$record_name] = $record_name;
- }
- }
-
-
-
- $ref_chado_fields = array();
- if ($field_type == 'foreign key') {
- $fk_rec = $form_state['values']['foreign_record'];
- if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
-
-
- $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
- $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
- foreach ($table_description['foreign keys'] as $key_table => $key_array) {
- foreach ($key_array['columns'] as $left_field => $right_field) {
- if ($key_table == $fk_table and $left_field == $field) {
- $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
- }
- }
- }
-
- $fk_description = tripal_core_get_chado_table_schema($fk_table);
- foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) {
-
- if (in_array('Foreign Key', $ref_chado_fields)) {
- if (in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
- continue;
- }
- }
- $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
- }
- }
- }
-
-
-
- switch ($field_type) {
- case 'table field':
-
- $form_state['values']['constant_value'] = NULL;
-
-
- break;
- case 'constant':
-
- $form_state['values']['column_number'] = NULL;
- $form_state['values']['column_exposed'] = NULL;
- $template_field['exposed'] = NULL;
- $form_state['values']['column_exposed_desc'] = NULL;
- $template_field['exposed_description'] = NULL;
-
-
- break;
- case 'foreign key':
-
- $form_state['values']['constant_value'] = NULL;
-
- $form_state['values']['column_number'] = NULL;
- $form_state['values']['column_exposed'] = NULL;
- $template_field['exposed'] = NULL;
- $form_state['values']['column_exposed_desc'] = NULL;
- $template_field['exposed_description'] = NULL;
- break;
- }
-
- $form['template_name'] = array(
- '#type' => 'item',
- '#title' => 'Template',
- '#value' => $template->name,
- );
- $form['template_id'] = array(
- '#type' => 'hidden',
- '#value' => $template_id,
- );
- $form['add_fields'] = array(
- '#type' => 'fieldset',
- '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
- '#suffix' => '</div>',
- );
- $form['add_fields']['field_type'] = array(
- '#type' => 'radios',
- '#title' => t('Type of Field'),
- '#options' => array(
- 'table field' => t('Data: A Field which maps to a column in the supplied file.'),
- 'constant' => t('Constant: Field which remains Constant throughout the file'),
- 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
- ),
- '#required' => TRUE,
- '#default_value' => $field_type,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- );
-
- if (!$form_state['storage']['record2priority']) {
- $groups = array();
- }
- else {
- $groups = array_flip($form_state['storage']['record2priority']);
- }
- $groups['NONE'] = 'Select a Record';
- $groups['NEW'] = 'New Record';
- $form['add_fields']['field_group'] = array(
- '#type' => 'select',
- '#title' => 'Record',
- '#description' => 'This is used to group a set of fields together allowing '
- .'multiple records to be inserted into the same table per line of the file',
- '#options' => $groups,
- '#default_value' => (preg_match('/\w+.*/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- '#required' => TRUE,
- );
- $form['add_fields']['record_name'] = array(
- '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
- '#title' => 'Unique Record Name',
- '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
- '#suffix' => '</div>',
- '#default_value' => $form_state['values']['record_name'],
- );
- $form['add_fields']['field_title'] = array(
- '#type' => 'textfield',
- '#title' => t('Human-readable Title for Field'),
- '#default_value' => $form_state['values']['field_title'],
- );
-
- $form['add_fields']['chado'] = array(
- '#type' => 'fieldset',
- '#title' => t('Chado Field/Column Details'),
- '#description' => t('Specify the Table/Field in chado that this field maps to.'),
- );
- $form['add_fields']['chado']['chado_table'] = array(
- '#type' => 'select',
- '#title' => t('Chado Table'),
- '#options' => $tables,
- '#default_value' => $table,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- );
- $form['add_fields']['chado']['chado_field'] = array(
- '#type' => 'select',
- '#title' => t('Chado Field/Column'),
- '#options' => $chado_fields,
- '#default_value' => $form_state['values']['chado_field'],
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- );
-
- $form['add_fields']['columns'] = array(
- '#type' => 'fieldset',
- '#title' => t('Data File Column'),
- '#collapsible' => TRUE,
- '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
- );
-
- $form['add_fields']['columns']['column_number'] = array(
- '#type' => 'textfield',
- '#title' => t('Column'),
- '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
- '#size' => 5,
- '#default_value' => $form_state['values']['column_number'],
- );
- $form['add_fields']['columns']['column_exposed'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow Column to be set for each Bulk Loading Job'),
- '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
- '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
- );
- $form['add_fields']['columns']['column_exposed_desc'] = array(
- '#type' => 'textfield',
- '#title' => t('Description for exposed field on bulk loading job'),
- '#description' => t('This description should tell the user what column should be entered here.'),
- '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
- );
-
- $form['add_fields']['constant'] = array(
- '#type' => 'fieldset',
- '#title' => t('Constant'),
- '#collapsible' => TRUE,
- '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
- );
- $form['add_fields']['constant']['constant_value'] = array(
- '#type' => 'textfield',
- '#title' => t('Constant Value'),
- '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
- '#default_value' => $form_state['values']['constant_value']
- );
- $form['add_fields']['constant']['constant_exposed'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
- '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.')
- );
- $form['add_fields']['constant']['constant_validate'] = array(
- '#type' => 'checkbox',
- '#title' => t('Ensure value is in table'),
- '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
- );
-
- $form['add_fields']['foreign_key'] = array(
- '#type' => 'fieldset',
- '#title' => 'Record Referral',
- '#collapsible' => TRUE,
- '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
- );
- $form['add_fields']['foreign_key']['show_all_records'] = array(
- '#type' => 'checkbox',
- '#title' => 'Refer to any record',
- '#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'),
- '#default_value' => $show_all,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- );
- $form['add_fields']['foreign_key']['foreign_record'] = array(
- '#type' => 'select',
- '#title' => 'Record to refer to',
- '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
- '#options' => $fk_options,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'],
- );
- $form['add_fields']['foreign_key']['foreign_field'] = array(
- '#type' => 'select',
- '#title' => 'Field to refer to',
- '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
- '#options' => $ref_chado_fields,
- '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign_field'],
- );
- $form['add_fields']['additional'] = array(
- '#type' => 'fieldset',
- '#title' => 'Additional Options',
- );
- $form['add_fields']['additional']['required'] = array(
- '#type' => 'checkbox',
- '#title' => 'Make this field required',
- '#default_value' => (!empty($form_state['values']['required'])) ? $form_state['values']['required'] : $template_field['required'],
- );
- $form['add_fields']['additional']['regex_transform'] = array(
- '#type' => 'fieldset',
- '#title' => 'Transform Data File Value Rules',
- '#collapsible' => TRUE,
- '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE,
- );
- $form['add_fields']['additional']['regex_transform']['regex_description'] = array(
- '#type' => 'item',
- '#value' => 'A transformation rule allows you to transform the original value '
- .'(usually from a user submitted data file) into the form you would like it stored '
- .'in the chado database. Each rule consists of a match pattern (a php regular expression '
- .'which determines which replacement patterns are applied and captures regions of the '
- .'original value) and a replacement pattern (a string which may contain capture references '
- .'that describes what the new value should be). Each rule is applied to the result of the '
- .'previous rule.'
- );
- $form['add_fields']['additional']['regex_transform']['regex-data'] = array(
- '#tree' => TRUE,
- );
- if (!$form_state['storage']['regex']['pattern']) {
- $form_state['storage']['regex']['pattern'] = array();
- }
- foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) {
- $data_element = array(
- 'pattern' => array(
- '#type' => 'item',
- '#value' => $pattern,
- ),
- 'replace' => array(
- '#type' => 'item',
- '#value' => $form_state['storage']['regex']['replace'][$index],
- ),
- 'old_index' => array(
- '#type' => 'hidden',
- '#value' => $index,
- ),
- 'new_index' => array(
- '#type' => 'select',
- '#options' => range(0, sizeof($form_state['storage']['regex']['pattern'])-1),
- '#default_value' => $index,
- ),
- 'id' => array(
- '#type' => 'hidden',
- '#value' => $index,
- ),
- 'submit-delete' => array(
- '#type' => 'submit',
- '#value' => 'Delete Transformation',
- '#name' => $index,
- ),
- );
- $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
- }
- $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
- '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden',
- '#value' => 'Save Transformation Rule Order'
- );
- $form['add_fields']['additional']['regex_transform']['new_regex'] = array(
- '#type' => 'fieldset',
- '#title' => 'Add a new Transformation Rule',
- );
- $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
- '#type' => 'textfield',
- '#title' => 'Match Pattern',
- '#description' => 'You can use standard php regular expressions in this field to specify a '
- .'pattern. Only if this pattern matches the value in the data file does the replacement '
- .'pattern get applied to the value. To capture a section of your value for use in the '
- .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
- .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
- .' To match and capture any value use <i>.*</i>',
- );
- $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
- '#type' => 'textfield',
- '#title' => 'Replacement Pattern',
- '#description' => 'This pattern should contain the text you want to replace the match pattern '
- .'mentioned above. It can include references of the form \n where n is the number of the '
- .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
- .'first set of round brackets.',
- );
- if ($field_type == 'table field') {
- $tab = '        ';
- $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
- .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
- .'This allows you to substitute other data file values into the current field. For example, '
- .'if you had the following line:<br />'
- . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
- .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
- .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
- .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
- .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
- }
- $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
- '#type' => 'submit',
- '#value' => 'Add Transformation',
- );
- $form['add_fields']['additional']['regex_transform']['test_regex'] = array(
- '#type' => 'fieldset',
- '#title' => 'Test Transformation Rules',
- );
- $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
- '#type' => 'textfield',
- '#title' => 'Test Value',
- '#description' => 'This should be a value that you expect the above transformation rules '
- .'to be applied to.',
- '#default_value' => $form_state['storage']['test_regex_test'],
- );
- $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
- '#type' => 'textfield',
- '#title' => 'Test Result',
- '#description' => 'This is the value that would be saved to the database after the above transformation '
- .'riles were applied to the Test Value.',
- '#default_value' => $form_state['storage']['test_regex_result'],
- );
- $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
- '#type' => 'submit',
- '#value' => 'Test Transformation Rules'
- );
- $form['add_fields']['submit-add_field'] = array(
- '#type' => 'submit',
- '#value' => 'Save Changes'
- );
- $form['add_fields']['submit-cancel'] = array(
- '#type' => 'submit',
- '#value' => 'Cancel'
- );
- return $form;
- }
- function tripal_bulk_loader_add_template_field_form_validate($form, $form_state) {
-
- if ($form_state['clicked_button']['#value'] == 'Save Changes') {
- $is_unique = tripal_bulk_loader_is_record_name_unique(
- $form_state['values']['record_name'],
- $form_state['values']['template_id'],
- $form_state['storage']['template_array']
- );
- $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
- if ((!$is_unique) AND $new_record) {
- form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
- }
- }
- }
- function tripal_bulk_loader_add_template_field_form_submit($form, &$form_state) {
- $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
- if (!$form_state['ahah_submission']) {
- if ($op == 'Save Changes') {
- $template = $form_state['storage']['template_array'];
-
- if (preg_match('/NEW/', $form_state['values']['field_group'])) {
- $record_name = $form_state['values']['record_name'];
- $priority = sizeof($form_state['storage']['template_array']) + 1;
- $record2priority[$record_name] = $priority;
- $template[$priority]['table'] = $form_state['values']['chado_table'];
- $template[$priority]['record_id'] = $record_name;
- }
- else {
- $priority = $form_state['values']['field_group'];
- $record_name = $record2priority[$priority];
- }
-
- if ($form_state['values']['field_type'] == 'table field') {
- $field = array(
- 'type' => 'table field',
- 'title' => $form_state['values']['field_title'],
- 'field' => $form_state['values']['chado_field'],
- 'required' => $form_state['values']['required'],
-
- 'spreadsheet column' => $form_state['values']['column_number'],
- 'exposed' => $form_state['values']['column_exposed'],
- 'exposed_description' => $form_state['values']['column_exposed_desc'],
- );
- }
- elseif ($form_state['values']['field_type'] == 'constant') {
- $field = array(
- 'type' => 'constant',
- 'title' => $form_state['values']['field_title'],
- 'field' => $form_state['values']['chado_field'],
- 'required' => $form_state['values']['required'],
-
- 'constant value' => $form_state['values']['constant_value'],
- 'exposed' => $form_state['values']['constant_exposed'],
- 'exposed_validate' => $form_state['values']['constant_validate'],
- );
- }
- elseif ($form_state['values']['field_type'] == 'foreign key') {
- $field = array(
- 'type' => 'foreign key',
- 'title' => $form_state['values']['field_title'],
- 'field' => $form_state['values']['chado_field'],
- 'show_all_records' => $form_state['values']['show_all_records'],
- 'foreign key' => $form_state['values']['foreign_record'],
- 'foreign field' => $form_state['values']['foreign_field'],
- 'required' => $form_state['values']['required'],
- );
- }
-
- if ($form_state['storage']['regex']) {
- $field['regex'] = $form_state['storage']['regex'];
- }
-
- $template[$priority]['fields'][] = $field;
- $form_state['storage']['template']->template_array = serialize($template);
- $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
- if ($success) {
- drupal_set_message(t('Successfully Added Field to Template'));
- drupal_set_message(t('Template Saved.'));
- $path = explode('?', $form_state['storage']['referring URL']);
- parse_str($path[1], $query);
- $query['template_id'] = $form_state['storage']['template']->template_id;
- drupal_goto($path[0], $query);
- }
- else {
- drupal_set_message(t('Unable to Save Template!'), 'error');
- watchdog('T_bulk_loader',
- 'Unable to save bulk loader template: %template',
- array('%template' => print_r($form_state['storage']['template'], TRUE)),
- WATCHDOG_ERROR
- );
- }
- }
- elseif ($op == 'Cancel') {
- $path = explode('?', $form_state['storage']['referring URL']);
- parse_str($path[1], $query);
- $query['template_id'] = $form_state['storage']['template']->template_id;
- drupal_goto($path[0], $query);
- }
- elseif ($op == 'Add Transformation') {
-
- $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
- $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
- drupal_set_message(t('Successfully Added Transformation Rule'));
- }
- elseif ($op == 'Save Transformation Rule Order') {
-
- $new_regex = array();
- $old_regex = $form_state['storage']['regex'];
- foreach ($form_state['values']['regex-data'] as $key => $element) {
- $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
- $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
- }
-
- ksort($new_regex['pattern']);
- ksort($new_regex['replace']);
- $form_state['storage']['regex'] = $new_regex;
- }
- elseif ($op == 'Delete Transformation') {
-
- $index = $form_state['clicked_button']['#name'];
- unset($form_state['storage']['regex']['pattern'][$index]);
- unset($form_state['storage']['regex']['replace'][$index]);
- }
- elseif ($op == 'Test Transformation Rules') {
- $patterns = $form_state['storage']['regex']['pattern'];
- $replaces = $form_state['storage']['regex']['replace'];
- $test_string = $form_state['values']['test_string'];
- $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
- $form_state['storage']['test_regex_test'] = $test_string;
- }
- }
- }
- function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
- $form = array();
- $form['#cache'] = TRUE;
-
- $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
-
- if (!$template_id) {
- return $form;
- }
-
-
- if (!$form_state['storage']['template']) {
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $template = db_fetch_object(db_query($sql, $template_id));
- $form_state['storage']['template_array'] = unserialize($template->template_array);
- $form_state['storage']['template'] = $template;
- $form_state['storage']['record2priority'] = array();
- foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
- if (!is_array($record_array)) {
- continue;
- }
- $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
- }
- $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
- }
- else {
- $template = $form_state['storage']['template'];
- }
-
- if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
- $priority = $_GET['record_id'];
- $field_index = $_GET['field_index'];
- $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
- $form_state['storage']['original_field'] = $template_field;
- $form_state['storage']['original_field']['priority'] = $priority;
- $form_state['storage']['original_field']['field_index'] = $field_index;
- }
-
- elseif (!empty($form_state['storage']['original_field']['priority']) && !empty($form_state['storage']['original_field']['field_index'])) {
- $priority = $form_state['storage']['original_field']['priority'];
- $field_index = $form_state['storage']['original_field']['field_index'];
- $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
- }
- $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
-
- $tables = tripal_core_get_chado_tables(TRUE);
- if ($form_state['values']) {
- $table = $form_state['values']['chado_table'];
- }
- else {
- $table = $form_state['storage']['template_array'][$priority]['table'];
- }
- $show_all = ($form_state['values']['show_all_records'] ? $form_state['values']['show_all_records'] : $template_field['show_all_records']);
- $table_description = tripal_core_get_chado_table_schema($table);
-
-
- $chado_fields = array();
- foreach ($table_description['fields'] as $field_name => $field_array) {
- $chado_fields[$field_name] = $field_name;
- }
- $ref_chado_fields = array();
- $fk_options = array();
- $fk_options['NULL'] = 'None';
- if ($field_type == 'foreign key' and !$show_all) {
- $foreign_field2table = array();
- foreach ($table_description['foreign keys'] as $key_table => $key_array) {
- foreach ($key_array['columns'] as $left_field => $right_field) {
-
- $foreign_field2table[$left_field] = $key_table;
- }
- }
-
- if (empty($chado_fields)) {
- $field = NULL;
- }
- elseif ($chado_fields[$form_state['values']['chado_field']]) {
- $field = $form_state['values']['chado_field'];
- }
- elseif ($template_field['field']) {
- $field = $template_field['field'];
- }
- else {
- $field = current($chado_fields);
- }
-
- $foreign_table = $foreign_field2table[$field];
- if ($foreign_table) {
- foreach ($form_state['storage']['record2priority'] as $record_name_ => $priority_ ) {
- if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority_]['table'])) {
- $fk_options[$record_name_] = $record_name_;
- }
- }
- }
- }
-
- elseif ($field_type == 'foreign key' and $show_all) {
- foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
- $fk_options[$record_name] = $record_name;
- }
- }
-
- if ($field_type == 'foreign key') {
- $fk_rec = $form_state['values']['foreign_record'] ? $form_state['values']['foreign_record'] : $template_field['foreign key'];
- if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
-
- $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
- $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
- foreach ($table_description['foreign keys'] as $key_table => $key_array) {
- foreach ($key_array['columns'] as $left_field => $right_field) {
- if ($key_table == $fk_table and $left_field == $field) {
- $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
- }
- }
- }
- $fk_description = tripal_core_get_chado_table_schema($fk_table);
- foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) {
-
- if (in_array('Foreign Key', $ref_chado_fields) and
- in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
- continue;
- }
- $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
- }
- }
- }
-
-
-
- switch ($field_type) {
- case 'table field':
-
- $form_state['values']['constant_value'] = NULL;
- $template_field['constant value'] = NULL;
- $form_state['values']['constant_exposed'] = NULL;
- $template_field['exposed'] = NULL;
- $form_state['values']['constant_validate'] = NULL;
- $template_field['exposed_validate'] = NULL;
-
-
- break;
- case 'constant':
-
- $form_state['values']['column_number'] = NULL;
- $form_state['values']['column_exposed'] = NULL;
- $template_field['exposed'] = NULL;
- $form_state['values']['column_exposed_desc'] = NULL;
- $template_field['exposed_description'] = NULL;
-
-
- break;
- case 'foreign key':
-
- $form_state['values']['constant_value'] = NULL;
- $template_field['constant value'] = NULL;
- $form_state['values']['constant_exposed'] = NULL;
- $template_field['exposed'] = NULL;
- $form_state['values']['constant_validate'] = NULL;
- $template_field['exposed_validate'] = NULL;
-
- $form_state['values']['column_number'] = NULL;
- $form_state['values']['column_exposed'] = NULL;
- $template_field['exposed'] = NULL;
- $form_state['values']['column_exposed_desc'] = NULL;
- $template_field['exposed_description'] = NULL;
- break;
- }
-
- $form['template_name'] = array(
- '#type' => 'item',
- '#title' => 'Template',
- '#value' => $template->name,
- );
- $form['template_id'] = array(
- '#type' => 'hidden',
- '#value' => $template_id,
- );
- $form['edit_fields'] = array(
- '#type' => 'fieldset',
- '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
- '#suffix' => '</div>',
- );
- $form['edit_fields']['field_type'] = array(
- '#type' => 'radios',
- '#title' => t('Type of Field'),
- '#options' => array(
- 'table field' => t('Data Field: Fields which maps to a data file column'),
- 'constant' => t('Constant: Field which remains Constant throughout the data file'),
- 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
- ),
- '#required' => TRUE,
- '#default_value' => $field_type,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-edit_field',
- 'effect' => 'fade'
- ),
- );
-
- if (!$form_state['storage']['record2priority']) {
- $groups = array();
- }
- else {
- $groups = array_flip($form_state['storage']['record2priority']);
- }
- $groups['NONE'] = 'Select a Record';
- $groups['NEW'] = 'New Record';
- $form['edit_fields']['field_group'] = array(
- '#type' => 'select',
- '#title' => 'Record',
- '#description' => 'This is used to group a set of fields together allowing '
- .'multiple records to be inserted into the same table per line of the data file',
- '#options' => $groups,
- '#default_value' => (preg_match('/(\d+|\w+)/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-edit_field',
- 'effect' => 'fade'
- ),
- '#required' => TRUE,
- );
- $form['edit_fields']['record_name'] = array(
- '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
- '#title' => 'Unique Record Name',
- '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
- '#suffix' => '</div>',
- '#default_value' => $form_state['values']['record_name'],
- );
- $form['edit_fields']['field_title'] = array(
- '#type' => 'textfield',
- '#title' => t('Human-readable Title for Field'),
- '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
- );
-
- $form['edit_fields']['chado'] = array(
- '#type' => 'fieldset',
- '#title' => t('Chado Field/Column Details'),
- '#description' => t('Specify the Table/Field in chado that this field maps to.'),
- );
- $form['edit_fields']['chado']['chado_table'] = array(
- '#type' => 'select',
- '#title' => t('Chado Table'),
- '#options' => $tables,
- '#default_value' => $table,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-edit_field',
- 'effect' => 'fade'
- ),
- );
- $form['edit_fields']['chado']['chado_field'] = array(
- '#type' => 'select',
- '#title' => t('Chado Field/Column'),
- '#options' => $chado_fields,
- '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-edit_field',
- 'effect' => 'fade'
- ),
- );
-
- $form['edit_fields']['columns'] = array(
- '#type' => 'fieldset',
- '#title' => t('Data File Column'),
- '#collapsible' => TRUE,
- '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
- );
-
- $form['edit_fields']['columns']['column_number'] = array(
- '#type' => 'textfield',
- '#title' => t('Column'),
- '#description' => t('Specify the column in the data file that this field maps to where the first column is 1.'),
- '#size' => 5,
- '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
- );
- $form['edit_fields']['columns']['column_exposed'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow Column to be set for each Bulk Loading Job'),
- '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
- '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
- );
- $form['edit_fields']['columns']['column_exposed_desc'] = array(
- '#type' => 'textfield',
- '#title' => t('Description for exposed field on bulk loading job'),
- '#description' => t('This description should tell the user what column should be entered here.'),
- '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
- );
-
- $form['edit_fields']['constant'] = array(
- '#type' => 'fieldset',
- '#title' => t('Constant'),
- '#collapsible' => TRUE,
- '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
- );
- $form['edit_fields']['constant']['constant_value'] = array(
- '#type' => 'textfield',
- '#title' => t('Constant Value'),
- '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
- '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
- );
- $form['edit_fields']['constant']['constant_exposed'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
- '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
- '#default_value' => ($form_state['values']['constant_exposed']) ? $form_state['values']['constant_exposed'] : $template_field['exposed'],
- );
- $form['edit_fields']['constant']['constant_validate'] = array(
- '#type' => 'checkbox',
- '#title' => t('Ensure value is in table'),
- '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
- '#default_value' => ($form_state['values']['constant_validate']) ? $form_state['values']['constant_validate'] : $template_field['exposed_validate'],
- );
-
- $form['edit_fields']['foreign_key'] = array(
- '#type' => 'fieldset',
- '#title' => 'Record Referral',
- '#collapsible' => TRUE,
- '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
- );
- $form['edit_fields']['foreign_key']['show_all_records'] = array(
- '#type' => 'checkbox',
- '#title' => 'Refer to any record',
- '#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'),
- '#default_value' => $show_all,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- );
- $form['edit_fields']['foreign_key']['foreign_record'] = array(
- '#type' => 'select',
- '#title' => 'Record to refer to',
- '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
- '#options' => $fk_options,
- '#ahah' => array(
- 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
- 'wrapper' => 'tripal_bulk_loader_template-add_field',
- 'effect' => 'fade'
- ),
- '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'],
- );
- $form['edit_fields']['foreign_key']['foreign_field'] = array(
- '#type' => 'select',
- '#title' => 'Field to refer to',
- '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
- '#options' => $ref_chado_fields,
- '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign field'],
- );
- $form['edit_fields']['additional'] = array(
- '#type' => 'fieldset',
- '#title' => 'Additional Options',
- );
- $form['edit_fields']['additional']['required'] = array(
- '#type' => 'checkbox',
- '#title' => 'Make this field required',
- '#default_value' => (!empty($form_state['values']['required'])) ? $form_state['values']['required'] : $template_field['required'],
- );
- $form['edit_fields']['additional']['regex_transform'] = array(
- '#type' => 'fieldset',
- '#title' => 'Transform Data File Value Rules',
- '#collapsible' => TRUE,
- '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE,
- );
- $transformation_msg = '<p>A transformation rule allows you to transform the original value '
- .'(usually from a user submitted data file) into the form you would like it stored '
- .'in the chado database. Each rule consists of a match pattern (a php regular expression '
- .'which determines which replacement patterns are applied and captures regions of the '
- .'original value) and a replacement pattern (a string which may contain capture references '
- .'that describes what the new value should be). Each rule is applied to the result of the '
- .'previous rule.<p>';
- $form['edit_fields']['additional']['regex_transform']['regex_description'] = array(
- '#type' => 'item',
- '#value' => $transformation_msg,
- );
- $form['edit_fields']['additional']['regex_transform']['regex-data'] = array(
- '#tree' => TRUE,
- );
- if (!is_array($template_field['regex']['pattern'])) {
- $template_field['regex']['pattern'] = array();
- }
- $key_options = array();
- foreach ($template_field['regex']['pattern'] as $k => $v) {
- $key_options[$k] = $k;
- }
- foreach ($template_field['regex']['pattern'] as $index => $pattern) {
- $data_element = array(
- 'pattern' => array(
- '#type' => 'item',
- '#value' => $pattern,
- ),
- 'replace' => array(
- '#type' => 'item',
- '#value' => $template_field['regex']['replace'][$index],
- ),
- 'old_index' => array(
- '#type' => 'hidden',
- '#value' => $index,
- ),
- 'new_index' => array(
- '#type' => 'select',
- '#options' => $key_options,
- '#default_value' => $index,
- ),
- 'id' => array(
- '#type' => 'hidden',
- '#value' => $index,
- ),
- 'submit-delete' => array(
- '#type' => 'submit',
- '#value' => 'Delete Transformation',
- '#name' => $index,
- ),
- );
- $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
- }
- $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
- '#type' => 'submit',
- '#value' => 'Save Transformation Rule Order'
- );
- $form['edit_fields']['additional']['regex_transform']['new_regex'] = array(
- '#type' => 'fieldset',
- '#title' => 'Add a new Transformation Rule',
- );
- $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
- '#type' => 'textfield',
- '#title' => 'Match Pattern',
- '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a '
- .'pattern. Only if this pattern matches the value in the data file does the replacement '
- .'pattern get applied to the value. To capture a section of your value for use in the '
- .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
- .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
- .' To match and capture any value use <i>.*</i>',
- );
- $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
- '#type' => 'textfield',
- '#title' => 'Replacement Pattern',
- '#description' => '<p>This pattern should contain the text you want to replace the match pattern '
- .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the '
- .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
- .'first set of round brackets.</p>',
- );
- if ($field_type == 'table field') {
- $tab = '        ';
- $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
- .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
- .'This allows you to substitute other data file values into the current field. For example, '
- .'if you had the following line:<br />'
- . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
- .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
- .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
- .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
- .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
- }
- $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
- '#type' => 'submit',
- '#value' => 'Add Transformation',
- );
- $form['edit_fields']['additional']['regex_transform']['test_regex'] = array(
- '#type' => 'fieldset',
- '#title' => 'Test Transformation Rules',
- );
- $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
- '#type' => 'textfield',
- '#title' => 'Test Value',
- '#description' => 'This should be a value that you expect the above transformation rules '
- .'to be applied to.',
- '#default_value' => $form_state['storage']['test_regex_test'],
- );
- $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
- '#type' => 'textfield',
- '#title' => 'Test Result',
- '#description' => 'This is the value that would be saved to the database after the above transformation '
- .'riles were applied to the Test Value.',
- '#default_value' => $form_state['storage']['test_regex_result'],
- );
- $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
- '#type' => 'submit',
- '#value' => 'Test Transformation Rules'
- );
- $form['edit_fields']['submit-edit_field'] = array(
- '#type' => 'submit',
- '#value' => 'Save Changes'
- );
- $form['edit_fields']['submit-cancel'] = array(
- '#type' => 'submit',
- '#value' => 'Cancel'
- );
- return $form;
- }
- function tripal_bulk_loader_edit_template_field_form_validate($form, $form_state) {
-
- if ($form_state['clicked_button']['#value'] == 'Save Changes') {
- $is_unique = tripal_bulk_loader_is_record_name_unique(
- $form_state['values']['record_name'],
- $form_state['values']['template_id'],
- $form_state['storage']['template_array']
- );
- $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
- if ((!$is_unique) AND $new_record) {
- form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
- }
- }
- }
- function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state) {
- $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
-
- $form_state['storage']['test_regex_result'] = NULL;
- $form_state['storage']['test_regex_test'] = NULL;
- if (!$form_state['ahah_submission']) {
- if ($op == 'Save Changes') {
-
- if (preg_match('/NEW/', $form_state['values']['field_group'])) {
-
- $record_name = $form_state['values']['record_name'];
- $priority = sizeof($form_state['storage']['template_array']) + 1;
- $old_priority = $form_state['storage']['original_field']['priority'];
- $field_index = $form_state['storage']['original_field']['field_index'];
- $form_state['storage']['record2priority'][$record_name] = $priority;
- $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
- $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
- }
- else {
- $priority = $form_state['values']['field_group'];
- $old_priority = $form_state['storage']['original_field']['priority'];
- $field_index = $form_state['storage']['original_field']['field_index'];
- $record_name = $form_state['storage']['record2priority'][$priority];
- }
- $field = $form_state['storage']['original_field'];
- if ($form_state['values']['field_type'] == 'table field') {
- $field['type'] = 'table field';
- $field['title'] = $form_state['values']['field_title'];
- $field['field'] = $form_state['values']['chado_field'];
- $field['required'] = $form_state['values']['required'];
-
- $field['spreadsheet column'] = $form_state['values']['column_number'];
- $field['exposed'] = $form_state['values']['column_exposed'];
- $field['exposed_description'] = $form_state['values']['column_exposed_desc'];
- }
- elseif ($form_state['values']['field_type'] == 'constant') {
- $field['type'] = 'constant';
- $field['title'] = $form_state['values']['field_title'];
- $field['field'] = $form_state['values']['chado_field'];
- $field['required'] = $form_state['values']['required'];
-
- $field['constant value'] = $form_state['values']['constant_value'];
- $field['exposed_validate'] = $form_state['values']['constant_validate'];
- $field['exposed'] = $form_state['values']['constant_exposed'];
- }
- elseif ($form_state['values']['field_type'] == 'foreign key') {
- $field['type'] = 'foreign key';
- $field['title'] = $form_state['values']['field_title'];
- $field['field'] = $form_state['values']['chado_field'];
- $field['show_all_records'] = $form_state['values']['show_all_records'];
- $field['foreign key'] = $form_state['values']['foreign_record'];
- $field['foreign field'] = $form_state['values']['foreign_field'];
- $field['required'] = $form_state['values']['required'];
- }
-
-
- $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
- if ($old_priority != $priority) {
- $form_state['storage']['template_array'][$priority]['fields'][] = $field;
- unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
-
- if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
- unset($form_state['storage']['template_array'][$old_priority]);
- }
- }
- else {
- $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
- }
-
- $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
- $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
- if ($success) {
- drupal_set_message(t('Successfully Updated Field'));
- drupal_set_message(t('Template Saved.'));
- $path = explode('?', $form_state['storage']['referring URL']);
- parse_str($path[1], $query);
- $query['template_id'] = $form_state['storage']['template']->template_id;
- drupal_goto($path[0], $query);
- }
- else {
- drupal_set_message(t('Unable to Save Template!'), 'error');
- watchdog('T_bulk_loader',
- 'Unable to save bulk loader template: %template',
- array('%template' => print_r($form_state['storage']['template'], TRUE)),
- WATCHDOG_ERROR
- );
- }
- }
- elseif ($op == 'Cancel') {
- $path = explode('?', $form_state['storage']['referring URL']);
- parse_str($path[1], $query);
- $query['template_id'] = $form_state['storage']['template']->template_id;
- drupal_goto($path[0], $query);
- }
- elseif ($op == 'Add Transformation') {
-
- $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
- $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace'];
-
- $priority = $form_state['storage']['original_field']['priority'];
- $field_index = $form_state['storage']['original_field']['field_index'];
- $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
- }
- elseif ($op == 'Save Transformation Rule Order') {
-
- $new_regex = array();
- $old_regex = $form_state['storage']['original_field']['regex'];
- foreach ($form_state['values']['regex-data'] as $key => $element) {
- $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
- $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
- }
-
- ksort($new_regex['pattern']);
- ksort($new_regex['replace']);
-
- $form_state['storage']['original_field']['regex'] = $new_regex;
- $priority = $form_state['storage']['original_field']['priority'];
- $field_index = $form_state['storage']['original_field']['field_index'];
- $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
-
- }
- elseif ($op == 'Delete Transformation') {
-
- $index = $form_state['clicked_button']['#name'];
- unset($form_state['storage']['original_field']['regex']['pattern'][$index]);
- unset($form_state['storage']['original_field']['regex']['replace'][$index]);
- $priority = $form_state['storage']['original_field']['priority'];
- $field_index = $form_state['storage']['original_field']['field_index'];
- $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
-
- }
- elseif ($op == 'Test Transformation Rules') {
- $patterns = $form_state['storage']['original_field']['regex']['pattern'];
- $replaces = $form_state['storage']['original_field']['regex']['replace'];
- $test_string = $form_state['values']['test_string'];
- $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
- $form_state['storage']['test_regex_test'] = $test_string;
- }
- }
- }
- function tripal_bulk_loader_add_field_ahah() {
- $form_state = array('storage' => NULL, 'submitted' => FALSE);
- $form_build_id = filter_xss($_POST['form_build_id']);
- $form = form_get_cache($form_build_id, $form_state);
- $args = $form['#parameters'];
- $form_id = array_shift($args);
- $form_state['post'] = $form['#post'] = $_POST;
-
- $form_state['ahah_submission'] = TRUE;
- $form['#programmed'] = $form['#redirect'] = FALSE;
- drupal_process_form($form_id, $form, $form_state);
- $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
- $form_element = $form['add_fields'];
-
-
- $output = theme('status_messages');
- $output .= drupal_render($form_element);
-
- print drupal_json(array('status' => TRUE, 'data' => $output));
- exit();
- }
- function tripal_bulk_loader_edit_field_ahah() {
- $form_state = array('storage' => NULL, 'submitted' => FALSE);
- $form_build_id = filter_xss($_POST['form_build_id']);
- $form = form_get_cache($form_build_id, $form_state);
- $args = $form['#parameters'];
- $form_id = array_shift($args);
- $form_state['post'] = $form['#post'] = $_POST;
-
- $form_state['ahah_submission'] = TRUE;
- $form['#programmed'] = $form['#redirect'] = FALSE;
- drupal_process_form($form_id, $form, $form_state);
- $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
- $form_element = $form['edit_fields'];
-
- unset($form_element['#prefix'], $form_element['#suffix']);
- $output = theme('status_messages');
- $output .= drupal_render($form_element);
-
- print drupal_json(array('status' => TRUE, 'data' => $output));
- exit();
- }
|