tripal_bulk_loader.constants.inc 19 KB

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