tripal_bulk_loader.constants.inc 17 KB

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