tripal_stock-relationships.inc 18 KB

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