tripal_feature-relationships.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 Stocks
  21. */
  22. function tripal_feature_add_ONE_relationship_form($form_state, $node) {
  23. $stock_id = $node->stock_id;
  24. $organism_id = $node->organism->organism_id;
  25. $_SESSION['organism'] = $organism_id; //needed for autocomplete enter stock 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, Fred is_the_paternal_parent_of Matty, where Fred & Matty are both stocks.')
  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 Stock 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 Stock 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_stock_id'] = array(
  67. '#type' => 'value',
  68. '#value' => $stock_id,
  69. '#required' => TRUE
  70. );
  71. $form['add_relationships']['r_stock_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 stock 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_stock_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. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_feature_relationship_cv', 'null') );
  216. ksort($type_options);
  217. $form["type_id-$i"] = array(
  218. '#type' => 'select',
  219. //'#title' => t('Type of Relationship'),
  220. '#options' => $type_options,
  221. '#required' => TRUE,
  222. '#default_value' => $r->relationship_type_id
  223. );
  224. if ( !empty($r->object_id) ) {
  225. $default = $r->object_uniquename;
  226. $description = l($r->object_name, 'node/'.$r->object_nid);
  227. } else { $default = $node->uniquename; $description = 'Current Feature'; }
  228. $form["object_id-$i"] = array(
  229. '#type' => 'textfield',
  230. //'#title' => t('Object'),
  231. '#required' => TRUE,
  232. '#size' => 30,
  233. '#default_value' => $default,
  234. '#description' => $description
  235. );
  236. $form["submit-$i"] = array(
  237. '#type' => 'submit',
  238. '#value' => t("Delete #$i")
  239. );
  240. }} //end of foreach relationship
  241. $form['num_relationships'] = array(
  242. '#type' => 'hidden',
  243. '#value' => $i
  244. );
  245. $form["submit-edits"] = array(
  246. '#type' => 'submit',
  247. '#value' => t('Update Relationships')
  248. );
  249. return $form;
  250. }
  251. /*************************************************************************
  252. *
  253. */
  254. function tripal_feature_edit_ALL_relationships_form_validate($form, &$form_state) {
  255. // Only Require if Updating Relationships
  256. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  257. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  258. // check valid stock selected for subject
  259. $criteria = array('unknown' => array('value'=>$form_state['values']["subject_id-$i"],
  260. 'columns'=>array('name','uniquename','accession','synonym') ));
  261. $subject_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']);
  262. if (sizeof($subject_results) > 1) {
  263. $links= array();
  264. for ($j=0; $j<sizeof($subject_results); $j++) { $links[] = l($j+1, "node/".$subject_results[$j]->nid); }
  265. $message = "Too many stocks match '".$form_state['values']["subject_id-$i"]."'! "
  266. . "Please refine your input to match ONLY ONE stock. <br>"
  267. . "To aid in this process, here are the stocks that match your initial input: "
  268. .join(', ',$links);
  269. form_set_error("subject_id-$i", $message);
  270. } elseif (sizeof($subject_results) < 1) {
  271. 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'));
  272. } elseif (sizeof($subject_results) == 1) {
  273. $form_state['values']["subject_id-$i"] = $subject_results[0]->stock_id;
  274. }
  275. // check valid stock selected for object
  276. $criteria = array('unknown' => array('value'=> $form_state['values']["object_id-$i"],
  277. 'columns'=>array('name','uniquename','accession','synonym') ));
  278. $object_results = get_chado_stocks($criteria,'ANY',$_SESSION['organism']);
  279. if (sizeof($object_results) > 1) {
  280. $links= array();
  281. for ($j=0; $j<sizeof($object_results); $j++) { $links[] = l($j+1, "node/".$object_results[$j]->nid); }
  282. $message = "Too many stocks match '".$form_state['values']["object_id-$i"]."'! "
  283. . "Please refine your input to match ONLY ONE stock. <br>"
  284. . "To aid in this process, here are the stocks that match your initial input: "
  285. .join(', ',$links);
  286. form_set_error("object_id-$i", $message);
  287. } elseif (sizeof($object_results) < 1) {
  288. 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'));
  289. } elseif (sizeof($object_results) == 1) {
  290. $form_state['values']["object_id-$i"] = $object_results[0]->stock_id;
  291. }
  292. // check valid type selected
  293. if ($form_state['values']["type_id-$i"] == 0) {
  294. form_set_error('type_id', 'Please select a type of relationship.');
  295. } else {
  296. $previous_db = tripal_db_set_active('chado');
  297. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']["type_id-$i"]));
  298. tripal_db_set_active($previous_db);
  299. if ($tmp_obj->count != 1) {
  300. form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
  301. }
  302. }
  303. // check either subject or object is the current stock
  304. if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
  305. if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
  306. form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').');
  307. }
  308. }
  309. } // end of for each relationship
  310. } //end of if updating relationships
  311. }
  312. /*************************************************************************
  313. *
  314. */
  315. function tripal_feature_edit_ALL_relationships_form_submit($form, &$form_state) {
  316. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  317. //Update all
  318. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  319. //process stock textfields
  320. tripal_feature_update_relationship(
  321. $form_state['values']["id-$i"],
  322. $form_state['values']["subject_id-$i"],
  323. $form_state['values']["type_id-$i"],
  324. $form_state['values']["object_id-$i"]
  325. );
  326. }
  327. drupal_set_message("Updated all Relationships");
  328. drupal_goto('node/'.$form_state['values']['nid']);
  329. } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  330. $i = $matches[1];
  331. tripal_feature_delete_relationship($form_state['values']["id-$i"]);
  332. drupal_set_message("Deleted Relationship");
  333. } elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
  334. drupal_goto('node/'.$form_state['values']['nid']);
  335. } else {
  336. drupal_set_message("Unrecognized Button Pressed",'error');
  337. }
  338. }
  339. /*************************************************************************
  340. *
  341. */
  342. function tripal_feature_update_relationship ($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
  343. $previous_db = db_set_active('chado');
  344. db_query(
  345. "UPDATE stock_relationship SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
  346. $subject_id,
  347. $cvterm_id,
  348. $object_id,
  349. $stock_relationship_id
  350. );
  351. db_set_active($previous_db);
  352. }
  353. /*************************************************************************
  354. *
  355. */
  356. function tripal_feature_delete_relationship ($stock_relationship_id) {
  357. $previous_db = db_set_active('chado');
  358. db_query(
  359. "DELETE FROM stock_relationship WHERE stock_relationship_id=%d",
  360. $stock_relationship_id
  361. );
  362. db_set_active($previous_db);
  363. }
  364. /*************************************************************************
  365. *
  366. */
  367. function theme_tripal_feature_edit_ALL_relationships_form ($form) {
  368. $output = '';
  369. $output .= '<br><fieldset>';
  370. $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  371. $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
  372. .'the subject and object of the relationship can contain the uniquename, name, database '
  373. .'reference or synonym of a stock of the same organism.</p>';
  374. $output .= '<table>';
  375. $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
  376. for ($i=1; $i<=$form['num_relationships']['#value']; $i++) {
  377. $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td><td>'
  378. .drupal_render($form["subject_id-$i"]).'</td><td>'
  379. .drupal_render($form["type_id-$i"]).'</td><td>'
  380. .drupal_render($form["object_id-$i"]).'</td><td>'
  381. .drupal_render($form["submit-$i"]).'</td></tr>';
  382. }
  383. $output .= '</table><br>';
  384. $output .= drupal_render($form);
  385. $output .= '</fieldset>';
  386. return $output;
  387. }
  388. /*************************************************************************************************************
  389. * @section
  390. * Supplementary Functions
  391. */
  392. /**
  393. *
  394. */
  395. function list_relationships_for_node($stock_name, $subject_relationships, $object_relationships) {
  396. if (!empty($subject_relationships) OR !empty($object_relationships) ) {
  397. $output = '<table>';
  398. $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
  399. if (!empty($subject_relationships) ) {
  400. foreach ($subject_relationships as $s) {
  401. $output .= '<tr><td>'.$s->subject_name.'</td><td>'.$s->relationship_type.'</td><td>'.$stock_name.'</td></tr>';
  402. }
  403. }
  404. if (!empty($object_relationships) ) {
  405. foreach ($object_relationships as $o) {
  406. $output .= '<tr><td>'.$stock_name.'</td><td>'.$o->relationship_type.'</td><td>'.$o->object_name.'</td></tr>';
  407. } // end of foreach property
  408. }
  409. $output .= '</table>';
  410. } else {
  411. $output = 'No Relationships Involving the Current Stock';
  412. }
  413. return $output;
  414. }