tripal_stock-relationships.inc 18 KB

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