tripal_bulk_loader.admin.inc 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. <?php
  2. /**
  3. * @file
  4. * Handles Create/Edit/Delete Template Admin Forms
  5. */
  6. /**
  7. * tripal_bulk_loader_admin_template
  8. */
  9. function tripal_bulk_loader_admin_template () {
  10. $add_url = url("admin/tripal/tripal_bulk_loader_template/add");
  11. $output = "<a href=\"$add_url\">Create a new bulk loader template</a><br>";
  12. $del_url = url("admin/tripal/tripal_bulk_loader_template/delete");
  13. $output .= "<a href=\"$del_url\">Delete a bulk loader template</a>";
  14. return $output;
  15. }
  16. //////////////////////////////////////////////////////////////////////////////////////
  17. // Modify Template
  18. //////////////////////////////////////////////////////////////////////////////////////
  19. /**
  20. *
  21. */
  22. function tripal_bulk_loader_modify_template_base_form ($form_state = NULL, $mode){
  23. $form = array();
  24. // get template id from path and rebuild form
  25. if ($_GET['template_id']) {
  26. if (preg_match('/^\d+$/', $_GET['template_id'])) {
  27. $form_state['storage']['template_id'] = $_GET['template_id'];
  28. }
  29. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  30. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  31. $form_state['storage']['template'] = unserialize($result->template_array);
  32. $form_state['storage']['template_name'] = $result->name;
  33. $form_state['storage']['record2priority'] = array();
  34. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  35. if (!is_array($record_array)) { continue; }
  36. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  37. }
  38. }
  39. $form['mode'] = array(
  40. '#type' => 'hidden',
  41. '#value' => $mode,
  42. );
  43. if ($form_state['storage']['template_id']) {
  44. $form['template_name'] = array(
  45. '#type' => 'item',
  46. '#title' => 'Template',
  47. '#value' => $form_state['storage']['template_name'],
  48. '#weight' => 1,
  49. );
  50. } else {
  51. if (preg_match('/create/', $mode)) {
  52. $form['new_template_name'] = array(
  53. '#type' => 'textfield',
  54. '#title' => 'Template Name',
  55. '#weight' => 1,
  56. );
  57. } elseif (preg_match('/edit/', $mode)) {
  58. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  59. $resource = db_query($sql);
  60. $templates = array();
  61. $templates[''] = 'Select a Template';
  62. while ($r = db_fetch_object($resource)) {
  63. $templates[$r->template_id] = $r->name;
  64. }
  65. $form['template_id'] = array(
  66. '#title' => t('Template'),
  67. '#description' => t('Please select the template you would like to edit.'),
  68. '#type' => 'select',
  69. '#options' => $templates,
  70. '#default_value' => $form_state['storage']['template_id'],
  71. '#weight' => 0,
  72. '#required' => TRUE,
  73. '#weight' => 1,
  74. );
  75. }
  76. }
  77. $form['records'] = array(
  78. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  79. '#title' => t('Current Records'),
  80. '#weight' => 2,
  81. );
  82. $form['records']['description'] = array(
  83. '#type' => 'item',
  84. '#value' => 'Records will be inserted into the chado database in the order listed below. To '
  85. .'change this order: <ul><li>Drag the rows into the correct order OR</li><li>Enter '
  86. .'the numbers 1 and up in the Order textboxes to indicate the correct order.</li></ul>',
  87. );
  88. $form['records']['records-data'] = array(
  89. '#tree' => TRUE,
  90. );
  91. $form['records']['no_records'] = array(
  92. '#type' => 'hidden',
  93. '#value' => TRUE,
  94. );
  95. $form['records']['submit-new_record'] = array(
  96. '#type' => 'submit',
  97. '#value' => 'New Record/Field',
  98. );
  99. $form['records']['submit-reorder'] = array(
  100. '#type' => 'submit',
  101. '#value' => 'Save Order',
  102. );
  103. $form['fields'] = array(
  104. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  105. '#title' => t('Current Fields'),
  106. '#weight' => 3,
  107. );
  108. $form['fields']['fields-data'] = array(
  109. '#tree' => TRUE,
  110. );
  111. if ($form_state['storage']['template']) {
  112. // List Current Fields -------------------------------------------------------------
  113. $i=1;
  114. foreach ($form_state['storage']['template'] as $priority => $table_array) {
  115. if (!is_array($table_array)) { continue; }
  116. $form['records']['no_records']['#value'] = FALSE;
  117. $form['records']['records-data'][$priority] = array(
  118. 'title' => array(
  119. '#type' => 'markup',
  120. '#value' => $table_array['record_id'],
  121. ),
  122. 'chado_table' => array(
  123. '#type' => 'markup',
  124. '#value' => $table_array['table'],
  125. ),
  126. 'mode' => array(
  127. '#type' => 'item',
  128. '#value' => ($table_array['mode']) ? $table_array['mode'] : 'insert',
  129. ),
  130. 'new_priority' => array(
  131. '#type' => 'select',
  132. '#options' => range(1, sizeof($form_state['storage']['template'])),
  133. '#default_value' => $priority,
  134. ),
  135. 'old_priority' => array(
  136. '#type' => 'hidden',
  137. '#value' => $priority,
  138. ),
  139. 'id' => array(
  140. '#type' => 'hidden',
  141. '#value' => $priority,
  142. ),
  143. 'submit-edit_record' => array(
  144. '#type' => 'submit',
  145. '#name' => (string)$priority,
  146. '#value' => 'Edit Record',
  147. ),
  148. 'submit-add_field' => array(
  149. '#type' => 'submit',
  150. '#name' => (string)$priority,
  151. '#value' => 'Add Field',
  152. ),
  153. );
  154. foreach ($table_array['fields'] as $field_index => $field) {
  155. $form['fields']['fields-data'][$i] = array(
  156. 'record_id' => array(
  157. '#type' => 'item',
  158. '#value' => $table_array['record_id'],
  159. ),
  160. 'priority_hidden' => array(
  161. '#type' => 'hidden',
  162. '#value' => $priority,
  163. ),
  164. 'field_name' => array(
  165. '#type' => 'item',
  166. '#value' => $field['title'],
  167. ),
  168. 'chado_table_name' => array(
  169. '#type' => 'item',
  170. '#value' => $table_array['table'],
  171. ),
  172. 'chado_table_hidden' => array(
  173. '#type' => 'hidden',
  174. '#value' => $table_array['table'],
  175. ),
  176. 'chado_field_name' => array(
  177. '#type' => 'item',
  178. '#value' => $field['field'],
  179. ),
  180. 'sheet_name' => array(
  181. '#type' => 'item',
  182. '#value' => $field['spreadsheet sheet'],
  183. ),
  184. 'column_num' => array(
  185. '#type' => 'item',
  186. '#value' => $field['spreadsheet column'],
  187. ),
  188. 'constant_value' => array(
  189. '#type' => 'item',
  190. '#value' => $field['constant value'],
  191. ),
  192. 'field_index' => array(
  193. '#type' => 'hidden',
  194. '#value' => $field_index
  195. ),
  196. 'foreign_record_id' => array(
  197. '#type' => 'item',
  198. '#value' => $field['foreign key'],
  199. ),
  200. 'edit_submit' => array(
  201. '#type' => 'submit',
  202. '#name' => (string)$i,
  203. '#value' => "Edit Field",
  204. ),
  205. 'delete_submit' => array(
  206. '#type' => 'submit',
  207. '#name' => (string)$i,
  208. '#value' => "Delete Field",
  209. ),
  210. );
  211. $i++;
  212. }
  213. }
  214. $form['fields']['total_fields'] = array(
  215. '#type' => 'item',
  216. '#value' => $i,
  217. );
  218. }
  219. if ($form['records']['no_records']['#value']) {
  220. $form['records']['description'] = array(
  221. '#type' => 'item',
  222. '#value' => 'There are currently no records.',
  223. );
  224. unset($form['records']['submit-reorder']);
  225. $form['fields']['description'] = array(
  226. '#type' => 'item',
  227. '#value' => 'There are currently no fields.',
  228. );
  229. }
  230. $mode_title = (preg_match('/create/',$mode)) ? 'Create Template' : 'Edit Template';
  231. $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
  232. $form['submit'] = array(
  233. '#type' => 'submit',
  234. '#value' => $value,
  235. '#weight' => 4,
  236. );
  237. return $form;
  238. }
  239. /**
  240. *
  241. */
  242. function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state){
  243. $form_state['rebuild'] = TRUE;
  244. if ($form_state['storage']['template_id']) {
  245. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  246. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  247. $form_state['storage']['template'] = unserialize($result->template_array);
  248. }
  249. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  250. switch ($op) {
  251. // Initialize after template is chosen ----------------------------------------
  252. case 'Edit Template':
  253. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  254. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  255. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  256. $form_state['storage']['template'] = unserialize($result->template_array);
  257. $form_state['storage']['template_name'] = $result->name;
  258. $form_state['storage']['record2priority'] = array();
  259. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  260. if (!is_array($record_array)) { continue; }
  261. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  262. }
  263. break;
  264. case 'Create Template':
  265. $record = array(
  266. 'name' => $form_state['values']['new_template_name'],
  267. 'template_array' => array(),
  268. );
  269. drupal_write_record('tripal_bulk_loader_template', $record);
  270. $form_state['storage']['template_id'] = $record['template_id'];
  271. $form_state['storage']['template_name'] = $record['name'];
  272. $form_state['storage']['template'] = array();
  273. break;
  274. // Save Reordered Records -----------------------------------------------------
  275. case 'Save Order':
  276. $new_template = $form_state['storage']['template'];
  277. // unset old elements
  278. $form_state['storage']['record2priority'] = array();
  279. foreach ($new_template as $priority => $record_array) {
  280. if (preg_match('/\d+/', $priority)) { unset($new_template[$priority]); }
  281. }
  282. //set elements in new order
  283. foreach ($form_state['values']['records-data'] as $item) {
  284. $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
  285. $record_name = $new_template[$item['new_priority']]['record_id'];
  286. $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
  287. }
  288. ksort($new_template);
  289. $form_state['storage']['template'] = $new_template;
  290. break;
  291. case 'New Record/Field':
  292. $query = array(
  293. 'template_id'=> $form_state['storage']['template_id'],
  294. 'record_id' => 'NEW',
  295. );
  296. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  297. break;
  298. case 'Edit Record':
  299. $query = array(
  300. 'template_id'=> $form_state['storage']['template_id'],
  301. 'record_id' => $form_state['clicked_button']['#name'],
  302. );
  303. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_record', $query);
  304. break;
  305. case 'Add Field':
  306. $query = array(
  307. 'template_id'=> $form_state['storage']['template_id'],
  308. 'record_id' => $form_state['clicked_button']['#name'],
  309. );
  310. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  311. break;
  312. case 'Edit Field':
  313. $field_data_index = $form_state['clicked_button']['#name'];
  314. $query = array(
  315. 'template_id'=> $form_state['storage']['template_id'],
  316. 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'],
  317. 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'],
  318. );
  319. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query);
  320. break;
  321. case 'Delete Field':
  322. $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']];
  323. $priority = $field_data['priority_hidden'];
  324. $field_key = $field_data['field_index'];
  325. unset($form_state['storage']['template'][$priority]['fields'][$field_key]);
  326. if (!$form_state['storage']['template'][$priority]['fields']) {
  327. unset($form_state['storage']['record2priority'][$form_state['storage']['template'][$priority]['record_id']]);
  328. unset($form_state['storage']['template'][$priority]);
  329. }
  330. drupal_set_message('Deleted Field from Template.');
  331. break;
  332. } //end of switch
  333. // Save Template
  334. $record = array(
  335. 'template_id' => $form_state['storage']['template_id'],
  336. 'template_array' => serialize($form_state['storage']['template'])
  337. );
  338. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  339. drupal_set_message('Template Saved.');
  340. }
  341. //////////////////////////////////////////////////////////////////////////////////////
  342. // Delete Template
  343. //////////////////////////////////////////////////////////////////////////////////////
  344. /**
  345. * Delete Template Form
  346. * This form allows admin to delete already existing templates
  347. */
  348. function tripal_bulk_loader_delete_template_base_form () {
  349. $form = array();
  350. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  351. $resource = db_query($sql);
  352. $templates = array();
  353. $templates[''] = 'Select a Template';
  354. while ($r = db_fetch_object($resource)) {
  355. $templates[$r->template_id] = $r->name;
  356. }
  357. $form['template_name'] = array(
  358. '#title' => t('Template'),
  359. '#description' => t('Please select the template you would like to delete.'),
  360. '#type' => 'select',
  361. '#options' => $templates,
  362. '#weight' => 0,
  363. '#required' => TRUE,
  364. );
  365. $form['submit'] = array(
  366. '#type' => 'submit',
  367. '#value' => 'Delete Template',
  368. );
  369. return $form;
  370. }
  371. /**
  372. * Delete Template Form Submit
  373. *
  374. * @param $form
  375. * The form that was submitted
  376. * @param $form_state
  377. * The values and storage that were submitted
  378. */
  379. function tripal_bulk_loader_delete_template_base_form_submit ($form, &$form_state) {
  380. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  381. db_query($sql, $form_state['values']['template_name']);
  382. }
  383. /**
  384. * Import/Export Template Form
  385. *
  386. * On export, simply selects the serialized array from the db for a given template
  387. * and presents it to the user. On import, a serialized template array and a name is
  388. * supplied and a template record is created.
  389. *
  390. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  391. *
  392. * @param $form_state
  393. * The values and storage for the form
  394. * @param $mode
  395. * Either 'import' or 'export' to indicate which function is being performed
  396. * @return
  397. * A form array to be rendered by drupal_get_form
  398. */
  399. function tripal_bulk_loader_import_export_template_form ($form_state = NULL, $mode) {
  400. $form = array();
  401. $form['mode'] = array(
  402. '#type' => 'hidden',
  403. '#value' => $mode,
  404. );
  405. if (preg_match('/import/', $mode)) {
  406. $form['new_template_name'] = array(
  407. '#type' => 'textfield',
  408. '#title' => 'Template Name',
  409. '#weight' => 1,
  410. );
  411. } elseif (preg_match('/export/', $mode)) {
  412. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  413. $resource = db_query($sql);
  414. $templates = array();
  415. $templates[''] = 'Select a Template';
  416. while ($r = db_fetch_object($resource)) {
  417. $templates[$r->template_id] = $r->name;
  418. }
  419. $form['template_id'] = array(
  420. '#title' => t('Template'),
  421. '#description' => t('Please select the template you would like to edit.'),
  422. '#type' => 'select',
  423. '#options' => $templates,
  424. '#default_value' => $form_state['storage']['template_id'],
  425. '#weight' => 0,
  426. '#required' => TRUE,
  427. '#weight' => 1,
  428. );
  429. }
  430. $form['template_array'] = array(
  431. '#type' => 'textarea',
  432. '#title' => 'Template Array',
  433. '#default_value' => $form_state['storage']['template_array'],
  434. '#weight' => 2,
  435. );
  436. $form['submit'] = array(
  437. '#type' => 'submit',
  438. '#value' => 'Submit',
  439. '#weight' => 10,
  440. );
  441. return $form;
  442. }
  443. /**
  444. * Import/Export Template Form Submit
  445. *
  446. * @param $form
  447. * The form that was submitted
  448. * @param $form_state
  449. * The values and storage that were submitted
  450. */
  451. function tripal_bulk_loader_import_export_template_form_submit ($form, &$form_state) {
  452. switch ($form_state['values']['mode']) {
  453. case 'export':
  454. $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
  455. $form_state['storage']['template_array'] = $record->template_array;
  456. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  457. break;
  458. case 'import':
  459. $record = array(
  460. 'name' => $form_state['values']['new_template_name'],
  461. 'template_array' => $form_state['values']['template_array'],
  462. );
  463. drupal_write_record('tripal_bulk_loader_template', $record);
  464. if ($record->template_id) {
  465. drupal_set_message('Successfully imported Tripal Bulk Loader Template.');
  466. }
  467. break;
  468. }
  469. }
  470. //////////////////////////////////////////////////////////////////////////////////////
  471. // Add/Edit Field/Record Forms
  472. //////////////////////////////////////////////////////////////////////////////////////
  473. /**
  474. * Edit Record Form
  475. *
  476. * This form is meant to be called from a bulk loader form. The following should be set
  477. * in the query section of the path:
  478. * - template_id=\d+: the template which the edited record is part of
  479. * - record_id=\d+: the priority or key in the template array of the record to be edited
  480. *
  481. * @param $form_state
  482. * Contains the values and storage for the form
  483. * @return
  484. * A form array to be rendered by drupal_get_form
  485. */
  486. function tripal_bulk_loader_edit_template_record_form (&$form_state = NULL) {
  487. $form['#cache'] = TRUE; // Make sure the form is cached.
  488. // get template id from path
  489. $template_id = ($_GET['template_id'] !== NULL) ? $_GET['template_id'] : $form_state['values']['template_id'];
  490. // if there is no template supplied don't return rest of form
  491. if (!$template_id) {
  492. return $form;
  493. }
  494. // Pre-process values/defaults ---------------------------
  495. // If this is the first load of the form (no form state) we need to initialize some variables
  496. if (!$form_state['storage']['template']) {
  497. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  498. $template = db_fetch_object(db_query($sql, $template_id));
  499. $form_state['storage']['template_array'] = unserialize($template->template_array);
  500. $form_state['storage']['template'] = $template;
  501. $form_state['storage']['record2priority'] = array();
  502. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  503. if (!is_array($record_array)) { continue; }
  504. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  505. }
  506. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  507. } else {
  508. $template = $form_state['storage']['template'];
  509. }
  510. // get the record_id from the path
  511. if ($_GET['record_id'] !== NULL) {
  512. $form_state['values']['field_group'] = $_GET['record_id'];
  513. $form_state['storage']['original_priority'] = $_GET['record_id'];
  514. }
  515. // Tables and default table
  516. $tables = tripal_core_get_chado_tables();
  517. if ($form_state['values']['chado_table']) {
  518. $table = $form_state['values']['chado_table'];
  519. } else {
  520. $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
  521. }
  522. //dpm($form_state, 'form state');
  523. // Form Proper -------------------------------------------
  524. $form['template_name'] = array(
  525. '#type' => 'item',
  526. '#title' => 'Template',
  527. '#value' => $template->name,
  528. );
  529. $form['template_id'] = array(
  530. '#type' => 'hidden',
  531. '#value' => $template_id,
  532. );
  533. $form['edit_record'] = array(
  534. '#type' => 'fieldset',
  535. );
  536. // check template array for records then add one more
  537. if (!$form_state['storage']['record2priority']) {
  538. $groups = array();
  539. } else {
  540. $groups = array_flip($form_state['storage']['record2priority']);
  541. }
  542. $priority_default = $form_state['values']['field_group'];
  543. $form['edit_record']['field_group'] = array(
  544. '#type' => 'select',
  545. '#title' => 'Record',
  546. '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
  547. '#options' => $groups,
  548. '#default_value' => $priority_default,
  549. '#required' => TRUE,
  550. );
  551. $form['edit_record']['record_name'] = array(
  552. '#type' => 'textfield',
  553. '#title' => 'Unique Record Name',
  554. '#default_value' => $groups[$priority_default],
  555. );
  556. $form['edit_record']['chado_table'] = array(
  557. '#type' => 'select',
  558. '#title' => t('Chado Table'),
  559. '#description' => 'This changes the chado table for all fields in this record.',
  560. '#options' => $tables,
  561. '#default_value' => $table,
  562. );
  563. $form['edit_record']['mode'] = array(
  564. '#type' => 'radios',
  565. '#title' => 'Action to take when Loading Record',
  566. '#options' => array(
  567. 'insert' => 'Insert the record if it doesn\'t already exist',
  568. 'select' => 'Don\'t insert this record: it\'s used to define a foreign key in another record',
  569. 'optional' => 'Record will only be inserted if all required data is filled in'
  570. ),
  571. '#default_value' => 'insert'
  572. );
  573. $form['edit_record']['submit-edit_record'] = array(
  574. '#type' => 'submit',
  575. '#value' => 'Edit Record'
  576. );
  577. $form['edit_record']['submit-cancel'] = array(
  578. '#type' => 'submit',
  579. '#value' => 'Cancel'
  580. );
  581. return $form;
  582. }
  583. /**
  584. * Edit Record Form Submit
  585. *
  586. * @param $form
  587. * The form that was submitted
  588. * @param $form_state
  589. * Contains the values and storage for the form
  590. */
  591. function tripal_bulk_loader_edit_template_record_form_submit ($form, &$form_state) {
  592. //dpm($form_state, 'form state -start submit');
  593. if (!$form_state['ahah_submission']) {
  594. if ($form_state['values']['op'] == 'Edit Record') {
  595. $template = $form_state['storage']['template_array'];
  596. // Edit Record
  597. $record = $template[ $form_state['storage']['original_priority'] ];
  598. $record['record_id'] = $form_state['values']['record_name'];
  599. $record['mode'] = $form_state['values']['mode'];
  600. $record['table'] = $form_state['values']['chado_table'];
  601. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  602. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  603. $template[ $form_state['values']['field_group'] ] = $record;
  604. unset($template[ $form_state['storage']['original_priority'] ]);
  605. } else {
  606. $template[ $form_state['storage']['original_priority'] ] = $record;
  607. }
  608. // Save Template
  609. $form_state['storage']['template']->template_array = serialize($template);
  610. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  611. if ($success) {
  612. drupal_set_message('Successfully Updated Template Record');
  613. drupal_set_message('Template Saved.');
  614. $path = explode('?',$form_state['storage']['referring URL']);
  615. parse_str($path[1], $query);
  616. $query['template_id'] = $form_state['storage']['template']->template_id;
  617. drupal_goto($path[0], $query);
  618. } else {
  619. drupal_set_message('Unable to Save Template!', 'error');
  620. watchdog('T_bulk_loader',
  621. 'Unable to save bulk loader template: %template',
  622. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  623. WATCHDOG_ERROR
  624. );
  625. }
  626. } elseif ($form_state['values']['op'] == 'Cancel') {
  627. $path = explode('?',$form_state['storage']['referring URL']);
  628. parse_str($path[1], $query);
  629. $query['template_id'] = $form_state['storage']['template']->template_id;
  630. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  631. drupal_goto($path[0], $query);
  632. }
  633. }
  634. }
  635. /**
  636. * Add Field Form
  637. *
  638. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  639. * can use the following in the query of the path to set defaults for a given template:
  640. * - template_id=\d+: the template to add the field to
  641. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  642. *
  643. * @param $form_state
  644. * Contains the values and storage for the form
  645. * @return
  646. * A form array to be rendered by drupal_get_form
  647. */
  648. function tripal_bulk_loader_add_template_field_form (&$form_state = NULL) {
  649. $form = array();
  650. $form['#cache'] = TRUE; // Make sure the form is cached.
  651. // get template id from path
  652. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  653. // if there is no template supplied don't return rest of form
  654. if (!$template_id) {
  655. return $form;
  656. }
  657. // Pre-set Variables needed for form proper------------------------------------------
  658. // If this is the first load of the form (no form state) we need to initialize some variables
  659. if (!$form_state['storage']['template']) {
  660. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  661. $template = db_fetch_object(db_query($sql, $template_id));
  662. $form_state['storage']['template_array'] = unserialize($template->template_array);
  663. $form_state['storage']['template'] = $template;
  664. $form_state['storage']['record2priority'] = array();
  665. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  666. if (!is_array($record_array)) { continue; }
  667. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  668. }
  669. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  670. } else {
  671. $template = $form_state['storage']['template'];
  672. }
  673. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  674. // Tables and default table
  675. $tables = tripal_core_get_chado_tables();
  676. if ($form_state['values']) {
  677. if (!preg_match('/^'.current($tables).'$/', $form_state['values']['chado_table'])) {
  678. $table = $form_state['values']['chado_table'];
  679. } elseif ($form_state['values']['record_name']) {
  680. $record_name = $form_state['values']['record_name'];
  681. $priority = $form_state['storage']['record2priority'][$record_name];
  682. $table = $form_state['storage']['template_array'][$priority]['table'];
  683. } else {
  684. $priority = $form_state['values']['field_group'];
  685. $table = $form_state['storage']['template_array'][$priority]['table'];
  686. }
  687. }
  688. if (!$table) {
  689. $table = reset($tables);
  690. }
  691. // get the record_id from the path
  692. if ($_GET['record_id'] !== NULL) {
  693. $form_state['values']['field_group'] = $_GET['record_id'];
  694. if (preg_match('/\d+/', $_GET['record_id'])) {
  695. $priority = $form_state['values']['field_group'];
  696. $table = $form_state['storage']['template_array'][$priority]['table'];
  697. }
  698. }
  699. // Fields and foreign key mappings
  700. $chado_fields = array();
  701. $fk_options = array();
  702. $fk_options['NULL'] = 'None';
  703. $table_description = module_invoke_all('chado_'.$table.'_schema');
  704. if ($field_type == 'foreign key') {
  705. $foreign_field2table = array();
  706. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  707. foreach ($key_array['columns'] as $left_field => $right_field) {
  708. $chado_fields[$left_field] = $left_field;
  709. $foreign_field2table[$left_field] = $key_table;
  710. }
  711. }
  712. reset($chado_fields);
  713. // set default field
  714. if (empty($chado_fields)) {
  715. $field = NULL;
  716. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  717. $field = $form_state['values']['chado_field'];
  718. } else {
  719. $field = current($chado_fields);
  720. }
  721. // Foreign key options
  722. $foreign_table = $foreign_field2table[$field];
  723. if ($foreign_table) {
  724. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  725. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priority]['table'])) {
  726. $fk_options[$record_name] = $record_name;
  727. }
  728. }
  729. }
  730. } else {
  731. foreach($table_description['fields'] as $field_name => $field_array) {
  732. $chado_fields[$field_name] = $field_name;
  733. }
  734. }
  735. $variables = array(
  736. 'form_state' => $form_state,
  737. 'tables' => $tables,
  738. 'default table' => $table,
  739. 'fields' => $chado_fields,
  740. 'default_field' => $field,
  741. 'priority' => $priority,
  742. 'record_name' => $record_name,
  743. 'table description' => $table_description,
  744. 'foreign key options' => $fk_options,
  745. 'foreign field=>table' => $foreign_field2table,
  746. 'foreign table' => $foreign_table,
  747. );
  748. //dpm($variables, 'variables');
  749. // Start of Form Proper--------------------------------------------------------------
  750. $form['template_name'] = array(
  751. '#type' => 'item',
  752. '#title' => 'Template',
  753. '#value' => $template->name,
  754. );
  755. $form['template_id'] = array(
  756. '#type' => 'hidden',
  757. '#value' => $template_id,
  758. );
  759. $form['add_fields'] = array(
  760. '#type' => 'fieldset',
  761. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  762. '#suffix' => '</div>',
  763. );
  764. $form['add_fields']['field_type'] = array(
  765. '#type' => 'radios',
  766. '#title' => t('Type of Field'),
  767. '#options' => array(
  768. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  769. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  770. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  771. ),
  772. '#required' => TRUE,
  773. '#default_value' => $field_type,
  774. '#ahah' => array(
  775. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  776. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  777. 'effect' => 'fade'
  778. ),
  779. );
  780. // check template array for records then add one more
  781. if (!$form_state['storage']['record2priority']) {
  782. $groups = array();
  783. } else {
  784. $groups = array_flip($form_state['storage']['record2priority']);
  785. }
  786. $groups['NONE'] = 'Select a Record';
  787. $groups['NEW'] = 'New Record';
  788. $form['add_fields']['field_group'] = array(
  789. '#type' => 'select',
  790. '#title' => 'Record',
  791. '#description' => 'This is used to group a set of fields together allowing '
  792. .'multiple records to be inserted into the same table per line of the spreadsheet',
  793. '#options' => $groups,
  794. '#default_value' => (preg_match('/\w+.*/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  795. '#ahah' => array(
  796. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  797. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  798. 'effect' => 'fade'
  799. ),
  800. '#required' => TRUE,
  801. );
  802. $form['add_fields']['record_name'] = array(
  803. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  804. '#title' => 'Unique Record Name',
  805. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  806. '#suffix' => '</div>',
  807. '#default_value' => $form_state['values']['record_name'],
  808. );
  809. $form['add_fields']['field_title'] = array(
  810. '#type' => 'textfield',
  811. '#title' => t('Human-readable Title for Field'),
  812. '#default_value' => $form_state['values']['field_title'],
  813. );
  814. // Spreadsheet column
  815. $form['add_fields']['columns'] = array(
  816. '#type' => 'fieldset',
  817. '#title' => t('Spreadsheet Column'),
  818. '#collapsible' => TRUE,
  819. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  820. );
  821. $form['add_fields']['columns']['sheet_name'] = array(
  822. '#type' => 'textfield',
  823. '#title' => t('Worksheet'),
  824. '#description' => t('Specify the name of the worksheet.'),
  825. '#size' => 5,
  826. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  827. );
  828. $form['add_fields']['columns']['column_number'] = array(
  829. '#type' => 'textfield',
  830. '#title' => t('Column'),
  831. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  832. '#size' => 5,
  833. '#default_value' => $form_state['values']['column_number'],
  834. );
  835. // Global Value
  836. $form['add_fields']['constant'] = array(
  837. '#type' => 'fieldset',
  838. '#title' => t('Constant'),
  839. '#collapsible' => TRUE,
  840. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  841. );
  842. $form['add_fields']['constant']['constant_value'] = array(
  843. '#type' => 'textfield',
  844. '#title' => t('Constant Value'),
  845. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  846. '#default_value' => $form_state['values']['constant_value']
  847. );
  848. // Foreign Key
  849. $form['add_fields']['foreign_key'] = array(
  850. '#type' => 'fieldset',
  851. '#title' => 'Foreign Key',
  852. '#collapsible' => TRUE,
  853. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  854. );
  855. $form['add_fields']['foreign_key']['foreign_record'] = array(
  856. '#type' => 'select',
  857. '#title' => 'Record to refer to',
  858. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  859. '#options' => $fk_options,
  860. );
  861. // Chado Field
  862. $form['add_fields']['chado'] = array(
  863. '#type' => 'fieldset',
  864. '#title' => t('Chado Field/Column Details'),
  865. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  866. );
  867. $form['add_fields']['chado']['chado_table'] = array(
  868. '#type' => 'select',
  869. '#title' => t('Chado Table'),
  870. '#options' => $tables,
  871. '#default_value' => $table,
  872. '#ahah' => array(
  873. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  874. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  875. 'effect' => 'fade'
  876. ),
  877. );
  878. $form['add_fields']['chado']['chado_field'] = array(
  879. '#type' => 'select',
  880. '#title' => t('Chado Field/Column'),
  881. '#options' => $chado_fields,
  882. '#default_value' => $form_state['values']['chado_field'],
  883. '#ahah' => array(
  884. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  885. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  886. 'effect' => 'fade'
  887. ),
  888. );
  889. $form['add_fields']['submit-add_field'] = array(
  890. '#type' => 'submit',
  891. '#value' => 'Add Field'
  892. );
  893. $form['add_fields']['submit-cancel'] = array(
  894. '#type' => 'submit',
  895. '#value' => 'Cancel'
  896. );
  897. return $form;
  898. }
  899. /**
  900. * Add Field Submit
  901. *
  902. * @param $form
  903. * The form that was submitted
  904. * @param $form_state
  905. * The values and storage for the form
  906. */
  907. function tripal_bulk_loader_add_template_field_form_submit ($form, &$form_state) {
  908. if (!$form_state['ahah_submission']) {
  909. if ($form_state['values']['op'] == 'Add Field') {
  910. $template = $form_state['storage']['template_array'];
  911. // If new record
  912. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  913. $record_name = $form_state['values']['record_name'];
  914. $priority = sizeof($form_state['storage']['template_array']) + 1;
  915. $record2priority[$record_name] = $priority;
  916. $template[$priority]['table'] = $form_state['values']['chado_table'];
  917. $template[$priority]['record_id'] = $record_name;
  918. } else {
  919. $priority = $form_state['values']['field_group'];
  920. $record_name = $record2priority[$priority];
  921. }
  922. // Add field to template array
  923. if ($form_state['values']['field_type'] == 'table field') {
  924. $template[$priority]['fields'][] = array(
  925. 'type' => 'table field',
  926. 'title' => $form_state['values']['field_title'],
  927. 'field' => $form_state['values']['chado_field'],
  928. //'required' => <true|false>,
  929. //'allowed values' => empty by default,
  930. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  931. 'spreadsheet column' => $form_state['values']['column_number'],
  932. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  933. //'mapping' => array(
  934. // 'from' => 'to'
  935. // '/from re/' => 'to'
  936. //),
  937. );
  938. } elseif ($form_state['values']['field_type'] == 'constant') {
  939. $template[$priority]['fields'][] = array(
  940. 'type' => 'constant',
  941. 'title' => $form_state['values']['field_title'],
  942. 'field' => $form_state['values']['chado_field'],
  943. //'required' => <true|false>,
  944. //'allowed values' => empty by default,
  945. 'constant value' => $form_state['values']['constant_value'],
  946. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  947. );
  948. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  949. $template[$priority]['fields'][] = array(
  950. 'type' => 'foreign key',
  951. 'title' => $form_state['values']['field_title'],
  952. 'field' => $form_state['values']['chado_field'],
  953. 'foreign key' => $form_state['values']['foreign_record'],
  954. //'required' => <true|false>,
  955. );
  956. }
  957. // Save Template
  958. $form_state['storage']['template']->template_array = serialize($template);
  959. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  960. if ($success) {
  961. drupal_set_message('Successfully Added Field to Template');
  962. drupal_set_message('Template Saved.');
  963. $path = explode('?',$form_state['storage']['referring URL']);
  964. parse_str($path[1], $query);
  965. $query['template_id'] = $form_state['storage']['template']->template_id;
  966. drupal_goto($path[0], $query);
  967. } else {
  968. drupal_set_message('Unable to Save Template!', 'error');
  969. watchdog('T_bulk_loader',
  970. 'Unable to save bulk loader template: %template',
  971. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  972. WATCHDOG_ERROR
  973. );
  974. }
  975. } elseif ($form_state['values']['op'] == 'Cancel') {
  976. $path = explode('?',$form_state['storage']['referring URL']);
  977. parse_str($path[1], $query);
  978. $query['template_id'] = $form_state['storage']['template']->template_id;
  979. drupal_goto($path[0], $query);
  980. }
  981. }
  982. }
  983. /**
  984. * Edit Field Form
  985. *
  986. * This form is meant to be called from a bulk loader form. The following should be set
  987. * in the query section of the path:
  988. * - template_id=\d+: the template which the edited field is part of
  989. * - record_id=\d+: the priority or key in the template array of the record the field
  990. * is currently part of
  991. * - field_index=\d+: the key of the field in the fields array of the previously
  992. * specified record
  993. *
  994. * @param $form_state
  995. * Contains the values and storage for the form
  996. * @return
  997. * A form array to be rendered by drupal_get_form
  998. */
  999. function tripal_bulk_loader_edit_template_field_form (&$form_state = NULL) {
  1000. $form = array();
  1001. $form['#cache'] = TRUE; // Make sure the form is cached.
  1002. // get template id from path
  1003. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1004. // if there is no template supplied don't return rest of form
  1005. if (!$template_id) {
  1006. return $form;
  1007. }
  1008. // Pre-set Variables needed for form proper------------------------------------------
  1009. // If this is the first load of the form (no form state) we need to initialize some variables
  1010. if (!$form_state['storage']['template']) {
  1011. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1012. $template = db_fetch_object(db_query($sql, $template_id));
  1013. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1014. $form_state['storage']['template'] = $template;
  1015. $form_state['storage']['record2priority'] = array();
  1016. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1017. if (!is_array($record_array)) { continue; }
  1018. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1019. }
  1020. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1021. } else {
  1022. $template = $form_state['storage']['template'];
  1023. }
  1024. // get the field from the path
  1025. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1026. $priority = $_GET['record_id'];
  1027. $field_index = $_GET['field_index'];
  1028. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1029. $form_state['storage']['original_field'] = $template_field;
  1030. $form_state['storage']['original_field']['priority'] = $priority;
  1031. $form_state['storage']['original_field']['field_index'] = $field_index;
  1032. }
  1033. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1034. // Tables and default table
  1035. $tables = tripal_core_get_chado_tables();
  1036. if ($form_state['values']) {
  1037. $table = $form_state['values']['chado_table'];
  1038. } else {
  1039. $table = $form_state['storage']['template_array'][$priority]['table'];
  1040. }
  1041. // Fields and foreign key mappings
  1042. $chado_fields = array();
  1043. $fk_options = array();
  1044. $fk_options['NULL'] = 'None';
  1045. $table_description = module_invoke_all('chado_'.$table.'_schema');
  1046. if ($field_type == 'foreign key') {
  1047. $foreign_field2table = array();
  1048. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1049. foreach ($key_array['columns'] as $left_field => $right_field) {
  1050. $chado_fields[$left_field] = $left_field;
  1051. $foreign_field2table[$left_field] = $key_table;
  1052. }
  1053. }
  1054. reset($chado_fields);
  1055. // set default field
  1056. if (empty($chado_fields)) {
  1057. $field = NULL;
  1058. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1059. $field = $form_state['values']['chado_field'];
  1060. } else {
  1061. $field = current($chado_fields);
  1062. }
  1063. // Foreign key options
  1064. $foreign_table = $foreign_field2table[$field];
  1065. if ($foreign_table) {
  1066. foreach ($form_state['storage']['record2priority'] as $record_nameT => $priorityT ) {
  1067. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priorityT]['table'])) {
  1068. $fk_options[$record_nameT] = $record_nameT;
  1069. }
  1070. }
  1071. }
  1072. } else {
  1073. foreach($table_description['fields'] as $field_name => $field_array) {
  1074. $chado_fields[$field_name] = $field_name;
  1075. }
  1076. }
  1077. // dpm(array( 'tables' => $tables, 'default table' => $table, 'record name' => $record_name, 'priority' => $priority,
  1078. // 'fields' => $chad_fields, 'default field' => $field, 'foreign field=>table' => $foreign_field2table,
  1079. // 'table desc' => $table_description, 'foreign record options' => $fk_options, 'foreign table' => $foreign_table
  1080. // ), 'Variables');
  1081. // Start of Form Proper--------------------------------------------------------------
  1082. $form['template_name'] = array(
  1083. '#type' => 'item',
  1084. '#title' => 'Template',
  1085. '#value' => $template->name,
  1086. );
  1087. $form['template_id'] = array(
  1088. '#type' => 'hidden',
  1089. '#value' => $template_id,
  1090. );
  1091. $form['edit_fields'] = array(
  1092. '#type' => 'fieldset',
  1093. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1094. '#suffix' => '</div>',
  1095. );
  1096. $form['edit_fields']['field_type'] = array(
  1097. '#type' => 'radios',
  1098. '#title' => t('Type of Field'),
  1099. '#options' => array(
  1100. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  1101. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  1102. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  1103. ),
  1104. '#required' => TRUE,
  1105. '#default_value' => $field_type,
  1106. '#ahah' => array(
  1107. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1108. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1109. 'effect' => 'fade'
  1110. ),
  1111. );
  1112. // check template array for records then edit one more
  1113. if (!$form_state['storage']['record2priority']) {
  1114. $groups = array();
  1115. } else {
  1116. $groups = array_flip($form_state['storage']['record2priority']);
  1117. }
  1118. $groups['NONE'] = 'Select a Record';
  1119. $groups['NEW'] = 'New Record';
  1120. $form['edit_fields']['field_group'] = array(
  1121. '#type' => 'select',
  1122. '#title' => 'Record',
  1123. '#description' => 'This is used to group a set of fields together allowing '
  1124. .'multiple records to be inserted into the same table per line of the spreadsheet',
  1125. '#options' => $groups,
  1126. '#default_value' => (preg_match('/(\d+|\w+)/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1127. '#ahah' => array(
  1128. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1129. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1130. 'effect' => 'fade'
  1131. ),
  1132. '#required' => TRUE,
  1133. );
  1134. $form['edit_fields']['record_name'] = array(
  1135. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1136. '#title' => 'Unique Record Name',
  1137. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1138. '#suffix' => '</div>',
  1139. '#default_value' => $form_state['values']['record_name'],
  1140. );
  1141. $form['edit_fields']['field_title'] = array(
  1142. '#type' => 'textfield',
  1143. '#title' => t('Human-readable Title for Field'),
  1144. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1145. );
  1146. // Spreadsheet column
  1147. $form['edit_fields']['columns'] = array(
  1148. '#type' => 'fieldset',
  1149. '#title' => t('Spreadsheet Column'),
  1150. '#collapsible' => TRUE,
  1151. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1152. );
  1153. $form['edit_fields']['columns']['sheet_name'] = array(
  1154. '#type' => 'textfield',
  1155. '#title' => t('Worksheet'),
  1156. '#description' => t('Specify the name of the worksheet.'),
  1157. '#size' => 5,
  1158. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1159. );
  1160. $form['edit_fields']['columns']['column_number'] = array(
  1161. '#type' => 'textfield',
  1162. '#title' => t('Column'),
  1163. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  1164. '#size' => 5,
  1165. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1166. );
  1167. // Global Value
  1168. $form['edit_fields']['constant'] = array(
  1169. '#type' => 'fieldset',
  1170. '#title' => t('Constant'),
  1171. '#collapsible' => TRUE,
  1172. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1173. );
  1174. $form['edit_fields']['constant']['constant_value'] = array(
  1175. '#type' => 'textfield',
  1176. '#title' => t('Constant Value'),
  1177. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  1178. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1179. );
  1180. // Foreign Key
  1181. $form['edit_fields']['foreign_key'] = array(
  1182. '#type' => 'fieldset',
  1183. '#title' => 'Foreign Key',
  1184. '#collapsible' => TRUE,
  1185. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1186. );
  1187. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1188. '#type' => 'select',
  1189. '#title' => 'Record to refer to',
  1190. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1191. '#options' => $fk_options,
  1192. );
  1193. // Chado Field
  1194. $form['edit_fields']['chado'] = array(
  1195. '#type' => 'fieldset',
  1196. '#title' => t('Chado Field/Column Details'),
  1197. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1198. );
  1199. $form['edit_fields']['chado']['chado_table'] = array(
  1200. '#type' => 'select',
  1201. '#title' => t('Chado Table'),
  1202. '#options' => $tables,
  1203. '#default_value' => $table,
  1204. '#ahah' => array(
  1205. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1206. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1207. 'effect' => 'fade'
  1208. ),
  1209. );
  1210. $form['edit_fields']['chado']['chado_field'] = array(
  1211. '#type' => 'select',
  1212. '#title' => t('Chado Field/Column'),
  1213. '#options' => $chado_fields,
  1214. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1215. '#ahah' => array(
  1216. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1217. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1218. 'effect' => 'fade'
  1219. ),
  1220. );
  1221. $form['edit_fields']['additional'] = array(
  1222. '#type' => 'fieldset',
  1223. '#title' => 'Additional Options',
  1224. );
  1225. $form['edit_fields']['additional']['required'] = array(
  1226. '#type' => 'checkbox',
  1227. '#title' => 'Make this file required',
  1228. '#default_value' => FALSE,
  1229. );
  1230. $form['edit_fields']['submit-edit_field'] = array(
  1231. '#type' => 'submit',
  1232. '#value' => 'Edit Field'
  1233. );
  1234. $form['edit_fields']['submit-cancel'] = array(
  1235. '#type' => 'submit',
  1236. '#value' => 'Cancel'
  1237. );
  1238. return $form;
  1239. }
  1240. /**
  1241. * Edit Field Form Submit
  1242. *
  1243. * @param $form
  1244. * The form that was submitted
  1245. * @param $form_state
  1246. * The values and storage for the form
  1247. */
  1248. function tripal_bulk_loader_edit_template_field_form_submit ($form, &$form_state) {
  1249. if (!$form_state['ahah_submission']) {
  1250. if ($form_state['values']['op'] == 'Edit Field') {
  1251. // If new record
  1252. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  1253. // add new record
  1254. $record_name = $form_state['values']['record_name'];
  1255. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1256. $old_priority = $form_state['storage']['original_field']['priority'];
  1257. $field_index = $form_state['storage']['original_field']['field_index'];
  1258. $form_state['storage']['record2priority'][$record_name] = $priority;
  1259. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1260. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1261. } else {
  1262. $priority = $form_state['values']['field_group'];
  1263. $old_priority = $form_state['storage']['original_field']['priority'];
  1264. $field_index = $form_state['storage']['original_field']['field_index'];
  1265. $record_name = $form_state['storage']['record2priority'][$priority];
  1266. }
  1267. if ($form_state['values']['field_type'] == 'table field') {
  1268. $field = array(
  1269. 'type' => 'table field',
  1270. 'title' => $form_state['values']['field_title'],
  1271. 'field' => $form_state['values']['chado_field'],
  1272. 'required' => $form_state['values']['required'],
  1273. //'allowed values' => empty by default,
  1274. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  1275. 'spreadsheet column' => $form_state['values']['column_number'],
  1276. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1277. //'mapping' => array(
  1278. // 'from' => 'to'
  1279. // '/from re/' => 'to'
  1280. //),
  1281. );
  1282. } elseif ($form_state['values']['field_type'] == 'constant') {
  1283. $field = array(
  1284. 'type' => 'constant',
  1285. 'title' => $form_state['values']['field_title'],
  1286. 'field' => $form_state['values']['chado_field'],
  1287. 'required' => $form_state['values']['required'],
  1288. //'allowed values' => empty by default,
  1289. 'constant value' => $form_state['values']['constant_value'],
  1290. //'exposed' => 'true|false' If exposed, will give a select box first from allowed values if set, second from database if values not set.
  1291. );
  1292. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  1293. $field = array(
  1294. 'type' => 'foreign key',
  1295. 'title' => $form_state['values']['field_title'],
  1296. 'field' => $form_state['values']['chado_field'],
  1297. 'foreign key' => $form_state['values']['foreign_record'],
  1298. 'required' => $form_state['values']['required'],
  1299. );
  1300. }
  1301. // Deal with any additional options
  1302. dpm($form_state['values'], 'values');
  1303. // if the record has changed...
  1304. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1305. if ($old_priority != $priority) {
  1306. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1307. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1308. // if there are no fields left delete the old record
  1309. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1310. unset($form_state['storage']['template_array'][$old_priority]);
  1311. }
  1312. } else {
  1313. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1314. }
  1315. // Save Template
  1316. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1317. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1318. if ($success) {
  1319. drupal_set_message('Successfully Updated Field');
  1320. drupal_set_message('Template Saved.');
  1321. $path = explode('?',$form_state['storage']['referring URL']);
  1322. parse_str($path[1], $query);
  1323. $query['template_id'] = $form_state['storage']['template']->template_id;
  1324. drupal_goto($path[0], $query);
  1325. } else {
  1326. drupal_set_message('Unable to Save Template!', 'error');
  1327. watchdog('T_bulk_loader',
  1328. 'Unable to save bulk loader template: %template',
  1329. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1330. WATCHDOG_ERROR
  1331. );
  1332. }
  1333. } elseif ($form_state['values']['op'] == 'Cancel') {
  1334. $path = explode('?',$form_state['storage']['referring URL']);
  1335. parse_str($path[1], $query);
  1336. $query['template_id'] = $form_state['storage']['template']->template_id;
  1337. drupal_goto($path[0], $query);
  1338. }
  1339. }
  1340. }
  1341. //////////////////////////////////////////////////////////////////////////////////////
  1342. // AHAH Callbacks
  1343. //////////////////////////////////////////////////////////////////////////////////////
  1344. /**
  1345. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  1346. *
  1347. * @return
  1348. * JSON Data printed to the screen
  1349. */
  1350. function tripal_bulk_loader_add_field_ahah () {
  1351. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1352. $form_build_id = $_POST['form_build_id'];
  1353. $form = form_get_cache($form_build_id, $form_state);
  1354. $args = $form['#parameters'];
  1355. $form_id = array_shift($args);
  1356. $form_state['post'] = $form['#post'] = $_POST;
  1357. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1358. $form_state['ahah_submission'] = TRUE;
  1359. $form['#programmed'] = $form['#redirect'] = FALSE;
  1360. drupal_process_form($form_id, $form, $form_state);
  1361. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1362. $form_element = $form['add_fields'];
  1363. // Remove the wrapper so we don't double it up.
  1364. unset($form_element['#prefix'], $form_element['#suffix']);
  1365. $output = theme('status_messages');
  1366. $output .= drupal_render($form_element);
  1367. // Final rendering callback.
  1368. print drupal_json(array('status' => TRUE, 'data' => $output));
  1369. exit();
  1370. }
  1371. /**
  1372. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  1373. *
  1374. * @return
  1375. * JSON Data printed to the screen
  1376. */
  1377. function tripal_bulk_loader_edit_field_ahah () {
  1378. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1379. $form_build_id = $_POST['form_build_id'];
  1380. $form = form_get_cache($form_build_id, $form_state);
  1381. $args = $form['#parameters'];
  1382. $form_id = array_shift($args);
  1383. $form_state['post'] = $form['#post'] = $_POST;
  1384. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1385. $form_state['ahah_submission'] = TRUE;
  1386. $form['#programmed'] = $form['#redirect'] = FALSE;
  1387. drupal_process_form($form_id, $form, $form_state);
  1388. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1389. $form_element = $form['edit_fields'];
  1390. // Remove the wrapper so we don't double it up.
  1391. unset($form_element['#prefix'], $form_element['#suffix']);
  1392. $output = theme('status_messages');
  1393. $output .= drupal_render($form_element);
  1394. // Final rendering callback.
  1395. print drupal_json(array('status' => TRUE, 'data' => $output));
  1396. exit();
  1397. }