tripal_bulk_loader.admin.inc 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. <?php
  2. /**
  3. * @file
  4. * Handles Create/Edit/Delete Template Admin Forms
  5. */
  6. /**
  7. * Provides a description page and quick links for the bulk loader
  8. */
  9. function tripal_bulk_loader_admin_template () {
  10. $output = '';
  11. $output .= '<br><h3>Quick Links:</h3>';
  12. $output .= l('Create a new bulk loader template', 'admin/tripal/tripal_bulk_loader_template/create')."<br>";
  13. $output .= l('Edit a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/edit')."<br>";
  14. $output .= l('Delete a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/delete')."<br>";
  15. $output .= l('Export a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/export')."<br>";
  16. $output .= l('Import a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/import')."<br>";
  17. $output .= '<br>';
  18. $output .= '<h3>Module Description:</h3>';
  19. $output .= '<p>This module provides the ability to create loading templates for any tab-delimited '
  20. .'data file allowing it to be loaded into chado. The Loading Templates are a direct mapping '
  21. .'between the columns in your file and the columns in chado tables. As such to use this tool '
  22. .'you need to be very familar with the chado schema -See '
  23. .l('Chado -Getting Started', 'http://gmod.org/wiki/Chado_-_Getting_Started')
  24. .'. The ability to add constants and specify foreign key contraints is also provided '
  25. .'in order for the loader to fill chado columns which may be required but are not specified '
  26. .'in your input file.</p>';
  27. $output .= '<br>';
  28. $output .= '<h3>Setup Instructions</h3>';
  29. $output .= '<p>After intallation of the bulk loader module, the following tasks should be performed:</p>';
  30. $output .= '<ol>';
  31. $output .= '<li><b>Install Theme:</b> In order for Bulk Loading pages to be displayed correctly, '
  32. .'the contents of the Tripal Bulk Loader theme directory ([drupal root]/sites/all/modules/tripal/tripal_bulk_loader/theme) '
  33. .'should be moved to the base directory of the Tripal theme ([drupal root]/sites/all/themes/tripal). '
  34. .'Finally the drupal cache should be cleared for the new theme to take effect -navigate to admin/settings/performance '
  35. .'and click the Clear Cached Data button.</li>';
  36. $output .= '</ol>';
  37. return $output;
  38. }
  39. //////////////////////////////////////////////////////////////////////////////////////
  40. // Modify Template
  41. //////////////////////////////////////////////////////////////////////////////////////
  42. /**
  43. * The main form reached at admin/tripal/tripal_bulk_loader/create and /edit
  44. */
  45. function tripal_bulk_loader_modify_template_base_form ($form_state = NULL, $mode){
  46. $form = array();
  47. // get template id from path and rebuild form
  48. if ($_GET['template_id']) {
  49. if (preg_match('/^\d+$/', $_GET['template_id'])) {
  50. $form_state['storage']['template_id'] = $_GET['template_id'];
  51. }
  52. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  53. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  54. $form_state['storage']['template'] = unserialize($result->template_array);
  55. $form_state['storage']['template_name'] = $result->name;
  56. $form_state['storage']['record2priority'] = array();
  57. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  58. if (!is_array($record_array)) { continue; }
  59. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  60. }
  61. }
  62. $form['mode'] = array(
  63. '#type' => 'hidden',
  64. '#value' => $mode,
  65. );
  66. if ($form_state['storage']['template_id']) {
  67. $form['template_name'] = array(
  68. '#type' => 'item',
  69. '#title' => 'Template',
  70. '#value' => $form_state['storage']['template_name'],
  71. '#weight' => 1,
  72. );
  73. } else {
  74. if (preg_match('/create/', $mode)) {
  75. $form['new_template_name'] = array(
  76. '#type' => 'textfield',
  77. '#title' => 'Template Name',
  78. '#weight' => 1,
  79. );
  80. } elseif (preg_match('/edit/', $mode)) {
  81. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  82. $resource = db_query($sql);
  83. $templates = array();
  84. $templates[''] = 'Select a Template';
  85. while ($r = db_fetch_object($resource)) {
  86. $templates[$r->template_id] = $r->name;
  87. }
  88. $form['template_id'] = array(
  89. '#title' => t('Template'),
  90. '#description' => t('Please select the template you would like to edit.'),
  91. '#type' => 'select',
  92. '#options' => $templates,
  93. '#default_value' => $form_state['storage']['template_id'],
  94. '#weight' => 0,
  95. '#required' => TRUE,
  96. '#weight' => 1,
  97. );
  98. }
  99. }
  100. $form['records'] = array(
  101. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  102. '#title' => t('Current Records'),
  103. '#collapsible' => TRUE,
  104. '#weight' => 2,
  105. );
  106. $form['records']['description'] = array(
  107. '#type' => 'item',
  108. '#value' => 'Records will be inserted into the chado database in the order listed below. To '
  109. .'change this order: <ul><li>Drag the rows into the correct order OR</li><li>Enter '
  110. .'the numbers 1 and up in the Order textboxes to indicate the correct order.</li></ul>',
  111. );
  112. $form['records']['records-data'] = array(
  113. '#tree' => TRUE,
  114. );
  115. $form['records']['no_records'] = array(
  116. '#type' => 'hidden',
  117. '#value' => TRUE,
  118. );
  119. $form['records']['submit-new_record'] = array(
  120. '#type' => 'submit',
  121. '#value' => 'New Record/Field',
  122. );
  123. $form['records']['submit-reorder'] = array(
  124. '#type' => 'submit',
  125. '#value' => 'Save Order',
  126. );
  127. $form['fields'] = array(
  128. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  129. '#title' => t('Current Fields'),
  130. '#collapsible' => TRUE,
  131. '#weight' => 3,
  132. );
  133. $form['fields']['fields-data'] = array(
  134. '#tree' => TRUE,
  135. );
  136. if ($form_state['storage']['template']) {
  137. // List Current Fields -------------------------------------------------------------
  138. $i=1;
  139. foreach ($form_state['storage']['template'] as $priority => $table_array) {
  140. if (!is_array($table_array)) { continue; }
  141. $form['records']['no_records']['#value'] = FALSE;
  142. $form['records']['records-data'][$priority] = array(
  143. 'title' => array(
  144. '#type' => 'markup',
  145. '#value' => $table_array['record_id'],
  146. ),
  147. 'chado_table' => array(
  148. '#type' => 'markup',
  149. '#value' => $table_array['table'],
  150. ),
  151. 'mode' => array(
  152. '#type' => 'item',
  153. '#value' => ($table_array['mode']) ? $table_array['mode'] : 'insert_unique',
  154. ),
  155. 'new_priority' => array(
  156. '#type' => 'select',
  157. '#options' => range(1, sizeof($form_state['storage']['template'])),
  158. '#default_value' => $priority,
  159. ),
  160. 'old_priority' => array(
  161. '#type' => 'hidden',
  162. '#value' => $priority,
  163. ),
  164. 'id' => array(
  165. '#type' => 'hidden',
  166. '#value' => $priority,
  167. ),
  168. 'submit-edit_record' => array(
  169. '#type' => 'submit',
  170. '#name' => (string)$priority,
  171. '#value' => 'Edit Record',
  172. ),
  173. 'submit-add_field' => array(
  174. '#type' => 'submit',
  175. '#name' => (string)$priority,
  176. '#value' => 'Add Field',
  177. ),
  178. 'submit-duplicate_record' => array(
  179. '#type' => 'submit',
  180. '#name' => (string)$priority,
  181. '#value' => 'Duplicate Record'
  182. ),
  183. );
  184. foreach ($table_array['fields'] as $field_index => $field) {
  185. $form['fields']['fields-data'][$i] = array(
  186. 'record_id' => array(
  187. '#type' => 'item',
  188. '#value' => $table_array['record_id'],
  189. ),
  190. 'priority_hidden' => array(
  191. '#type' => 'hidden',
  192. '#value' => $priority,
  193. ),
  194. 'field_name' => array(
  195. '#type' => 'item',
  196. '#value' => $field['title'],
  197. ),
  198. 'chado_table_name' => array(
  199. '#type' => 'item',
  200. '#value' => $table_array['table'],
  201. ),
  202. 'chado_table_hidden' => array(
  203. '#type' => 'hidden',
  204. '#value' => $table_array['table'],
  205. ),
  206. 'chado_field_name' => array(
  207. '#type' => 'item',
  208. '#value' => $field['field'],
  209. ),
  210. 'sheet_name' => array(
  211. '#type' => 'item',
  212. '#value' => $field['spreadsheet sheet'],
  213. ),
  214. 'column_num' => array(
  215. '#type' => 'item',
  216. '#value' => $field['spreadsheet column'],
  217. ),
  218. 'constant_value' => array(
  219. '#type' => 'item',
  220. '#value' => $field['constant value'],
  221. ),
  222. 'field_index' => array(
  223. '#type' => 'hidden',
  224. '#value' => $field_index
  225. ),
  226. 'foreign_record_id' => array(
  227. '#type' => 'item',
  228. '#value' => $field['foreign key'],
  229. ),
  230. 'edit_submit' => array(
  231. '#type' => 'submit',
  232. '#name' => (string)$i,
  233. '#value' => "Edit Field",
  234. ),
  235. 'delete_submit' => array(
  236. '#type' => 'submit',
  237. '#name' => (string)$i,
  238. '#value' => "Delete Field",
  239. ),
  240. );
  241. $i++;
  242. }
  243. }
  244. $form['fields']['total_fields'] = array(
  245. '#type' => 'item',
  246. '#value' => $i,
  247. );
  248. }
  249. if ($form['records']['no_records']['#value']) {
  250. $form['records']['description'] = array(
  251. '#type' => 'item',
  252. '#value' => 'There are currently no records.',
  253. );
  254. unset($form['records']['submit-reorder']);
  255. $form['fields']['description'] = array(
  256. '#type' => 'item',
  257. '#value' => 'There are currently no fields.',
  258. );
  259. }
  260. $mode_title = (preg_match('/create/',$mode)) ? 'Create Template' : 'Edit Template';
  261. $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
  262. $form['submit'] = array(
  263. '#type' => 'submit',
  264. '#value' => $value,
  265. '#weight' => 4,
  266. );
  267. return $form;
  268. }
  269. /**
  270. * Submit for tripal_bulk_loader_modify_template_base_form
  271. */
  272. function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state){
  273. $form_state['rebuild'] = TRUE;
  274. if ($form_state['storage']['template_id']) {
  275. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  276. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  277. $form_state['storage']['template'] = unserialize($result->template_array);
  278. }
  279. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  280. switch ($op) {
  281. // Initialize after template is chosen ----------------------------------------
  282. case 'Edit Template':
  283. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  284. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  285. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  286. $form_state['storage']['template'] = unserialize($result->template_array);
  287. $form_state['storage']['template_name'] = $result->name;
  288. $form_state['storage']['record2priority'] = array();
  289. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  290. if (!is_array($record_array)) { continue; }
  291. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  292. }
  293. break;
  294. case 'Create Template':
  295. $record = array(
  296. 'name' => $form_state['values']['new_template_name'],
  297. 'template_array' => array(),
  298. );
  299. drupal_write_record('tripal_bulk_loader_template', $record);
  300. $form_state['storage']['template_id'] = $record['template_id'];
  301. $form_state['storage']['template_name'] = $record['name'];
  302. $form_state['storage']['template'] = array();
  303. break;
  304. // Save Reordered Records -----------------------------------------------------
  305. case 'Save Order':
  306. $new_template = $form_state['storage']['template'];
  307. // unset old elements
  308. $form_state['storage']['record2priority'] = array();
  309. foreach ($new_template as $priority => $record_array) {
  310. if (preg_match('/\d+/', $priority)) { unset($new_template[$priority]); }
  311. }
  312. //set elements in new order
  313. foreach ($form_state['values']['records-data'] as $item) {
  314. $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
  315. $record_name = $new_template[$item['new_priority']]['record_id'];
  316. $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
  317. }
  318. ksort($new_template);
  319. $form_state['storage']['template'] = $new_template;
  320. break;
  321. case 'New Record/Field':
  322. $query = array(
  323. 'template_id'=> $form_state['storage']['template_id'],
  324. 'record_id' => 'NEW',
  325. );
  326. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  327. break;
  328. case 'Edit Record':
  329. $query = array(
  330. 'template_id'=> $form_state['storage']['template_id'],
  331. 'record_id' => $form_state['clicked_button']['#name'],
  332. );
  333. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_record', $query);
  334. break;
  335. case 'Add Field':
  336. $query = array(
  337. 'template_id'=> $form_state['storage']['template_id'],
  338. 'record_id' => $form_state['clicked_button']['#name'],
  339. );
  340. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  341. break;
  342. case 'Duplicate Record':
  343. // original record (one to be duplicated)
  344. $orig_priority = $form_state['clicked_button']['#name'];
  345. $record = $form_state['storage']['template'][ $orig_priority ];
  346. // new record
  347. $new_priority = sizeof($form_state['storage']['template']) + 1;
  348. $record['record_id'] = $record['record_id'] . '_' . date('YzHi');
  349. $form_state['storage']['template'][ $new_priority ] = $record;
  350. break;
  351. case 'Edit Field':
  352. $field_data_index = $form_state['clicked_button']['#name'];
  353. $query = array(
  354. 'template_id'=> $form_state['storage']['template_id'],
  355. 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'],
  356. 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'],
  357. );
  358. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query);
  359. break;
  360. case 'Delete Field':
  361. $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']];
  362. $priority = $field_data['priority_hidden'];
  363. $field_key = $field_data['field_index'];
  364. unset($form_state['storage']['template'][$priority]['fields'][$field_key]);
  365. if (!$form_state['storage']['template'][$priority]['fields']) {
  366. unset($form_state['storage']['record2priority'][$form_state['storage']['template'][$priority]['record_id']]);
  367. unset($form_state['storage']['template'][$priority]);
  368. }
  369. drupal_set_message('Deleted Field from Template.');
  370. break;
  371. } //end of switch
  372. // Save Template
  373. $record = array(
  374. 'template_id' => $form_state['storage']['template_id'],
  375. 'template_array' => serialize($form_state['storage']['template'])
  376. );
  377. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  378. drupal_set_message('Template Saved.');
  379. }
  380. //////////////////////////////////////////////////////////////////////////////////////
  381. // Delete Template
  382. //////////////////////////////////////////////////////////////////////////////////////
  383. /**
  384. * Delete Template Form
  385. * This form allows admin to delete already existing templates
  386. */
  387. function tripal_bulk_loader_delete_template_base_form () {
  388. $form = array();
  389. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  390. $resource = db_query($sql);
  391. $templates = array();
  392. $templates[''] = 'Select a Template';
  393. while ($r = db_fetch_object($resource)) {
  394. $templates[$r->template_id] = $r->name;
  395. }
  396. $form['template_name'] = array(
  397. '#title' => t('Template'),
  398. '#description' => t('Please select the template you would like to delete.'),
  399. '#type' => 'select',
  400. '#options' => $templates,
  401. '#weight' => 0,
  402. '#required' => TRUE,
  403. );
  404. $form['submit'] = array(
  405. '#type' => 'submit',
  406. '#value' => 'Delete Template',
  407. );
  408. return $form;
  409. }
  410. /**
  411. * Delete Template Form Submit
  412. *
  413. * @param $form
  414. * The form that was submitted
  415. * @param $form_state
  416. * The values and storage that were submitted
  417. */
  418. function tripal_bulk_loader_delete_template_base_form_submit ($form, &$form_state) {
  419. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  420. db_query($sql, $form_state['values']['template_name']);
  421. }
  422. //////////////////////////////////////////////////////////////////////////////////////
  423. // Import/Export Template
  424. //////////////////////////////////////////////////////////////////////////////////////
  425. /**
  426. * Import/Export Template Form
  427. *
  428. * On export, simply selects the serialized array from the db for a given template
  429. * and presents it to the user. On import, a serialized template array and a name is
  430. * supplied and a template record is created.
  431. *
  432. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  433. *
  434. * @param $form_state
  435. * The values and storage for the form
  436. * @param $mode
  437. * Either 'import' or 'export' to indicate which function is being performed
  438. * @return
  439. * A form array to be rendered by drupal_get_form
  440. */
  441. function tripal_bulk_loader_import_export_template_form ($form_state = NULL, $mode) {
  442. $form = array();
  443. $form['mode'] = array(
  444. '#type' => 'hidden',
  445. '#value' => $mode,
  446. );
  447. if (preg_match('/import/', $mode)) {
  448. $form['new_template_name'] = array(
  449. '#type' => 'textfield',
  450. '#title' => 'Template Name',
  451. '#weight' => 1,
  452. );
  453. } elseif (preg_match('/export/', $mode)) {
  454. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  455. $resource = db_query($sql);
  456. $templates = array();
  457. $templates[''] = 'Select a Template';
  458. while ($r = db_fetch_object($resource)) {
  459. $templates[$r->template_id] = $r->name;
  460. }
  461. $form['template_id'] = array(
  462. '#title' => t('Template'),
  463. '#description' => t('Please select the template you would like to edit.'),
  464. '#type' => 'select',
  465. '#options' => $templates,
  466. '#default_value' => $form_state['storage']['template_id'],
  467. '#weight' => 0,
  468. '#required' => TRUE,
  469. '#weight' => 1,
  470. );
  471. }
  472. $form['template_array'] = array(
  473. '#type' => 'textarea',
  474. '#title' => 'Template Array',
  475. '#default_value' => $form_state['storage']['template_array'],
  476. '#weight' => 2,
  477. );
  478. $form['submit'] = array(
  479. '#type' => 'submit',
  480. '#value' => 'Submit',
  481. '#weight' => 10,
  482. );
  483. return $form;
  484. }
  485. /**
  486. * Import/Export Template Form Submit
  487. *
  488. * @param $form
  489. * The form that was submitted
  490. * @param $form_state
  491. * The values and storage that were submitted
  492. */
  493. function tripal_bulk_loader_import_export_template_form_submit ($form, &$form_state) {
  494. switch ($form_state['values']['mode']) {
  495. case 'export':
  496. $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
  497. $form_state['storage']['template_array'] = $record->template_array;
  498. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  499. break;
  500. case 'import':
  501. $record = array(
  502. 'name' => $form_state['values']['new_template_name'],
  503. 'template_array' => $form_state['values']['template_array'],
  504. );
  505. drupal_write_record('tripal_bulk_loader_template', $record);
  506. if ($record->template_id) {
  507. drupal_set_message('Successfully imported Tripal Bulk Loader Template.');
  508. }
  509. break;
  510. }
  511. }
  512. //////////////////////////////////////////////////////////////////////////////////////
  513. // Edit Record Form
  514. //////////////////////////////////////////////////////////////////////////////////////
  515. /**
  516. * Edit Record Form
  517. *
  518. * This form is meant to be called from a bulk loader form. The following should be set
  519. * in the query section of the path:
  520. * - template_id=\d+: the template which the edited record is part of
  521. * - record_id=\d+: the priority or key in the template array of the record to be edited
  522. *
  523. * @param $form_state
  524. * Contains the values and storage for the form
  525. * @return
  526. * A form array to be rendered by drupal_get_form
  527. */
  528. function tripal_bulk_loader_edit_template_record_form (&$form_state = NULL) {
  529. $form['#cache'] = TRUE; // Make sure the form is cached.
  530. // get template id from path
  531. $template_id = ($_GET['template_id'] !== NULL) ? $_GET['template_id'] : $form_state['values']['template_id'];
  532. // if there is no template supplied don't return rest of form
  533. if (!$template_id) {
  534. return $form;
  535. }
  536. // Pre-process values/defaults ---------------------------
  537. // If this is the first load of the form (no form state) we need to initialize some variables
  538. if (!$form_state['storage']['template']) {
  539. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  540. $template = db_fetch_object(db_query($sql, $template_id));
  541. $form_state['storage']['template_array'] = unserialize($template->template_array);
  542. $form_state['storage']['template'] = $template;
  543. $form_state['storage']['record2priority'] = array();
  544. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  545. if (!is_array($record_array)) { continue; }
  546. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  547. }
  548. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  549. } else {
  550. $template = $form_state['storage']['template'];
  551. }
  552. // get the record_id from the path
  553. if ($_GET['record_id'] !== NULL) {
  554. $form_state['values']['field_group'] = $_GET['record_id'];
  555. $form_state['storage']['original_priority'] = $_GET['record_id'];
  556. }
  557. // Tables and default table
  558. $tables = tripal_core_get_chado_tables();
  559. if ($form_state['values']['chado_table']) {
  560. $table = $form_state['values']['chado_table'];
  561. } else {
  562. $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
  563. }
  564. //dpm($form_state, 'form state');
  565. // Form Proper -------------------------------------------
  566. $form['template_name'] = array(
  567. '#type' => 'item',
  568. '#title' => 'Template',
  569. '#value' => $template->name,
  570. );
  571. $form['template_id'] = array(
  572. '#type' => 'hidden',
  573. '#value' => $template_id,
  574. );
  575. $form['edit_record'] = array(
  576. '#type' => 'fieldset',
  577. );
  578. // check template array for records then add one more
  579. if (!$form_state['storage']['record2priority']) {
  580. $groups = array();
  581. } else {
  582. $groups = array_flip($form_state['storage']['record2priority']);
  583. }
  584. $priority_default = $form_state['values']['field_group'];
  585. $form['edit_record']['field_group'] = array(
  586. '#type' => 'select',
  587. '#title' => 'Record',
  588. '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
  589. '#options' => $groups,
  590. '#default_value' => $priority_default,
  591. '#required' => TRUE,
  592. );
  593. $form['edit_record']['record_name'] = array(
  594. '#type' => 'textfield',
  595. '#title' => 'Unique Record Name',
  596. '#default_value' => $groups[$priority_default],
  597. );
  598. $form['edit_record']['chado_table'] = array(
  599. '#type' => 'select',
  600. '#title' => t('Chado Table'),
  601. '#description' => 'This changes the chado table for all fields in this record.',
  602. '#options' => $tables,
  603. '#default_value' => $table,
  604. );
  605. $form['edit_record']['mode'] = array(
  606. '#type' => 'radios',
  607. '#title' => 'Action to take when Loading Record',
  608. '#options' => array(
  609. 'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
  610. 'insert' => 'INSERT: Insert the record',
  611. 'optional' => 'OPTIONAL: Record will only be inserted if all required data is filled in',
  612. 'insert_once' => 'INSERT ONCE: Record will be inserted once for the entire spreadsheet',
  613. 'insert_unique' => 'INSERT UNIQUE: Only insert record if there isn\'t a record with the same values',
  614. ),
  615. '#default_value' => 'insert_unique'
  616. );
  617. $form['edit_record']['submit-edit_record'] = array(
  618. '#type' => 'submit',
  619. '#value' => 'Edit Record'
  620. );
  621. $form['edit_record']['submit-cancel'] = array(
  622. '#type' => 'submit',
  623. '#value' => 'Cancel'
  624. );
  625. return $form;
  626. }
  627. /**
  628. * Edit Record Form Submit
  629. *
  630. * @param $form
  631. * The form that was submitted
  632. * @param $form_state
  633. * Contains the values and storage for the form
  634. */
  635. function tripal_bulk_loader_edit_template_record_form_submit ($form, &$form_state) {
  636. //dpm($form_state, 'form state -start submit');
  637. if (!$form_state['ahah_submission']) {
  638. if ($form_state['values']['op'] == 'Edit Record') {
  639. $template = $form_state['storage']['template_array'];
  640. // Edit Record
  641. $record = $template[ $form_state['storage']['original_priority'] ];
  642. $record['record_id'] = $form_state['values']['record_name'];
  643. $record['mode'] = $form_state['values']['mode'];
  644. $record['table'] = $form_state['values']['chado_table'];
  645. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  646. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  647. $template[ $form_state['values']['field_group'] ] = $record;
  648. unset($template[ $form_state['storage']['original_priority'] ]);
  649. } else {
  650. $template[ $form_state['storage']['original_priority'] ] = $record;
  651. }
  652. // Save Template
  653. $form_state['storage']['template']->template_array = serialize($template);
  654. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  655. if ($success) {
  656. drupal_set_message('Successfully Updated Template Record');
  657. drupal_set_message('Template Saved.');
  658. $path = explode('?',$form_state['storage']['referring URL']);
  659. parse_str($path[1], $query);
  660. $query['template_id'] = $form_state['storage']['template']->template_id;
  661. drupal_goto($path[0], $query);
  662. } else {
  663. drupal_set_message('Unable to Save Template!', 'error');
  664. watchdog('T_bulk_loader',
  665. 'Unable to save bulk loader template: %template',
  666. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  667. WATCHDOG_ERROR
  668. );
  669. }
  670. } elseif ($form_state['values']['op'] == 'Cancel') {
  671. $path = explode('?',$form_state['storage']['referring URL']);
  672. parse_str($path[1], $query);
  673. $query['template_id'] = $form_state['storage']['template']->template_id;
  674. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  675. drupal_goto($path[0], $query);
  676. }
  677. }
  678. }
  679. //////////////////////////////////////////////////////////////////////////////////////
  680. // Add/Edit Field Forms
  681. //////////////////////////////////////////////////////////////////////////////////////
  682. /**
  683. * Add Field Form
  684. *
  685. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  686. * can use the following in the query of the path to set defaults for a given template:
  687. * - template_id=\d+: the template to add the field to
  688. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  689. *
  690. * @param $form_state
  691. * Contains the values and storage for the form
  692. * @return
  693. * A form array to be rendered by drupal_get_form
  694. */
  695. function tripal_bulk_loader_add_template_field_form (&$form_state = NULL) {
  696. $form = array();
  697. $form['#cache'] = TRUE; // Make sure the form is cached.
  698. // get template id from path
  699. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  700. // if there is no template supplied don't return rest of form
  701. if (!$template_id) {
  702. return $form;
  703. }
  704. // Pre-set Variables needed for form proper------------------------------------------
  705. // If this is the first load of the form (no form state) we need to initialize some variables
  706. if (!$form_state['storage']['template']) {
  707. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  708. $template = db_fetch_object(db_query($sql, $template_id));
  709. $form_state['storage']['template_array'] = unserialize($template->template_array);
  710. $form_state['storage']['template'] = $template;
  711. $form_state['storage']['record2priority'] = array();
  712. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  713. if (!is_array($record_array)) { continue; }
  714. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  715. }
  716. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  717. } else {
  718. $template = $form_state['storage']['template'];
  719. }
  720. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  721. // Tables and default table
  722. $tables = tripal_core_get_chado_tables();
  723. if ($form_state['values']) {
  724. if (!preg_match('/^'.current($tables).'$/', $form_state['values']['chado_table'])) {
  725. $table = $form_state['values']['chado_table'];
  726. } elseif ($form_state['values']['record_name']) {
  727. $record_name = $form_state['values']['record_name'];
  728. $priority = $form_state['storage']['record2priority'][$record_name];
  729. $table = $form_state['storage']['template_array'][$priority]['table'];
  730. } else {
  731. $priority = $form_state['values']['field_group'];
  732. $table = $form_state['storage']['template_array'][$priority]['table'];
  733. }
  734. }
  735. if (!$table) {
  736. $table = reset($tables);
  737. }
  738. // get the record_id from the path
  739. if ($_GET['record_id'] !== NULL) {
  740. $form_state['values']['field_group'] = $_GET['record_id'];
  741. if (preg_match('/\d+/', $_GET['record_id'])) {
  742. $priority = $form_state['values']['field_group'];
  743. $table = $form_state['storage']['template_array'][$priority]['table'];
  744. }
  745. }
  746. // Fields and foreign key mappings
  747. $chado_fields = array();
  748. $fk_options = array();
  749. $fk_options['NULL'] = 'None';
  750. $table_description = module_invoke_all('chado_'.$table.'_schema');
  751. //dpm($table_description, 'table description for |'.$table.'|');
  752. if ($field_type == 'foreign key') {
  753. $foreign_field2table = array();
  754. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  755. foreach ($key_array['columns'] as $left_field => $right_field) {
  756. $chado_fields[$left_field] = $left_field;
  757. $foreign_field2table[$left_field] = $key_table;
  758. }
  759. }
  760. reset($chado_fields);
  761. // set default field
  762. if (empty($chado_fields)) {
  763. $field = NULL;
  764. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  765. $field = $form_state['values']['chado_field'];
  766. } else {
  767. $field = current($chado_fields);
  768. }
  769. // Foreign key options
  770. $foreign_table = $foreign_field2table[$field];
  771. if ($foreign_table) {
  772. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  773. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priority]['table'])) {
  774. $fk_options[$record_name] = $record_name;
  775. }
  776. }
  777. }
  778. } else {
  779. foreach($table_description['fields'] as $field_name => $field_array) {
  780. $chado_fields[$field_name] = $field_name;
  781. }
  782. }
  783. $variables = array(
  784. 'form_state' => $form_state,
  785. 'tables' => $tables,
  786. 'default table' => $table,
  787. 'fields' => $chado_fields,
  788. 'default_field' => $field,
  789. 'priority' => $priority,
  790. 'record_name' => $record_name,
  791. 'table description' => $table_description,
  792. 'foreign key options' => $fk_options,
  793. 'foreign field=>table' => $foreign_field2table,
  794. 'foreign table' => $foreign_table,
  795. );
  796. //dpm($variables, 'variables');
  797. // Start of Form Proper--------------------------------------------------------------
  798. //dpm($form_state, 'Form State');
  799. $form['template_name'] = array(
  800. '#type' => 'item',
  801. '#title' => 'Template',
  802. '#value' => $template->name,
  803. );
  804. $form['template_id'] = array(
  805. '#type' => 'hidden',
  806. '#value' => $template_id,
  807. );
  808. $form['add_fields'] = array(
  809. '#type' => 'fieldset',
  810. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  811. '#suffix' => '</div>',
  812. );
  813. $form['add_fields']['field_type'] = array(
  814. '#type' => 'radios',
  815. '#title' => t('Type of Field'),
  816. '#options' => array(
  817. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  818. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  819. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  820. ),
  821. '#required' => TRUE,
  822. '#default_value' => $field_type,
  823. '#ahah' => array(
  824. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  825. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  826. 'effect' => 'fade'
  827. ),
  828. );
  829. // check template array for records then add one more
  830. if (!$form_state['storage']['record2priority']) {
  831. $groups = array();
  832. } else {
  833. $groups = array_flip($form_state['storage']['record2priority']);
  834. }
  835. $groups['NONE'] = 'Select a Record';
  836. $groups['NEW'] = 'New Record';
  837. $form['add_fields']['field_group'] = array(
  838. '#type' => 'select',
  839. '#title' => 'Record',
  840. '#description' => 'This is used to group a set of fields together allowing '
  841. .'multiple records to be inserted into the same table per line of the spreadsheet',
  842. '#options' => $groups,
  843. '#default_value' => (preg_match('/\w+.*/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  844. '#ahah' => array(
  845. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  846. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  847. 'effect' => 'fade'
  848. ),
  849. '#required' => TRUE,
  850. );
  851. $form['add_fields']['record_name'] = array(
  852. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  853. '#title' => 'Unique Record Name',
  854. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  855. '#suffix' => '</div>',
  856. '#default_value' => $form_state['values']['record_name'],
  857. );
  858. $form['add_fields']['field_title'] = array(
  859. '#type' => 'textfield',
  860. '#title' => t('Human-readable Title for Field'),
  861. '#default_value' => $form_state['values']['field_title'],
  862. );
  863. // Spreadsheet column
  864. $form['add_fields']['columns'] = array(
  865. '#type' => 'fieldset',
  866. '#title' => t('Spreadsheet Column'),
  867. '#collapsible' => TRUE,
  868. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  869. );
  870. $form['add_fields']['columns']['sheet_name'] = array(
  871. '#type' => 'textfield',
  872. '#title' => t('Worksheet'),
  873. '#description' => t('Specify the name of the worksheet.'),
  874. '#size' => 5,
  875. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  876. );
  877. $form['add_fields']['columns']['column_number'] = array(
  878. '#type' => 'textfield',
  879. '#title' => t('Column'),
  880. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  881. '#size' => 5,
  882. '#default_value' => $form_state['values']['column_number'],
  883. );
  884. // Global Value
  885. $form['add_fields']['constant'] = array(
  886. '#type' => 'fieldset',
  887. '#title' => t('Constant'),
  888. '#collapsible' => TRUE,
  889. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  890. );
  891. $form['add_fields']['constant']['constant_value'] = array(
  892. '#type' => 'textfield',
  893. '#title' => t('Constant Value'),
  894. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  895. '#default_value' => $form_state['values']['constant_value']
  896. );
  897. // Foreign Key
  898. $form['add_fields']['foreign_key'] = array(
  899. '#type' => 'fieldset',
  900. '#title' => 'Foreign Key',
  901. '#collapsible' => TRUE,
  902. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  903. );
  904. $form['add_fields']['foreign_key']['foreign_record'] = array(
  905. '#type' => 'select',
  906. '#title' => 'Record to refer to',
  907. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  908. '#options' => $fk_options,
  909. );
  910. // Chado Field
  911. $form['add_fields']['chado'] = array(
  912. '#type' => 'fieldset',
  913. '#title' => t('Chado Field/Column Details'),
  914. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  915. );
  916. $form['add_fields']['chado']['chado_table'] = array(
  917. '#type' => 'select',
  918. '#title' => t('Chado Table'),
  919. '#options' => $tables,
  920. '#default_value' => $table,
  921. '#ahah' => array(
  922. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  923. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  924. 'effect' => 'fade'
  925. ),
  926. );
  927. $form['add_fields']['chado']['chado_field'] = array(
  928. '#type' => 'select',
  929. '#title' => t('Chado Field/Column'),
  930. '#options' => $chado_fields,
  931. '#default_value' => $form_state['values']['chado_field'],
  932. '#ahah' => array(
  933. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  934. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  935. 'effect' => 'fade'
  936. ),
  937. );
  938. $form['add_fields']['additional'] = array(
  939. '#type' => 'fieldset',
  940. '#title' => 'Additional Options',
  941. );
  942. $form['add_fields']['additional']['required'] = array(
  943. '#type' => 'checkbox',
  944. '#title' => 'Make this file required',
  945. );
  946. $form['add_fields']['additional']['regex_transform'] = array(
  947. '#type' => 'fieldset',
  948. '#title' => 'Transform Spreadsheet Value Rules',
  949. '#collapsible' => TRUE,
  950. '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE,
  951. );
  952. $form['add_fields']['additional']['regex_transform']['regex_description'] = array(
  953. '#type' => 'item',
  954. '#value' => 'A transformation rule allows you to transform the original value '
  955. .'(usually from a user submitted spreadsheet) into the form you would like it stored '
  956. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  957. .'which determines which replacement patterns are applied and captures regions of the '
  958. .'original value) and a replacement pattern (a string which may contain capture references '
  959. .'that describes what the new value should be). Each rule is applied to the result of the '
  960. .'previous rule.'
  961. );
  962. $form['add_fields']['additional']['regex_transform']['regex-data'] = array(
  963. '#tree' => TRUE,
  964. );
  965. if (!$form_state['storage']['regex']['pattern']) { $form_state['storage']['regex']['pattern'] = array(); }
  966. foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) {
  967. $data_element = array(
  968. 'pattern' => array(
  969. '#type' => 'item',
  970. '#value' => $pattern,
  971. ),
  972. 'replace' => array(
  973. '#type' => 'item',
  974. '#value' => $form_state['storage']['regex']['replace'][$index],
  975. ),
  976. 'old_index' => array(
  977. '#type' => 'hidden',
  978. '#value' => $index,
  979. ),
  980. 'new_index' => array(
  981. '#type' => 'select',
  982. '#options' => range(0,sizeof($form_state['storage']['regex']['pattern'])-1),
  983. '#default_value' => $index,
  984. ),
  985. 'id' => array(
  986. '#type' => 'hidden',
  987. '#value' => $index,
  988. ),
  989. 'submit-delete' => array(
  990. '#type' => 'submit',
  991. '#value' => 'Delete Transformation',
  992. '#name' => $index,
  993. ),
  994. );
  995. $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  996. }
  997. $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  998. '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden',
  999. '#value' => 'Save Transformation Rule Order'
  1000. );
  1001. $form['add_fields']['additional']['regex_transform']['new_regex'] = array(
  1002. '#type' => 'fieldset',
  1003. '#title' => 'Add a new Transformation Rule',
  1004. );
  1005. $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1006. '#type' => 'textfield',
  1007. '#title' => 'Match Pattern',
  1008. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1009. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  1010. .'pattern get applied to the value. To capture a section of your value for use in the '
  1011. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1012. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1013. .' To match and capture any value use <i>.*</i>',
  1014. );
  1015. $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1016. '#type' => 'textfield',
  1017. '#title' => 'Replacement Pattern',
  1018. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1019. .'mentioned above. It can include references of the form \n where n is the number of the '
  1020. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1021. .'first set of round brackets.',
  1022. );
  1023. if ($field_type == 'table field') {
  1024. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1025. $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1026. .'The following references are also available for spreadsheet fields: <b><#column:<i>number</i>#></b>. '
  1027. .'This allows you to substitute other spreadsheet values into the current field. For example, '
  1028. .'if you had the following line:<br>'
  1029. .$tab.'SNP'.$tab.'15-Jan-2011'.$tab.'1'.$tab.'54'.$tab.'Contig34355'
  1030. .'<br> and your current field is for column #1 and you\'re inserting into the chado field '
  1031. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1032. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1033. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1034. }
  1035. $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1036. '#type' => 'submit',
  1037. '#value' => 'Add Transformation',
  1038. );
  1039. $form['add_fields']['additional']['regex_transform']['test_regex'] = array(
  1040. '#type' => 'fieldset',
  1041. '#title' => 'Test Transformation Rules',
  1042. );
  1043. $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1044. '#type' => 'textfield',
  1045. '#title' => 'Test Value',
  1046. '#description' => 'This should be a value that you expect the above transformation rules '
  1047. .'to be applied to.',
  1048. '#default_value' => $form_state['storage']['test_regex_test'],
  1049. );
  1050. $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1051. '#type' => 'textfield',
  1052. '#title' => 'Test Result',
  1053. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1054. .'riles were applied to the Test Value.',
  1055. '#default_value' => $form_state['storage']['test_regex_result'],
  1056. );
  1057. $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1058. '#type' => 'submit',
  1059. '#value' => 'Test Transformation Rules'
  1060. );
  1061. $form['add_fields']['submit-add_field'] = array(
  1062. '#type' => 'submit',
  1063. '#value' => 'Add Field'
  1064. );
  1065. $form['add_fields']['submit-cancel'] = array(
  1066. '#type' => 'submit',
  1067. '#value' => 'Cancel'
  1068. );
  1069. return $form;
  1070. }
  1071. /**
  1072. * Add Field Submit
  1073. *
  1074. * @param $form
  1075. * The form that was submitted
  1076. * @param $form_state
  1077. * The values and storage for the form
  1078. */
  1079. function tripal_bulk_loader_add_template_field_form_submit ($form, &$form_state) {
  1080. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1081. if (!$form_state['ahah_submission']) {
  1082. if ($op == 'Add Field') {
  1083. $template = $form_state['storage']['template_array'];
  1084. // If new record
  1085. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  1086. $record_name = $form_state['values']['record_name'];
  1087. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1088. $record2priority[$record_name] = $priority;
  1089. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1090. $template[$priority]['record_id'] = $record_name;
  1091. } else {
  1092. $priority = $form_state['values']['field_group'];
  1093. $record_name = $record2priority[$priority];
  1094. }
  1095. // Add field to template array
  1096. if ($form_state['values']['field_type'] == 'table field') {
  1097. $field = array(
  1098. 'type' => 'table field',
  1099. 'title' => $form_state['values']['field_title'],
  1100. 'field' => $form_state['values']['chado_field'],
  1101. 'required' => $form_state['values']['required'],
  1102. //'allowed values' => empty by default,
  1103. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  1104. 'spreadsheet column' => $form_state['values']['column_number'],
  1105. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1106. );
  1107. } elseif ($form_state['values']['field_type'] == 'constant') {
  1108. $field = array(
  1109. 'type' => 'constant',
  1110. 'title' => $form_state['values']['field_title'],
  1111. 'field' => $form_state['values']['chado_field'],
  1112. 'required' => $form_state['values']['required'],
  1113. //'allowed values' => empty by default,
  1114. 'constant value' => $form_state['values']['constant_value'],
  1115. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1116. );
  1117. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  1118. $field = array(
  1119. 'type' => 'foreign key',
  1120. 'title' => $form_state['values']['field_title'],
  1121. 'field' => $form_state['values']['chado_field'],
  1122. 'foreign key' => $form_state['values']['foreign_record'],
  1123. 'required' => $form_state['values']['required'],
  1124. );
  1125. }
  1126. // Deal with any additional options
  1127. if ($form_state['storage']['regex']) {
  1128. $field['regex'] = $form_state['storage']['regex'];
  1129. }
  1130. // Save Template
  1131. $template[$priority]['fields'][] = $field;
  1132. $form_state['storage']['template']->template_array = serialize($template);
  1133. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1134. if ($success) {
  1135. drupal_set_message('Successfully Added Field to Template');
  1136. drupal_set_message('Template Saved.');
  1137. $path = explode('?',$form_state['storage']['referring URL']);
  1138. parse_str($path[1], $query);
  1139. $query['template_id'] = $form_state['storage']['template']->template_id;
  1140. drupal_goto($path[0], $query);
  1141. } else {
  1142. drupal_set_message('Unable to Save Template!', 'error');
  1143. watchdog('T_bulk_loader',
  1144. 'Unable to save bulk loader template: %template',
  1145. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1146. WATCHDOG_ERROR
  1147. );
  1148. }
  1149. } elseif ($op == 'Cancel') {
  1150. $path = explode('?',$form_state['storage']['referring URL']);
  1151. parse_str($path[1], $query);
  1152. $query['template_id'] = $form_state['storage']['template']->template_id;
  1153. drupal_goto($path[0], $query);
  1154. } elseif ($op == 'Add Transformation') {
  1155. // Add transformation rule to original field
  1156. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1157. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1158. drupal_set_message('Successfully Added Transformation Rule');
  1159. } elseif ($op == 'Save Transformation Rule Order') {
  1160. // Generate new regex array
  1161. $new_regex = array();
  1162. $old_regex = $form_state['storage']['regex'];
  1163. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1164. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1165. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1166. }
  1167. // sort new regex arrays
  1168. asort($new_regex['pattern']);
  1169. asort($new_regex['replace']);
  1170. $form_state['storage']['regex'] = $new_regex;
  1171. } elseif ($op == 'Delete Transformation') {
  1172. // Unset regex rule
  1173. $index = $form_state['clicked_button']['#name'];
  1174. unset($form_state['storage']['regex']['pattern'][$index]);
  1175. unset($form_state['storage']['regex']['replace'][$index]);
  1176. } elseif ($op == 'Test Transformation Rules') {
  1177. $patterns = $form_state['storage']['regex']['pattern'];
  1178. $replaces = $form_state['storage']['regex']['replace'];
  1179. $test_string = $form_state['values']['test_string'];
  1180. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1181. $form_state['storage']['test_regex_test'] = $test_string;
  1182. }
  1183. }
  1184. }
  1185. /**
  1186. * Edit Field Form
  1187. *
  1188. * This form is meant to be called from a bulk loader form. The following should be set
  1189. * in the query section of the path:
  1190. * - template_id=\d+: the template which the edited field is part of
  1191. * - record_id=\d+: the priority or key in the template array of the record the field
  1192. * is currently part of
  1193. * - field_index=\d+: the key of the field in the fields array of the previously
  1194. * specified record
  1195. *
  1196. * @param $form_state
  1197. * Contains the values and storage for the form
  1198. * @return
  1199. * A form array to be rendered by drupal_get_form
  1200. */
  1201. function tripal_bulk_loader_edit_template_field_form (&$form_state = NULL) {
  1202. $form = array();
  1203. $form['#cache'] = TRUE; // Make sure the form is cached.
  1204. // get template id from path
  1205. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1206. // if there is no template supplied don't return rest of form
  1207. if (!$template_id) {
  1208. return $form;
  1209. }
  1210. // Pre-set Variables needed for form proper------------------------------------------
  1211. // If this is the first load of the form (no form state) we need to initialize some variables
  1212. if (!$form_state['storage']['template']) {
  1213. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1214. $template = db_fetch_object(db_query($sql, $template_id));
  1215. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1216. $form_state['storage']['template'] = $template;
  1217. $form_state['storage']['record2priority'] = array();
  1218. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1219. if (!is_array($record_array)) { continue; }
  1220. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1221. }
  1222. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1223. } else {
  1224. $template = $form_state['storage']['template'];
  1225. }
  1226. // get the field from the path
  1227. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1228. $priority = $_GET['record_id'];
  1229. $field_index = $_GET['field_index'];
  1230. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1231. $form_state['storage']['original_field'] = $template_field;
  1232. $form_state['storage']['original_field']['priority'] = $priority;
  1233. $form_state['storage']['original_field']['field_index'] = $field_index;
  1234. }
  1235. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1236. // Tables and default table
  1237. $tables = tripal_core_get_chado_tables();
  1238. if ($form_state['values']) {
  1239. $table = $form_state['values']['chado_table'];
  1240. } else {
  1241. $table = $form_state['storage']['template_array'][$priority]['table'];
  1242. }
  1243. // Fields and foreign key mappings
  1244. $chado_fields = array();
  1245. $fk_options = array();
  1246. $fk_options['NULL'] = 'None';
  1247. $table_description = module_invoke_all('chado_'.$table.'_schema');
  1248. if ($field_type == 'foreign key') {
  1249. $foreign_field2table = array();
  1250. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1251. foreach ($key_array['columns'] as $left_field => $right_field) {
  1252. $chado_fields[$left_field] = $left_field;
  1253. $foreign_field2table[$left_field] = $key_table;
  1254. }
  1255. }
  1256. reset($chado_fields);
  1257. // set default field
  1258. if (empty($chado_fields)) {
  1259. $field = NULL;
  1260. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1261. $field = $form_state['values']['chado_field'];
  1262. } elseif ($template_field['field']) {
  1263. $field = $template_field['field'];
  1264. } else {
  1265. $field = current($chado_fields);
  1266. }
  1267. //dpm($field, 'field');
  1268. // Foreign key options
  1269. $foreign_table = $foreign_field2table[$field];
  1270. if ($foreign_table) {
  1271. foreach ($form_state['storage']['record2priority'] as $record_nameT => $priorityT ) {
  1272. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priorityT]['table'])) {
  1273. $fk_options[$record_nameT] = $record_nameT;
  1274. }
  1275. }
  1276. }
  1277. } else {
  1278. foreach($table_description['fields'] as $field_name => $field_array) {
  1279. $chado_fields[$field_name] = $field_name;
  1280. }
  1281. }
  1282. // dpm(array( 'tables' => $tables, 'default table' => $table, 'record name' => $record_name, 'priority' => $priority,
  1283. // 'fields' => $chad_fields, 'default field' => $field, 'foreign field=>table' => $foreign_field2table,
  1284. // 'table desc' => $table_description, 'foreign record options' => $fk_options, 'foreign table' => $foreign_table
  1285. // ), 'Variables');
  1286. // Start of Form Proper--------------------------------------------------------------
  1287. $form['template_name'] = array(
  1288. '#type' => 'item',
  1289. '#title' => 'Template',
  1290. '#value' => $template->name,
  1291. );
  1292. $form['template_id'] = array(
  1293. '#type' => 'hidden',
  1294. '#value' => $template_id,
  1295. );
  1296. $form['edit_fields'] = array(
  1297. '#type' => 'fieldset',
  1298. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1299. '#suffix' => '</div>',
  1300. );
  1301. $form['edit_fields']['field_type'] = array(
  1302. '#type' => 'radios',
  1303. '#title' => t('Type of Field'),
  1304. '#options' => array(
  1305. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  1306. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  1307. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  1308. ),
  1309. '#required' => TRUE,
  1310. '#default_value' => $field_type,
  1311. '#ahah' => array(
  1312. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1313. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1314. 'effect' => 'fade'
  1315. ),
  1316. );
  1317. // check template array for records then edit one more
  1318. if (!$form_state['storage']['record2priority']) {
  1319. $groups = array();
  1320. } else {
  1321. $groups = array_flip($form_state['storage']['record2priority']);
  1322. }
  1323. $groups['NONE'] = 'Select a Record';
  1324. $groups['NEW'] = 'New Record';
  1325. $form['edit_fields']['field_group'] = array(
  1326. '#type' => 'select',
  1327. '#title' => 'Record',
  1328. '#description' => 'This is used to group a set of fields together allowing '
  1329. .'multiple records to be inserted into the same table per line of the spreadsheet',
  1330. '#options' => $groups,
  1331. '#default_value' => (preg_match('/(\d+|\w+)/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1332. '#ahah' => array(
  1333. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1334. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1335. 'effect' => 'fade'
  1336. ),
  1337. '#required' => TRUE,
  1338. );
  1339. $form['edit_fields']['record_name'] = array(
  1340. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1341. '#title' => 'Unique Record Name',
  1342. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1343. '#suffix' => '</div>',
  1344. '#default_value' => $form_state['values']['record_name'],
  1345. );
  1346. $form['edit_fields']['field_title'] = array(
  1347. '#type' => 'textfield',
  1348. '#title' => t('Human-readable Title for Field'),
  1349. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1350. );
  1351. // Spreadsheet column
  1352. $form['edit_fields']['columns'] = array(
  1353. '#type' => 'fieldset',
  1354. '#title' => t('Spreadsheet Column'),
  1355. '#collapsible' => TRUE,
  1356. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1357. );
  1358. $form['edit_fields']['columns']['sheet_name'] = array(
  1359. '#type' => 'textfield',
  1360. '#title' => t('Worksheet'),
  1361. '#description' => t('Specify the name of the worksheet.'),
  1362. '#size' => 5,
  1363. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1364. );
  1365. $form['edit_fields']['columns']['column_number'] = array(
  1366. '#type' => 'textfield',
  1367. '#title' => t('Column'),
  1368. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  1369. '#size' => 5,
  1370. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1371. );
  1372. // Global Value
  1373. $form['edit_fields']['constant'] = array(
  1374. '#type' => 'fieldset',
  1375. '#title' => t('Constant'),
  1376. '#collapsible' => TRUE,
  1377. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1378. );
  1379. $form['edit_fields']['constant']['constant_value'] = array(
  1380. '#type' => 'textfield',
  1381. '#title' => t('Constant Value'),
  1382. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  1383. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1384. );
  1385. // Foreign Key
  1386. $form['edit_fields']['foreign_key'] = array(
  1387. '#type' => 'fieldset',
  1388. '#title' => 'Foreign Key',
  1389. '#collapsible' => TRUE,
  1390. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1391. );
  1392. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1393. '#type' => 'select',
  1394. '#title' => 'Record to refer to',
  1395. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1396. '#options' => $fk_options,
  1397. );
  1398. // Chado Field
  1399. $form['edit_fields']['chado'] = array(
  1400. '#type' => 'fieldset',
  1401. '#title' => t('Chado Field/Column Details'),
  1402. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1403. );
  1404. $form['edit_fields']['chado']['chado_table'] = array(
  1405. '#type' => 'select',
  1406. '#title' => t('Chado Table'),
  1407. '#options' => $tables,
  1408. '#default_value' => $table,
  1409. '#ahah' => array(
  1410. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1411. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1412. 'effect' => 'fade'
  1413. ),
  1414. );
  1415. $form['edit_fields']['chado']['chado_field'] = array(
  1416. '#type' => 'select',
  1417. '#title' => t('Chado Field/Column'),
  1418. '#options' => $chado_fields,
  1419. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1420. '#ahah' => array(
  1421. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1422. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1423. 'effect' => 'fade'
  1424. ),
  1425. );
  1426. $form['edit_fields']['additional'] = array(
  1427. '#type' => 'fieldset',
  1428. '#title' => 'Additional Options',
  1429. );
  1430. $form['edit_fields']['additional']['required'] = array(
  1431. '#type' => 'checkbox',
  1432. '#title' => 'Make this file required',
  1433. '#default_value' => $template_field['required'],
  1434. );
  1435. $form['edit_fields']['additional']['regex_transform'] = array(
  1436. '#type' => 'fieldset',
  1437. '#title' => 'Transform Spreadsheet Value Rules',
  1438. '#collapsible' => TRUE,
  1439. '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE,
  1440. );
  1441. $transformation_msg = '<p>A transformation rule allows you to transform the original value '
  1442. .'(usually from a user submitted spreadsheet) into the form you would like it stored '
  1443. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1444. .'which determines which replacement patterns are applied and captures regions of the '
  1445. .'original value) and a replacement pattern (a string which may contain capture references '
  1446. .'that describes what the new value should be). Each rule is applied to the result of the '
  1447. .'previous rule.<p>';
  1448. $form['edit_fields']['additional']['regex_transform']['regex_description'] = array(
  1449. '#type' => 'item',
  1450. '#value' => $transformation_msg,
  1451. );
  1452. $form['edit_fields']['additional']['regex_transform']['regex-data'] = array(
  1453. '#tree' => TRUE,
  1454. );
  1455. foreach ($template_field['regex']['pattern'] as $index => $pattern) {
  1456. $data_element = array(
  1457. 'pattern' => array(
  1458. '#type' => 'item',
  1459. '#value' => $pattern,
  1460. ),
  1461. 'replace' => array(
  1462. '#type' => 'item',
  1463. '#value' => $template_field['regex']['replace'][$index],
  1464. ),
  1465. 'old_index' => array(
  1466. '#type' => 'hidden',
  1467. '#value' => $index,
  1468. ),
  1469. 'new_index' => array(
  1470. '#type' => 'select',
  1471. '#options' => range(0,sizeof($template_field['regex']['pattern'])-1),
  1472. '#default_value' => $index,
  1473. ),
  1474. 'id' => array(
  1475. '#type' => 'hidden',
  1476. '#value' => $index,
  1477. ),
  1478. 'submit-delete' => array(
  1479. '#type' => 'submit',
  1480. '#value' => 'Delete Transformation',
  1481. '#name' => $index,
  1482. ),
  1483. );
  1484. $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1485. }
  1486. $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1487. '#type' => 'submit',
  1488. '#value' => 'Save Transformation Rule Order'
  1489. );
  1490. $form['edit_fields']['additional']['regex_transform']['new_regex'] = array(
  1491. '#type' => 'fieldset',
  1492. '#title' => 'Add a new Transformation Rule',
  1493. );
  1494. $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1495. '#type' => 'textfield',
  1496. '#title' => 'Match Pattern',
  1497. '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a '
  1498. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  1499. .'pattern get applied to the value. To capture a section of your value for use in the '
  1500. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1501. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1502. .' To match and capture any value use <i>.*</i>',
  1503. );
  1504. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1505. '#type' => 'textfield',
  1506. '#title' => 'Replacement Pattern',
  1507. '#description' => '<p>This pattern should contain the text you want to replace the match pattern '
  1508. .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the '
  1509. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1510. .'first set of round brackets.</p>',
  1511. );
  1512. if ($field_type == 'table field') {
  1513. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1514. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1515. .'The following references are also available for spreadsheet fields: <b><#column:<i>number</i>#></b>. '
  1516. .'This allows you to substitute other spreadsheet values into the current field. For example, '
  1517. .'if you had the following line:<br>'
  1518. .$tab.'SNP'.$tab.'15-Jan-2011'.$tab.'1'.$tab.'54'.$tab.'Contig34355'
  1519. .'<br> and your current field is for column #1 and you\'re inserting into the chado field '
  1520. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1521. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1522. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1523. }
  1524. $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1525. '#type' => 'submit',
  1526. '#value' => 'Add Transformation',
  1527. );
  1528. $form['edit_fields']['additional']['regex_transform']['test_regex'] = array(
  1529. '#type' => 'fieldset',
  1530. '#title' => 'Test Transformation Rules',
  1531. );
  1532. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1533. '#type' => 'textfield',
  1534. '#title' => 'Test Value',
  1535. '#description' => 'This should be a value that you expect the above transformation rules '
  1536. .'to be applied to.',
  1537. '#default_value' => $form_state['storage']['test_regex_test'],
  1538. );
  1539. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1540. '#type' => 'textfield',
  1541. '#title' => 'Test Result',
  1542. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1543. .'riles were applied to the Test Value.',
  1544. '#default_value' => $form_state['storage']['test_regex_result'],
  1545. );
  1546. $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1547. '#type' => 'submit',
  1548. '#value' => 'Test Transformation Rules'
  1549. );
  1550. $form['edit_fields']['submit-edit_field'] = array(
  1551. '#type' => 'submit',
  1552. '#value' => 'Edit Field'
  1553. );
  1554. $form['edit_fields']['submit-cancel'] = array(
  1555. '#type' => 'submit',
  1556. '#value' => 'Cancel'
  1557. );
  1558. return $form;
  1559. }
  1560. /**
  1561. * Edit Field Form Submit
  1562. *
  1563. * @param $form
  1564. * The form that was submitted
  1565. * @param $form_state
  1566. * The values and storage for the form
  1567. */
  1568. function tripal_bulk_loader_edit_template_field_form_submit ($form, &$form_state) {
  1569. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1570. //dpm($op, 'Operation Submitted');
  1571. //Clear Test
  1572. $form_state['storage']['test_regex_result'] = NULL;
  1573. $form_state['storage']['test_regex_test'] = NULL;
  1574. if (!$form_state['ahah_submission']) {
  1575. if ($op == 'Edit Field') {
  1576. // If new record
  1577. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  1578. // add new record
  1579. $record_name = $form_state['values']['record_name'];
  1580. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1581. $old_priority = $form_state['storage']['original_field']['priority'];
  1582. $field_index = $form_state['storage']['original_field']['field_index'];
  1583. $form_state['storage']['record2priority'][$record_name] = $priority;
  1584. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1585. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1586. } else {
  1587. $priority = $form_state['values']['field_group'];
  1588. $old_priority = $form_state['storage']['original_field']['priority'];
  1589. $field_index = $form_state['storage']['original_field']['field_index'];
  1590. $record_name = $form_state['storage']['record2priority'][$priority];
  1591. }
  1592. $field = $form_state['storage']['original_field'];
  1593. if ($form_state['values']['field_type'] == 'table field') {
  1594. $field['type'] = 'table field';
  1595. $field['title'] = $form_state['values']['field_title'];
  1596. $field['field'] = $form_state['values']['chado_field'];
  1597. $field['required'] = $form_state['values']['required'];
  1598. //$field['allowed values'] = empty by default;
  1599. $field['spreadsheet sheet'] = $form_state['values']['sheet_name'];
  1600. $field['spreadsheet column'] = $form_state['values']['column_number'];
  1601. //$field['exposed'] = 'true|false'; If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1602. } elseif ($form_state['values']['field_type'] == 'constant') {
  1603. $field['type'] = 'constant';
  1604. $field['title'] = $form_state['values']['field_title'];
  1605. $field['field'] = $form_state['values']['chado_field'];
  1606. $field['required'] = $form_state['values']['required'];
  1607. //$field['allowed values'] = empty by default;
  1608. $field['constant value'] = $form_state['values']['constant_value'];
  1609. //$field['exposed'] = 'true|false'; If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1610. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  1611. $field['type'] = 'foreign key';
  1612. $field['title'] = $form_state['values']['field_title'];
  1613. $field['field'] = $form_state['values']['chado_field'];
  1614. $field['foreign key'] = $form_state['values']['foreign_record'];
  1615. $field['required'] = $form_state['values']['required'];
  1616. }
  1617. // Deal with any additional options
  1618. // if the record has changed...
  1619. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1620. if ($old_priority != $priority) {
  1621. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1622. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1623. // if there are no fields left delete the old record
  1624. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1625. unset($form_state['storage']['template_array'][$old_priority]);
  1626. }
  1627. } else {
  1628. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1629. }
  1630. // Save Template
  1631. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1632. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1633. if ($success) {
  1634. drupal_set_message('Successfully Updated Field');
  1635. drupal_set_message('Template Saved.');
  1636. $path = explode('?',$form_state['storage']['referring URL']);
  1637. parse_str($path[1], $query);
  1638. $query['template_id'] = $form_state['storage']['template']->template_id;
  1639. drupal_goto($path[0], $query);
  1640. } else {
  1641. drupal_set_message('Unable to Save Template!', 'error');
  1642. watchdog('T_bulk_loader',
  1643. 'Unable to save bulk loader template: %template',
  1644. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1645. WATCHDOG_ERROR
  1646. );
  1647. }
  1648. } elseif ($op == 'Cancel') {
  1649. $path = explode('?',$form_state['storage']['referring URL']);
  1650. parse_str($path[1], $query);
  1651. $query['template_id'] = $form_state['storage']['template']->template_id;
  1652. drupal_goto($path[0], $query);
  1653. } elseif ($op == 'Add Transformation') {
  1654. // Add transformation rule to original field
  1655. $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1656. $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace'];
  1657. // Add original field back into template
  1658. $priority = $form_state['storage']['original_field']['priority'];
  1659. $field_index = $form_state['storage']['original_field']['field_index'];
  1660. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1661. // Save Template
  1662. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1663. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1664. if ($success) {
  1665. drupal_set_message('Successfully Added Transformation Rule');
  1666. drupal_set_message('Template Saved.');
  1667. } else {
  1668. drupal_set_message('Unable to Save Template!', 'error');
  1669. watchdog('T_bulk_loader',
  1670. 'Unable to save bulk loader template: %template',
  1671. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1672. WATCHDOG_ERROR
  1673. );
  1674. }
  1675. } elseif ($op == 'Save Transformation Rule Order') {
  1676. // Generate new regex array
  1677. $new_regex = array();
  1678. $old_regex = $form_state['storage']['original_field']['regex'];
  1679. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1680. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1681. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1682. }
  1683. // sort new regex arrays
  1684. asort($new_regex['pattern']);
  1685. asort($new_regex['replace']);
  1686. // Add back to original field
  1687. $form_state['storage']['original_field']['regex'] = $new_regex;
  1688. $priority = $form_state['storage']['original_field']['priority'];
  1689. $field_index = $form_state['storage']['original_field']['field_index'];
  1690. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1691. // Save Template
  1692. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1693. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1694. if ($success) {
  1695. drupal_set_message('Successfully Reordered Transformation Rules');
  1696. drupal_set_message('Template Saved.');
  1697. } else {
  1698. drupal_set_message('Unable to Save Template!', 'error');
  1699. watchdog('T_bulk_loader',
  1700. 'Unable to save bulk loader template: %template',
  1701. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1702. WATCHDOG_ERROR
  1703. );
  1704. }
  1705. } elseif ($op == 'Delete Transformation') {
  1706. // Unset regex rule
  1707. $index = $form_state['clicked_button']['#name'];
  1708. unset($form_state['storage']['original_field']['regex']['pattern'][$index]);
  1709. unset($form_state['storage']['original_field']['regex']['replace'][$index]);
  1710. $priority = $form_state['storage']['original_field']['priority'];
  1711. $field_index = $form_state['storage']['original_field']['field_index'];
  1712. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1713. // Save Template
  1714. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1715. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1716. if ($success) {
  1717. drupal_set_message('Successfully Reordered Transformation Rules');
  1718. drupal_set_message('Template Saved.');
  1719. } else {
  1720. drupal_set_message('Unable to Save Template!', 'error');
  1721. watchdog('T_bulk_loader',
  1722. 'Unable to save bulk loader template: %template',
  1723. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1724. WATCHDOG_ERROR
  1725. );
  1726. }
  1727. } elseif ($op == 'Test Transformation Rules') {
  1728. $patterns = $form_state['storage']['original_field']['regex']['pattern'];
  1729. $replaces = $form_state['storage']['original_field']['regex']['replace'];
  1730. $test_string = $form_state['values']['test_string'];
  1731. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1732. $form_state['storage']['test_regex_test'] = $test_string;
  1733. }
  1734. }
  1735. }
  1736. //////////////////////////////////////////////////////////////////////////////////////
  1737. // AHAH Callbacks
  1738. //////////////////////////////////////////////////////////////////////////////////////
  1739. /**
  1740. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  1741. *
  1742. * @return
  1743. * JSON Data printed to the screen
  1744. */
  1745. function tripal_bulk_loader_add_field_ahah () {
  1746. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1747. $form_build_id = $_POST['form_build_id'];
  1748. $form = form_get_cache($form_build_id, $form_state);
  1749. $args = $form['#parameters'];
  1750. $form_id = array_shift($args);
  1751. $form_state['post'] = $form['#post'] = $_POST;
  1752. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1753. $form_state['ahah_submission'] = TRUE;
  1754. $form['#programmed'] = $form['#redirect'] = FALSE;
  1755. drupal_process_form($form_id, $form, $form_state);
  1756. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1757. $form_element = $form['add_fields'];
  1758. // Remove the wrapper so we don't double it up.
  1759. //unset($form_element['#prefix'], $form_element['#suffix']);
  1760. $output = theme('status_messages');
  1761. $output .= drupal_render($form_element);
  1762. // Final rendering callback.
  1763. print drupal_json(array('status' => TRUE, 'data' => $output));
  1764. exit();
  1765. }
  1766. /**
  1767. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  1768. *
  1769. * @return
  1770. * JSON Data printed to the screen
  1771. */
  1772. function tripal_bulk_loader_edit_field_ahah () {
  1773. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1774. $form_build_id = $_POST['form_build_id'];
  1775. $form = form_get_cache($form_build_id, $form_state);
  1776. $args = $form['#parameters'];
  1777. $form_id = array_shift($args);
  1778. $form_state['post'] = $form['#post'] = $_POST;
  1779. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1780. $form_state['ahah_submission'] = TRUE;
  1781. $form['#programmed'] = $form['#redirect'] = FALSE;
  1782. drupal_process_form($form_id, $form, $form_state);
  1783. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1784. $form_element = $form['edit_fields'];
  1785. // Remove the wrapper so we don't double it up.
  1786. unset($form_element['#prefix'], $form_element['#suffix']);
  1787. $output = theme('status_messages');
  1788. $output .= drupal_render($form_element);
  1789. // Final rendering callback.
  1790. print drupal_json(array('status' => TRUE, 'data' => $output));
  1791. exit();
  1792. }