tripal_bulk_loader.admin.templates.inc 86 KB

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