tripal_bulk_loader.constants.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. $previous_db = tripal_db_set_active('chado');
  243. $result = db_fetch_object(db_query(
  244. "SELECT 1 as valid FROM %s WHERE %s='%s'",
  245. $template[$record_id]['table'],
  246. $template[$record_id]['fields'][$field_id]['field'],
  247. $form_state['values'][$record_id . '-' . $field_id]
  248. ));
  249. tripal_db_set_active($previous_db);
  250. if (!$result->valid) {
  251. $msg = 'A ' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'] . ' of "' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value'] . '" must already exist!';
  252. form_set_error($record_id . '-' . $field_id, $msg);
  253. }
  254. else {
  255. drupal_set_message(
  256. t(
  257. 'Confirmed a %title of "%value" already exists.',
  258. array(
  259. '%title' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'],
  260. '%value' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value']
  261. )
  262. )
  263. );
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. /**
  271. * Insert/update the constants associated with this node
  272. */
  273. function tripal_bulk_loader_set_constants_form_submit($form, $form_state) {
  274. // Insert/Update constants
  275. $template = unserialize($form_state['values']['template']);
  276. $indexes = unserialize($form_state['values']['indexes']);
  277. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  278. if (strcmp('Add Constant Set', $op) == 0) {
  279. $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']));
  280. foreach ($indexes as $record_id => $array) {
  281. foreach ($array as $field_id) {
  282. tripal_bulk_loader_update_constant(
  283. $form_state['values']['nid'],
  284. $max_group->value+1,
  285. $form_state['values'][$record_id . '-' . $field_id . '-table'],
  286. $form_state['values'][$record_id . '-' . $field_id . '-field'],
  287. $record_id,
  288. $field_id,
  289. $form_state['values'][$record_id . '-' . $field_id]
  290. );
  291. }
  292. }
  293. }
  294. }
  295. function theme_tripal_bulk_loader_set_constants_form($form) {
  296. $output = '';
  297. $exposed_fields = unserialize($form['exposed_array']['#value']);
  298. // need to put in the context of a node so we can use the has_exposed_fields function
  299. if ($exposed_fields) {
  300. $node->exposed_fields = $exposed_fields;
  301. }
  302. else {
  303. $node->exposed_fields = array();
  304. }
  305. // Add draggable table for constant sets
  306. if (tripal_bulk_loader_has_exposed_fields($node)) {
  307. $i=1;
  308. foreach (element_children($form['exposed_fields']['existing']) as $key) {
  309. $element = &$form['exposed_fields']['existing'][$key];
  310. $element['group']['#attributes']['class'] = 'weight-group';
  311. $row = array();
  312. foreach ($exposed_fields as $exposed) {
  313. if ($i==1) {
  314. $header[] = $exposed['title'];
  315. }
  316. $k = $exposed['record_id'] . '-' . $exposed['field_id'];
  317. $row[] = drupal_render($element[$k]);
  318. }
  319. $row[] = drupal_render($element['delete']);
  320. $row[] = drupal_render($element['group']) . drupal_render($element['id']);
  321. if (!empty($row[0])) {
  322. $rows[] = array('data' => $row, 'class' => 'draggable');
  323. }
  324. $i++;
  325. }
  326. //drupal_add_tabledrag('mytable', 'order', 'sibling', 'weight-group');
  327. // @coder-ignore: no user input thus don't need to filter
  328. $form['exposed_fields']['existing'] = array(
  329. '#type' => 'markup',
  330. '#value' => theme('table', $header, $rows, array('id' => 'mytable')) . '<br />',
  331. );
  332. }
  333. $output .= drupal_render($form);
  334. return $output;
  335. }
  336. ///////////////////////////////////////////////////////////
  337. // Set Constants Form (on Bulk Loader Node)
  338. ///////////////////////////////////////////////////////////
  339. /**
  340. * Edit a constant set (exposed fields in template)
  341. *
  342. * @param $form_state
  343. * The current state of the form
  344. * @param $node
  345. * The node to set constants for
  346. * @param $group_id
  347. * The constant set to edit
  348. *
  349. * @return
  350. * A form array to be rendered by drupal_get_form()
  351. */
  352. function tripal_bulk_loader_edit_constant_set_form($form_state, $node, $group_id) {
  353. $form = array();
  354. $form['#redirect'] = 'node/' . $node->nid;
  355. $form['nid'] = array(
  356. '#type' => 'hidden',
  357. '#value' => $node->nid,
  358. );
  359. $form['group_id'] = array(
  360. '#type' => 'hidden',
  361. '#value' => $group_id,
  362. );
  363. $form['explanation'] = array(
  364. '#type' => 'item',
  365. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  366. );
  367. // Add textifelds for exposed fields of the current template
  368. $exposed_fields = FALSE;
  369. $indexes = array();
  370. if (tripal_bulk_loader_has_exposed_fields($node)) {
  371. foreach ($node->exposed_fields as $exposed_index) {
  372. $record_id = $exposed_index['record_id'];
  373. $record = $node->template->template_array[$record_id];
  374. $field_id = $exposed_index['field_id'];
  375. $field = $node->template->template_array[$record_id]['fields'][$field_id];
  376. if ($field['exposed']) {
  377. $exposed_fields = TRUE;
  378. $indexes[$record_id][] = $field_id;
  379. switch ($field['type']) {
  380. case 'table field':
  381. $form[$record_id . '-' . $field_id] = array(
  382. '#type' => 'textfield',
  383. '#title' => t('%title', array('%title' => $field['title'])),
  384. '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
  385. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  386. );
  387. break;
  388. case 'constant':
  389. $form[$record_id . '-' . $field_id] = array(
  390. '#type' => 'textfield',
  391. '#title' => t('%title', array('%title' => $field['title'])),
  392. '#description' => t('Enter the case-sensitive value of this constant for your data file'),
  393. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  394. );
  395. break;
  396. }
  397. $form[$record_id . '-' . $field_id . '-table'] = array(
  398. '#type' => 'hidden',
  399. '#value' => $record['table'],
  400. );
  401. $form[$record_id . '-' . $field_id . '-field'] = array(
  402. '#type' => 'hidden',
  403. '#value' => $field['field'],
  404. );
  405. $form[$record_id . '-' . $field_id . '-type'] = array(
  406. '#type' => 'hidden',
  407. '#value' => $field['type'],
  408. );
  409. }
  410. }
  411. }
  412. $form['template'] = array(
  413. '#type' => 'hidden',
  414. '#value' => serialize($node->template->template_array)
  415. );
  416. $form['indexes'] = array(
  417. '#type' => 'hidden',
  418. '#value' => serialize($indexes),
  419. );
  420. $form['save'] = array(
  421. '#type' => 'submit',
  422. '#value' => 'Save',
  423. );
  424. $form['cancel'] = array(
  425. '#type' => 'submit',
  426. '#value' => 'Cancel',
  427. );
  428. return $form;
  429. }
  430. /**
  431. * Edit constants in the current constant set
  432. */
  433. function tripal_bulk_loader_edit_constant_set_form_submit($form, $form_state) {
  434. // Update constants
  435. $template = unserialize($form_state['values']['template']);
  436. $indexes = unserialize($form_state['values']['indexes']);
  437. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  438. if (strcmp('Save', $op) == 0) {
  439. foreach ($indexes as $record_id => $array) {
  440. foreach ($array as $field_id) {
  441. tripal_bulk_loader_update_constant(
  442. $form_state['values']['nid'],
  443. $form_state['values']['group_id'],
  444. $form_state['values'][$record_id . '-' . $field_id . '-table'],
  445. $form_state['values'][$record_id . '-' . $field_id . '-field'],
  446. $record_id,
  447. $field_id,
  448. $form_state['values'][$record_id . '-' . $field_id]
  449. );
  450. }
  451. }
  452. drupal_set_message(t('The constant set was successfully updated.'));
  453. }
  454. }
  455. /**
  456. * Delete a constant set (exposed fields in template)
  457. *
  458. * @param $form_state
  459. * The current state of the form
  460. * @param $node
  461. * The node to set constants for
  462. * @param $group_id
  463. * The constant set to delete
  464. *
  465. * @return
  466. * A form array to be rendered by drupal_get_form()
  467. */
  468. function tripal_bulk_loader_delete_constant_set_form($form_state, $node, $group_id) {
  469. $form = array();
  470. $form['#redirect'] = 'node/' . $node->nid;
  471. $form['nid'] = array(
  472. '#type' => 'value',
  473. '#value' => $node->nid,
  474. );
  475. $form['group_id'] = array(
  476. '#type' => 'hidden',
  477. '#value' => $group_id,
  478. );
  479. return confirm_form($form,
  480. t('Are you sure you want to delete this constant set?'),
  481. 'node/' . $node->nid,
  482. t('This action cannot be undone.'),
  483. t('Delete'),
  484. t('Cancel')
  485. );
  486. }
  487. /**
  488. * Delete the current constant set
  489. */
  490. function tripal_bulk_loader_delete_constant_set_form_submit($form, $form_state) {
  491. $group_id = $form_state['values']['group_id'];
  492. $nid = $form_state['values']['nid'];
  493. if ($nid && $form_state['values']['confirm']) {
  494. db_query("DELETE FROM {tripal_bulk_loader_constants} WHERE nid=%d AND group_id=%d", $nid, $group_id);
  495. drupal_set_message(t('Constant set successfully deleted.'));
  496. }
  497. }