tripal_stock-relationships.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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', $node);
  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->stock_id;
  26. $organism_id = $node->stock->organism_id->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->stock->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->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->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. // All Stock Relationships
  177. $node = tripal_core_expand_chado_vars($node, 'table', 'stock_relationship');
  178. // compile all relationships into one variable
  179. $relationships = array();
  180. if (is_array($node->stock->stock_relationship->subject_id)) {
  181. foreach ($node->stock->stock_relationship->subject_id as $r) {
  182. $relationships[$r->stock_relationship_id] = $r;
  183. }
  184. } elseif (is_object($node->stock->stock_relationship->subject_id)) {
  185. $relationships[$node->stock->stock_relationship->subject_id->stock_relationship_id] = $node->stock->stock_relationship->subject_id;
  186. }
  187. if (is_array($node->stock->stock_relationship->object_id)) {
  188. foreach ($node->stock->stock_relationship->object_id as $r) {
  189. $relationships[$r->stock_relationship_id] = $r;
  190. }
  191. } elseif (is_object($node->stock->stock_relationship->object_id)) {
  192. $relationships[$node->stock->stock_relationship->object_id->stock_relationship_id] = $node->stock->stock_relationship->object_id;
  193. }
  194. $form['nid'] = array(
  195. '#type' => 'hidden',
  196. '#value' => $node->nid
  197. );
  198. $form['r_stock_uniquename'] = array(
  199. '#type' => 'hidden',
  200. '#value' => $node->stock->uniquename
  201. );
  202. $i=0;
  203. if (sizeof($relationships) != 0) {
  204. foreach ($relationships as $r) {
  205. $i++;
  206. $form["num-$i"] = array(
  207. '#type' => 'item',
  208. '#value' => $i.'.'
  209. );
  210. $form["id-$i"] = array(
  211. '#type' => 'hidden',
  212. '#value' => $r->stock_relationship_id
  213. );
  214. //Enter relationship specific fields
  215. if ( $node->stock->stock_id != $r->subject_id->stock_id ) {
  216. $default = $r->subject_id->uniquename;
  217. $description = l($r->subject_id->name, 'node/'.$r->subject_id->nid);
  218. } else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
  219. $form["subject_id-$i"] = array(
  220. '#type' => 'textfield',
  221. //'#title' => t('Subject'),
  222. '#required' => TRUE,
  223. '#size' => 30,
  224. '#default_value' => $default,
  225. '#description' => $description,
  226. );
  227. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
  228. ksort($type_options);
  229. $form["type_id-$i"] = array(
  230. '#type' => 'select',
  231. //'#title' => t('Type of Relationship'),
  232. '#options' => $type_options,
  233. '#required' => TRUE,
  234. '#default_value' => $r->type_id->cvterm_id
  235. );
  236. if ( $node->stock->stock_id != $r->object_id->stock_id ) {
  237. $default = $r->object_id->uniquename;
  238. $description = l($r->object_id->name, 'node/'.$r->object_id->nid);
  239. } else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
  240. $form["object_id-$i"] = array(
  241. '#type' => 'textfield',
  242. //'#title' => t('Object'),
  243. '#required' => TRUE,
  244. '#size' => 30,
  245. '#default_value' => $default,
  246. '#description' => $description
  247. );
  248. $form["submit-$i"] = array(
  249. '#type' => 'submit',
  250. '#value' => t("Delete #$i")
  251. );
  252. }} //end of foreach relationship
  253. $form['num_relationships'] = array(
  254. '#type' => 'hidden',
  255. '#value' => $i
  256. );
  257. $form["submit-edits"] = array(
  258. '#type' => 'submit',
  259. '#value' => t('Update Relationships')
  260. );
  261. return $form;
  262. }
  263. /**
  264. *
  265. *
  266. * @ingroup tripal_stock
  267. */
  268. function tripal_stock_edit_ALL_relationships_form_validate($form, &$form_state) {
  269. // Only Require if Updating Relationships
  270. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  271. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  272. // check valid stock selected for subject
  273. $subject_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["subject_id-$i"], $_SESSION['organism']);
  274. if (sizeof($subject_results) > 1) {
  275. $links= array();
  276. for ($j=0; $j<sizeof($subject_results); $j++) { $links[] = l($j+1, "node/".$subject_results[$j]->nid); }
  277. $message = "Too many stocks match '".$form_state['values']["subject_id-$i"]."'! "
  278. . "Please refine your input to match ONLY ONE stock. <br>"
  279. . "To aid in this process, here are the stocks that match your initial input: "
  280. .join(', ',$links);
  281. form_set_error("subject_id-$i", $message);
  282. } elseif (sizeof($subject_results) < 1) {
  283. 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'));
  284. } elseif (sizeof($subject_results) == 1) {
  285. $form_state['values']["subject_id-$i"] = $subject_results[0]->stock->stock_id;
  286. }
  287. // check valid stock selected for object
  288. $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["object_id-$i"], $_SESSION['organism']);
  289. if (sizeof($object_results) > 1) {
  290. $links= array();
  291. for ($j=0; $j<sizeof($object_results); $j++) { $links[] = l($j+1, "node/".$object_results[$j]->nid); }
  292. $message = "Too many stocks match '".$form_state['values']["object_id-$i"]."'! "
  293. . "Please refine your input to match ONLY ONE stock. <br>"
  294. . "To aid in this process, here are the stocks that match your initial input: "
  295. .join(', ',$links);
  296. form_set_error("object_id-$i", $message);
  297. } elseif (sizeof($object_results) < 1) {
  298. 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'));
  299. } elseif (sizeof($object_results) == 1) {
  300. $form_state['values']["object_id-$i"] = $object_results[0]->stock->stock_id;
  301. }
  302. // check valid type selected
  303. if ($form_state['values']["type_id-$i"] == 0) {
  304. form_set_error('type_id', 'Please select a type of relationship.');
  305. } else {
  306. $previous_db = tripal_db_set_active('chado');
  307. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM cvterm WHERE cvterm_id=%d",$form_state['values']["type_id-$i"]));
  308. tripal_db_set_active($previous_db);
  309. if ($tmp_obj->count != 1) {
  310. form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
  311. }
  312. }
  313. // check either subject or object is the current stock
  314. if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
  315. if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
  316. form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock ('.$form_state['values']['r_stock_uniquename'].').');
  317. }
  318. }
  319. } // end of for each relationship
  320. } //end of if updating relationships
  321. }
  322. /**
  323. *
  324. *
  325. * @ingroup tripal_stock
  326. */
  327. function tripal_stock_edit_ALL_relationships_form_submit($form, &$form_state) {
  328. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  329. //Update all
  330. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  331. //process stock textfields
  332. tripal_stock_update_relationship(
  333. $form_state['values']["id-$i"],
  334. $form_state['values']["subject_id-$i"],
  335. $form_state['values']["type_id-$i"],
  336. $form_state['values']["object_id-$i"]
  337. );
  338. }
  339. drupal_set_message("Updated all Relationships");
  340. drupal_goto('node/'.$form_state['values']['nid']);
  341. } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  342. $i = $matches[1];
  343. tripal_stock_delete_relationship($form_state['values']["id-$i"]);
  344. drupal_set_message("Deleted Relationship");
  345. } elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
  346. drupal_goto('node/'.$form_state['values']['nid']);
  347. } else {
  348. drupal_set_message("Unrecognized Button Pressed",'error');
  349. }
  350. }
  351. /**
  352. *
  353. *
  354. * @ingroup tripal_stock
  355. */
  356. function tripal_stock_update_relationship ($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
  357. $previous_db = tripal_db_set_active('chado');
  358. db_query(
  359. "UPDATE stock_relationship SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
  360. $subject_id,
  361. $cvterm_id,
  362. $object_id,
  363. $stock_relationship_id
  364. );
  365. tripal_db_set_active($previous_db);
  366. }
  367. /**
  368. *
  369. *
  370. * @ingroup tripal_stock
  371. */
  372. function tripal_stock_delete_relationship ($stock_relationship_id) {
  373. $previous_db = tripal_db_set_active('chado');
  374. db_query(
  375. "DELETE FROM stock_relationship WHERE stock_relationship_id=%d",
  376. $stock_relationship_id
  377. );
  378. tripal_db_set_active($previous_db);
  379. }
  380. /**
  381. *
  382. *
  383. * @ingroup tripal_stock
  384. */
  385. function theme_tripal_stock_edit_ALL_relationships_form ($form) {
  386. $output = '';
  387. $output .= '<br><fieldset>';
  388. $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  389. $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
  390. .'the subject and object of the relationship can contain the uniquename, name, database '
  391. .'reference or synonym of a stock of the same organism.</p>';
  392. $output .= '<table>';
  393. $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
  394. for ($i=1; $i<=$form['num_relationships']['#value']; $i++) {
  395. $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td><td>'
  396. .drupal_render($form["subject_id-$i"]).'</td><td>'
  397. .drupal_render($form["type_id-$i"]).'</td><td>'
  398. .drupal_render($form["object_id-$i"]).'</td><td>'
  399. .drupal_render($form["submit-$i"]).'</td></tr>';
  400. }
  401. $output .= '</table><br>';
  402. $output .= drupal_render($form);
  403. $output .= '</fieldset>';
  404. return $output;
  405. }
  406. /**
  407. *
  408. *
  409. * @ingroup tripal_stock
  410. */
  411. function tripal_stock_list_relationships_for_node($stock_name, $subject_relationships, $object_relationships) {
  412. if (!empty($subject_relationships) OR !empty($object_relationships) ) {
  413. $output = '<table>';
  414. $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
  415. if (!empty($subject_relationships) ) {
  416. foreach ($subject_relationships as $s) {
  417. $output .= '<tr><td>'.$s->subject_name.'</td><td>'.$s->relationship_type.'</td><td>'.$stock_name.'</td></tr>';
  418. }
  419. }
  420. if (!empty($object_relationships) ) {
  421. foreach ($object_relationships as $o) {
  422. $output .= '<tr><td>'.$stock_name.'</td><td>'.$o->relationship_type.'</td><td>'.$o->object_name.'</td></tr>';
  423. } // end of foreach property
  424. }
  425. $output .= '</table>';
  426. } else {
  427. $output = 'No Relationships Involving the Current Stock';
  428. }
  429. return $output;
  430. }