tripal_core.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. require_once "jobs.php";
  3. require_once "mviews.php";
  4. require_once "cvterms.php";
  5. require_once "chado_install.php";
  6. require_once "bulk_loader.php";
  7. require_once "tripal_core.api.inc";
  8. /*************************************************************************
  9. *
  10. */
  11. function tripal_core_init(){
  12. // the two lines below are necessary to ensure that the search_path
  13. // variable is always set. In the case where a view needs to query the
  14. // chado schema when it is local to the Drupal database. Otherwise the
  15. // search_path isn't set. When tripal_db_set_active is called it
  16. // automatically sets the serach path if chado is local to the
  17. // Drupal database
  18. $previous = tripal_db_set_active('chado');
  19. tripal_db_set_active($previous);
  20. // create the 'tripal' controlled volcabulary in chado but only if it doesn't already exist, and
  21. // only if the chado database is present.
  22. if(tripal_core_is_chado_installed()){
  23. $previous_db = tripal_db_set_active('chado'); // use chado database
  24. if(!db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = 'tripal'"))){
  25. $results = db_query("INSERT INTO {cv} (name,definition) ".
  26. "VALUES ('tripal','Terms used by Tripal for modules to manage data such as that stored in property tables like featureprop, analysisprop, etc')");
  27. }
  28. if(!db_fetch_object(db_query("SELECT * FROM {db} WHERE name = 'tripal'"))){
  29. $results = db_query("INSERT INTO {db} (name,description) ".
  30. "VALUES ('tripal','Used as a database placeholder for tripal defined objects such as tripal cvterms')");
  31. }
  32. tripal_db_set_active($previous_db); // now use drupal database
  33. }
  34. }
  35. /*************************************************************************
  36. *
  37. */
  38. function tripal_create_moddir($module_name){
  39. // make the data directory for this module
  40. $data_dir = file_directory_path() . "/tripal/$module_name";
  41. if(!file_check_directory($data_dir,FILE_CREATE_DIRECTORY|FILE_MODIFY_PERMISSIONS)){
  42. $message = "Cannot create directory $data_dir. This module may not ".
  43. "behave correctly without this directory. Please create ".
  44. "the directory manually or fix the problem and reinstall.";
  45. drupal_set_message($message,'error');
  46. watchdog('tripal_core',$message,array(),WATCHDOG_ERROR);
  47. }
  48. }
  49. /*************************************************************************
  50. *
  51. */
  52. function tripal_get_moddir($module_name){
  53. $data_dir = file_directory_path() . "/tripal/$module_name";
  54. return $data_dir;
  55. }
  56. /*************************************************************************
  57. *
  58. */
  59. function tripal_core_menu() {
  60. $items = array();
  61. // Triapl setting groups
  62. $items['admin/tripal'] = array(
  63. 'title' => 'Tripal Management',
  64. 'description' => "Manage the behavior or Tripal and its various modules.",
  65. 'position' => 'right',
  66. 'weight' => -5,
  67. 'page callback' => 'system_admin_menu_block_page',
  68. 'access arguments' => array('administer site configuration'),
  69. 'file' => 'system.admin.inc',
  70. 'file path' => drupal_get_path('module', 'system'),
  71. );
  72. // the administative settings menu
  73. /* $items['admin/tripal/tripal_core'] = array(
  74. 'title' => 'Tripal core settings',
  75. 'description' => 'Tripal Settings',
  76. 'page callback' => 'drupal_get_form',
  77. 'page arguments' => array('tripal_core_admin'),
  78. 'access arguments' => array('access administration pages'),
  79. 'type' => MENU_NORMAL_ITEM,
  80. );
  81. */
  82. $items['admin/tripal/tripal_jobs'] = array(
  83. 'title' => 'Jobs',
  84. 'description' => 'Jobs managed by Tripal',
  85. 'page callback' => 'tripal_jobs_report',
  86. 'access arguments' => array('access administration pages'),
  87. 'type' => MENU_NORMAL_ITEM,
  88. );
  89. $items['admin/tripal/tripal_mview/%'] = array(
  90. 'title' => 'Materialized View',
  91. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  92. 'page callback' => 'tripal_mview_report',
  93. 'page arguments' => array(3),
  94. 'access arguments' => array('access administration pages'),
  95. 'type' => MENU_NORMAL_ITEM,
  96. );
  97. $items['admin/tripal/tripal_mviews'] = array(
  98. 'title' => 'Materialized Views',
  99. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  100. 'page callback' => 'tripal_mviews_report',
  101. 'access arguments' => array('access administration pages'),
  102. 'type' => MENU_NORMAL_ITEM,
  103. );
  104. $items['admin/tripal/tripal_mviews/new'] = array(
  105. 'title' => 'Create View',
  106. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  107. 'page callback' => 'drupal_get_form',
  108. 'page arguments' => array('tripal_mviews_form'),
  109. 'access arguments' => array('access administration pages'),
  110. 'type' => MENU_NORMAL_ITEM,
  111. );
  112. $items['admin/tripal/tripal_mviews/edit/%'] = array(
  113. 'title' => 'Edit View',
  114. 'page callback' => 'drupal_get_form',
  115. 'page arguments' => array('tripal_mviews_form',4),
  116. 'access arguments' => array('access administration pages'),
  117. 'type' => MENU_NORMAL_ITEM,
  118. );
  119. $items['admin/tripal/tripal_mviews/action/%/%'] = array(
  120. 'title' => 'Create View',
  121. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  122. 'page callback' => 'tripal_mviews_action',
  123. 'page arguments' => array(4,5),
  124. 'access arguments' => array('access administration pages'),
  125. 'type' => MENU_CALLBACK,
  126. );
  127. $items['tripal_toggle_box_menu/%/%/%'] = array(
  128. 'title' => t('Libraries'),
  129. 'page callback' => 'tripal_toggle_box_menu',
  130. 'page arguments' => array(1,2,3),
  131. 'access arguments' => array('access administration pages'),
  132. 'type' => MENU_CALLBACK | MENU_LINKS_TO_PARENT
  133. );
  134. $items['admin/tripal/chado_1_11_install'] = array(
  135. 'title' => 'Install Chado v1.11',
  136. 'description' => 'Installs Chado version 1.11 inside the current Drupal database',
  137. 'page callback' => 'drupal_get_form',
  138. 'page arguments' => array('tripal_core_chado_v1_11_load_form'),
  139. 'access arguments' => array('access administration pages'),
  140. 'type' => MENU_NORMAL_ITEM,
  141. );
  142. $items['admin/tripal/bulk_load/create'] = array(
  143. 'title' => 'Create Bulk Loader',
  144. 'description' => 'Create a bulk loader template for loading data into Chado',
  145. 'page callback' => 'tripal_core_bulk_loader_create',
  146. 'access arguments' => array('access administration pages'),
  147. 'type' => MENU_NORMAL_ITEM,
  148. );
  149. $items['admin/tripal/bulk_load/step2_get_type/%'] = array(
  150. 'title' => 'Create Bulk Loader',
  151. 'description' => 'Create a bulk loader template for loading data into Chado',
  152. 'page callback' => 'tripal_core_bulk_loader_ahah_step2_feature_type',
  153. 'page arguments' => array(4),
  154. 'access arguments' => array('access administration pages'),
  155. 'type' => MENU_CALLBACK,
  156. );
  157. $items['test2'] = array(
  158. 'title' => 'test',
  159. 'description' => 'test',
  160. 'page callback' => 'tripal_core_chado_insert_test',
  161. 'access arguments' => array('access administration pages'),
  162. 'type' => MENU_CALLBACK,
  163. );
  164. return $items;
  165. }
  166. /************************************************************************
  167. * The typical display for information on feature, organism, library, etc
  168. * pages is to use the Tripal expandable boxes. However, some sites may
  169. * prefer to use a menu system to keep the pages less cluttered. This
  170. * function provides a common interface for setting a Drupal variable
  171. * that indicates whether or not the content is displayed in a box or as
  172. * a menu item. This function just reverses the setting each time it is
  173. * called
  174. */
  175. function tripal_toggle_box_menu($module,$box_name,$nid){
  176. // if the content is not in a menu then we wnat to turn on the
  177. // menu. If the content is in a menu item then we want to turn
  178. // on the box.
  179. if(strcmp(variable_get("$module-box-$box_name","menu_off"),"menu_off")==0){
  180. variable_set("$module-box-$box_name","menu_on");
  181. } else {
  182. variable_set("$module-box-$box_name","menu_off");
  183. }
  184. drupal_goto("node/$nid");
  185. }
  186. /************************************************************************
  187. *
  188. */
  189. function tripal_core_admin () {
  190. $form['chado_feature_data_url'] = array (
  191. '#title' => t('URL for data files'),
  192. '#type' => t('textfield'),
  193. '#description' => t("This is the base URL location (without a leading forward slash) for where files (e.g. blast .xml files) related to each feature are stored. All files available for download or parsing that a feature needs for display should be located in this base directory."),
  194. '#required' => TRUE,
  195. '#default_value' => variable_get('chado_feature_data_url','sites/default/files/data'),
  196. );
  197. return system_settings_form($form);
  198. }
  199. /************************************************************************
  200. * The tripal_db_set_active function is used to prevent namespace collisions
  201. * when chado and drupal are installed in the same database but in different
  202. * schemas. It is also used for backwards compatibility with older versions
  203. * of tripal or in cases where chado is located outside of the Drupal database.
  204. */
  205. function tripal_db_set_active($dbname){
  206. global $db_url, $db_type;
  207. $chado_exists = 0;
  208. // only postgres can support search paths. So if this is MysQL then
  209. // just run the normal tripal_db_set_active function.
  210. if(strcmp($db_type,'pgsql')==0){
  211. // if the 'chado' database is in the $db_url variable then chado is
  212. // not in the same Drupal database
  213. if(is_array($db_url)){
  214. if(isset($db_url[$dbname])){
  215. return db_set_active($dbname);
  216. }
  217. }
  218. // check to make sure the chado schema exists
  219. $sql = "select nspname from pg_catalog.pg_namespace where nspname = 'chado'";
  220. if(db_fetch_object(db_query($sql))){
  221. $chado_exists = 1;
  222. }
  223. // here we make the assumption that the default database schema is
  224. // 'public'. This will most likely always be the case but if not,
  225. // then this code will break
  226. if($chado_exists && strcmp($dbname,'chado')==0){
  227. db_query("set search_path to %s",'chado,public');
  228. return 'public,chado';
  229. }
  230. elseif($chado_exists) {
  231. db_query("set search_path to %s",'public,chado');
  232. return 'chado,public';
  233. }
  234. else {
  235. return db_set_active($dbname);
  236. }
  237. }
  238. else return db_set_active($dbname);
  239. }
  240. /*************************************************************************
  241. *
  242. */
  243. function tripal_core_is_chado_installed(){
  244. global $db_url, $db_type;
  245. // first check if chado is in the db_url of the
  246. // settings.php file
  247. if(is_array($db_url)){
  248. if(isset($db_url['chado'])){
  249. return true;
  250. }
  251. }
  252. // check to make sure the chado schema exists
  253. $sql = "select nspname from pg_catalog.pg_namespace where nspname = 'chado'";
  254. if(db_fetch_object(db_query($sql))){
  255. return true;
  256. }
  257. return false;
  258. }
  259. /*************************************************************************
  260. * Implements hook_views_api()
  261. * Purpose: Essentially this hook tells drupal that there is views support for
  262. * for this module which then includes tripal_core.views.inc where all the
  263. * views integration code is
  264. */
  265. function tripal_core_views_api() {
  266. return array(
  267. 'api' => 2.0,
  268. );
  269. }
  270. /*************************************************************************
  271. * Purpose: Get max rank for a given set of criteria
  272. * This function was developed with the many property tables in chado in mind
  273. *
  274. * @params tablename: the name of the chado table you want to select the max rank from
  275. * this table must contain a rank column of type integer
  276. * @params where_options: array(
  277. * <column_name> => array(
  278. * 'type' => <type of column: INT/STRING>,
  279. * 'value' => <the value you want to filter on>,
  280. * 'exact' => <if TRUE use =; if FALSE use ~>,
  281. * )
  282. * )
  283. * where options should include the id and type for that table to correctly
  284. * group a set of records together where the only difference are the value and rank
  285. * @return the maximum rank
  286. *
  287. */
  288. function tripal_get_max_chado_rank ($tablename, $where_options) {
  289. $where= array();
  290. //generate the where clause from supplied options
  291. // the key is the column name
  292. foreach ($where_options as $key => $val_array) {
  293. if (preg_match('/INT/', $val_array['type'])) {
  294. $where[] = $key."=".$val_array['value'];
  295. } else {
  296. if ($val_array['exact']) { $operator='='; }
  297. else { $operator='~'; }
  298. $where[] = $key.$operator."'".$val_array['value']."'";
  299. }
  300. }
  301. $previous_db = tripal_db_set_active('chado');
  302. $result = db_fetch_object(db_query(
  303. "SELECT max(rank) as max_rank, count(rank) as count FROM %s WHERE %s",
  304. $tablename,
  305. implode(' AND ',$where)
  306. ));
  307. tripal_db_set_active($previous_db);
  308. //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
  309. if ($result->count > 0) {
  310. return $result->max_rank;
  311. } else {
  312. return -1;
  313. }
  314. }