tripal_bulk_loader.constants.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_select('tripal_bulk_loader_constants', 'c')
  42. ->fields('c',array('constant_id'))
  43. ->condition('nid',$record['nid'])
  44. ->condition('record_id',$record['record_id'])
  45. ->condition('field_id',$record['field_id'])
  46. ->condition('group_id',$record['group_id'])
  47. ->execute();
  48. $exists = $exists->fetchObject();
  49. if (!isset($exists)) {
  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, $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) {
  138. $form['exposed_fields']['explanation-1'] = array(
  139. '#type' => 'item',
  140. '#markup' => 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. $rows = array();
  147. foreach ($node->constants as $group_id => $set) {
  148. $row = array();
  149. $row['group_id'] = $group_id;
  150. $header['group_id'] = t('Group');
  151. foreach ($set as $record) {
  152. foreach ($record as $field) {
  153. $index = $field['record_id'] . '-' . $field['field_id'];
  154. $header[$index] = $field['value'];
  155. $row[$index] = $field['value'];
  156. }
  157. }
  158. $header['operations'] = t('Operations');
  159. $row['operations'] = filter_xss(l(t('Edit'), 'node/' . $node->nid . '/constants/' . $group_id . '/edit') . ' | ' .
  160. l(t('Delete'), 'node/' . $node->nid . '/constants/' . $group_id . '/delete'));
  161. $rows[] = $row;
  162. }
  163. //get the header using the last constant set
  164. $header = array();
  165. $header['group_id'] = '';
  166. $header_row = array();
  167. $header_row[0]['group_id'] = array('data' => t('Group'),'header'=>TRUE);
  168. foreach ($set as $record) {
  169. foreach ($record as $field) {
  170. $header[$field['record_id']]['data'] = $node->template->template_array[$field['record_id']]['record_id'];
  171. $header[$field['record_id']]['colspan'] = sizeof($record);
  172. $index = $field['record_id'] . '-' . $field['field_id'];
  173. $header_row[0][$index] = array(
  174. 'data' => $node->template->template_array[$field['record_id']]['fields'][$field['field_id']]['title'],
  175. 'header' => TRUE
  176. );
  177. }
  178. }
  179. $header['operations'] = '';
  180. $header_row[0]['operations'] = array('data' => t('Operations'),'header'=>TRUE);
  181. $full_header = array(
  182. 0 => $header,
  183. 1 => $header_row[0]
  184. );
  185. $all_rows = array_merge($header_row, $rows);
  186. $form['exposed_fields']['existing'] = array(
  187. '#type' => 'markup',
  188. '#markup' => theme('table', array(
  189. 'header' => $header,
  190. 'rows' => $all_rows
  191. )),
  192. // Added to make accessible to subthemeing
  193. '#rows' => serialize($rows),
  194. '#header' => serialize($full_header)
  195. );
  196. }
  197. $form['exposed_fields']['new'] = array(
  198. '#type' => 'fieldset',
  199. '#title' => t('New set of Constants'),
  200. );
  201. $form['exposed_fields']['new']['explanation-2'] = array(
  202. '#type' => 'item',
  203. '#markup' => t('The following fields are constants in the selected template that you need to set values for.')
  204. );
  205. // Add textifelds for exposed fields of the current template
  206. $exposed_fields = FALSE;
  207. $indexes = array();
  208. if (tripal_bulk_loader_has_exposed_fields($node)) {
  209. foreach ($node->exposed_fields as $exposed_index) {
  210. $record_id = $exposed_index['record_id'];
  211. $field_id = $exposed_index['field_id'];
  212. $field = $node->template->template_array[$record_id]['fields'][$field_id];
  213. if ($field['exposed']) {
  214. $exposed_fields = TRUE;
  215. $indexes[$record_id][] = $field_id;
  216. $default_value = '';
  217. if (isset($node->constants[$record_id])) {
  218. if (isset($node->constants[$record_id][$field_id])) {
  219. $default_value = $node->constants[$record_id][$field_id]['value'];
  220. }
  221. } elseif (isset($field['constant value'])) {
  222. $default_value = $field['constant value'];
  223. } elseif(isset($field['spreadsheet column'])) {
  224. $default_value = $field['spreadsheet column'];
  225. }
  226. switch ($field['type']) {
  227. case 'table field':
  228. $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
  229. '#type' => 'textfield',
  230. '#title' => t('%title', array('%title' => $field['title'])),
  231. '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
  232. '#default_value' => $default_value,
  233. );
  234. break;
  235. case 'constant':
  236. $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
  237. '#type' => 'textfield',
  238. '#title' => t('%title', array('%title' => $field['title']) ),
  239. '#description' => t('Enter the case-sensitive value of this constant for your data file'),
  240. '#default_value' => $default_value,
  241. );
  242. break;
  243. }
  244. $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-table'] = array(
  245. '#type' => 'hidden',
  246. '#value' => $node->template->template_array[$record_id]['table'],
  247. );
  248. $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-field'] = array(
  249. '#type' => 'hidden',
  250. '#value' => $field['field'],
  251. );
  252. $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-type'] = array(
  253. '#type' => 'hidden',
  254. '#value' => $field['type'],
  255. );
  256. }
  257. }
  258. }
  259. $form['template'] = array(
  260. '#type' => 'hidden',
  261. '#value' => serialize($node->template->template_array)
  262. );
  263. $form['exposed_fields']['new']['indexes'] = array(
  264. '#type' => 'hidden',
  265. '#value' => serialize($indexes),
  266. );
  267. if (!$exposed_fields) {
  268. $form['exposed_fields']['new']['explanation'] = array(
  269. '#type' => 'item',
  270. '#value' => t('There are no exposed fields for this template.')
  271. );
  272. }
  273. $form['exposed_fields']['new']['submit-2'] = array(
  274. '#type' => 'submit',
  275. '#name' => 'add_constant',
  276. '#value' => t('Add Constant Set')
  277. );
  278. return $form;
  279. }
  280. /**
  281. * Validate that the values entered exist in the database
  282. * if indicated in hte template array
  283. *
  284. * @ingroup tripal_bulk_loader
  285. */
  286. function tripal_bulk_loader_set_constants_form_validate($form, $form_state) {
  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. foreach ($indexes as $record_id => $array) {
  292. foreach ($array as $field_id) {
  293. if (isset($template[$record_id]['fields'][$field_id]['exposed_validate'])) {
  294. if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
  295. $result = db_fetch_object(chado_query(
  296. "SELECT 1 as valid FROM {%s} WHERE %s='%s'",
  297. $template[$record_id]['table'],
  298. $template[$record_id]['fields'][$field_id]['field'],
  299. $form_state['values'][$record_id . '-' . $field_id]
  300. ));
  301. if (!$result->valid) {
  302. $msg = 'A ' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'] . ' of "' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value'] . '" must already exist!';
  303. form_set_error($record_id . '-' . $field_id, $msg);
  304. }
  305. else {
  306. drupal_set_message(
  307. t(
  308. 'Confirmed a %title of "%value" already exists.',
  309. array(
  310. '%title' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'],
  311. '%value' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value']
  312. )
  313. )
  314. );
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Insert/update the constants associated with this node
  324. *
  325. * @ingroup tripal_bulk_loader
  326. */
  327. function tripal_bulk_loader_set_constants_form_submit($form, $form_state) {
  328. // Insert/Update constants
  329. $template = unserialize($form_state['values']['template']);
  330. $indexes = unserialize($form_state['values']['indexes']);
  331. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  332. if (strcmp('Add Constant Set', $op) == 0) {
  333. $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();
  334. foreach ($indexes as $record_id => $array) {
  335. foreach ($array as $field_id) {
  336. tripal_bulk_loader_update_constant(
  337. $form_state['values']['nid'],
  338. $max_group->value+1,
  339. $form_state['values'][$record_id . '-' . $field_id . '-table'],
  340. $form_state['values'][$record_id . '-' . $field_id . '-field'],
  341. $record_id,
  342. $field_id,
  343. $form_state['values'][$record_id . '-' . $field_id]
  344. );
  345. }
  346. }
  347. }
  348. }
  349. ///////////////////////////////////////////////////////////
  350. // Set Constants Form (on Bulk Loader Node)
  351. ///////////////////////////////////////////////////////////
  352. /**
  353. * Edit a constant set (exposed fields in template)
  354. *
  355. * @param $form_state
  356. * The current state of the form
  357. * @param $node
  358. * The node to set constants for
  359. * @param $group_id
  360. * The constant set to edit
  361. *
  362. * @return
  363. * A form array to be rendered by drupal_get_form()
  364. *
  365. * @ingroup tripal_bulk_loader
  366. */
  367. function tripal_bulk_loader_edit_constant_set_form($form, $form_state, $node, $group_id) {
  368. $form = array();
  369. $form['nid'] = array(
  370. '#type' => 'hidden',
  371. '#value' => $node->nid,
  372. );
  373. $form['group_id'] = array(
  374. '#type' => 'hidden',
  375. '#value' => $group_id,
  376. );
  377. $form['explanation'] = array(
  378. '#type' => 'item',
  379. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  380. );
  381. // Add textifelds for exposed fields of the current template
  382. $exposed_fields = FALSE;
  383. $indexes = array();
  384. if (tripal_bulk_loader_has_exposed_fields($node)) {
  385. foreach ($node->exposed_fields as $exposed_index) {
  386. $record_id = $exposed_index['record_id'];
  387. $record = $node->template->template_array[$record_id];
  388. $field_id = $exposed_index['field_id'];
  389. $field = $node->template->template_array[$record_id]['fields'][$field_id];
  390. if ($field['exposed']) {
  391. $exposed_fields = TRUE;
  392. $indexes[$record_id][] = $field_id;
  393. switch ($field['type']) {
  394. case 'table field':
  395. $form[$record_id . '-' . $field_id] = array(
  396. '#type' => 'textfield',
  397. '#title' => t('%title', array('%title' => $field['title'])),
  398. '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
  399. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  400. );
  401. break;
  402. case 'constant':
  403. $form[$record_id . '-' . $field_id] = array(
  404. '#type' => 'textfield',
  405. '#title' => t('%title', array('%title' => $field['title'])),
  406. '#description' => t('Enter the case-sensitive value of this constant for your data file'),
  407. '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
  408. );
  409. break;
  410. }
  411. $form[$record_id . '-' . $field_id . '-table'] = array(
  412. '#type' => 'hidden',
  413. '#value' => $record['table'],
  414. );
  415. $form[$record_id . '-' . $field_id . '-field'] = array(
  416. '#type' => 'hidden',
  417. '#value' => $field['field'],
  418. );
  419. $form[$record_id . '-' . $field_id . '-type'] = array(
  420. '#type' => 'hidden',
  421. '#value' => $field['type'],
  422. );
  423. }
  424. }
  425. }
  426. $form['template'] = array(
  427. '#type' => 'hidden',
  428. '#value' => serialize($node->template->template_array)
  429. );
  430. $form['indexes'] = array(
  431. '#type' => 'hidden',
  432. '#value' => serialize($indexes),
  433. );
  434. $form['save'] = array(
  435. '#type' => 'submit',
  436. '#value' => 'Save',
  437. );
  438. $form['cancel'] = array(
  439. '#type' => 'link',
  440. '#title' => 'Cancel',
  441. '#href' => 'node/'.$node->nid
  442. );
  443. return $form;
  444. }
  445. /**
  446. * Edit constants in the current constant set
  447. *
  448. * @ingroup tripal_bulk_loader
  449. */
  450. function tripal_bulk_loader_edit_constant_set_form_submit($form, &$form_state) {
  451. // Update constants
  452. $template = unserialize($form_state['values']['template']);
  453. $indexes = unserialize($form_state['values']['indexes']);
  454. $nid = $form_state['values']['nid'];
  455. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  456. if (strcmp('Save', $op) == 0) {
  457. $form_state['redirect'] = 'node/' . $nid;
  458. $form_state['rebuild'] = FALSE;
  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, $form_state, $node, $group_id) {
  491. $form = array();
  492. $form['nid'] = array(
  493. '#type' => 'value',
  494. '#value' => $node->nid,
  495. );
  496. $form['group_id'] = array(
  497. '#type' => 'hidden',
  498. '#value' => $group_id,
  499. );
  500. return confirm_form($form,
  501. t('Are you sure you want to delete this constant set?'),
  502. 'node/' . $node->nid,
  503. t('This action cannot be undone.'),
  504. t('Delete'),
  505. t('Cancel')
  506. );
  507. }
  508. /**
  509. * Delete the current constant set
  510. *
  511. * @ingroup tripal_bulk_loader
  512. */
  513. function tripal_bulk_loader_delete_constant_set_form_submit($form, &$form_state) {
  514. $group_id = $form_state['values']['group_id'];
  515. $nid = $form_state['values']['nid'];
  516. if ($nid && $form_state['values']['confirm']) {
  517. $form_state['redirect'] = 'node/' . $nid;
  518. $form_state['rebuild'] = FALSE;
  519. db_delete('tripal_bulk_loader_constants')
  520. ->condition('nid', $nid)
  521. ->condition('group_id',$group_id)
  522. ->execute();
  523. drupal_set_message(t('Constant set successfully deleted.'));
  524. }
  525. }