tripal_stock-relationships.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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++) {
  92. $links[] = l($i+1, "node/" . $subject_results[$i]->nid); }
  93. $message = "Too many stocks match '" . $form_state['values']['subject_id'] . "'! "
  94. . " Please refine your input to match ONLY ONE stock. <br>"
  95. . "To aid in this process, here are the stocks that match your initial input: "
  96. . join(', ', $links);
  97. form_set_error('subject_id', $message);
  98. }
  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. }
  102. elseif (sizeof($subject_results) == 1) {
  103. $form_state['values']['subject_id'] = $subject_results[0]->stock->stock_id;
  104. }
  105. // check valid stock selected for object
  106. $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']['object_id'], $_SESSION['organism']);
  107. if (sizeof($object_results) > 1) {
  108. $links= array();
  109. for ($i=0; $i<sizeof($object_results); $i++) {
  110. $links[] = l($i+1, "node/" . $object_results[$i]->nid); }
  111. $message = "Too many stocks match '" . $form_state['values']['object_id'] . "'! "
  112. . "Please refine your input to match ONLY ONE stock. <br>"
  113. . "To aid in this process, here are the stocks that match your initial input: "
  114. . join(', ', $links);
  115. form_set_error('object_id', $message);
  116. }
  117. elseif (sizeof($object_results) < 1) {
  118. 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'));
  119. }
  120. elseif (sizeof($object_results) == 1) {
  121. $form_state['values']['object_id'] = $object_results[0]->stock->stock_id;
  122. }
  123. // check valid type selected
  124. if ($form_state['values']['type_id'] == 0) {
  125. form_set_error('type_id', 'Please select a type of relationship.');
  126. }
  127. else {
  128. $previous_db = tripal_db_set_active('chado');
  129. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['type_id']));
  130. tripal_db_set_active($previous_db);
  131. if ($tmp_obj->count != 1) {
  132. form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
  133. }
  134. }
  135. // check either subject or object is the current stock
  136. if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) {
  137. if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) {
  138. form_set_error('subject_id', 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
  139. }
  140. }
  141. } //end of require validation if adding relationship
  142. }
  143. /**
  144. *
  145. *
  146. * @ingroup tripal_stock
  147. */
  148. function tripal_stock_add_ONE_relationship_form_submit($form, &$form_state) {
  149. if ($form_state['values']['subject_id'] > 0) {
  150. $previous_db = tripal_db_set_active('chado');
  151. db_query(
  152. "INSERT INTO {stock_relationship} (subject_id, type_id, object_id, value) VALUES (%d, %d, %d, '%s')",
  153. $form_state['values']['subject_id'],
  154. $form_state['values']['type_id'],
  155. $form_state['values']['object_id'],
  156. $form_state['values']['r_description']
  157. );
  158. tripal_db_set_active($previous_db);
  159. drupal_set_message('Successfully Added Relationship.');
  160. } //end of insert relationship
  161. }
  162. /**
  163. *
  164. *
  165. * @ingroup tripal_stock
  166. */
  167. function tripal_stock_edit_ALL_relationships_page($node) {
  168. $output = '';
  169. $output .= drupal_get_form('tripal_stock_edit_ALL_relationships_form', $node);
  170. $output .= '<br>';
  171. $output .= drupal_get_form('tripal_stock_add_ONE_relationship_form', $node);
  172. $output .= '<br>';
  173. $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
  174. return $output;
  175. }
  176. /**
  177. * Implements Hook_form()
  178. *
  179. * @ingroup tripal_stock
  180. */
  181. function tripal_stock_edit_ALL_relationships_form($form_state, $node) {
  182. $form = array();
  183. // All Stock Relationships
  184. $node = tripal_core_expand_chado_vars($node, 'table', 'stock_relationship');
  185. // compile all relationships into one variable
  186. $relationships = array();
  187. if (is_array($node->stock->stock_relationship->subject_id)) {
  188. foreach ($node->stock->stock_relationship->subject_id as $r) {
  189. $relationships[$r->stock_relationship_id] = $r;
  190. }
  191. }
  192. elseif (is_object($node->stock->stock_relationship->subject_id)) {
  193. $relationships[$node->stock->stock_relationship->subject_id->stock_relationship_id] = $node->stock->stock_relationship->subject_id;
  194. }
  195. if (is_array($node->stock->stock_relationship->object_id)) {
  196. foreach ($node->stock->stock_relationship->object_id as $r) {
  197. $relationships[$r->stock_relationship_id] = $r;
  198. }
  199. }
  200. elseif (is_object($node->stock->stock_relationship->object_id)) {
  201. $relationships[$node->stock->stock_relationship->object_id->stock_relationship_id] = $node->stock->stock_relationship->object_id;
  202. }
  203. $form['nid'] = array(
  204. '#type' => 'hidden',
  205. '#value' => $node->nid
  206. );
  207. $form['r_stock_uniquename'] = array(
  208. '#type' => 'hidden',
  209. '#value' => $node->stock->uniquename
  210. );
  211. $i=0;
  212. if (sizeof($relationships) != 0) {
  213. foreach ($relationships as $r) {
  214. $i++;
  215. $form["num-$i"] = array(
  216. '#type' => 'item',
  217. '#value' => $i . '.'
  218. );
  219. $form["id-$i"] = array(
  220. '#type' => 'hidden',
  221. '#value' => $r->stock_relationship_id
  222. );
  223. //Enter relationship specific fields
  224. if ( $node->stock->stock_id != $r->subject_id->stock_id ) {
  225. $default = $r->subject_id->uniquename;
  226. $description = l($r->subject_id->name, 'node/' . $r->subject_id->nid);
  227. }
  228. else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
  229. $form["subject_id-$i"] = array(
  230. '#type' => 'textfield',
  231. //'#title' => t('Subject'),
  232. '#required' => TRUE,
  233. '#size' => 30,
  234. '#default_value' => $default,
  235. '#description' => $description,
  236. );
  237. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
  238. ksort($type_options);
  239. $form["type_id-$i"] = array(
  240. '#type' => 'select',
  241. //'#title' => t('Type of Relationship'),
  242. '#options' => $type_options,
  243. '#required' => TRUE,
  244. '#default_value' => $r->type_id->cvterm_id
  245. );
  246. if ( $node->stock->stock_id != $r->object_id->stock_id ) {
  247. $default = $r->object_id->uniquename;
  248. $description = l($r->object_id->name, 'node/' . $r->object_id->nid);
  249. }
  250. else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
  251. $form["object_id-$i"] = array(
  252. '#type' => 'textfield',
  253. //'#title' => t('Object'),
  254. '#required' => TRUE,
  255. '#size' => 30,
  256. '#default_value' => $default,
  257. '#description' => $description
  258. );
  259. $form["submit-$i"] = array(
  260. '#type' => 'submit',
  261. '#value' => t("Delete #$i")
  262. );
  263. }} //end of foreach relationship
  264. $form['num_relationships'] = array(
  265. '#type' => 'hidden',
  266. '#value' => $i
  267. );
  268. $form["submit-edits"] = array(
  269. '#type' => 'submit',
  270. '#value' => t('Update Relationships')
  271. );
  272. return $form;
  273. }
  274. /**
  275. *
  276. *
  277. * @ingroup tripal_stock
  278. */
  279. function tripal_stock_edit_ALL_relationships_form_validate($form, &$form_state) {
  280. // Only Require if Updating Relationships
  281. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  282. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  283. // check valid stock selected for subject
  284. $subject_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["subject_id-$i"], $_SESSION['organism']);
  285. if (sizeof($subject_results) > 1) {
  286. $links= array();
  287. for ($j=0; $j<sizeof($subject_results); $j++) {
  288. $links[] = l($j+1, "node/" . $subject_results[$j]->nid); }
  289. $message = "Too many stocks match '" . $form_state['values']["subject_id-$i"] . "'! "
  290. . "Please refine your input to match ONLY ONE stock. <br>"
  291. . "To aid in this process, here are the stocks that match your initial input: "
  292. . join(', ', $links);
  293. form_set_error("subject_id-$i", $message);
  294. }
  295. elseif (sizeof($subject_results) < 1) {
  296. 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'));
  297. }
  298. elseif (sizeof($subject_results) == 1) {
  299. $form_state['values']["subject_id-$i"] = $subject_results[0]->stock->stock_id;
  300. }
  301. // check valid stock selected for object
  302. $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["object_id-$i"], $_SESSION['organism']);
  303. if (sizeof($object_results) > 1) {
  304. $links= array();
  305. for ($j=0; $j<sizeof($object_results); $j++) {
  306. $links[] = l($j+1, "node/" . $object_results[$j]->nid); }
  307. $message = "Too many stocks match '" . $form_state['values']["object_id-$i"] . "'! "
  308. . "Please refine your input to match ONLY ONE stock. <br>"
  309. . "To aid in this process, here are the stocks that match your initial input: "
  310. . join(', ', $links);
  311. form_set_error("object_id-$i", $message);
  312. }
  313. elseif (sizeof($object_results) < 1) {
  314. 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'));
  315. }
  316. elseif (sizeof($object_results) == 1) {
  317. $form_state['values']["object_id-$i"] = $object_results[0]->stock->stock_id;
  318. }
  319. // check valid type selected
  320. if ($form_state['values']["type_id-$i"] == 0) {
  321. form_set_error('type_id', 'Please select a type of relationship.');
  322. }
  323. else {
  324. $previous_db = tripal_db_set_active('chado');
  325. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']["type_id-$i"]));
  326. tripal_db_set_active($previous_db);
  327. if ($tmp_obj->count != 1) {
  328. form_set_error("type_id-$i", 'The type you selected is not valid. Please choose another one.');
  329. }
  330. }
  331. // check either subject or object is the current stock
  332. if ( $subject_results[0]->nid != $form_state['values']['nid'] ) {
  333. if ( $object_results[0]->nid != $form_state['values']['nid'] ) {
  334. form_set_error("subject_id-$i", 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
  335. }
  336. }
  337. } // end of for each relationship
  338. } //end of if updating relationships
  339. }
  340. /**
  341. *
  342. *
  343. * @ingroup tripal_stock
  344. */
  345. function tripal_stock_edit_ALL_relationships_form_submit($form, &$form_state) {
  346. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  347. //Update all
  348. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  349. //process stock textfields
  350. tripal_stock_update_relationship(
  351. $form_state['values']["id-$i"],
  352. $form_state['values']["subject_id-$i"],
  353. $form_state['values']["type_id-$i"],
  354. $form_state['values']["object_id-$i"]
  355. );
  356. }
  357. drupal_set_message("Updated all Relationships");
  358. drupal_goto('node/' . $form_state['values']['nid']);
  359. }
  360. elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  361. $i = $matches[1];
  362. tripal_stock_delete_relationship($form_state['values']["id-$i"]);
  363. drupal_set_message("Deleted Relationship");
  364. }
  365. elseif ($form_state['clicked_button']['#value'] == t('Back to Stock') ) {
  366. drupal_goto('node/' . $form_state['values']['nid']);
  367. }
  368. else {
  369. drupal_set_message("Unrecognized Button Pressed", 'error');
  370. }
  371. }
  372. /**
  373. *
  374. *
  375. * @ingroup tripal_stock
  376. */
  377. function tripal_stock_update_relationship($stock_relationship_id, $subject_id, $cvterm_id, $object_id) {
  378. $previous_db = tripal_db_set_active('chado');
  379. db_query(
  380. "UPDATE {stock_relationship} SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
  381. $subject_id,
  382. $cvterm_id,
  383. $object_id,
  384. $stock_relationship_id
  385. );
  386. tripal_db_set_active($previous_db);
  387. }
  388. /**
  389. *
  390. *
  391. * @ingroup tripal_stock
  392. */
  393. function tripal_stock_delete_relationship($stock_relationship_id) {
  394. $previous_db = tripal_db_set_active('chado');
  395. db_query(
  396. "DELETE FROM {stock_relationship} WHERE stock_relationship_id=%d",
  397. $stock_relationship_id
  398. );
  399. tripal_db_set_active($previous_db);
  400. }
  401. /**
  402. *
  403. *
  404. * @ingroup tripal_stock
  405. */
  406. function theme_tripal_stock_edit_ALL_relationships_form($form) {
  407. $output = '';
  408. $output .= '<br><fieldset>';
  409. $output .= '<legend>Edit Already Existing Relationships<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  410. $output .= '<p>Each relationship for this stock is listed below, one per line. The textboxes indicating '
  411. . 'the subject and object of the relationship can contain the uniquename, name, database '
  412. . 'reference or synonym of a stock of the same organism.</p>';
  413. $output .= '<table>';
  414. $output .= '<tr><th>#</th><th>Subject</th><th>Type</th><th>Object</th><th></th></tr>';
  415. for ($i=1; $i<=$form['num_relationships']['#value']; $i++) {
  416. $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
  417. . drupal_render($form["subject_id-$i"]) . '</td><td>'
  418. . drupal_render($form["type_id-$i"]) . '</td><td>'
  419. . drupal_render($form["object_id-$i"]) . '</td><td>'
  420. . drupal_render($form["submit-$i"]) . '</td></tr>';
  421. }
  422. $output .= '</table><br>';
  423. $output .= drupal_render($form);
  424. $output .= '</fieldset>';
  425. return $output;
  426. }
  427. /**
  428. *
  429. *
  430. * @ingroup tripal_stock
  431. */
  432. function tripal_stock_list_relationships_for_node($stock_name, $subject_relationships, $object_relationships) {
  433. if (!empty($subject_relationships) OR !empty($object_relationships) ) {
  434. $output = '<table>';
  435. $output .= '<tr><th>Subject</th><th>Relationship Type</th><th>Object</th></tr>';
  436. if (!empty($subject_relationships) ) {
  437. foreach ($subject_relationships as $s) {
  438. $output .= '<tr><td>' . $s->subject_name . '</td><td>' . $s->relationship_type . '</td><td>' . $stock_name . '</td></tr>';
  439. }
  440. }
  441. if (!empty($object_relationships) ) {
  442. foreach ($object_relationships as $o) {
  443. $output .= '<tr><td>' . $stock_name . '</td><td>' . $o->relationship_type . '</td><td>' . $o->object_name . '</td></tr>';
  444. } // end of foreach property
  445. }
  446. $output .= '</table>';
  447. }
  448. else {
  449. $output = 'No Relationships Involving the Current Stock';
  450. }
  451. return $output;
  452. }