tripal_stock-relationships.inc 18 KB

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