tripal_feature-relationships.inc 19 KB

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