tripal_feature-relationships.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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_relationships_page($node) {
  12. $output = '';
  13. $output .= tripal_feature_implement_add_chado_properties_progress('relationships') . '<br />';
  14. $output .= '<b>All Relationships should include the CURRENT Individual (' . $node->uniquename . ')</b><br />';
  15. $output .= '<br /><b>Current Relationships</b><br />';
  16. $output .= list_relationships_for_node($node->uniquename, $node->subject_relationships, $node->object_relationships);
  17. $output .= '<br /><br />';
  18. $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node);
  19. $output .= '<br />';
  20. $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'relationships', $node->nid);
  21. return $output;
  22. }
  23. /**
  24. * Implements Hook_form()
  25. * Handles adding of Relationships to Features
  26. *
  27. * @ingroup tripal_feature
  28. */
  29. function tripal_feature_add_ONE_relationship_form($form_state, $node) {
  30. $feature_id = $node->feature_id;
  31. $organism_id = $node->organism->organism_id;
  32. $_SESSION['organism'] = $organism_id; //needed for autocomplete enter feature to work
  33. $form['rel_nid'] = array(
  34. '#type' => 'hidden',
  35. '#value' => $node->nid
  36. );
  37. $form['add_relationships'] = array(
  38. '#type' => 'fieldset',
  39. '#title' => t('Add Relationships') . '<span class="form-optional" title="This field is optional">(optional)</span>',
  40. );
  41. $form['add_relationships']['description'] = array(
  42. '#type' => 'item',
  43. '#value' => t('Relationships are specified as follows: (Subject) (Type of Relationship) (Object). For example, X part_of Y, where X & Y are genomic features.')
  44. );
  45. $form['add_relationships']['subject_id'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Subject'),
  48. '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
  49. );
  50. $cv = tripal_cv_get_cv_by_name('relationship');
  51. $type_options = tripal_cv_get_cvterm_options($cv->cv_id);
  52. $type_options[0] = 'Select a Type';
  53. ksort($type_options);
  54. $form['add_relationships']['type_id'] = array(
  55. '#type' => 'select',
  56. '#title' => t('Type of Relationship'),
  57. '#options' => $type_options
  58. );
  59. $form['add_relationships']['object_id'] = array(
  60. '#type' => 'textfield',
  61. '#title' => t('Object'),
  62. '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Feature can be used here',
  63. );
  64. $form['add_relationships']['r_description'] = array(
  65. '#type' => 'textarea',
  66. '#title' => t('Notes on the relationship') . '<span class="form-optional" title="This field is optional">+</span>',
  67. '#description' => t('Should not include Genotypes and Phenotypes'),
  68. );
  69. $form['add_relationships']['submit'] = array(
  70. '#type' => 'submit',
  71. '#value' => t('Add a Relationship')
  72. );
  73. $form['add_relationships']['r_feature_id'] = array(
  74. '#type' => 'value',
  75. '#value' => $feature_id,
  76. '#required' => TRUE
  77. );
  78. $form['add_relationships']['r_feature_uniquename'] = array(
  79. '#type' => 'value',
  80. '#value' => $node->uniquename,
  81. '#required' => TRUE
  82. );
  83. return $form;
  84. }
  85. /**
  86. *
  87. *
  88. * @ingroup tripal_feature
  89. */
  90. function tripal_feature_add_ONE_relationship_form_validate($form, &$form_state) {
  91. //Require Validation if adding
  92. if ($form_state['clicked_button']['#value'] == t('Add a Relationship') ) {
  93. // check valid feature selected for subject
  94. $criteria = array('unknown' => array('value' => $form_state['values']['subject_id'],
  95. 'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
  96. $subject_results = get_chado_stocks($criteria, 'ANY', $_SESSION['organism']);
  97. if (sizeof($subject_results) > 1) {
  98. $links= array();
  99. for ($i=0; $i<sizeof($subject_results); $i++) {
  100. $links[] = l($i+1, "node/" . $subject_results[$i]->nid); }
  101. $message = "Too many stocks match '" . $form_state['values']['subject_id'] . "'! "
  102. . " Please refine your input to match ONLY ONE stock. <br />"
  103. . "To aid in this process, here are the stocks that match your initial input: "
  104. . join(', ', $links);
  105. form_set_error('subject_id', $message);
  106. }
  107. elseif (sizeof($subject_results) < 1) {
  108. form_set_error('subject_id', t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock %link", array('%link' => l(t('here'), 'stocks'))));
  109. }
  110. elseif (sizeof($subject_results) == 1) {
  111. $form_state['values']['subject_id'] = $subject_results[0]->stock_id;
  112. }
  113. // check valid stock selected for object
  114. $criteria = array('unknown' => array('value' => $form_state['values']['object_id'],
  115. 'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
  116. $object_results = get_chado_stocks($criteria, 'ANY', $_SESSION['organism']);
  117. if (sizeof($object_results) > 1) {
  118. $links= array();
  119. for ($i=0; $i<sizeof($object_results); $i++) {
  120. $links[] = l($i+1, "node/" . $object_results[$i]->nid); }
  121. $message = "Too many stocks match '" . $form_state['values']['object_id'] . "'! "
  122. . "Please refine your input to match ONLY ONE stock. <br />"
  123. . "To aid in this process, here are the stocks that match your initial input: "
  124. . join(', ', $links);
  125. form_set_error('object_id', $message);
  126. }
  127. elseif (sizeof($object_results) < 1) {
  128. form_set_error('object_id', t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock %link", array('%link' => l(t('here'), 'stocks'))));
  129. }
  130. elseif (sizeof($object_results) == 1) {
  131. $form_state['values']['object_id'] = $object_results[0]->stock_id;
  132. }
  133. // check valid type selected
  134. if ($form_state['values']['type_id'] == 0) {
  135. form_set_error('type_id', 'Please select a type of relationship.');
  136. }
  137. else {
  138. $previous_db = tripal_db_set_active('chado');
  139. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['type_id']));
  140. tripal_db_set_active($previous_db);
  141. if ($tmp_obj->count != 1) {
  142. form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
  143. }
  144. }
  145. // check either subject or object is the current stock
  146. if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) {
  147. if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) {
  148. form_set_error('subject_id', 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
  149. }
  150. }
  151. } //end of require validation if adding relationship
  152. }
  153. /**
  154. *
  155. *
  156. * @ingroup tripal_feature
  157. */
  158. function tripal_feature_add_ONE_relationship_form_submit($form, &$form_state) {
  159. if ($form_state['values']['subject_id'] > 0) {
  160. $previous_db = db_set_active('chado');
  161. db_query(
  162. "INSERT INTO {stock_relationship} (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')",
  163. $form_state['values']['subject_id'],
  164. $form_state['values']['type_id'],
  165. $form_state['values']['object_id'],
  166. $form_state['values']['r_description']
  167. );
  168. db_set_active($previous_db);
  169. drupal_set_message(t('Successfully Added Relationship.'));
  170. } //end of insert relationship
  171. }
  172. /**
  173. *
  174. *
  175. * @ingroup tripal_feature
  176. */
  177. function tripal_feature_edit_ALL_relationships_page($node) {
  178. $output = '';
  179. $output .= drupal_get_form('tripal_feature_edit_ALL_relationships_form', $node);
  180. $output .= '<br />';
  181. $output .= drupal_get_form('tripal_feature_add_ONE_relationship_form', $node);
  182. $output .= '<br />';
  183. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  184. return $output;
  185. }
  186. /**
  187. * Implements Hook_form()
  188. * Handles adding of Properties & Synonyms to Stocks
  189. *
  190. * @ingroup tripal_feature
  191. */
  192. function tripal_feature_edit_ALL_relationships_form($form_state, $node) {
  193. $form = array();
  194. $form['nid'] = array(
  195. '#type' => 'hidden',
  196. '#value' => $node->nid
  197. );
  198. $form['r_feature_uniquename'] = array(
  199. '#type' => 'hidden',
  200. '#value' => $node->uniquename
  201. );
  202. $i=0;
  203. $feature = $node->feature;
  204. $orelationships = tripal_feature_load_relationships($feature->feature_id, 'as_object');
  205. $srelationships = tripal_feature_load_relationships($feature->feature_id, 'as_subject');
  206. $relationships = array_merge($orelationships, $srelationships);
  207. if (sizeof($relationships) != 0) {
  208. foreach ($relationships as $r) {
  209. $i++;
  210. $form["num-$i"] = array(
  211. '#type' => 'fieldset',
  212. '#title' => t("Relationship %i", array('%i' => $i)),
  213. );
  214. $form["num-$i"]["id-$i"] = array(
  215. '#type' => 'hidden',
  216. '#value' => $r->stock_relationship_id
  217. );
  218. //Enter relationship specific fields
  219. if ( !empty($r->subject_id) ) {
  220. $default = $r->subject_uniquename;
  221. $description = l($r->subject_name, 'node/' . $r->subject_nid);
  222. }
  223. else {
  224. $default = $node->uniquename;
  225. $description = "Current Feature";
  226. }
  227. $description .= " (" . $r->subject_type . ")";
  228. $form["num-$i"]["subject_id-$i"] = array(
  229. '#type' => 'textfield',
  230. //'#title' => t('Subject'),
  231. '#required' => TRUE,
  232. '#size' => 30,
  233. '#default_value' => $default,
  234. '#description' => t('%description', array('%description' => $description)),
  235. );
  236. $cv = tripal_cv_get_cv_by_name('relationship');
  237. $type_options = tripal_cv_get_cvterm_options($cv->cv_id);
  238. ksort($type_options);
  239. $form["num-$i"]["type_id-$i"] = array(
  240. '#type' => 'select',
  241. //'#title' => t('Type of Relationship'),
  242. '#options' => $type_options,
  243. '#required' => TRUE,
  244. '#default_value' => $r->relationship_type_id
  245. );
  246. if (!empty($r->object_id) ) {
  247. $default = $r->object_uniquename;
  248. $description = l($r->object_name, 'node/' . $r->object_nid);
  249. }
  250. else {
  251. $default = $node->uniquename;
  252. $description = 'Current Feature';
  253. }
  254. $description .= " (" . $r->object_type . ")";
  255. $form["num-$i"]["object_id-$i"] = array(
  256. '#type' => 'textfield',
  257. //'#title' => t('Object'),
  258. '#required' => TRUE,
  259. '#size' => 30,
  260. '#default_value' => $default,
  261. '#description' => $description
  262. );
  263. $form["num-$i"]["delete-$i"] = array(
  264. '#type' => 'submit',
  265. '#value' => t("Delete"),
  266. '#name' => "delete-$i",
  267. );
  268. } //end of foreach relationship
  269. $form['num_relationships'] = array(
  270. '#type' => 'hidden',
  271. '#value' => $i
  272. );
  273. $form["submit-edits"] = array(
  274. '#type' => 'submit',
  275. '#value' => t('Update All Relationships')
  276. );
  277. }
  278. else {
  279. $form["info"] = array(
  280. '#type' => 'markup',
  281. '#value' => t('No relationships currently exist for this feature.')
  282. );
  283. }
  284. return $form;
  285. }
  286. /**
  287. *
  288. *
  289. * @ingroup tripal_feature
  290. */
  291. function tripal_feature_edit_ALL_relationships_form_validate($form, &$form_state) {
  292. // Only Require if Updating Relationships
  293. if ($form_state['clicked_button']['#value'] == t('Update All Relationships') ) {
  294. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  295. // check valid stock selected for subject
  296. $criteria = array('unknown' => array('value' => $form_state['values']["subject_id-$i"],
  297. 'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
  298. $subject_results = get_chado_stocks($criteria, 'ANY', $_SESSION['organism']);
  299. if (sizeof($subject_results) > 1) {
  300. $links= array();
  301. for ($j=0; $j<sizeof($subject_results); $j++) {
  302. $links[] = l($j+1, "node/" . $subject_results[$j]->nid); }
  303. $message = "Too many stocks match '" . $form_state['values']["subject_id-$i"] . "'! "
  304. . "Please refine your input to match ONLY ONE stock. <br />"
  305. . "To aid in this process, here are the stocks that match your initial input: "
  306. . join(', ', $links);
  307. form_set_error("subject_id-$i", $message);
  308. }
  309. elseif (sizeof($subject_results) < 1) {
  310. form_set_error("subject_id-$i", t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock %link", array('%link' => l(t('here'), 'stocks'))));
  311. }
  312. elseif (sizeof($subject_results) == 1) {
  313. $form_state['values']["subject_id-$i"] = $subject_results[0]->stock_id;
  314. }
  315. // check valid stock selected for object
  316. $criteria = array('unknown' => array('value' => $form_state['values']["object_id-$i"],
  317. 'columns' => array('name', 'uniquename', 'accession', 'synonym') ));
  318. $object_results = get_chado_stocks($criteria, 'ANY' , $_SESSION['organism']);
  319. if (sizeof($object_results) > 1) {
  320. $links= array();
  321. for ($j=0; $j<sizeof($object_results); $j++) {
  322. $links[] = l($j+1, "node/" . $object_results[$j]->nid); }
  323. $message = "Too many stocks match '" . $form_state['values']["object_id-$i"] . "'! "
  324. . "Please refine your input to match ONLY ONE stock. <br />"
  325. . "To aid in this process, here are the stocks that match your initial input: "
  326. . join(', ', $links);
  327. form_set_error("object_id-$i", $message);
  328. }
  329. elseif (sizeof($object_results) < 1) {
  330. form_set_error("object_id-$i", t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock %link", array('%link' => l(t('here'), 'stocks'))));
  331. }
  332. elseif (sizeof($object_results) == 1) {
  333. $form_state['values']["object_id-$i"] = $object_results[0]->stock_id;
  334. }
  335. // check valid type selected
  336. if ($form_state['values']["type_id-$i"] == 0) {
  337. form_set_error('type_id', 'Please select a type of relationship.');
  338. }
  339. else {
  340. $previous_db = tripal_db_set_active('chado');
  341. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d" , $form_state['values']["type_id-$i"]));
  342. tripal_db_set_active($previous_db);
  343. if ($tmp_obj->count != 1) {
  344. form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
  345. }
  346. }
  347. // check either subject or object is the current stock
  348. if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
  349. if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
  350. form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
  351. }
  352. }
  353. } // end of for each relationship
  354. } //end of if updating relationships
  355. }
  356. /**
  357. *
  358. *
  359. * @ingroup tripal_feature
  360. */
  361. function tripal_feature_edit_ALL_relationships_form_submit($form, &$form_state) {
  362. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  363. //Update all
  364. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  365. //process stock textfields
  366. tripal_feature_update_relationship(
  367. $form_state['values']["id-$i"],
  368. $form_state['values']["subject_id-$i"],
  369. $form_state['values']["type_id-$i"],
  370. $form_state['values']["object_id-$i"]
  371. );
  372. }
  373. drupal_set_message(t("Updated all Relationships"));
  374. drupal_goto('node/' . $form_state['values']['nid']);
  375. }
  376. elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  377. $i = $matches[1];
  378. tripal_feature_delete_relationship($form_state['values']["id-$i"]);
  379. drupal_set_message(t("Deleted Relationship"));
  380. }
  381. elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
  382. drupal_goto('node/' . $form_state['values']['nid']);
  383. }
  384. else {
  385. drupal_set_message(t("Unrecognized Button Pressed"), 'error');
  386. }
  387. }
  388. /**
  389. *
  390. *
  391. * @ingroup tripal_feature
  392. */
  393. function tripal_feature_update_relationship($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
  394. $previous_db = db_set_active('chado');
  395. db_query(
  396. "UPDATE {stock_relationship} SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
  397. $subject_id,
  398. $cvterm_id,
  399. $object_id,
  400. $stock_relationship_id
  401. );
  402. db_set_active($previous_db);
  403. }
  404. /**
  405. *
  406. *
  407. * @ingroup tripal_feature
  408. */
  409. function tripal_feature_delete_relationship($stock_relationship_id) {
  410. $previous_db = db_set_active('chado');
  411. db_query(
  412. "DELETE FROM {stock_relationship} WHERE stock_relationship_id=%d",
  413. $stock_relationship_id
  414. );
  415. db_set_active($previous_db);
  416. }
  417. /**
  418. *
  419. *
  420. * @ingroup tripal_feature
  421. */
  422. function theme_tripal_feature_edit_ALL_relationships_form($form) {
  423. $output = '';
  424. $output .= '<br /><fieldset>';
  425. $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  426. $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
  427. .'the subject and object of the relationship can contain the uniquename, name, database '
  428. .'reference or synonym of a stock of the same organism.</p>';
  429. $output .= '<table>';
  430. $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
  431. for ($i=1; $i <= $form['num_relationships']['#value']; $i++) {
  432. $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td>' .
  433. '<td>' . drupal_render($form["subject_id-$i"]) . '</td>' .
  434. '<td>' . drupal_render($form["type_id-$i"]) . '</td>' .
  435. '<td>' . drupal_render($form["object_id-$i"]) . '</td>' .
  436. '<td>' . drupal_render($form["submit-$i"]) . '</td></tr>';
  437. }
  438. $output .= '</table><br />';
  439. $output .= drupal_render($form);
  440. $output .= '</fieldset>';
  441. return $output;
  442. }
  443. /**
  444. *
  445. *
  446. * @ingroup tripal_feature
  447. */
  448. function tripal_feature_list_relationships_for_node($feature_name, $subject_relationships, $object_relationships) {
  449. if (!empty($subject_relationships) OR !empty($object_relationships) ) {
  450. $output = '<table>';
  451. $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
  452. if (!empty($subject_relationships) ) {
  453. foreach ($subject_relationships as $s) {
  454. $output .= '<tr><td>' . $s->subject_name . '</td><td>' . $s->relationship_type . '</td><td>' . $feature_name . '</td></tr>';
  455. }
  456. }
  457. if (!empty($object_relationships) ) {
  458. foreach ($object_relationships as $o) {
  459. $output .= '<tr><td>' . $feature_name . '</td><td>' . $o->relationship_type . '</td><td>' . $o->object_name . '</td></tr>';
  460. } // end of foreach property
  461. }
  462. $output .= '</table>';
  463. }
  464. else {
  465. $output = 'No Relationships For the Current Feature';
  466. }
  467. return $output;
  468. }