tripal_bulk_loader.constants.inc 18 KB

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