tripal_bulk_loader.admin.inc 78 KB

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