tripal_feature-db_references.inc

@todo Add file header description

File

tripal_feature/includes/tripal_feature-db_references.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. *
  8. *
  9. * @ingroup tripal_feature
  10. */
  11. function tripal_feature_add_ALL_dbreferences_page($node) {
  12. $output = '';
  13. $output .= tripal_feature_implement_add_chado_properties_progress('db_references') . '<br />';
  14. $output .= '<b>All Database References should strictly pertain to THE CURRENT Individual</b><br />';
  15. $output .= '<br /><b>Current Database References</b><br />';
  16. $output .= list_dbreferences_for_node($node->db_references);
  17. $output .= '<br /><br />';
  18. $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
  19. $output .= '<br />';
  20. $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'db_references', $node->nid);
  21. return $output;
  22. }
  23. /**
  24. * Implements Hook_form()
  25. * Handles adding of Database References to features
  26. *
  27. * @ingroup tripal_feature
  28. */
  29. function tripal_feature_add_ONE_dbreference_form($form_state, $node) {
  30. $form['nid'] = array(
  31. '#type' => 'hidden',
  32. '#value' => $node->nid
  33. );
  34. $form['feature_id'] = array(
  35. '#type' => 'hidden',
  36. '#value' => $node->feature->feature_id,
  37. );
  38. $form['add_dbreference'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('Add Database References') . '<span class="form-optional" title="This field is optional">(optional)</span>',
  41. );
  42. $form['add_dbreference']['accession'] = array(
  43. '#type' => 'textfield',
  44. '#title' => t('Accession'),
  45. '#required' => TRUE,
  46. );
  47. $form['add_dbreference']['description'] = array(
  48. '#type' => 'textarea',
  49. '#title' => t('Description of Reference') . '<span class="form-optional" title="This field is optional">+</span>',
  50. '#description' => t('Optionally enter a description about the database accession.'),
  51. );
  52. $db_options = tripal_db_get_db_options();
  53. $db_options[0] = 'Select a Database';
  54. ksort($db_options);
  55. $form['add_dbreference']['db_id'] = array(
  56. '#type' => 'select',
  57. '#title' => t('Database'),
  58. '#options' => $db_options,
  59. '#required' => TRUE,
  60. );
  61. $form['add_dbreference']['submit'] = array(
  62. '#type' => 'submit',
  63. '#value' => t('Add Database Reference')
  64. );
  65. return $form;
  66. }
  67. /**
  68. *
  69. *
  70. * @ingroup tripal_feature
  71. */
  72. function tripal_feature_add_ONE_dbreference_form_validate($form, &$form_state) {
  73. $db_id = $form_state['values']['db_id'];
  74. $accession = $form_state['values']['accession'];
  75. $description = $form_state['values']['description'];
  76. $feature_id = $form_state['values']['feature_id'];
  77. $nid = $form_state['values']['nid'];
  78. // Check database is valid db_id in chado
  79. $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {db} WHERE db_id=%d", $db_id));
  80. if ($tmp_obj->count != 1) {
  81. form_set_error('database', 'The database you selected is not valid. Please choose another one.');
  82. }
  83. // Check Accession is unique for database
  84. $sql = "SELECT count(*) as count FROM {dbxref} WHERE accession='%s' and db_id = %d";
  85. $tmp_obj = db_fetch_object(chado_query($sql, $accession, $db_id));
  86. if ($tmp_obj->count > 0) {
  87. form_set_error('accession', 'This accession has already been assigned to another feature in the selected database.');
  88. }
  89. }
  90. /**
  91. *
  92. *
  93. * @ingroup tripal_feature
  94. */
  95. function tripal_feature_add_ONE_dbreference_form_submit($form, &$form_state) {
  96. $db_id = $form_state['values']['db_id'];
  97. $accession = $form_state['values']['accession'];
  98. $description = $form_state['values']['description'];
  99. $feature_id = $form_state['values']['feature_id'];
  100. $nid = $form_state['values']['nid'];
  101. // create dbxref
  102. $isql = "INSERT INTO {dbxref} (db_id, accession, description) VALUES (%d, '%s', '%s')";
  103. chado_query($isql, $db_id, $accession, $description);
  104. //create feature_dbxref
  105. $dbxref = tripal_db_get_dbxref( array('db_id' => array('type' => 'INT', 'value' => $form_state['values']['db_id']),
  106. 'accession' => array('type' => 'STRING', 'exact' => TRUE, 'value' => $form_state['values']['accession']) ) );
  107. if (!empty($dbxref->dbxref_id)) {
  108. $isql = "INSERT INTO {feature_dbxref} (feature_id, dbxref_id) VALUES (%d, %d)";
  109. chado_query($isql, $feature_id, $dbxref->dbxref_id);
  110. drupal_set_message(t('Successfully Added Database Reference'));
  111. drupal_goto('node/' . $nid);
  112. }
  113. else {
  114. drupal_set_message(t('Database reference NOT successfully created...'), 'error');
  115. } //end of if dbxref was created successfully
  116. }
  117. /**
  118. *
  119. *
  120. * @ingroup tripal_feature
  121. */
  122. function tripal_feature_edit_ALL_dbreferences_page($node) {
  123. $output = '';
  124. $output .= drupal_get_form('tripal_feature_edit_ALL_db_references_form', $node);
  125. $output .= '<br />';
  126. $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
  127. $output .= '<br />';
  128. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  129. return $output;
  130. }
  131. /**
  132. * Implements Hook_form()
  133. * Handles adding of DB References to Features
  134. *
  135. * @ingroup tripal_feature
  136. */
  137. function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
  138. $form = array();
  139. $form['nid'] = array(
  140. '#type' => 'hidden',
  141. '#value' => $node->nid
  142. );
  143. $i=0;
  144. $feature = $node->feature;
  145. $references = tripal_feature_load_references($feature->feature_id);
  146. // pre populate the database options
  147. $db_options = tripal_db_get_db_options();
  148. $db_options[0] = 'Select a Database';
  149. ksort($db_options);
  150. if (sizeof($references) != 0) {
  151. foreach ($references as $ref) {
  152. $i++;
  153. $form["num-$i"] = array(
  154. '#type' => 'fieldset',
  155. '#title' => t("Database Reference") . " $i"
  156. );
  157. $form["num-$i"]["accession-$i"] = array(
  158. '#type' => 'textfield',
  159. '#title' => t('Accession'),
  160. '#size' => 30,
  161. '#required' => TRUE,
  162. '#default_value' => $ref->accession
  163. );
  164. $form["num-$i"]["db_id-$i"] = array(
  165. '#type' => 'select',
  166. '#title' => t('Database'),
  167. '#options' => $db_options,
  168. '#required' => TRUE,
  169. '#default_value' => $ref->db_id
  170. );
  171. $form["num-$i"]["dbxref_id-$i"] = array(
  172. '#type' => 'hidden',
  173. '#value' => $ref->dbxref_id
  174. );
  175. $form["num-$i"]["delete-$i"] = array(
  176. '#type' => 'submit',
  177. '#value' => t("Delete"),
  178. '#name' => "delete-$i",
  179. );
  180. }
  181. $form['num_db_references'] = array(
  182. '#type' => 'hidden',
  183. '#value' => $i
  184. );
  185. $form["submit-edits"] = array(
  186. '#type' => 'submit',
  187. '#value' => t('Update All References')
  188. );
  189. } //end of foreach db ref
  190. return $form;
  191. }
  192. /**
  193. *
  194. *
  195. * @ingroup tripal_feature
  196. */
  197. function tripal_feature_edit_ALL_db_references_form_submit($form, &$form_state) {
  198. $num_refs = $form_state['values']['num_db_references'];
  199. $action = $form_state['clicked_button']['#value'];
  200. $button = $form_state['clicked_button']['#name'];
  201. $nid = $form_state['values']['nid'];
  202. if (strcmp($action, 'Update All References')==0) {
  203. for ($i=1; $i<=$num_refs; $i++) {
  204. $dbxref_id = $form_state['values']["dbxref_id-$i"];
  205. $db_id = $form_state['values']["db_id-$i"];
  206. $accession = $form_state['values']["accession-$i"];
  207. tripal_feature_update_db_reference($dbxref_id, $db_id, $accession);
  208. }
  209. drupal_set_message(t("Updated all Database References"));
  210. drupal_goto('node/' . $nid);
  211. }
  212. elseif (strcmp($action, 'Delete')==0) {
  213. if (preg_match('/delete-(\d+)/', $button, $matches) ) {
  214. $i = $matches[1];
  215. $dbxref_id = $form_state['values']["dbxref_id-$i"];
  216. tripal_feature_delete_db_reference($dbxref_id);
  217. drupal_set_message(t("Deleted Database Reference"));
  218. drupal_goto('node/' . $nid);
  219. }
  220. else {
  221. drupal_set_message(t("Could not remove database reference:"), 'error');
  222. }
  223. }
  224. else {
  225. drupal_set_message(t("Unrecognized Button Pressed"), 'error');
  226. }
  227. }
  228. /**
  229. *
  230. *
  231. * @ingroup tripal_feature
  232. */
  233. function tripal_feature_update_db_reference($dbxref_id, $db_id, $accession) {
  234. $sql = "UPDATE {dbxref} SET db_id=%d, accession='%s' WHERE dbxref_id=%d";
  235. chado_query($sql, $db_id, $accession, $dbxref_id);
  236. }
  237. /**
  238. *
  239. *
  240. * @ingroup tripal_feature
  241. */
  242. function tripal_feature_delete_db_reference($dbxref_id) {
  243. chado_query(
  244. "DELETE FROM {dbxref} WHERE dbxref_id=%d",
  245. $dbxref_id
  246. );
  247. chado_query(
  248. "DELETE FROM {feature_dbxref} WHERE dbxref_id=%d",
  249. $dbxref_id
  250. );
  251. }
  252. /**
  253. *
  254. *
  255. * @ingroup tripal_feature
  256. */
  257. function theme_tripal_feature_edit_ALL_db_references_form($form) {
  258. $output = '';
  259. $output .= '<br /><fieldset>';
  260. $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  261. $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
  262. .'is a unique identifier for this feature in the specified database.</p>';
  263. $output .= '<table>';
  264. $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
  265. for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
  266. $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
  267. . drupal_render($form["database-$i"]) . '</td><td>'
  268. . drupal_render($form["accession-$i"]) . '</td><td>'
  269. . drupal_render($form["submit-$i"]) . '</td></tr>';
  270. }
  271. $output .= '</table><br />';
  272. $output .= drupal_render($form);
  273. $output .= '</fieldset>';
  274. return $output;
  275. }
  276. /**
  277. *
  278. *
  279. * @ingroup tripal_feature
  280. */
  281. function list_dbreferences_for_node($db_references) {
  282. if (!empty($db_references) ) {
  283. $output = '<table>';
  284. $output .= '<tr><th>Database</th><th>Accession</th></tr>';
  285. foreach ($db_references as $db) {
  286. $output .= '<tr><td>' . $db->db_name . '</td><td>' . $db->accession . '</td></tr>';
  287. } // end of foreach db reference
  288. $output .= '</table>';
  289. }
  290. else {
  291. $output = 'No Database References Added to the Current Feature';
  292. }
  293. return $output;
  294. }