tripal_stock-relationships.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_stock
  6. */
  7. function tripal_stock_add_ALL_relationships_page($node) {
  8. $output = '';
  9. $output .= tripal_stock_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 .= tripal_stock_list_relationships_for_node($node->uniquename, $node->subject_relationships, $node->object_relationships);
  13. $output .= '<br><br>';
  14. $output .= drupal_get_form('tripal_stock_add_ONE_relationship_form', $node);
  15. $output .= '<br>';
  16. $output .= drupal_get_form('tripal_stock_add_chado_properties_navigate', 'relationships', $node->nid);
  17. return $output;
  18. }
  19. /**
  20. * Implements Hook_form(): Handles adding of Relationships to Stocks
  21. *
  22. * @ingroup tripal_stock
  23. */
  24. function tripal_stock_add_ONE_relationship_form($form_state, $node) {
  25. $stock_id = $node->stock_id;
  26. $organism_id = $node->organism->organism_id;
  27. $_SESSION['organism'] = $organism_id; //needed for autocomplete enter stock to work
  28. $form['rel_nid'] = array(
  29. '#type' => 'hidden',
  30. '#value' => $node->nid
  31. );
  32. $form['add_relationships'] = array(
  33. '#type' => 'fieldset',
  34. '#title' => t('Add Relationships') . '<span class="form-optional" title="This field is optional">(optional)</span>',
  35. );
  36. $form['add_relationships']['description'] = array(
  37. '#type' => 'item',
  38. '#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.')
  39. );
  40. $form['add_relationships']['subject_id'] = array(
  41. '#type' => 'textfield',
  42. '#title' => t('Subject'),
  43. '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Stock can be used here',
  44. );
  45. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
  46. $type_options[0] = 'Select a Type';
  47. ksort($type_options);
  48. $form['add_relationships']['type_id'] = array(
  49. '#type' => 'select',
  50. '#title' => t('Type of Relationship'),
  51. '#options' => $type_options
  52. );
  53. $form['add_relationships']['object_id'] = array(
  54. '#type' => 'textfield',
  55. '#title' => t('Object'),
  56. '#description' => 'The Uniquename, Name, Database Reference or Synonym of a Stock can be used here',
  57. );
  58. $form['add_relationships']['r_description'] = array(
  59. '#type' => 'textarea',
  60. '#title' => t('Notes on the relationship') . '<span class="form-optional" title="This field is optional">+</span>',
  61. '#description' => t('Should not include Genotypes and Phenotypes'),
  62. );
  63. $form['add_relationships']['submit'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Add a Relationship')
  66. );
  67. $form['add_relationships']['r_stock_id'] = array(
  68. '#type' => 'value',
  69. '#value' => $stock_id,
  70. '#required' => TRUE
  71. );
  72. $form['add_relationships']['r_stock_uniquename'] = array(
  73. '#type' => 'value',
  74. '#value' => $node->uniquename,
  75. '#required' => TRUE
  76. );
  77. return $form;
  78. }
  79. /**
  80. *
  81. *
  82. * @ingroup tripal_stock
  83. */
  84. function tripal_stock_add_ONE_relationship_form_validate($form, &$form_state) {
  85. //Require Validation if adding
  86. if ($form_state['clicked_button']['#value'] == t('Add a Relationship') ) {
  87. // check valid stock selected for subject
  88. $subject_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']['subject_id'], $_SESSION['organism']);
  89. if (sizeof($subject_results) > 1) {
  90. $links= array();
  91. for ($i=0; $i<sizeof($subject_results); $i++) { $links[] = l($i+1, "node/".$subject_results[$i]->nid); }
  92. $message = "Too many stocks match '".$form_state['values']['subject_id']."'! "
  93. . " Please refine your input to match ONLY ONE stock. <br>"
  94. . "To aid in this process, here are the stocks that match your initial input: "
  95. .join(', ',$links);
  96. form_set_error('subject_id', $message);
  97. } elseif (sizeof($subject_results) < 1) {
  98. 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'));
  99. } elseif (sizeof($subject_results) == 1) {
  100. $form_state['values']['subject_id'] = $subject_results[0]->stock_id;
  101. }
  102. // check valid stock selected for object
  103. $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']['object_id'], $_SESSION['organism']);
  104. if (sizeof($object_results) > 1) {
  105. $links= array();
  106. for ($i=0; $i<sizeof($object_results); $i++) { $links[] = l($i+1, "node/".$object_results[$i]->nid); }
  107. $message = "Too many stocks match '".$form_state['values']['object_id']."'! "
  108. . "Please refine your input to match ONLY ONE stock. <br>"
  109. . "To aid in this process, here are the stocks that match your initial input: "
  110. .join(', ',$links);
  111. form_set_error('object_id', $message);
  112. } elseif (sizeof($object_results) < 1) {
  113. 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'));
  114. } elseif (sizeof($object_results) == 1) {
  115. $form_state['values']['object_id'] = $object_results[0]->stock_id;
  116. }
  117. // check valid type selected
  118. if ($form_state['values']['type_id'] == 0) {
  119. form_set_error('type_id', 'Please select a type of relationship.');
  120. } else {
  121. $previous_db = tripal_db_set_active('chado');
  122. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']['type_id']));
  123. tripal_db_set_active($previous_db);
  124. if ($tmp_obj->count != 1) {
  125. form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
  126. }
  127. }
  128. // check either subject or object is the current stock
  129. if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) {
  130. if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) {
  131. form_set_error('subject_id', 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').');
  132. }
  133. }
  134. } //end of require validation if adding relationship
  135. }
  136. /**
  137. *
  138. *
  139. * @ingroup tripal_stock
  140. */
  141. function tripal_stock_add_ONE_relationship_form_submit($form, &$form_state) {
  142. if ($form_state['values']['subject_id'] > 0) {
  143. $previous_db = tripal_db_set_active('chado');
  144. db_query(
  145. "INSERT INTO stock_relationship (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')",
  146. $form_state['values']['subject_id'],
  147. $form_state['values']['type_id'],
  148. $form_state['values']['object_id'],
  149. $form_state['values']['r_description']
  150. );
  151. tripal_db_set_active($previous_db);
  152. drupal_set_message('Successfully Added Relationship.');
  153. } //end of insert relationship
  154. }
  155. /**
  156. *
  157. *
  158. * @ingroup tripal_stock
  159. */
  160. function tripal_stock_edit_ALL_relationships_page($node) {
  161. $output = '';
  162. $output .= drupal_get_form('tripal_stock_edit_ALL_relationships_form', $node);
  163. $output .= '<br>';
  164. $output .= drupal_get_form('tripal_stock_add_ONE_relationship_form', $node);
  165. $output .= '<br>';
  166. $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
  167. return $output;
  168. }
  169. /**
  170. * Implements Hook_form()
  171. *
  172. * @ingroup tripal_stock
  173. */
  174. function tripal_stock_edit_ALL_relationships_form($form_state, $node) {
  175. $form = array();
  176. $form['nid'] = array(
  177. '#type' => 'hidden',
  178. '#value' => $node->nid
  179. );
  180. $form['r_stock_uniquename'] = array(
  181. '#type' => 'hidden',
  182. '#value' => $node->uniquename
  183. );
  184. $i=0;
  185. $relationships = array_merge($node->object_relationships, $node->subject_relationships);
  186. if (sizeof($relationships) != 0) {
  187. foreach ($relationships as $r) {
  188. $i++;
  189. $form["num-$i"] = array(
  190. '#type' => 'item',
  191. '#value' => $i.'.'
  192. );
  193. $form["id-$i"] = array(
  194. '#type' => 'hidden',
  195. '#value' => $r->stock_relationship_id
  196. );
  197. //Enter relationship specific fields
  198. if ( !empty($r->subject_id) ) {
  199. $default = $r->subject_uniquename;
  200. $description = l($r->subject_name, 'node/'.$r->subject_nid);
  201. } else { $default = $node->uniquename; $description = 'Current Stock'; }
  202. $form["subject_id-$i"] = array(
  203. '#type' => 'textfield',
  204. //'#title' => t('Subject'),
  205. '#required' => TRUE,
  206. '#size' => 30,
  207. '#default_value' => $default,
  208. '#description' => $description,
  209. );
  210. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
  211. ksort($type_options);
  212. $form["type_id-$i"] = array(
  213. '#type' => 'select',
  214. //'#title' => t('Type of Relationship'),
  215. '#options' => $type_options,
  216. '#required' => TRUE,
  217. '#default_value' => $r->relationship_type_id
  218. );
  219. if ( !empty($r->object_id) ) {
  220. $default = $r->object_uniquename;
  221. $description = l($r->object_name, 'node/'.$r->object_nid);
  222. } else { $default = $node->uniquename; $description = 'Current Stock'; }
  223. $form["object_id-$i"] = array(
  224. '#type' => 'textfield',
  225. //'#title' => t('Object'),
  226. '#required' => TRUE,
  227. '#size' => 30,
  228. '#default_value' => $default,
  229. '#description' => $description
  230. );
  231. $form["submit-$i"] = array(
  232. '#type' => 'submit',
  233. '#value' => t("Delete #$i")
  234. );
  235. }} //end of foreach relationship
  236. $form['num_relationships'] = array(
  237. '#type' => 'hidden',
  238. '#value' => $i
  239. );
  240. $form["submit-edits"] = array(
  241. '#type' => 'submit',
  242. '#value' => t('Update Relationships')
  243. );
  244. return $form;
  245. }
  246. /**
  247. *
  248. *
  249. * @ingroup tripal_stock
  250. */
  251. function tripal_stock_edit_ALL_relationships_form_validate($form, &$form_state) {
  252. // Only Require if Updating Relationships
  253. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  254. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  255. // check valid stock selected for subject
  256. $subject_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["subject_id-$i"], $_SESSION['organism']);
  257. if (sizeof($subject_results) > 1) {
  258. $links= array();
  259. for ($j=0; $j<sizeof($subject_results); $j++) { $links[] = l($j+1, "node/".$subject_results[$j]->nid); }
  260. $message = "Too many stocks match '".$form_state['values']["subject_id-$i"]."'! "
  261. . "Please refine your input to match ONLY ONE stock. <br>"
  262. . "To aid in this process, here are the stocks that match your initial input: "
  263. .join(', ',$links);
  264. form_set_error("subject_id-$i", $message);
  265. } elseif (sizeof($subject_results) < 1) {
  266. 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'));
  267. } elseif (sizeof($subject_results) == 1) {
  268. $form_state['values']["subject_id-$i"] = $subject_results[0]->stock_id;
  269. }
  270. // check valid stock selected for object
  271. $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["object_id-$i"], $_SESSION['organism']);
  272. if (sizeof($object_results) > 1) {
  273. $links= array();
  274. for ($j=0; $j<sizeof($object_results); $j++) { $links[] = l($j+1, "node/".$object_results[$j]->nid); }
  275. $message = "Too many stocks match '".$form_state['values']["object_id-$i"]."'! "
  276. . "Please refine your input to match ONLY ONE stock. <br>"
  277. . "To aid in this process, here are the stocks that match your initial input: "
  278. .join(', ',$links);
  279. form_set_error("object_id-$i", $message);
  280. } elseif (sizeof($object_results) < 1) {
  281. 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'));
  282. } elseif (sizeof($object_results) == 1) {
  283. $form_state['values']["object_id-$i"] = $object_results[0]->stock_id;
  284. }
  285. // check valid type selected
  286. if ($form_state['values']["type_id-$i"] == 0) {
  287. form_set_error('type_id', 'Please select a type of relationship.');
  288. } else {
  289. $previous_db = tripal_db_set_active('chado');
  290. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']["type_id-$i"]));
  291. tripal_db_set_active($previous_db);
  292. if ($tmp_obj->count != 1) {
  293. form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
  294. }
  295. }
  296. // check either subject or object is the current stock
  297. if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
  298. if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
  299. form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').');
  300. }
  301. }
  302. } // end of for each relationship
  303. } //end of if updating relationships
  304. }
  305. /**
  306. *
  307. *
  308. * @ingroup tripal_stock
  309. */
  310. function tripal_stock_edit_ALL_relationships_form_submit($form, &$form_state) {
  311. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  312. //Update all
  313. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  314. //process stock textfields
  315. tripal_stock_update_relationship(
  316. $form_state['values']["id-$i"],
  317. $form_state['values']["subject_id-$i"],
  318. $form_state['values']["type_id-$i"],
  319. $form_state['values']["object_id-$i"]
  320. );
  321. }
  322. drupal_set_message("Updated all Relationships");
  323. drupal_goto('node/'.$form_state['values']['nid']);
  324. } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  325. $i = $matches[1];
  326. tripal_stock_delete_relationship($form_state['values']["id-$i"]);
  327. drupal_set_message("Deleted Relationship");
  328. } elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
  329. drupal_goto('node/'.$form_state['values']['nid']);
  330. } else {
  331. drupal_set_message("Unrecognized Button Pressed",'error');
  332. }
  333. }
  334. /**
  335. *
  336. *
  337. * @ingroup tripal_stock
  338. */
  339. function tripal_stock_update_relationship ($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
  340. $previous_db = tripal_db_set_active('chado');
  341. db_query(
  342. "UPDATE stock_relationship SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
  343. $subject_id,
  344. $cvterm_id,
  345. $object_id,
  346. $stock_relationship_id
  347. );
  348. tripal_db_set_active($previous_db);
  349. }
  350. /**
  351. *
  352. *
  353. * @ingroup tripal_stock
  354. */
  355. function tripal_stock_delete_relationship ($stock_relationship_id) {
  356. $previous_db = tripal_db_set_active('chado');
  357. db_query(
  358. "DELETE FROM stock_relationship WHERE stock_relationship_id=%d",
  359. $stock_relationship_id
  360. );
  361. tripal_db_set_active($previous_db);
  362. }
  363. /**
  364. *
  365. *
  366. * @ingroup tripal_stock
  367. */
  368. function theme_tripal_stock_edit_ALL_relationships_form ($form) {
  369. $output = '';
  370. $output .= '<br><fieldset>';
  371. $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  372. $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
  373. .'the subject and object of the relationship can contain the uniquename, name, database '
  374. .'reference or synonym of a stock of the same organism.</p>';
  375. $output .= '<table>';
  376. $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
  377. for ($i=1; $i<=$form['num_relationships']['#value']; $i++) {
  378. $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td><td>'
  379. .drupal_render($form["subject_id-$i"]).'</td><td>'
  380. .drupal_render($form["type_id-$i"]).'</td><td>'
  381. .drupal_render($form["object_id-$i"]).'</td><td>'
  382. .drupal_render($form["submit-$i"]).'</td></tr>';
  383. }
  384. $output .= '</table><br>';
  385. $output .= drupal_render($form);
  386. $output .= '</fieldset>';
  387. return $output;
  388. }
  389. /**
  390. *
  391. *
  392. * @ingroup tripal_stock
  393. */
  394. function tripal_stock_list_relationships_for_node($stock_name, $subject_relationships, $object_relationships) {
  395. if (!empty($subject_relationships) OR !empty($object_relationships) ) {
  396. $output = '<table>';
  397. $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
  398. if (!empty($subject_relationships) ) {
  399. foreach ($subject_relationships as $s) {
  400. $output .= '<tr><td>'.$s->subject_name.'</td><td>'.$s->relationship_type.'</td><td>'.$stock_name.'</td></tr>';
  401. }
  402. }
  403. if (!empty($object_relationships) ) {
  404. foreach ($object_relationships as $o) {
  405. $output .= '<tr><td>'.$stock_name.'</td><td>'.$o->relationship_type.'</td><td>'.$o->object_name.'</td></tr>';
  406. } // end of foreach property
  407. }
  408. $output .= '</table>';
  409. } else {
  410. $output = 'No Relationships Involving the Current Stock';
  411. }
  412. return $output;
  413. }