tripal_feature-relationships.inc 19 KB

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