tripal.term_lookup.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * Provides the content for the Controlled vocabulary home page.
  4. */
  5. function tripal_vocabulary_lookup_page() {
  6. // set the breadcrumb
  7. $breadcrumb = array();
  8. $breadcrumb[] = l('Home', '<front>');
  9. drupal_set_breadcrumb($breadcrumb);
  10. $vocabs = tripal_get_vocabularies();
  11. $rows = array();
  12. foreach ($vocabs as $vocabulary) {
  13. $rows[] = array(
  14. l($vocabulary['short_name'], 'cv/lookup/' . $vocabulary['short_name']),
  15. $vocabulary['name'],
  16. $vocabulary['description'],
  17. array(
  18. 'data' => number_format($vocabulary['num_terms']),
  19. 'style' => 'text-align: right;'
  20. ),
  21. );
  22. }
  23. $headers = array('Short Name', 'Vocabulary Name(s)', 'Description', 'Loaded Terms');
  24. $table = array(
  25. 'header' => $headers,
  26. 'rows' => $rows,
  27. 'attributes' => array(),
  28. 'sticky' => FALSE,
  29. 'caption' => '',
  30. 'colgroups' => array(),
  31. 'empty' => t('There are no controlled vocabularies'),
  32. );
  33. $content = array(
  34. 'description' => array(
  35. '#type' => 'markup',
  36. '#markup' => '<p>The following controlled vocabularies are used on this site. Click a vocabulary short name for more details.</p>',
  37. ),
  38. 'vocab_table' => array(
  39. '#type' => 'item',
  40. '#markup' => theme_table($table),
  41. ),
  42. );
  43. return $content;
  44. }
  45. /**
  46. * Provides the content for a single controlled vocabulary.
  47. */
  48. function tripal_vocabulary_lookup_vocab_page($vocabulary) {
  49. // set the breadcrumb
  50. $breadcrumb = array();
  51. $breadcrumb[] = l('Home', '<front>');
  52. $breadcrumb[] = l('Controlled Vocabularies', 'cv/lookup');
  53. drupal_set_breadcrumb($breadcrumb);
  54. $vocab = tripal_get_vocabulary_details($vocabulary);
  55. if ($vocab['description']) {
  56. drupal_set_title($vocabulary . ': ' . $vocab['description']);
  57. }
  58. else {
  59. drupal_set_title($vocabulary);
  60. }
  61. // If we can't find the term then just return a message.
  62. if (!$vocab) {
  63. drupal_set_message('The vocabulary cannot be found on this site', 'error');
  64. return '';
  65. }
  66. $headers = array();
  67. $rows = array();
  68. $vocab_name = $vocab['name'];
  69. $short_name = $vocab['short_name'];
  70. if ($vocab['url']) {
  71. $short_name = l($vocab['short_name'], $vocab['url'], array('attributes' => array('target' => '_blank')));
  72. }
  73. $vocab_desc = $vocab['description'];
  74. $rows[] = array(
  75. array(
  76. 'data' => 'Short Name',
  77. 'header' => TRUE,
  78. 'width' => '20%',
  79. ),
  80. $short_name,
  81. );
  82. $rows[] = array(
  83. array(
  84. 'data' => 'Vocabulary Name(s)',
  85. 'header' => TRUE,
  86. 'width' => '20%',
  87. ),
  88. $vocab_name,
  89. );
  90. $rows[] = array(
  91. array(
  92. 'data' => 'Description',
  93. 'header' => TRUE,
  94. 'width' => '20%',
  95. ),
  96. $vocab_desc,
  97. );
  98. $rows[] = array(
  99. array(
  100. 'data' => 'Number of Terms Loaded on This Site',
  101. 'header' => TRUE,
  102. 'width' => '20%',
  103. ),
  104. number_format($vocab['num_terms']),
  105. );
  106. $table = array(
  107. 'header' => $headers,
  108. 'rows' => $rows,
  109. 'attributes' => array(),
  110. 'sticky' => FALSE,
  111. 'caption' => '',
  112. 'colgroups' => array(),
  113. 'empty' => '',
  114. );
  115. $has_root = TRUE;
  116. $root_terms = tripal_get_vocabulary_root_terms($vocabulary);
  117. // If this vocabulary doesn't have root terms then it's either not an
  118. // ontology or not all of the terms are loaded. In this case, let's get
  119. // a paged list of all terms
  120. if (count($root_terms) == 0) {
  121. $root_terms = tripal_get_vocabulary_terms($vocabulary, 25);
  122. $has_root = FALSE;
  123. }
  124. $items = tripal_vocabulary_lookup_term_children_format($root_terms);
  125. if (count($root_terms) == 0) {
  126. $items = '<p>This vocabulary has no terms loaded</p>';
  127. }
  128. else {
  129. $items = '<p>Click the + icon (if present) to expand the tree. If ' .
  130. 'the full ontology or the term heirarchy is not loaded into this site, ' .
  131. 'then the tree will consist of all terms at the same level. ' .
  132. 'For some vocabularies, only a subset of terms are loaded</p>' . $items;
  133. }
  134. drupal_add_js(array(
  135. 'tripal' => array(
  136. 'cv_lookup' => array(
  137. 'vocabulary' => $vocabulary,
  138. ),
  139. ),
  140. ), 'setting');
  141. $content = array(
  142. 'vocab_table' => array(
  143. '#type' => 'item',
  144. '#title' => 'Details',
  145. '#markup' => '<p>A vocabulary is always identified by its short name and sometimes it may offer multiple sub-vocabularies with different names. Both are listed below.</p>' . theme_table($table),
  146. ),
  147. 'vocab_browser' => array(
  148. '#type' => 'item',
  149. '#title' => 'Term Browser',
  150. '#markup' => $items,
  151. ),
  152. );
  153. if (!$has_root) {
  154. $content['pager']= array(
  155. '#type' => 'markup',
  156. '#markup' => theme('pager'),
  157. );
  158. }
  159. // Add support for our custom tree viewer
  160. drupal_add_css(drupal_get_path('module', 'tripal') . '/theme/css/tripal.cv_lookup.css');
  161. drupal_add_js(drupal_get_path('module', 'tripal') . '/theme/js/tripal.cv_lookup.js', 'file');
  162. return $content;
  163. }
  164. /**
  165. * A helper function to format an array of terms into a list for the web page.
  166. *
  167. * @param $children
  168. * A list of children terms.
  169. */
  170. function tripal_vocabulary_lookup_term_children_format($children) {
  171. $items = '<ul id="tripal-cv-lookup-tree">';
  172. foreach ($children as $child) {
  173. $grand = tripal_get_term_children($child['vocabulary']['short_name'], $child['accession']);
  174. $num_grand = count($grand);
  175. $items .= '<li vocabulary = "' . $child['vocabulary']['short_name'] . '" ' .
  176. 'accession = "' . $child['accession'] . '" ' .
  177. 'children = "' . $num_grand . '" ' .
  178. 'state = "closed" '.
  179. 'class = "cv-lookup-tree-node">';
  180. $class = 'tree-node-closed';
  181. if ($num_grand == 0) {
  182. $class = 'tree-node-single';
  183. }
  184. $items .= '<i class = "tree-node-icon ' . $class . '"></i>';
  185. $items .= l($child['name'], 'cv/lookup/' . $child['vocabulary']['short_name'] . '/' . $child['accession'], array('attributes' => array('target' => '_blank')));
  186. if ($child['accession'] != $child['name']) {
  187. $items .= ' [' . $child['vocabulary']['short_name'] . ':' . $child['accession'] . '] ';
  188. }
  189. if ($num_grand > 0) {
  190. $items .= ' (' . $num_grand . ')';
  191. }
  192. $items .= '</li>';
  193. }
  194. $items .= '</ul>';
  195. if (count($children)== 0) {
  196. $items ='';
  197. }
  198. return $items;
  199. }
  200. /**
  201. * An ajax callback to get the children of a term.
  202. *
  203. * @param $vocabulary
  204. * The short name of the vocabulary (e.g. SO, GO, etc.)
  205. * @param $accession
  206. * The term accession.
  207. *
  208. * @return
  209. * A JSON array compatible with the JSTree library.
  210. * https://www.jstree.com/docs/json/
  211. */
  212. function tripal_vocabulary_lookup_term_children_ajax($vocabulary, $accession) {
  213. $term = tripal_get_term_details($vocabulary, $accession);
  214. $children = tripal_get_term_children($vocabulary, $accession);
  215. $response = array(
  216. 'vocabulary' => $vocabulary,
  217. 'accession' => $accession,
  218. 'content' => tripal_vocabulary_lookup_term_children_format($children)
  219. );
  220. drupal_json_output($response);
  221. }
  222. /**
  223. *
  224. * @param $vocabulary
  225. * @param $accession
  226. *
  227. * @return
  228. */
  229. function tripal_vocabulary_lookup_term_page($vocabulary, $accession) {
  230. // set the breadcrumb
  231. $breadcrumb = array();
  232. $breadcrumb[] = l('Home', '<front>');
  233. $breadcrumb[] = l('Controlled Vocabularies', 'cv/lookup');
  234. $breadcrumb[] = l($vocabulary, 'cv/lookup/' . $vocabulary);
  235. drupal_set_breadcrumb($breadcrumb);
  236. $term = tripal_get_term_details($vocabulary, $accession);
  237. drupal_set_title($term['name']);
  238. // If we can't find the term then just return a message.
  239. if (!$term) {
  240. drupal_set_message('The term cannot be found on this site', 'error');
  241. return '';
  242. }
  243. // Build the Term table.
  244. $headers = array();
  245. $rows = array();
  246. $term_name = $term['name'];
  247. $accession = $term['vocabulary']['short_name'] . ':' . $term['accession'];
  248. if ($term['url']) {
  249. $term_name = l($term['name'], $term['url'], array('attributes' => array('target' => '_blank')));
  250. }
  251. $rows[] = array(
  252. array(
  253. 'data' => 'Term',
  254. 'header' => TRUE,
  255. 'width' => '20%',
  256. ),
  257. $accession,
  258. );
  259. $rows[] = array(
  260. array(
  261. 'data' => 'Name',
  262. 'header' => TRUE,
  263. 'width' => '20%',
  264. ),
  265. $term_name,
  266. );
  267. $rows[] = array(
  268. array(
  269. 'data' => 'Definition',
  270. 'header' => TRUE,
  271. 'width' => '20%',
  272. ),
  273. $term['definition'],
  274. );
  275. $table = array(
  276. 'header' => $headers,
  277. 'rows' => $rows,
  278. 'attributes' => array(),
  279. 'sticky' => FALSE,
  280. 'caption' => 'Term Details',
  281. 'colgroups' => array(),
  282. 'empty' => '',
  283. );
  284. $content = theme_table($table);
  285. $rows = array();
  286. $vocab_name = $term['vocabulary']['name'];
  287. if ($term['vocabulary']['url']) {
  288. $vocab_name = l($term['vocabulary']['name'], $term['vocabulary']['url'], array('attributes' => array('target' => '_blank')));
  289. }
  290. $short_name = $term['vocabulary']['short_name'];
  291. $vocab_desc = $term['vocabulary']['description'];
  292. $rows[] = array(
  293. array(
  294. 'data' => 'Name',
  295. 'header' => TRUE,
  296. 'width' => '20%',
  297. ),
  298. $vocab_name,
  299. );
  300. $rows[] = array(
  301. array(
  302. 'data' => 'Short Name',
  303. 'header' => TRUE,
  304. 'width' => '20%',
  305. ),
  306. $short_name,
  307. );
  308. $rows[] = array(
  309. array(
  310. 'data' => 'Description',
  311. 'header' => TRUE,
  312. 'width' => '20%',
  313. ),
  314. $vocab_desc,
  315. );
  316. $table = array(
  317. 'header' => $headers,
  318. 'rows' => $rows,
  319. 'attributes' => array(),
  320. 'sticky' => FALSE,
  321. 'caption' => 'Term Vocabulary details',
  322. 'colgroups' => array(),
  323. 'empty' => '',
  324. );
  325. $content .= theme_table($table);
  326. drupal_add_js(array(
  327. 'tripal' => array(
  328. 'cv_lookup' => array(
  329. 'vocabulary' => $vocabulary,
  330. 'accession' => $accession,
  331. ),
  332. ),
  333. ), 'setting');
  334. return $content;
  335. }