tripal_stock-relationships.inc 18 KB

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