tripal_feature-relationships.inc 19 KB

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