tripal_stock-relationships.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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', t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('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 = t("Too many stocks match '%object'! Please refine your input to match
  116. ONLY ONE stock. <br /> To aid in this process, here are the stocks that match your
  117. initial input: %stocks.",
  118. array('%object' => $form_state['values']['object_id'],
  119. '%stocks' => join(', ', $links)
  120. )
  121. );
  122. form_set_error('object_id', $message);
  123. }
  124. elseif (sizeof($object_results) < 1) {
  125. form_set_error('object_id', t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('stocks'))));
  126. }
  127. elseif (sizeof($object_results) == 1) {
  128. $form_state['values']['object_id'] = $object_results[0]->stock->stock_id;
  129. }
  130. // check valid type selected
  131. if ($form_state['values']['type_id'] == 0) {
  132. form_set_error('type_id', 'Please select a type of relationship.');
  133. }
  134. else {
  135. $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']['type_id']));
  136. if ($tmp_obj->count != 1) {
  137. form_set_error('type_id', 'The type you selected is not valid. Please choose another one.');
  138. }
  139. }
  140. // check either subject or object is the current stock
  141. if ( $subject_results[0]->nid != $form_state['values']['rel_nid'] ) {
  142. if ( $object_results[0]->nid != $form_state['values']['rel_nid'] ) {
  143. form_set_error('subject_id', 'Either Subject or Object must be the current stock (' . $form_state['values']['r_stock_uniquename'] . ').');
  144. }
  145. }
  146. } //end of require validation if adding relationship
  147. }
  148. /**
  149. *
  150. *
  151. * @ingroup tripal_stock
  152. */
  153. function tripal_stock_add_ONE_relationship_form_submit($form, &$form_state) {
  154. if ($form_state['values']['subject_id'] > 0) {
  155. chado_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. drupal_set_message(t('Successfully Added Relationship.'));
  163. } //end of insert relationship
  164. }
  165. /**
  166. *
  167. *
  168. * @ingroup tripal_stock
  169. */
  170. function tripal_stock_edit_ALL_relationships_page($node) {
  171. $output = '';
  172. $output .= drupal_get_form('tripal_stock_edit_ALL_relationships_form', $node);
  173. $output .= '<br />';
  174. $output .= drupal_get_form('tripal_stock_add_ONE_relationship_form', $node);
  175. $output .= '<br />';
  176. $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
  177. return $output;
  178. }
  179. /**
  180. * Implements Hook_form()
  181. *
  182. * @ingroup tripal_stock
  183. */
  184. function tripal_stock_edit_ALL_relationships_form($form_state, $node) {
  185. $form = array();
  186. // All Stock Relationships
  187. $node = tripal_core_expand_chado_vars($node, 'table', 'stock_relationship');
  188. // compile all relationships into one variable
  189. $relationships = array();
  190. if (is_array($node->stock->stock_relationship->subject_id)) {
  191. foreach ($node->stock->stock_relationship->subject_id as $r) {
  192. $relationships[$r->stock_relationship_id] = $r;
  193. }
  194. }
  195. elseif (is_object($node->stock->stock_relationship->subject_id)) {
  196. $relationships[$node->stock->stock_relationship->subject_id->stock_relationship_id] = $node->stock->stock_relationship->subject_id;
  197. }
  198. if (is_array($node->stock->stock_relationship->object_id)) {
  199. foreach ($node->stock->stock_relationship->object_id as $r) {
  200. $relationships[$r->stock_relationship_id] = $r;
  201. }
  202. }
  203. elseif (is_object($node->stock->stock_relationship->object_id)) {
  204. $relationships[$node->stock->stock_relationship->object_id->stock_relationship_id] = $node->stock->stock_relationship->object_id;
  205. }
  206. $form['nid'] = array(
  207. '#type' => 'hidden',
  208. '#value' => $node->nid
  209. );
  210. $form['r_stock_uniquename'] = array(
  211. '#type' => 'hidden',
  212. '#value' => $node->stock->uniquename
  213. );
  214. $i=0;
  215. if (sizeof($relationships) != 0) {
  216. foreach ($relationships as $r) {
  217. $i++;
  218. $form["num-$i"] = array(
  219. '#type' => 'item',
  220. '#value' => $i . '.'
  221. );
  222. $form["id-$i"] = array(
  223. '#type' => 'hidden',
  224. '#value' => $r->stock_relationship_id
  225. );
  226. //Enter relationship specific fields
  227. if ( $node->stock->stock_id != $r->subject_id->stock_id ) {
  228. $default = $r->subject_id->uniquename;
  229. $description = l($r->subject_id->name, 'node/' . $r->subject_id->nid);
  230. }
  231. else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
  232. $form["subject_id-$i"] = array(
  233. '#type' => 'textfield',
  234. //'#title' => t('Subject'),
  235. '#required' => TRUE,
  236. '#size' => 30,
  237. '#default_value' => $default,
  238. '#description' => t('%description', array('%description' => $description)),
  239. );
  240. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
  241. ksort($type_options);
  242. $form["type_id-$i"] = array(
  243. '#type' => 'select',
  244. //'#title' => t('Type of Relationship'),
  245. '#options' => $type_options,
  246. '#required' => TRUE,
  247. '#default_value' => $r->type_id->cvterm_id
  248. );
  249. if ( $node->stock->stock_id != $r->object_id->stock_id ) {
  250. $default = $r->object_id->uniquename;
  251. $description = l($r->object_id->name, 'node/' . $r->object_id->nid);
  252. }
  253. else { $default = $node->stock->uniquename; $description = 'Current Stock'; }
  254. $form["object_id-$i"] = array(
  255. '#type' => 'textfield',
  256. //'#title' => t('Object'),
  257. '#required' => TRUE,
  258. '#size' => 30,
  259. '#default_value' => $default,
  260. '#description' => $description
  261. );
  262. $form["submit-$i"] = array(
  263. '#type' => 'submit',
  264. '#value' => t("Delete #$i")
  265. );
  266. }} //end of foreach relationship
  267. $form['num_relationships'] = array(
  268. '#type' => 'hidden',
  269. '#value' => $i
  270. );
  271. $form["submit-edits"] = array(
  272. '#type' => 'submit',
  273. '#value' => t('Update Relationships')
  274. );
  275. return $form;
  276. }
  277. /**
  278. *
  279. *
  280. * @ingroup tripal_stock
  281. */
  282. function tripal_stock_edit_ALL_relationships_form_validate($form, &$form_state) {
  283. // Only Require if Updating Relationships
  284. if ($form_state['clicked_button']['#value'] == t('Update Relationships') ) {
  285. for ($i=1; $i<=$form_state['values']['num_relationships']; $i++) {
  286. // check valid stock selected for subject
  287. $subject_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["subject_id-$i"], $_SESSION['organism']);
  288. if (sizeof($subject_results) > 1) {
  289. $links= array();
  290. for ($j=0; $j<sizeof($subject_results); $j++) {
  291. $links[] = l($j+1, "node/" . $subject_results[$j]->nid); }
  292. $message = t("Too many stocks match '%subject'! Please refine your input to match
  293. ONLY ONE stock. <br /> To aid in this process, here are the stocks that match
  294. your initial input: %stocks",
  295. array('%subject' => $form_state['values']["subject_id-$i"],
  296. '%stocks' => join(', ', $links)
  297. )
  298. );
  299. form_set_error("subject_id-$i", $message);
  300. }
  301. elseif (sizeof($subject_results) < 1) {
  302. form_set_error("subject_id-$i", t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('stocks'))));
  303. }
  304. elseif (sizeof($subject_results) == 1) {
  305. $form_state['values']["subject_id-$i"] = $subject_results[0]->stock->stock_id;
  306. }
  307. // check valid stock selected for object
  308. $object_results = tripal_stock_get_stock_by_name_identifier( $form_state['values']["object_id-$i"], $_SESSION['organism']);
  309. if (sizeof($object_results) > 1) {
  310. $links= array();
  311. for ($j=0; $j<sizeof($object_results); $j++) {
  312. $links[] = l($j+1, "node/" . $object_results[$j]->nid); }
  313. $message = "Too many stocks match '" . $form_state['values']["object_id-$i"] . "'! "
  314. . "Please refine your input to match ONLY ONE stock. <br />"
  315. . "To aid in this process, here are the stocks that match your initial input: "
  316. . join(', ', $links);
  317. form_set_error("object_id-$i", $message);
  318. }
  319. elseif (sizeof($object_results) < 1) {
  320. form_set_error("object_id-$i", t("There are no stocks matching your input. Please check your input for typos and/or lookup the stock <a href='!url'>here</a>", array('!url' => url('stocks'))));
  321. }
  322. elseif (sizeof($object_results) == 1) {
  323. $form_state['values']["object_id-$i"] = $object_results[0]->stock->stock_id;
  324. }
  325. // check valid type selected
  326. if ($form_state['values']["type_id-$i"] == 0) {
  327. form_set_error('type_id', 'Please select a type of relationship.');
  328. }
  329. else {
  330. $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {cvterm} WHERE cvterm_id=%d", $form_state['values']["type_id-$i"]));
  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(t("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(t("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(t("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. chado_query(
  383. "UPDATE {stock_relationship} SET subject_id=%d, type_id=%d, object_id=%d WHERE stock_relationship_id=%d",
  384. $subject_id,
  385. $cvterm_id,
  386. $object_id,
  387. $stock_relationship_id
  388. );
  389. }
  390. /**
  391. *
  392. *
  393. * @ingroup tripal_stock
  394. */
  395. function tripal_stock_delete_relationship($stock_relationship_id) {
  396. chado_query(
  397. "DELETE FROM {stock_relationship} WHERE stock_relationship_id=%d",
  398. $stock_relationship_id
  399. );
  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. }