tripal.term_lookup.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. );
  18. }
  19. $headers = array('Short Name', 'Vocabulary Name(s)', 'Description');
  20. $table = array(
  21. 'header' => $headers,
  22. 'rows' => $rows,
  23. 'attributes' => array(),
  24. 'sticky' => FALSE,
  25. 'caption' => '',
  26. 'colgroups' => array(),
  27. 'empty' => '',
  28. );
  29. $content = array(
  30. 'description' => array(
  31. '#type' => 'markup',
  32. '#markup' => '<p>The following controlled vocabularies are used on this site. Click a vocabulary short name for more details.</p>',
  33. ),
  34. 'vocab_table' => array(
  35. '#type' => 'item',
  36. '#markup' => theme_table($table),
  37. ),
  38. );
  39. return $content;
  40. }
  41. /**
  42. * Provides the content for a single controlled vocabulary.
  43. */
  44. function tripal_vocabulary_lookup_vocab_page($vocabulary) {
  45. // set the breadcrumb
  46. $breadcrumb = array();
  47. $breadcrumb[] = l('Home', '<front>');
  48. $breadcrumb[] = l('Controlled Vocabularies', 'cv/lookup');
  49. drupal_set_breadcrumb($breadcrumb);
  50. $vocab = tripal_get_vocabulary_details($vocabulary);
  51. if ($vocab['description']) {
  52. drupal_set_title($vocab['description']);
  53. }
  54. else {
  55. drupal_set_title($vocabulary);
  56. }
  57. // If we can't find the term then just return a message.
  58. if (!$vocab) {
  59. drupal_set_message('The vocabulary cannot be found on this site', 'error');
  60. return '';
  61. }
  62. $headers = array();
  63. $rows = array();
  64. $vocab_name = $vocab['name'];
  65. if ($vocab['url']) {
  66. $vocab_name = l($vocab['name'], $vocab['url'], array('attributes' => array('target' => '_blank')));
  67. }
  68. $short_name = $vocab['short_name'];
  69. $vocab_desc = $vocab['description'];
  70. $rows[] = array(
  71. array(
  72. 'data' => 'Vocabulary Name(s)',
  73. 'header' => TRUE,
  74. 'width' => '20%',
  75. ),
  76. $vocab_name,
  77. );
  78. $rows[] = array(
  79. array(
  80. 'data' => 'Short Name',
  81. 'header' => TRUE,
  82. 'width' => '20%',
  83. ),
  84. $short_name,
  85. );
  86. $rows[] = array(
  87. array(
  88. 'data' => 'Description',
  89. 'header' => TRUE,
  90. 'width' => '20%',
  91. ),
  92. $vocab_desc,
  93. );
  94. $table = array(
  95. 'header' => $headers,
  96. 'rows' => $rows,
  97. 'attributes' => array(),
  98. 'sticky' => FALSE,
  99. 'caption' => '',
  100. 'colgroups' => array(),
  101. 'empty' => '',
  102. );
  103. $root_terms = tripal_get_vocabulary_root_terms($vocabulary);
  104. $items = tripal_vocabulary_lookup_term_children_format($root_terms);
  105. if (count($root_terms) == 0) {
  106. $items = '<p>Sometimes a vocabulary may not be fully loaded, but individual terms are. In this case the term browser is not available.</p>';
  107. }
  108. drupal_add_js(array(
  109. 'tripal' => array(
  110. 'cv_lookup' => array(
  111. 'vocabulary' => $vocabulary,
  112. ),
  113. ),
  114. ), 'setting');
  115. $content = array(
  116. 'vocab_table' => array(
  117. '#type' => 'item',
  118. '#title' => 'Details',
  119. '#markup' => theme_table($table),
  120. ),
  121. 'vocab_browser' => array(
  122. '#type' => 'item',
  123. '#title' => 'Term Browser',
  124. '#markup' => $items,
  125. ),
  126. );
  127. // Add support for our custom tree viewer
  128. drupal_add_css(drupal_get_path('module', 'tripal') . '/theme/css/tripal.cv_lookup.css');
  129. drupal_add_js(drupal_get_path('module', 'tripal') . '/theme/js/tripal.cv_lookup.js', 'file');
  130. return $content;
  131. }
  132. /**
  133. * A helper function to format an array of terms into a list for the web page.
  134. *
  135. * @param $children
  136. * A list of children terms.
  137. */
  138. function tripal_vocabulary_lookup_term_children_format($children) {
  139. $items = '<ul id="tripal-cv-lookup-tree">';
  140. foreach ($children as $child) {
  141. $grand = tripal_get_term_children($child['vocabulary']['short_name'], $child['accession']);
  142. $num_grand = count($grand);
  143. $items .= '<li vocabulary = "' . $child['vocabulary']['short_name'] . '" ' .
  144. 'accession = "' . $child['accession'] . '" ' .
  145. 'children = "' . $num_grand . '" ' .
  146. 'state = "closed" '.
  147. 'class = "cv-lookup-tree-node">';
  148. $class = 'tree-node-closed';
  149. if ($num_grand == 0) {
  150. $class = 'tree-node-single';
  151. }
  152. $items .= '<i class = "tree-node-icon ' . $class . '"></i>';
  153. $items .= l($child['name'], 'cv/lookup/' . $child['vocabulary']['short_name'] . '/' . $child['accession'], array('attributes' => array('target' => '_blank'))) . ' (' . $num_grand . ')';
  154. $items .= '</li>';
  155. }
  156. $items .= '</ul>';
  157. if (count($children)== 0) {
  158. $items ='';
  159. }
  160. return $items;
  161. }
  162. /**
  163. * An ajax callback to get the children of a term.
  164. *
  165. * @param $vocabulary
  166. * The short name of the vocabulary (e.g. SO, GO, etc.)
  167. * @param $accession
  168. * The term accession.
  169. *
  170. * @return
  171. * A JSON array compatible with the JSTree library.
  172. * https://www.jstree.com/docs/json/
  173. */
  174. function tripal_vocabulary_lookup_term_children_ajax($vocabulary, $accession) {
  175. $term = tripal_get_term_details($vocabulary, $accession);
  176. $children = tripal_get_term_children($vocabulary, $accession);
  177. $response = array(
  178. 'vocabulary' => $vocabulary,
  179. 'accession' => $accession,
  180. 'content' => tripal_vocabulary_lookup_term_children_format($children)
  181. );
  182. drupal_json_output($response);
  183. }
  184. /**
  185. *
  186. * @param $vocabulary
  187. * @param $accession
  188. *
  189. * @return
  190. */
  191. function tripal_vocabulary_lookup_term_page($vocabulary, $accession) {
  192. // set the breadcrumb
  193. $breadcrumb = array();
  194. $breadcrumb[] = l('Home', '<front>');
  195. $breadcrumb[] = l('Controlled Vocabularies', 'cv/lookup');
  196. $breadcrumb[] = l($vocabulary, 'cv/lookup/' . $vocabulary);
  197. drupal_set_breadcrumb($breadcrumb);
  198. $term = tripal_get_term_details($vocabulary, $accession);
  199. drupal_set_title($term['name']);
  200. // If we can't find the term then just return a message.
  201. if (!$term) {
  202. drupal_set_message('The term cannot be found on this site', 'error');
  203. return '';
  204. }
  205. // Build the Term table.
  206. $headers = array();
  207. $rows = array();
  208. $term_name = $term['name'];
  209. $accession = $term['vocabulary']['short_name'] . ':' . $term['accession'];
  210. if ($term['url']) {
  211. $term_name = l($term['name'], $term['url'], array('attributes' => array('target' => '_blank')));
  212. }
  213. $rows[] = array(
  214. array(
  215. 'data' => 'Term',
  216. 'header' => TRUE,
  217. 'width' => '20%',
  218. ),
  219. $accession,
  220. );
  221. $rows[] = array(
  222. array(
  223. 'data' => 'Name',
  224. 'header' => TRUE,
  225. 'width' => '20%',
  226. ),
  227. $term_name,
  228. );
  229. $rows[] = array(
  230. array(
  231. 'data' => 'Definition',
  232. 'header' => TRUE,
  233. 'width' => '20%',
  234. ),
  235. $term['definition'],
  236. );
  237. $table = array(
  238. 'header' => $headers,
  239. 'rows' => $rows,
  240. 'attributes' => array(),
  241. 'sticky' => FALSE,
  242. 'caption' => 'Term Details',
  243. 'colgroups' => array(),
  244. 'empty' => '',
  245. );
  246. $content = theme_table($table);
  247. $rows = array();
  248. $vocab_name = $term['vocabulary']['name'];
  249. if ($term['vocabulary']['url']) {
  250. $vocab_name = l($term['vocabulary']['name'], $term['vocabulary']['url'], array('attributes' => array('target' => '_blank')));
  251. }
  252. $short_name = $term['vocabulary']['short_name'];
  253. $vocab_desc = $term['vocabulary']['description'];
  254. $rows[] = array(
  255. array(
  256. 'data' => 'Name',
  257. 'header' => TRUE,
  258. 'width' => '20%',
  259. ),
  260. $vocab_name,
  261. );
  262. $rows[] = array(
  263. array(
  264. 'data' => 'Short Name',
  265. 'header' => TRUE,
  266. 'width' => '20%',
  267. ),
  268. $short_name,
  269. );
  270. $rows[] = array(
  271. array(
  272. 'data' => 'Description',
  273. 'header' => TRUE,
  274. 'width' => '20%',
  275. ),
  276. $vocab_desc,
  277. );
  278. $table = array(
  279. 'header' => $headers,
  280. 'rows' => $rows,
  281. 'attributes' => array(),
  282. 'sticky' => FALSE,
  283. 'caption' => 'Term Vocabulary details',
  284. 'colgroups' => array(),
  285. 'empty' => '',
  286. );
  287. $content .= theme_table($table);
  288. drupal_add_js(array(
  289. 'tripal' => array(
  290. 'cv_lookup' => array(
  291. 'vocabulary' => $vocabulary,
  292. 'accession' => $accession,
  293. ),
  294. ),
  295. ), 'setting');
  296. return $content;
  297. }