tripal_bulk_loader.admin.templates.inc 78 KB

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