tripal_bulk_loader.constants.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /**
  3. * Inserts/Updates a tripal bulk loading job constant
  4. *
  5. * @param $nid
  6. * The node ID of the the tripal bulk loading job the constant is associated with
  7. * @param $table
  8. * The chado table the constant is associated with
  9. * @param $field
  10. * The chado field the constant is associated with
  11. * @param $record_id
  12. * The index in the template array for this record
  13. * @param $field_id
  14. * The index in the template array for this field
  15. *
  16. * NOTE: $template_array[$record_id]['table'] = $table and $template_array[$record_id]['fields'][$field_id]['field'] = $field
  17. * both are included as a means of double-checking the constant still is still in thesame place in the template array.
  18. * For example, that the template was not edited and the records moved around after the job was submitted but before it was run.
  19. *
  20. * @return
  21. * On success it returns the object (with primary key if inserted);
  22. * on failure it returns FALSE
  23. */
  24. function tripal_bulk_loader_update_constant ($nid, $group_id, $table, $field, $record_id, $field_id, $value) {
  25. $record = array(
  26. 'nid'=>$nid,
  27. 'group_id' => $group_id,
  28. 'chado_table'=>$table,
  29. 'chado_field'=>$field,
  30. 'record_id'=>$record_id,
  31. 'field_id'=>$field_id,
  32. 'value'=>$value
  33. );
  34. // Check to see if already exists
  35. $exists = db_fetch_object(db_query(
  36. "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=%d AND record_id=%d AND field_id=%d AND group_id=%d",
  37. $record['nid'],
  38. $record['record_id'],
  39. $record['field_id'],
  40. $record['group_id']
  41. ));
  42. if ($exists->constant_id) {
  43. $record['constant_id'] = $exists->constant_id;
  44. $status = drupal_write_record('tripal_bulk_loader_constants',$record,'constant_id');
  45. if ($status) {
  46. return $record;
  47. } else {
  48. return FALSE;
  49. }
  50. } else {
  51. $status = drupal_write_record('tripal_bulk_loader_constants',$record);
  52. if ($status) {
  53. return $record;
  54. } else {
  55. return FALSE;
  56. }
  57. }
  58. }
  59. ///////////////////////////////////////////////////////////
  60. // Set Constants Form (on Bulk Loader Node)
  61. ///////////////////////////////////////////////////////////
  62. /**
  63. * Set constants (exposed fields in template)
  64. *
  65. * @param $form_state
  66. * The current state of the form
  67. * @param $node
  68. * The node to set constants for
  69. *
  70. * @return
  71. * A form array to be rendered by drupal_get_form()
  72. */
  73. function tripal_bulk_loader_set_constants_form ($form_state, $node) {
  74. $form = array();
  75. $form['nid'] = array(
  76. '#type' => 'hidden',
  77. '#value' => $node->nid
  78. );
  79. $form['exposed_fields'] = array(
  80. '#type' => 'fieldset',
  81. '#title' => t('Constant Values'),
  82. '#collapsible' => TRUE,
  83. '#collapsed' => ($node->template_id) ? FALSE : TRUE,
  84. '#prefix' => '<div id="set-constants">',
  85. '#suffix' => '</div>',
  86. );
  87. // Display table of already added constant sets with the ability to re-arrange and delete
  88. if (sizeof($node->constants) > 0) {
  89. $form['exposed_fields']['explanation-1'] = array(
  90. '#type' => 'item',
  91. '#value' => t('You have already added constants to this bulk loading job. Each '
  92. .'row in the following table represents a set of constants. Each set will be used '
  93. .'to load your spreadsheet with the specified template resulting in the each record '
  94. .'in the template to be loaded x number of times where there are x sets of '
  95. .'constants (rows in the following table).')
  96. );
  97. $form['exposed_fields']['existing'] = array(
  98. '#tree' => TRUE,
  99. );
  100. foreach ($node->constants as $set) {
  101. foreach ($set as $record) {
  102. foreach ($record as $field) {
  103. $index = $field['record_id'].'-'.$field['field_id'];
  104. $group = $field['group_id'];
  105. $form['exposed_fields']['existing'][$group][$index] = array(
  106. '#type' => 'markup',
  107. '#value' => $field['value'],
  108. );
  109. }
  110. }
  111. $form['exposed_fields']['existing'][$group]['delete'] = array(
  112. '#type' => 'markup',
  113. '#value' => l('Edit', 'node/'. $node->nid . '/constants/'.$group.'/edit') .'<br />'.
  114. l('Delete', 'node/'. $node->nid . '/constants/'.$group.'/delete'),
  115. );
  116. }
  117. }
  118. $form['exposed_fields']['new'] = array(
  119. '#type' => 'fieldset',
  120. '#title' => t('New set of Constants'),
  121. );
  122. $form['exposed_fields']['new']['explanation-2'] = array(
  123. '#type' => 'item',
  124. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  125. );
  126. // Add textifelds for exposed fields of the current template
  127. $exposed_fields = FALSE;
  128. $indexes = array();
  129. if ($node->template_id) {
  130. if (isset($node->template)) {
  131. foreach ($node->template->template_array as $record_id => $record) {
  132. foreach ($record['fields'] as $field_id => $field) {
  133. if ($field['exposed']) {
  134. $exposed_fields = TRUE;
  135. $indexes[$record_id][] = $field_id;
  136. switch($field['type']) {
  137. case 'table field':
  138. $form['exposed_fields']['new'][$record_id.'-'.$field_id] = array(
  139. '#type' => 'textfield',
  140. '#title' => t($field['title']),
  141. '#description' => t($field['exposed_description']),
  142. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  143. );
  144. break;
  145. case 'constant':
  146. $form['exposed_fields']['new'][$record_id.'-'.$field_id] = array(
  147. '#type' => 'textfield',
  148. '#title' => t($field['title']),
  149. '#description' => t('Enter the case-sensitive value of this constant for your spreadsheet'),
  150. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  151. );
  152. break;
  153. }
  154. $form['exposed_fields']['new'][$record_id.'-'.$field_id.'-table'] = array(
  155. '#type' => 'hidden',
  156. '#value' => $record['table'],
  157. );
  158. $form['exposed_fields']['new'][$record_id.'-'.$field_id.'-field'] = array(
  159. '#type' => 'hidden',
  160. '#value' => $field['field'],
  161. );
  162. $form['exposed_fields']['new'][$record_id.'-'.$field_id.'-type'] = array(
  163. '#type' => 'hidden',
  164. '#value' => $field['type'],
  165. );
  166. }
  167. }
  168. }
  169. }
  170. }
  171. $form['template'] = array(
  172. '#type' => 'hidden',
  173. '#value' => serialize($node->template->template_array)
  174. );
  175. $form['exposed_fields']['new']['indexes'] = array(
  176. '#type' => 'hidden',
  177. '#value' => serialize($indexes),
  178. );
  179. if (!$exposed_fields) {
  180. $form['exposed_fields']['new']['explanation'] = array(
  181. '#type' => 'item',
  182. '#value' => t('There are no exposed fields for this template.')
  183. );
  184. }
  185. $form['exposed_fields']['new']['submit-2'] = array(
  186. '#type' => 'submit',
  187. '#name' => 'add_constant',
  188. '#value' => t('Add Constant Set')
  189. );
  190. return $form;
  191. }
  192. /**
  193. * Validate that the values entered exist in the database
  194. * if indicated in hte template array
  195. */
  196. function tripal_bulk_loader_set_constants_form_validate ($form, $form_state) {
  197. $template = unserialize($form_state['values']['template']);
  198. $indexes = unserialize($form_state['values']['indexes']);
  199. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  200. if (strcmp('Add Constant Set',$op) == 0) {
  201. foreach ($indexes as $record_id => $array) {
  202. foreach($array as $field_id) {
  203. if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
  204. $result = db_fetch_object(db_query(
  205. "SELECT 1 as valid FROM %s WHERE %s='%s'",
  206. $template[$record_id]['table'],
  207. $template[$record_id]['fields'][$field_id]['field'],
  208. $form_state['values'][$record_id.'-'.$field_id]
  209. ));
  210. if (!$result->valid) {
  211. $msg = 'A '.$form['exposed_fields']['new'][$record_id.'-'.$field_id]['#title'].' of "'.$form['exposed_fields']['new'][$record_id.'-'.$field_id]['#value'].'" must already exist!';
  212. form_set_error($record_id.'-'.$field_id, $msg);
  213. } else {
  214. drupal_set_message('Confirmed a '.$form['exposed_fields']['new'][$record_id.'-'.$field_id]['#title'].' of "'.$form['exposed_fields']['new'][$record_id.'-'.$field_id]['#value'].'" already exists.');
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. /**
  222. * Insert/update the constants associated with this node
  223. */
  224. function tripal_bulk_loader_set_constants_form_submit ($form, $form_state) {
  225. // Insert/Update constants
  226. $template = unserialize($form_state['values']['template']);
  227. $indexes = unserialize($form_state['values']['indexes']);
  228. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  229. if (strcmp('Add Constant Set',$op) == 0) {
  230. $max_group = db_fetch_object(db_query("SELECT max(group_id) as value FROM tripal_bulk_loader_constants WHERE nid=%d",$form_state['values']['nid']));
  231. foreach ($indexes as $record_id => $array) {
  232. foreach($array as $field_id) {
  233. tripal_bulk_loader_update_constant(
  234. $form_state['values']['nid'],
  235. $max_group->value+1,
  236. $form_state['values'][$record_id.'-'.$field_id.'-table'],
  237. $form_state['values'][$record_id.'-'.$field_id.'-field'],
  238. $record_id,
  239. $field_id,
  240. $form_state['values'][$record_id.'-'.$field_id]
  241. );
  242. }
  243. }
  244. }
  245. }
  246. function theme_tripal_bulk_loader_set_constants_form ($form) {
  247. $output = '';
  248. // Add draggable table for constant sets
  249. $exposed_field_keys = array();
  250. //$header[] = ' ';
  251. foreach ($form['exposed_fields']['new'] as $k => $element) {
  252. if (preg_match('/^\d+-\d+$/',$k)) {
  253. $exposed_field_keys[] = $k;
  254. $header[] = $element['#title'];
  255. }
  256. }
  257. foreach (element_children($form['exposed_fields']['existing']) as $key) {
  258. $element = &$form['exposed_fields']['existing'][$key];
  259. $element['group']['#attributes']['class'] = 'weight-group';
  260. $row = array();
  261. foreach ($exposed_field_keys as $k) {
  262. $row[] = drupal_render($element[$k]);
  263. }
  264. $row[] = drupal_render($element['delete']);
  265. $row[] = drupal_render($element['group']) . drupal_render($element['id']);
  266. $rows[] = array('data' => $row, 'class' => 'draggable');
  267. }
  268. //drupal_add_tabledrag('mytable', 'order', 'sibling', 'weight-group');
  269. $form['exposed_fields']['existing'] = array(
  270. '#type' => 'markup',
  271. '#value' => theme('table', $header, $rows, array('id' => 'mytable')) . '<br />'
  272. );
  273. $output .= drupal_render($form);
  274. return $output;
  275. }
  276. ///////////////////////////////////////////////////////////
  277. // Set Constants Form (on Bulk Loader Node)
  278. ///////////////////////////////////////////////////////////
  279. /**
  280. * Edit a constant set (exposed fields in template)
  281. *
  282. * @param $form_state
  283. * The current state of the form
  284. * @param $node
  285. * The node to set constants for
  286. * @param $group_id
  287. * The constant set to edit
  288. *
  289. * @return
  290. * A form array to be rendered by drupal_get_form()
  291. */
  292. function tripal_bulk_loader_edit_constant_set_form ($form_state, $node, $group_id) {
  293. $form = array();
  294. $form['#redirect'] = 'node/'.$node->nid;
  295. $form['nid'] = array(
  296. '#type' => 'hidden',
  297. '#value' => $node->nid,
  298. );
  299. $form['group_id'] = array(
  300. '#type' => 'hidden',
  301. '#value' => $group_id,
  302. );
  303. $form['explanation'] = array(
  304. '#type' => 'item',
  305. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  306. );
  307. // Add textifelds for exposed fields of the current template
  308. $exposed_fields = FALSE;
  309. $indexes = array();
  310. if ($node->template_id) {
  311. if (isset($node->template)) {
  312. foreach ($node->template->template_array as $record_id => $record) {
  313. foreach ($record['fields'] as $field_id => $field) {
  314. if ($field['exposed']) {
  315. $exposed_fields = TRUE;
  316. $indexes[$record_id][] = $field_id;
  317. switch($field['type']) {
  318. case 'table field':
  319. $form[$record_id.'-'.$field_id] = array(
  320. '#type' => 'textfield',
  321. '#title' => t($field['title']),
  322. '#description' => t($field['exposed_description']),
  323. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  324. );
  325. break;
  326. case 'constant':
  327. $form[$record_id.'-'.$field_id] = array(
  328. '#type' => 'textfield',
  329. '#title' => t($field['title']),
  330. '#description' => t('Enter the case-sensitive value of this constant for your spreadsheet'),
  331. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  332. );
  333. break;
  334. }
  335. $form[$record_id.'-'.$field_id.'-table'] = array(
  336. '#type' => 'hidden',
  337. '#value' => $record['table'],
  338. );
  339. $form[$record_id.'-'.$field_id.'-field'] = array(
  340. '#type' => 'hidden',
  341. '#value' => $field['field'],
  342. );
  343. $form[$record_id.'-'.$field_id.'-type'] = array(
  344. '#type' => 'hidden',
  345. '#value' => $field['type'],
  346. );
  347. }
  348. }
  349. }
  350. }
  351. }
  352. $form['template'] = array(
  353. '#type' => 'hidden',
  354. '#value' => serialize($node->template->template_array)
  355. );
  356. $form['indexes'] = array(
  357. '#type' => 'hidden',
  358. '#value' => serialize($indexes),
  359. );
  360. $form['save'] = array(
  361. '#type' => 'submit',
  362. '#value' => 'Save',
  363. );
  364. $form['cancel'] = array(
  365. '#type' => 'submit',
  366. '#value' => 'Cancel',
  367. );
  368. return $form;
  369. }
  370. /**
  371. * Edit constants in the current constant set
  372. */
  373. function tripal_bulk_loader_edit_constant_set_form_submit ($form, $form_state) {
  374. // Update constants
  375. $template = unserialize($form_state['values']['template']);
  376. $indexes = unserialize($form_state['values']['indexes']);
  377. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  378. if (strcmp('Save',$op) == 0) {
  379. foreach ($indexes as $record_id => $array) {
  380. foreach($array as $field_id) {
  381. tripal_bulk_loader_update_constant(
  382. $form_state['values']['nid'],
  383. $form_state['values']['group_id'],
  384. $form_state['values'][$record_id.'-'.$field_id.'-table'],
  385. $form_state['values'][$record_id.'-'.$field_id.'-field'],
  386. $record_id,
  387. $field_id,
  388. $form_state['values'][$record_id.'-'.$field_id]
  389. );
  390. }
  391. }
  392. drupal_set_message('The constant set was successfully updated.');
  393. }
  394. }
  395. /**
  396. * Delete a constant set (exposed fields in template)
  397. *
  398. * @param $form_state
  399. * The current state of the form
  400. * @param $node
  401. * The node to set constants for
  402. * @param $group_id
  403. * The constant set to delete
  404. *
  405. * @return
  406. * A form array to be rendered by drupal_get_form()
  407. */
  408. function tripal_bulk_loader_delete_constant_set_form ($form_state, $node, $group_id) {
  409. $form = array();
  410. $form['#redirect'] = 'node/'.$node->nid;
  411. $form['nid'] = array(
  412. '#type' => 'value',
  413. '#value' => $node->nid,
  414. );
  415. $form['group_id'] = array(
  416. '#type' => 'hidden',
  417. '#value' => $group_id,
  418. );
  419. return confirm_form($form,
  420. t('Are you sure you want to delete this constant set?'),
  421. 'node/'.$node->nid,
  422. t('This action cannot be undone.'),
  423. t('Delete'),
  424. t('Cancel')
  425. );
  426. }
  427. /**
  428. * Delete the current constant set
  429. */
  430. function tripal_bulk_loader_delete_constant_set_form_submit ($form, $form_state) {
  431. $group_id = $form_state['values']['group_id'];
  432. $nid = $form_state['values']['nid'];
  433. if ($nid && $form_state['values']['confirm']) {
  434. db_query("DELETE FROM {tripal_bulk_loader_constants} WHERE nid=%d AND group_id=%d",$nid, $group_id);
  435. drupal_set_message('Constant set successfully deleted.');
  436. }
  437. }