tripal_bulk_loader.constants.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. }
  48. else {
  49. return FALSE;
  50. }
  51. }
  52. else {
  53. $status = drupal_write_record('tripal_bulk_loader_constants', $record);
  54. if ($status) {
  55. return $record;
  56. }
  57. else {
  58. return FALSE;
  59. }
  60. }
  61. }
  62. function tripal_bulk_loader_has_exposed_fields($node) {
  63. // exposed fields isn't set
  64. if (!isset($node->exposed_fields)) {
  65. return FALSE;
  66. }
  67. // exposed fields has at least one element
  68. if (sizeof($node->exposed_fields) == 1) {
  69. // need to check if single element is an empty array
  70. $element = reset($node->exposed_fields);
  71. if ($element) {
  72. return TRUE;
  73. }
  74. else {
  75. return FALSE;
  76. }
  77. }
  78. elseif (sizeof($node->exposed_fields) > 1) {
  79. return TRUE;
  80. }
  81. else {
  82. return FALSE;
  83. }
  84. return FALSE;
  85. }
  86. ///////////////////////////////////////////////////////////
  87. // Set Constants Form (on Bulk Loader Node)
  88. ///////////////////////////////////////////////////////////
  89. /**
  90. * Set constants (exposed fields in template)
  91. *
  92. * @param $form_state
  93. * The current state of the form
  94. * @param $node
  95. * The node to set constants for
  96. *
  97. * @return
  98. * A form array to be rendered by drupal_get_form()
  99. */
  100. function tripal_bulk_loader_set_constants_form($form_state, $node) {
  101. $form = array();
  102. $form['nid'] = array(
  103. '#type' => 'hidden',
  104. '#value' => $node->nid
  105. );
  106. if (!tripal_bulk_loader_has_exposed_fields($node)) {
  107. return $form;
  108. }
  109. $form['exposed_array'] = array(
  110. '#type' => 'hidden',
  111. '#value' => serialize($node->exposed_fields),
  112. );
  113. $form['exposed_fields'] = array(
  114. '#type' => 'fieldset',
  115. '#title' => t('Constant Values'),
  116. '#collapsible' => TRUE,
  117. '#collapsed' => ($node->template_id) ? FALSE : TRUE,
  118. '#prefix' => '<div id="set-constants">',
  119. '#suffix' => '</div>',
  120. );
  121. // Display table of already added constant sets with the ability to re-arrange and delete
  122. if (sizeof($node->constants) > 0) {
  123. $form['exposed_fields']['explanation-1'] = array(
  124. '#type' => 'item',
  125. '#value' => t('You have already added constants to this bulk loading job. Each '
  126. .'row in the following table represents a set of constants. Each set will be used '
  127. .'to load your spreadsheet with the specified template resulting in the each record '
  128. .'in the template to be loaded x number of times where there are x sets of '
  129. .'constants (rows in the following table).')
  130. );
  131. $form['exposed_fields']['existing'] = array(
  132. '#tree' => TRUE,
  133. );
  134. foreach ($node->constants as $set) {
  135. foreach ($set as $record) {
  136. foreach ($record as $field) {
  137. $index = $field['record_id'] . '-' . $field['field_id'];
  138. $group = $field['group_id'];
  139. $form['exposed_fields']['existing'][$group][$index] = array(
  140. '#type' => 'markup',
  141. '#value' => $field['value'],
  142. );
  143. }
  144. }
  145. $form['exposed_fields']['existing'][$group]['delete'] = array(
  146. '#type' => 'markup',
  147. '#value' => l('Edit', 'node/' . $node->nid . '/constants/' . $group . '/edit') . '<br />' .
  148. l('Delete', 'node/' . $node->nid . '/constants/' . $group . '/delete'),
  149. );
  150. }
  151. }
  152. $form['exposed_fields']['new'] = array(
  153. '#type' => 'fieldset',
  154. '#title' => t('New set of Constants'),
  155. );
  156. $form['exposed_fields']['new']['explanation-2'] = array(
  157. '#type' => 'item',
  158. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  159. );
  160. // Add textifelds for exposed fields of the current template
  161. $exposed_fields = FALSE;
  162. $indexes = array();
  163. if (tripal_bulk_loader_has_exposed_fields($node)) {
  164. foreach ($node->exposed_fields as $exposed_index) {
  165. $record_id = $exposed_index['record_id'];
  166. $field_id = $exposed_index['field_id'];
  167. $field = $node->template->template_array[$record_id]['fields'][$field_id];
  168. if ($field['exposed']) {
  169. $exposed_fields = TRUE;
  170. $indexes[$record_id][] = $field_id;
  171. switch ($field['type']) {
  172. case 'table field':
  173. $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
  174. '#type' => 'textfield',
  175. '#title' => t($field['title']),
  176. '#description' => t($field['exposed_description']),
  177. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  178. );
  179. break;
  180. case 'constant':
  181. $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
  182. '#type' => 'textfield',
  183. '#title' => t($field['title']),
  184. '#description' => t('Enter the case-sensitive value of this constant for your spreadsheet'),
  185. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  186. );
  187. break;
  188. }
  189. $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-table'] = array(
  190. '#type' => 'hidden',
  191. '#value' => $node->template->template_array[$record_id]['table'],
  192. );
  193. $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-field'] = array(
  194. '#type' => 'hidden',
  195. '#value' => $field['field'],
  196. );
  197. $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-type'] = array(
  198. '#type' => 'hidden',
  199. '#value' => $field['type'],
  200. );
  201. }
  202. }
  203. }
  204. $form['template'] = array(
  205. '#type' => 'hidden',
  206. '#value' => serialize($node->template->template_array)
  207. );
  208. $form['exposed_fields']['new']['indexes'] = array(
  209. '#type' => 'hidden',
  210. '#value' => serialize($indexes),
  211. );
  212. if (!$exposed_fields) {
  213. $form['exposed_fields']['new']['explanation'] = array(
  214. '#type' => 'item',
  215. '#value' => t('There are no exposed fields for this template.')
  216. );
  217. }
  218. $form['exposed_fields']['new']['submit-2'] = array(
  219. '#type' => 'submit',
  220. '#name' => 'add_constant',
  221. '#value' => t('Add Constant Set')
  222. );
  223. return $form;
  224. }
  225. /**
  226. * Validate that the values entered exist in the database
  227. * if indicated in hte template array
  228. */
  229. function tripal_bulk_loader_set_constants_form_validate($form, $form_state) {
  230. $template = unserialize($form_state['values']['template']);
  231. $indexes = unserialize($form_state['values']['indexes']);
  232. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  233. if (strcmp('Add Constant Set', $op) == 0) {
  234. foreach ($indexes as $record_id => $array) {
  235. foreach ($array as $field_id) {
  236. if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
  237. $result = db_fetch_object(db_query(
  238. "SELECT 1 as valid FROM %s WHERE %s='%s'",
  239. $template[$record_id]['table'],
  240. $template[$record_id]['fields'][$field_id]['field'],
  241. $form_state['values'][$record_id . '-' . $field_id]
  242. ));
  243. if (!$result->valid) {
  244. $msg = 'A ' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'] . ' of "' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value'] . '" must already exist!';
  245. form_set_error($record_id . '-' . $field_id, $msg);
  246. }
  247. else {
  248. 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.');
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. /**
  256. * Insert/update the constants associated with this node
  257. */
  258. function tripal_bulk_loader_set_constants_form_submit($form, $form_state) {
  259. // Insert/Update constants
  260. $template = unserialize($form_state['values']['template']);
  261. $indexes = unserialize($form_state['values']['indexes']);
  262. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  263. if (strcmp('Add Constant Set', $op) == 0) {
  264. $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']));
  265. foreach ($indexes as $record_id => $array) {
  266. foreach ($array as $field_id) {
  267. tripal_bulk_loader_update_constant(
  268. $form_state['values']['nid'],
  269. $max_group->value+1,
  270. $form_state['values'][$record_id . '-' . $field_id . '-table'],
  271. $form_state['values'][$record_id . '-' . $field_id . '-field'],
  272. $record_id,
  273. $field_id,
  274. $form_state['values'][$record_id . '-' . $field_id]
  275. );
  276. }
  277. }
  278. }
  279. }
  280. function theme_tripal_bulk_loader_set_constants_form($form) {
  281. $output = '';
  282. $exposed_fields = unserialize($form['exposed_array']['#value']);
  283. // need to put in the context of a node so we can use the has_exposed_fields function
  284. if ($exposed_fields) {
  285. $node->exposed_fields = $exposed_fields;
  286. }
  287. else {
  288. $node->exposed_fields = array();
  289. }
  290. // Add draggable table for constant sets
  291. if (tripal_bulk_loader_has_exposed_fields($node)) {
  292. $i=1;
  293. foreach (element_children($form['exposed_fields']['existing']) as $key) {
  294. $element = &$form['exposed_fields']['existing'][$key];
  295. $element['group']['#attributes']['class'] = 'weight-group';
  296. $row = array();
  297. foreach ($exposed_fields as $exposed) {
  298. if ($i==1) {
  299. $header[] = $exposed['title'];
  300. }
  301. $k = $exposed['record_id'] . '-' . $exposed['field_id'];
  302. $row[] = drupal_render($element[$k]);
  303. }
  304. $row[] = drupal_render($element['delete']);
  305. $row[] = drupal_render($element['group']) . drupal_render($element['id']);
  306. $rows[] = array('data' => $row, 'class' => 'draggable');
  307. $i++;
  308. }
  309. //drupal_add_tabledrag('mytable', 'order', 'sibling', 'weight-group');
  310. $form['exposed_fields']['existing'] = array(
  311. '#type' => 'markup',
  312. '#value' => theme('table', $header, $rows, array('id' => 'mytable')) . '<br />'
  313. );
  314. }
  315. $output .= drupal_render($form);
  316. return $output;
  317. }
  318. ///////////////////////////////////////////////////////////
  319. // Set Constants Form (on Bulk Loader Node)
  320. ///////////////////////////////////////////////////////////
  321. /**
  322. * Edit a constant set (exposed fields in template)
  323. *
  324. * @param $form_state
  325. * The current state of the form
  326. * @param $node
  327. * The node to set constants for
  328. * @param $group_id
  329. * The constant set to edit
  330. *
  331. * @return
  332. * A form array to be rendered by drupal_get_form()
  333. */
  334. function tripal_bulk_loader_edit_constant_set_form($form_state, $node, $group_id) {
  335. $form = array();
  336. $form['#redirect'] = 'node/' . $node->nid;
  337. $form['nid'] = array(
  338. '#type' => 'hidden',
  339. '#value' => $node->nid,
  340. );
  341. $form['group_id'] = array(
  342. '#type' => 'hidden',
  343. '#value' => $group_id,
  344. );
  345. $form['explanation'] = array(
  346. '#type' => 'item',
  347. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  348. );
  349. // Add textifelds for exposed fields of the current template
  350. $exposed_fields = FALSE;
  351. $indexes = array();
  352. if (tripal_bulk_loader_has_exposed_fields($node)) {
  353. foreach ($node->exposed_fields as $exposed_index) {
  354. $record_id = $exposed_index['record_id'];
  355. $field_id = $exposed_index['field_id'];
  356. $field = $node->template->template_array[$record_id]['fields'][$field_id];
  357. if ($field['exposed']) {
  358. $exposed_fields = TRUE;
  359. $indexes[$record_id][] = $field_id;
  360. switch ($field['type']) {
  361. case 'table field':
  362. $form[$record_id . '-' . $field_id] = array(
  363. '#type' => 'textfield',
  364. '#title' => t($field['title']),
  365. '#description' => t($field['exposed_description']),
  366. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  367. );
  368. break;
  369. case 'constant':
  370. $form[$record_id . '-' . $field_id] = array(
  371. '#type' => 'textfield',
  372. '#title' => t($field['title']),
  373. '#description' => t('Enter the case-sensitive value of this constant for your spreadsheet'),
  374. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  375. );
  376. break;
  377. }
  378. $form[$record_id . '-' . $field_id . '-table'] = array(
  379. '#type' => 'hidden',
  380. '#value' => $record['table'],
  381. );
  382. $form[$record_id . '-' . $field_id . '-field'] = array(
  383. '#type' => 'hidden',
  384. '#value' => $field['field'],
  385. );
  386. $form[$record_id . '-' . $field_id . '-type'] = array(
  387. '#type' => 'hidden',
  388. '#value' => $field['type'],
  389. );
  390. }
  391. }
  392. }
  393. $form['template'] = array(
  394. '#type' => 'hidden',
  395. '#value' => serialize($node->template->template_array)
  396. );
  397. $form['indexes'] = array(
  398. '#type' => 'hidden',
  399. '#value' => serialize($indexes),
  400. );
  401. $form['save'] = array(
  402. '#type' => 'submit',
  403. '#value' => 'Save',
  404. );
  405. $form['cancel'] = array(
  406. '#type' => 'submit',
  407. '#value' => 'Cancel',
  408. );
  409. return $form;
  410. }
  411. /**
  412. * Edit constants in the current constant set
  413. */
  414. function tripal_bulk_loader_edit_constant_set_form_submit($form, $form_state) {
  415. // Update constants
  416. $template = unserialize($form_state['values']['template']);
  417. $indexes = unserialize($form_state['values']['indexes']);
  418. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  419. if (strcmp('Save', $op) == 0) {
  420. foreach ($indexes as $record_id => $array) {
  421. foreach ($array as $field_id) {
  422. tripal_bulk_loader_update_constant(
  423. $form_state['values']['nid'],
  424. $form_state['values']['group_id'],
  425. $form_state['values'][$record_id . '-' . $field_id . '-table'],
  426. $form_state['values'][$record_id . '-' . $field_id . '-field'],
  427. $record_id,
  428. $field_id,
  429. $form_state['values'][$record_id . '-' . $field_id]
  430. );
  431. }
  432. }
  433. drupal_set_message('The constant set was successfully updated.');
  434. }
  435. }
  436. /**
  437. * Delete a constant set (exposed fields in template)
  438. *
  439. * @param $form_state
  440. * The current state of the form
  441. * @param $node
  442. * The node to set constants for
  443. * @param $group_id
  444. * The constant set to delete
  445. *
  446. * @return
  447. * A form array to be rendered by drupal_get_form()
  448. */
  449. function tripal_bulk_loader_delete_constant_set_form($form_state, $node, $group_id) {
  450. $form = array();
  451. $form['#redirect'] = 'node/' . $node->nid;
  452. $form['nid'] = array(
  453. '#type' => 'value',
  454. '#value' => $node->nid,
  455. );
  456. $form['group_id'] = array(
  457. '#type' => 'hidden',
  458. '#value' => $group_id,
  459. );
  460. return confirm_form($form,
  461. t('Are you sure you want to delete this constant set?'),
  462. 'node/' . $node->nid,
  463. t('This action cannot be undone.'),
  464. t('Delete'),
  465. t('Cancel')
  466. );
  467. }
  468. /**
  469. * Delete the current constant set
  470. */
  471. function tripal_bulk_loader_delete_constant_set_form_submit($form, $form_state) {
  472. $group_id = $form_state['values']['group_id'];
  473. $nid = $form_state['values']['nid'];
  474. if ($nid && $form_state['values']['confirm']) {
  475. db_query("DELETE FROM {tripal_bulk_loader_constants} WHERE nid=%d AND group_id=%d", $nid, $group_id);
  476. drupal_set_message('Constant set successfully deleted.');
  477. }
  478. }