tripal_stock-relationships.inc 17 KB

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