tripal_feature-relationships.inc 19 KB

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