tripal_feature-relationships.inc 18 KB

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