tripal_bulk_loader.constants.inc 18 KB

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