tripal_feature-relationships.inc 19 KB

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