tripal_bulk_loader.constants.inc

  1. 2.x tripal_bulk_loader/includes/tripal_bulk_loader.constants.inc
  2. 3.x tripal_bulk_loader/includes/tripal_bulk_loader.constants.inc
  3. 1.x tripal_bulk_loader/includes/tripal_bulk_loader.constants.inc

@todo Add file header description

File

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