tripal_organism.module 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. <?php
  2. require_once "tripal_organism.api.inc";
  3. /*************************************************************************
  4. *
  5. */
  6. function tripal_organism_init(){
  7. // add the jGCharts JS and CSS
  8. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_organism.js');
  9. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_organism.css');
  10. }
  11. /*******************************************************************************
  12. * Provide information to drupal about the node types that we're creating
  13. * in this module
  14. */
  15. function tripal_organism_node_info() {
  16. $nodes = array();
  17. $nodes['chado_organism'] = array(
  18. 'name' => t('Organism'),
  19. 'module' => 'chado_organism',
  20. 'description' => t('An organism from the chado database'),
  21. 'has_title' => FALSE,
  22. 'title_label' => t('Organism'),
  23. 'has_body' => FALSE,
  24. 'body_label' => t('Organism Description'),
  25. 'locked' => TRUE
  26. );
  27. return $nodes;
  28. }
  29. /*******************************************************************************
  30. * Display block with organisms
  31. * @param op - parameter to define the phase being called for the block
  32. * @param delta - id of the block to return (ignored when op is list)
  33. * @param edit - when op is save, contains the submitted form data
  34. */
  35. function tripal_organism_block($op = 'list', $delta = '0', $edit = array()){
  36. switch($op){
  37. case 'list':
  38. $blocks['base']['info'] = t('Tripal Organism Details');
  39. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  40. $blocks['description']['info'] = t('Tripal Organism Description');
  41. $blocks['description']['cache'] = BLOCK_NO_CACHE;
  42. $blocks['image']['info'] = t('Tripal Organism Image');
  43. $blocks['image']['cache'] = BLOCK_NO_CACHE;
  44. return $blocks;
  45. case 'view':
  46. if(user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  47. $nid = arg(1);
  48. $node = node_load($nid);
  49. $block = array();
  50. switch($delta){
  51. case 'base':
  52. $block['subject'] = t('Organism Details');
  53. $block['content'] = theme('tripal_organism_base',$node);
  54. break;
  55. case 'description':
  56. $block['subject'] = t('Organism Description');
  57. $block['content'] = theme('tripal_organism_description',$node);
  58. break;
  59. case 'image':
  60. $block['subject'] = t('Organism Image');
  61. $block['content'] = theme('tripal_organism_image',$node);
  62. break;
  63. default:
  64. }
  65. return $block;
  66. }
  67. }
  68. }
  69. /*******************************************************************************
  70. * Menu items are automatically added for the new node types created
  71. * by this module to the 'Create Content' Navigation menu item. This function
  72. * adds more menu items needed for this module.
  73. */
  74. function tripal_organism_menu() {
  75. $items = array();
  76. $items['organisms'] = array(
  77. 'menu_name' => ('primary-links'), //Enable the 'Organism' primary link
  78. 'title' => t('Organisms'),
  79. 'page callback' => 'tripal_organism_show_organisms',
  80. 'access arguments' => array('access chado_organism content'),
  81. 'type' => MENU_NORMAL_ITEM
  82. );
  83. // the administative settings menu
  84. $items['admin/tripal/tripal_organism'] = array(
  85. 'title' => 'Organisms',
  86. 'description' => 'Basic Description of Tripal Organism Module Functionality',
  87. 'page callback' => 'tripal_organism_module_description_page',
  88. 'access arguments' => array('administer site configuration'),
  89. 'type' => MENU_NORMAL_ITEM,
  90. );
  91. $items['admin/tripal/tripal_organism/configuration'] = array(
  92. 'title' => 'Configuration',
  93. 'description' => 'Manage integration of Chado organisms including associated features',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('tripal_organism_admin'),
  96. 'access arguments' => array('administer site configuration'),
  97. 'type' => MENU_NORMAL_ITEM,
  98. );
  99. return $items;
  100. }
  101. /*******************************************************************************
  102. * The following function proves access control for users trying to
  103. * perform actions on data managed by this module
  104. */
  105. function chado_organism_access($op, $node, $account){
  106. switch ($op){
  107. case 'create':
  108. return user_access('create chado_organism content', $account);
  109. case 'update':
  110. return user_access('edit chado_organism content', $account);
  111. case 'delete':
  112. return user_access('delete chado_organism content', $account);
  113. case 'view' :
  114. return user_access('access chado_organism content', $account);
  115. }
  116. }
  117. /*******************************************************************************
  118. * Set the permission types that the chado module uses. Essentially we
  119. * want permissionis that protect creation, editing and deleting of chado
  120. # data objects
  121. */
  122. function tripal_organism_perm(){
  123. return array(
  124. 'access chado_organism content',
  125. 'create chado_organism content',
  126. 'delete chado_organism content',
  127. 'edit chado_organism content',
  128. );
  129. }
  130. /*************************************************************************
  131. * Purpose: Provide Guidance to new Tripal Admin
  132. *
  133. * @return HTML Formatted text
  134. */
  135. function tripal_organism_module_description_page() {
  136. $text = '';
  137. $text .= '<h3>Description:</h3>';
  138. //================================================================================
  139. $text .= '<p>The Tripal Organism module allows you to add, edit and/or delete chado organisms. Furthermore, it also provides listing of organisms and details page for each organism. Basically, the chado organism module is designed to hold information about a given species. For more information on the chado organism module see the <a href="http://gmod.org/wiki/Chado_Organism_Module">GMOD wiki page</a></p>';
  140. $text .= '<h3>Post Installation Instructions:</h3>';
  141. //================================================================================
  142. $text .= '<p>This module also provides <b>User Permissions</b> to control which users or groups of users (roles) can view/access organism content (access chado_organism content), create organisms (create chado_organism content), edit or delete organisms (edit chado_organism content or delete chado_organism content). The default is that only the original administration account has these permissions. To allow additional users/roles any combination of the above permissions:</p>';
  143. $text .= '<ol>';
  144. $text .= '<li><a href="../user/roles">Add Roles</a> to provide permissions to. For example, you might want a "View Tripal organism Content" and a "Manage Tripal organism Content" Role. If you only want to provide permissions based on whether the user is logged in (authenticated) or not (anonymous) then you don\'t need to create roles.</li>';
  145. $text .= '<li><a href="../user/permissions">Assign permissions</a> to roles. Specically focus on those mentioned above, then for each permission add a checkmark to the rolw (columns) that you want to have this permission.</li>';
  146. $text .= '<li><a href="../user/user">Assign Users Roles</a>. This is what gives a given user the set of permissions associated with a given role. Notice that you can assign more than one role to a user and that each user is "Authenticated" by default. A user has permission is any of his/her roles have that permission.</li>';
  147. $text .= '</ol>';
  148. $text .= '<p>Another important step, <b>if you chado database already contains organisms, is to sync\' Chado with Drupal</b>. This creates Drupal Content including detail pages for each organism (known as nodes in Drupal). To sync\' Chado with Drupal simply go to the <a href="tripal_organism/configuration">Configuration Page for organisms</a> and in the "Sync Organisms" Fieldset select the Organisms you would like to sync.</p>';
  149. $text .= '<h3>Features of this Module:</h3>';
  150. //================================================================================
  151. $text .= '<b><a href="../../node/add/chado_organism">Create an Organism:</a></b>';
  152. $text .= '<p>This allows you to create content in your drupal and chado for an organism (only the unique organism identifier is duplicated). An organism must have a genus, species, abreviation, common name. In addition, you can optionally supply a short description.</p>';
  153. $text .= '<b>Details Page of a Organism:</b>';
  154. $text .= '<p>Each organism get\'s it\'s own page on this website. This page is meant to give an overall picture of the organism and it\'s associated details. To understand where it is -All page content in Drupal is known as a node and is given a unique identifier or nid. Thus every drupal page has a path of node/<nid>. You can get to the Details page for a given organism from either of the organism listings described below.</p>';
  155. $text .= '<p>If you want to customize the look of the Organism Details page simply copy the PHP/HTML template node-chado_organism.tpl.php from theme_tripal to the base theme you are currently using. Then edit it as desired. There are plans to integrate this details page with Drupal Panels which will provide a much more user-friendly and no-programming-needed method to customize this page.</p>';
  156. $text .= '<b>Updating/Deleting Organisms:</b>';
  157. $text .= '<p>The Organisms Details Page also acts as a landing pad for updating/deleting organisms. To <b>update an organism</b>, go to the organism details page and click on the Edit tab near the top of the page. This tab will only be visable if you have permission to edit chado organism content (See post installation steps above for information on setting user permissions). If you want to <b>delete an organism</b>, click the Edit tab and then near the bottom of the form, click the Delete button. This will delete the entire organism and cannot be undone.</p>';
  158. $text .= '<b><a href="../../organisms">Basic Listing of Organisms:</a></b>';
  159. $text .= '<p>This module also provides a basic listing of all organisms currently sync\'d with Drupal. To access this listing, there should be a Stocks Primary Menu item which links you to <a href="../../organisms">this page</a>. This page lists each organism in it\'s own triapl expandable box and provides a link to each organism by clicking on it\'s common name. Currently there is no way to easily customize this listing.</p>';
  160. $text .= '<b><a href="../build/views/">Flexible Listing of Organisms using Drupal Views:</a></b>';
  161. $text .= '<p>In order to access a more flexible listing of organisms you must first install the <a href="http://drupal.org/project/views">Drupal Views2 module</a>. You should then be able to access the default views <a href="../build/views/">here</a>. Essentially, Views is a module which allows you to create custom SQL queries completely through the web interface without knowing SQL. Furthermore, it also does some formatting of the results allowing you to display them as HTML lists, tables or grids. You can also expose filters to the user to let them customize the results they see and even implement various sorting.</p>';
  162. $text .= '<p>To use one of the Default Views simply click "Enable" and then "Edit" to change it to show exactly what you want. To view the current listing simply clikc "View Page" at the top of the Edit user interface. There are a number of good tutorials out there for Views2, any of which can be used to help you create your own custom listings of biological content. (Note: there aren\'t any tutorials specifically for tripal content but any tutorial for Views2 will show you how to use the views interface.</p>';
  163. return $text;
  164. }
  165. /*******************************************************************************
  166. * Administrative settings for chado_orgnism
  167. */
  168. function tripal_organism_admin () {
  169. $form = array();
  170. // before proceeding check to see if we have any
  171. // currently processing jobs. If so, we don't want
  172. // to give the opportunity to sync libraries
  173. $active_jobs = FALSE;
  174. if(tripal_get_module_active_jobs('tripal_organism')){
  175. $active_jobs = TRUE;
  176. }
  177. // add the field set for syncing libraries
  178. if(!$active_jobs){
  179. get_tripal_organism_admin_form_sync_set($form);
  180. get_tripal_organism_admin_form_reindex_set($form);
  181. get_tripal_organism_admin_form_taxonomy_set($form);
  182. get_tripal_organism_admin_form_cleanup_set($form);
  183. } else {
  184. $form['notice'] = array(
  185. '#type' => 'fieldset',
  186. '#title' => t('Organism Management Temporarily Unavailable')
  187. );
  188. $form['notice']['message'] = array(
  189. '#value' => t('Currently, organism management jobs are waiting or are running. . Managemment features have been hidden until these jobs complete. Please check back later once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.'),
  190. );
  191. }
  192. return system_settings_form($form);
  193. }
  194. /************************************************************************
  195. *
  196. */
  197. function get_tripal_organism_admin_form_cleanup_set(&$form) {
  198. $form['cleanup'] = array(
  199. '#type' => 'fieldset',
  200. '#title' => t('Clean Up')
  201. );
  202. $form['cleanup']['description'] = array(
  203. '#type' => 'item',
  204. '#value' => t("With Drupal and chado residing in different databases ".
  205. "it is possible that nodes in Drupal and organisms in Chado become ".
  206. "\"orphaned\". This can occur if an organism node in Drupal is ".
  207. "deleted but the corresponding chado organism is not and/or vice ".
  208. "versa. Click the button below to resolve these discrepancies."),
  209. '#weight' => 1,
  210. );
  211. $form['cleanup']['button'] = array(
  212. '#type' => 'submit',
  213. '#value' => t('Clean up orphaned organisms'),
  214. '#weight' => 2,
  215. );
  216. }
  217. /************************************************************************
  218. *
  219. */
  220. function get_tripal_organism_admin_form_taxonomy_set(&$form) {
  221. $form['taxonify'] = array(
  222. '#type' => 'fieldset',
  223. '#title' => t('Assign Drupal Taxonomy to Organism Features')
  224. );
  225. // get the list of libraries
  226. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  227. $previous_db = tripal_db_set_active('chado'); // use chado database
  228. $org_rset = db_query($sql);
  229. tripal_db_set_active($previous_db); // now use drupal database
  230. // iterate through all of the libraries
  231. $org_boxes = array();
  232. while($organism = db_fetch_object($org_rset)){
  233. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  234. }
  235. $form['taxonify']['description'] = array(
  236. '#type' => 'item',
  237. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  238. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  239. "for setting taxonomy only for features that belong to the selected organisms below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  240. '#weight' => 1,
  241. );
  242. $form['taxonify']['tx-organisms'] = array (
  243. '#title' => t('Organisms'),
  244. '#type' => t('checkboxes'),
  245. '#description' => t("Check the organisms whose features you want to reset taxonomy. Note: this list contains all organisms, even those that may not be synced."),
  246. '#required' => FALSE,
  247. '#prefix' => '<div id="lib_boxes">',
  248. '#suffix' => '</div>',
  249. '#options' => $org_boxes,
  250. '#weight' => 2
  251. );
  252. $form['taxonify']['tx-button'] = array(
  253. '#type' => 'submit',
  254. '#value' => t('Set Feature Taxonomy'),
  255. '#weight' => 3
  256. );
  257. }
  258. /************************************************************************
  259. *
  260. */
  261. function get_tripal_organism_admin_form_reindex_set(&$form) {
  262. // define the fieldsets
  263. $form['reindex'] = array(
  264. '#type' => 'fieldset',
  265. '#title' => t('Reindex Organism Features')
  266. );
  267. // get the list of libraries
  268. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  269. $previous_db = tripal_db_set_active('chado'); // use chado database
  270. $org_rset = db_query($sql);
  271. tripal_db_set_active($previous_db); // now use drupal database
  272. // iterate through all of the libraries
  273. $org_boxes = array();
  274. while($organism = db_fetch_object($org_rset)){
  275. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species";
  276. }
  277. $form['reindex']['description'] = array(
  278. '#type' => 'item',
  279. '#value' => t("This option allows for reindexing of only those features that belong to the selected organisms below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
  280. '#weight' => 1,
  281. );
  282. $form['reindex']['re-organisms'] = array (
  283. '#title' => t('Organisms'),
  284. '#type' => t('checkboxes'),
  285. '#description' => t("Check the organisms whoee features you want to reindex. Note: this list contains all organisms, even those that may not be synced."),
  286. '#required' => FALSE,
  287. '#prefix' => '<div id="lib_boxes">',
  288. '#suffix' => '</div>',
  289. '#options' => $org_boxes,
  290. '#weight' => 2,
  291. );
  292. $form['reindex']['re-button'] = array(
  293. '#type' => 'submit',
  294. '#value' => t('Reindex Features'),
  295. '#weight' => 3,
  296. );
  297. }
  298. /************************************************************************
  299. *
  300. */
  301. function get_tripal_organism_admin_form_sync_set (&$form) {
  302. // define the fieldsets
  303. $form['sync'] = array(
  304. '#type' => 'fieldset',
  305. '#title' => t('Sync Organisms')
  306. );
  307. // before proceeding check to see if we have any
  308. // currently processing jobs. If so, we don't want
  309. // to give the opportunity to sync libraries
  310. $active_jobs = FALSE;
  311. if(tripal_get_module_active_jobs('tripal_organism')){
  312. $active_jobs = TRUE;
  313. }
  314. if(!$active_jobs){
  315. // get the list of organisms
  316. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  317. $previous_db = tripal_db_set_active('chado'); // use chado database
  318. $org_rset = db_query($sql);
  319. tripal_db_set_active($previous_db); // now use drupal database
  320. // if we've added any organisms to the list that can be synced
  321. // then we want to build the form components to allow the user
  322. // to select one or all of them. Otherwise, just present
  323. // a message stating that all organisms are currently synced.
  324. $org_boxes = array();
  325. $added = 0;
  326. while($organism = db_fetch_object($org_rset)){
  327. // check to see if the organism is already present as a node in drupal.
  328. // if so, then skip it.
  329. $sql = "SELECT * FROM {chado_organism} WHERE organism_id = %d";
  330. if(!db_fetch_object(db_query($sql,$organism->organism_id))){
  331. $org_boxes[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  332. $added++;
  333. }
  334. }
  335. // if we have organisms we need to add to the checkbox then
  336. // build that form element
  337. if($added > 0){
  338. $org_boxes['all'] = "All Organisms";
  339. $form['sync']['organisms'] = array (
  340. '#title' => t('Available Organisms'),
  341. '#type' => t('checkboxes'),
  342. '#description' => t("Check the organisms you want to sync. Drupal content will be created for each of the organisms listed above. Select 'All Organisms' to sync all of them."),
  343. '#required' => FALSE,
  344. '#prefix' => '<div id="org_boxes">',
  345. '#suffix' => '</div>',
  346. '#options' => $org_boxes,
  347. );
  348. $form['sync']['button'] = array(
  349. '#type' => 'submit',
  350. '#value' => t('Submit Sync Job')
  351. );
  352. }
  353. // we don't have any organisms to select from
  354. else {
  355. $form['sync']['value'] = array(
  356. '#value' => t('All organisms in Chado are currently synced with Drupal.')
  357. );
  358. }
  359. }
  360. // we don't want to present a form since we have an active job running
  361. else {
  362. $form['sync']['value'] = array(
  363. '#value' => t('Currently, jobs exist related to chado organisms. Please check back later for organisms that can by synced once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.')
  364. );
  365. }
  366. }
  367. /************************************************************************
  368. *
  369. */
  370. function tripal_organism_admin_validate($form, &$form_state) {
  371. global $user; // we need access to the user info
  372. $job_args = array();
  373. if ($form_state['values']['op'] == t('Submit Sync Job')) {
  374. // check to see if the user wants to sync chado and drupal. If
  375. // so then we need to register a job to do so with tripal
  376. $organisms = $form_state['values']['organisms'];
  377. $do_all = FALSE;
  378. $to_sync = array();
  379. foreach ($organisms as $organism_id){
  380. if(preg_match("/^all$/i",$organism_id)){
  381. $do_all = TRUE;
  382. }
  383. if($organism_id and preg_match("/^\d+$/i",$organism_id)){
  384. // get the list of organisms
  385. $sql = "SELECT * FROM {Organism} WHERE organism_id = %d";
  386. $previous_db = tripal_db_set_active('chado'); // use chado database
  387. $organism = db_fetch_object(db_query($sql,$organism_id));
  388. tripal_db_set_active($previous_db); // now use drupal database
  389. $to_sync[$organism_id] = "$organism->genus $organism->species";
  390. }
  391. }
  392. // submit the job the tripal job manager
  393. if($do_all){
  394. tripal_add_job('Sync all organisms','tripal_organism',
  395. 'tripal_organism_sync_organisms',$job_args,$user->uid);
  396. }
  397. else{
  398. foreach($to_sync as $organism_id => $name){
  399. $job_args[0] = $organism_id;
  400. tripal_add_job("Sync organism: $name",'tripal_organism',
  401. 'tripal_organism_sync_organisms',$job_args,$user->uid);
  402. }
  403. }
  404. }
  405. // -------------------------------------
  406. // Submit the Reindex Job if selected
  407. if ($form_state['values']['op'] == t('Reindex Features')) {
  408. $organisms = $form_state['values']['re-organisms'];
  409. foreach ($organisms as $organism_id){
  410. if($organism_id and preg_match("/^\d+$/i",$organism_id)){
  411. // get the organism info
  412. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  413. $previous_db = tripal_db_set_active('chado'); // use chado database
  414. $organism = db_fetch_object(db_query($sql,$organism_id));
  415. tripal_db_set_active($previous_db); // now use drupal database
  416. $job_args[0] = $organism_id;
  417. tripal_add_job("Reindex features for organism: $organism->genus ".
  418. "$organism->species",'tripal_organism',
  419. 'tripal_organism_reindex_features',$job_args,$user->uid);
  420. }
  421. }
  422. }
  423. // -------------------------------------
  424. // Submit the taxonomy Job if selected
  425. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  426. $organisms = $form_state['values']['tx-organisms'];
  427. foreach ($organisms as $organism_id){
  428. if($organism_id and preg_match("/^\d+$/i",$organism_id)){
  429. // get the organism info
  430. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  431. $previous_db = tripal_db_set_active('chado'); // use chado database
  432. $organism = db_fetch_object(db_query($sql,$organism_id));
  433. tripal_db_set_active($previous_db); // now use drupal database
  434. $job_args[0] = $organism_id;
  435. tripal_add_job("Set taxonomy for features in organism: ".
  436. "$organism->genus $organism->species",'tripal_organism',
  437. 'tripal_organism_taxonify_features',$job_args,$user->uid);
  438. }
  439. }
  440. }
  441. // -------------------------------------
  442. // Submit the Cleanup Job if selected
  443. if ($form_state['values']['op'] == t('Clean up orphaned organisms')) {
  444. tripal_add_job('Cleanup orphaned organisms','tripal_organism',
  445. 'tripal_organisms_cleanup',$job_args,$user->uid);
  446. }
  447. }
  448. /*******************************************************************************
  449. * We need to let drupal know about our theme functions and their arguments.
  450. * We create theme functions to allow users of the module to customize the
  451. * look and feel of the output generated in this module
  452. */
  453. function tripal_organism_theme () {
  454. return array(
  455. 'tripal_organism_organism_page' => array (
  456. 'arguments' => array('organisms'),
  457. ),
  458. 'tripal_organism_base' => array (
  459. 'arguments' => array('node'=> null),
  460. 'template' => 'tripal_organism_base',
  461. ),
  462. 'tripal_organism_description' => array (
  463. 'arguments' => array('node'=> null),
  464. 'template' => 'tripal_organism_description',
  465. ),
  466. 'tripal_organism_image' => array (
  467. 'arguments' => array('node'=> null),
  468. 'template' => 'tripal_organism_image',
  469. ),
  470. );
  471. }
  472. /*******************************************************************************
  473. *
  474. */
  475. function tripal_organism_nodeapi(&$node, $op, $teaser, $page) {
  476. switch ($op) {
  477. case 'view':
  478. switch($node->type){
  479. }
  480. }
  481. }
  482. /*******************************************************************************
  483. *
  484. */
  485. function tripal_organism_cron (){
  486. // we want to make sure that any new organisms or features that were
  487. // added to the database external to drupal automatically get new
  488. // nodes created for themselves in drupal.
  489. // tripal_organism_sync_organisms();
  490. }
  491. /*******************************************************************************
  492. * When a new chado_organism node is created we also need to add information
  493. * to our chado_organism table. This function is called on insert of a new node
  494. * of type 'chado_organism' and inserts the necessary information.
  495. */
  496. function chado_organism_insert($node){
  497. // If this organism already exists then don't recreate it in chado
  498. $o_sql = "SELECT organism_id ".
  499. "FROM {Organism} ".
  500. "WHERE genus = '%s' ".
  501. "AND species = '%s'";
  502. $previous_db = tripal_db_set_active('chado');
  503. $organism = db_fetch_object(db_query($o_sql, $node->genus,$node->species));
  504. tripal_db_set_active($previous_db);
  505. // if the feature doesn't exist then let's create it in chado.
  506. if(!$organism){
  507. // First add the item to the chado oganism table
  508. $sql = "INSERT INTO {organism} ".
  509. " (abbreviation, genus, species,common_name,comment) VALUES ".
  510. " ('%s','%s','%s','%s','%s')";
  511. $previous_db = tripal_db_set_active('chado'); // use chado database
  512. db_query($sql,$node->abbreviation, $node->genus,
  513. $node->species,$node->common_name,$node->description);
  514. // find the newly entered organism_id
  515. $organism = db_fetch_object(db_query($o_sql, $node->genus,$node->species));
  516. tripal_db_set_active($previous_db); // switch back to drupal database
  517. }
  518. // Make sure the entry for this feature doesn't already exist in the
  519. // chado_feature table if it doesn't exist then we want to add it.
  520. $node_check_sql = "SELECT * FROM {chado_organism} ".
  521. "WHERE organism_id = %d";
  522. $node_check = db_fetch_object(db_query($node_check_sql,
  523. $organism->organism_id));
  524. if(!$node_check){
  525. // next add the item to the drupal table
  526. $sql = "INSERT INTO {chado_organism} (nid, vid, organism_id) ".
  527. "VALUES (%d, %d, %d)";
  528. db_query($sql,$node->nid,$node->vid,$organism->organism_id);
  529. }
  530. $record = new stdClass();
  531. $record->title = "$node->genus $node->species";
  532. $record->nid = $node->nid;
  533. drupal_write_record('node',$record,'nid');
  534. drupal_write_record('node_revisions',$record,'nid');
  535. }
  536. /*******************************************************************************
  537. * Delete organism from both drupal and chado databases. Check dependency before
  538. * deleting from chado.
  539. */
  540. function chado_organism_delete($node){
  541. // Before removing, get organism_id so we can remove it from chado database
  542. // later
  543. $sql_drupal = "SELECT organism_id ".
  544. "FROM {chado_organism} ".
  545. "WHERE nid = %d ".
  546. "AND vid = %d";
  547. $organism_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  548. // Remove data from the {chado_organism}, {node}, and {node_revisions} tables
  549. $sql_del = "DELETE FROM {chado_organism} ".
  550. "WHERE nid = %d ".
  551. "AND vid = %d";
  552. db_query($sql_del, $node->nid, $node->vid);
  553. $sql_del = "DELETE FROM {node} ".
  554. "WHERE nid = %d ".
  555. "AND vid = %d";
  556. db_query($sql_del, $node->nid, $node->vid);
  557. $sql_del = "DELETE FROM {node_revisions} ".
  558. "WHERE nid = %d ".
  559. "AND vid = %d";
  560. db_query($sql_del, $node->nid, $node->vid);
  561. // Test dependency before deleting from chado database. If a library or
  562. // feature depends on this organism, don't delete it
  563. $sql = "SELECT feature_id FROM {feature} WHERE organism_id = %d";
  564. $previous_db = tripal_db_set_active('chado');
  565. $check_feature = db_result(db_query($sql, $organism_id));
  566. $sql = "SELECT library_id FROM {library} WHERE organism_id = %d";
  567. $check_lib = db_result(db_query($sql, $organism_id));
  568. if ($check_lib == 0 && $check_feature == 0) {
  569. // Remove from organism/organism_dbxref/organismprop tables of chado
  570. // database as well
  571. db_query("DELETE FROM {organism} WHERE organism_id = %d", $organism_id);
  572. db_query("DELETE FROM {organism_dbxref} WHERE organism_id = %d",
  573. $organism_id);
  574. db_query("DELETE FROM {organismprop} WHERE organism_id = %d",
  575. $organism_id);
  576. } else {
  577. drupal_set_message("Organism deleted from drupal. Warning: at least one ".
  578. "library or feature depends on this organism. It was ".
  579. "not removed from chado.");
  580. }
  581. tripal_db_set_active($previous_db);
  582. }
  583. /*******************************************************************************
  584. *
  585. */
  586. function chado_organism_validate($node){
  587. // check to see if a file was uploaded. If so then copy it to the images
  588. // directory for display with the organism
  589. if (isset($_FILES['files']) && $_FILES['files']['name']['organism-image'] &&
  590. is_uploaded_file($_FILES['files']['tmp_name']['organism-image'])) {
  591. $dest = file_directory_path() . "/tripal/tripal_organism/images";
  592. $validators = array(
  593. 'file_validate_is_image' => array(),
  594. );
  595. file_check_directory($dest,FILE_CREATE_DIRECTORY,'organism-image');
  596. if(!$file = file_save_upload('organism-image',$validators,$dest)){
  597. drupal_set_message("Organism image was not uploaded.");
  598. }
  599. // move this image into the images directory
  600. file_move($file->filepath,$dest . "/".$node->genus."_".$node->species.".jpg",FILE_EXISTS_REPLACE);
  601. }
  602. }
  603. /*******************************************************************************
  604. * Update organisms
  605. */
  606. function chado_organism_update($node){
  607. if($node->revision){
  608. // TODO -- decide what to do about revisions
  609. } else {
  610. // get the organism_id for this node:
  611. $sql = "SELECT organism_id ".
  612. "FROM {chado_organism} ".
  613. "WHERE nid = %d";
  614. $org = db_fetch_object(db_query($sql, $node->nid));
  615. $sql = "UPDATE {organism} ".
  616. " SET comment = '%s', ".
  617. " abbreviation = '%s', ".
  618. " genus = '%s', ".
  619. " species = '%s', ".
  620. " common_name = '%s' ".
  621. "WHERE organism_id = %d ";
  622. $previous_db = tripal_db_set_active('chado'); // use chado database
  623. db_query($sql,$node->description, $node->abbreviation, $node->genus,
  624. $node->species,$node->common_name,$org->organism_id);
  625. tripal_db_set_active($previous_db); // now use drupal database
  626. //$sql = "UPDATE {node} SET title = '$node->genus $node->species' WHERE nid = $node->nid";
  627. //db_query($sql);
  628. $record = new stdClass();
  629. $record->title = "$node->genus $node->species";
  630. $record->nid = $node->nid;
  631. drupal_write_record('node',$record,'nid');
  632. drupal_write_record('node_revisions',$record,'nid');
  633. }
  634. }
  635. /*******************************************************************************
  636. * When editing or creating a new node of type 'chado_organism' we need
  637. * a form. This function creates the form that will be used for this.
  638. */
  639. function chado_organism_form ($node, $param){
  640. $organism = $node->organism;
  641. $type = node_get_types('type',$node);
  642. $form = array();
  643. $form['#attributes']['enctype'] = 'multipart/form-data';
  644. $form['abbreviation']= array(
  645. '#type' => 'textfield',
  646. '#title' => t('Abbreviation'),
  647. '#required' => TRUE,
  648. '#default_value' => $organism->abbreviation,
  649. '#weight' => 3
  650. );
  651. $form['genus']= array(
  652. '#type' => 'textfield',
  653. '#title' => t('Genus'),
  654. '#required' => TRUE,
  655. '#default_value' => $organism->genus,
  656. '#weight' => 1
  657. );
  658. $form['species']= array(
  659. '#type' => 'textfield',
  660. '#title' => t('Species'),
  661. '#required' => TRUE,
  662. '#default_value' => $organism->species,
  663. '#weight' => 2
  664. );
  665. $form['common_name']= array(
  666. '#type' => 'textfield',
  667. '#title' => t('Common Name'),
  668. '#required' => TRUE,
  669. '#default_value' => $organism->common_name,
  670. '#weight' => 4
  671. );
  672. $form['description']= array(
  673. '#type' => 'textarea',
  674. '#rows' => 15,
  675. '#title' => t('Description'),
  676. '#required' => TRUE,
  677. '#default_value' => $organism->description,
  678. '#weight' => 5
  679. );
  680. $form['organism-image']= array(
  681. '#type' => 'file',
  682. '#title' => t('Organism Image'),
  683. '#description' => 'Add an image for this organism',
  684. '#weight' => 6
  685. );
  686. return $form;
  687. }
  688. /*******************************************************************************
  689. * When a node is requested by the user this function is called to allow us
  690. * to add auxiliary data to the node object.
  691. */
  692. function chado_organism_load($node){
  693. // get the organism_id for this node:
  694. $sql = "SELECT organism_id FROM {chado_organism} WHERE vid = %d";
  695. $org_node = db_fetch_object(db_query($sql, $node->vid));
  696. // get information about this organism and add it to the items in this node
  697. $previous_db = tripal_db_set_active('chado'); // use chado database
  698. if ($org_node->organism_id) {
  699. $sql = "SELECT O.organism_id,O.genus,O.species, ".
  700. " O.common_name, O.comment as description, O.abbreviation ".
  701. "FROM {Organism} O ".
  702. "WHERE O.Organism_id = $org_node->organism_id";
  703. $organism = db_fetch_object(db_query($sql));
  704. $additions->organism = $organism;
  705. }
  706. tripal_db_set_active($previous_db); // now use drupal database
  707. return $additions;
  708. }
  709. /*******************************************************************************
  710. * This function customizes the view of the chado_organism node. It allows
  711. * us to generate the markup.
  712. */
  713. function chado_organism_view ($node, $teaser = FALSE, $page = FALSE) {
  714. // use drupal's default node view:
  715. $node = node_prepare($node, $teaser);
  716. return $node;
  717. }
  718. /*******************************************************************************
  719. * Synchronize organisms from chado to drupal
  720. */
  721. function tripal_organism_sync_organisms ($organism_id = NULL, $job_id = NULL){
  722. global $user;
  723. $page_content = '';
  724. if(!$organism_id){
  725. $sql = "SELECT * FROM {Organism} O";
  726. $previous_db = tripal_db_set_active('chado'); // use chado database
  727. $results = db_query($sql);
  728. tripal_db_set_active($previous_db); // now use drupal database
  729. } else {
  730. $sql = "SELECT * FROM {Organism} L WHERE organism_id = %d";
  731. $previous_db = tripal_db_set_active('chado'); // use chado database
  732. $results = db_query($sql,$organism_id);
  733. tripal_db_set_active($previous_db); // now use drupal database
  734. }
  735. // We'll use the following SQL statement for checking if the organism
  736. // already exists as a drupal node.
  737. $sql = "SELECT * FROM {chado_organism} ".
  738. "WHERE organism_id = %d";
  739. while($organism = db_fetch_object($results)){
  740. // check if this organism already exists in the drupal database. if it
  741. // does then skip this organism and go to the next one.
  742. if(!db_fetch_object(db_query($sql,$organism->organism_id))){
  743. $new_node = new stdClass();
  744. $new_node->type = 'chado_organism';
  745. $new_node->uid = $user->uid;
  746. $new_node->title = "$organism->genus $organism->species";
  747. $new_node->organism_id = $organism->organism_id;
  748. $new_node->genus = $organism->genus;
  749. $new_node->species = $organism->species;
  750. $new_node->description = '';
  751. node_validate($new_node);
  752. if(!form_get_errors()){
  753. $node = node_submit($new_node);
  754. node_save($node);
  755. if($node->nid){
  756. $page_content .= "Added $organism->common_name<br>";
  757. }
  758. }
  759. } else {
  760. $page_content .= "Skipped $organism->common_name<br>";
  761. }
  762. }
  763. return $page_content;
  764. }
  765. /*******************************************************************************
  766. * Display help and module information
  767. * @param path which path of the site we're displaying help
  768. * @param arg array that holds the current path as would be returned from arg()
  769. * function
  770. * @return help text for the path
  771. */
  772. function tripal_organism_help($path, $arg) {
  773. $output = '';
  774. switch ($path) {
  775. case "admin/help#tripal_organism":
  776. $output = '<p>'.
  777. t("Displays links to nodes created on this date").
  778. '</p>';
  779. break;
  780. }
  781. return $output;
  782. }
  783. /*******************************************************************************
  784. * Display the summary view of organisms when click on the 'Organisms'
  785. * primary-link
  786. */
  787. function tripal_organism_show_organisms (){
  788. // Show libraries stored in Drupal's {chado_organism} table
  789. $sql = "SELECT COUNT(organism_id) FROM {chado_organism}";
  790. $no_orgs = db_result(db_query ($sql));
  791. if($no_orgs != 0) {
  792. $organisms = get_chado_organisms ();
  793. return theme('tripal_organism_organism_page', $organisms);
  794. } else {
  795. return t("No organism exists. Please contact administrators to ".
  796. "synchronize organisms.");
  797. }
  798. }
  799. /*******************************************************************************
  800. *
  801. */
  802. function theme_tripal_organism_organism_page($organisms) {
  803. $output = "<br><a id=\"tripal_expandableBox_toggle_button\" onClick=\"toggleExpandableBoxes()\">[-] Collapse All</a>";
  804. foreach($organisms as $organism){
  805. // Prepare information for html output
  806. $org_url = url("node/$organism->node_id");
  807. // Generate html output
  808. $output .= "<div class=\"tripal_chado_organism-info-box\" style=\"padding:5px\">
  809. <div class=\"tripal_expandableBox\">
  810. <h3>$organism->common_name</h3>
  811. </div>
  812. <div class=\"tripal_expandableBoxContent\">
  813. <span>
  814. <table class=\"tripal_chado_analysis_content\">
  815. <tr><td>
  816. Name: <a href=\"$org_url\">$organism->common_name</a>
  817. </td></tr>
  818. <tr><td>
  819. Genus: $organism->genus
  820. </td></tr>
  821. <tr><td>
  822. Species: $organism->species
  823. </td></tr>
  824. <tr><td>
  825. Description: $organism->comment
  826. </td></tr>
  827. </table>
  828. </span>
  829. </div>
  830. </div>";
  831. }
  832. return $output;
  833. }
  834. /*******************************************************************************
  835. * This function uses organism_id's of all drupal organism nodes as input and
  836. * pull the organism information (genus, species, common_name, comment) from
  837. * chado database. The return type is an object array that stores $organism
  838. * objects sorted by common_name
  839. */
  840. function get_chado_organisms() {
  841. $sql_drupal = "SELECT COUNT (organism_id) FROM {chado_organism}";
  842. $no_orgs = db_result(db_query($sql_drupal));
  843. if ($no_orgs != 0) {
  844. $sql = "SELECT organism_id, nid FROM {chado_organism}";
  845. $result = db_query($sql);
  846. $previous_db = tripal_db_set_active('chado');
  847. $sql = "SELECT genus, species, common_name, comment ".
  848. "FROM {Organism} ".
  849. "WHERE organism_id=%d";
  850. $organisms = array();
  851. $count = 0;
  852. while ($data = db_fetch_object($result)) {
  853. $organism = db_fetch_object(db_query($sql, $data->organism_id));
  854. $organism->node_id = $data->nid;
  855. // Use common_name plus $count as the key so we can sort by common
  856. // name later. Since common_name is not unique, we need to add $count
  857. // to the key
  858. $key = strtolower($organism->common_name).$count;
  859. $organisms [$key] = $organism;
  860. $count ++;
  861. }
  862. tripal_db_set_active($previous_db);
  863. //Sort organisms by common name
  864. ksort($organisms, SORT_STRING);
  865. return $organisms;
  866. }
  867. }
  868. /*******************************************************************************
  869. *
  870. */
  871. function tripal_organism_reindex_features ($organism_id = NULL, $job_id = NULL){
  872. $i = 0;
  873. if(!$organism_id){
  874. return;
  875. }
  876. $sql = "SELECT * ".
  877. "FROM {feature} ".
  878. "WHERE organism_id = $organism_id ".
  879. "ORDER BY feature_id";
  880. $previous_db = tripal_db_set_active('chado'); // use chado database
  881. $results = db_query($sql);
  882. tripal_db_set_active($previous_db); // now use drupal databa tripal_db_set_active($previous_db); // now use drupal database
  883. // load into ids array
  884. $count = 0;
  885. $ids = array();
  886. while($id = db_fetch_object($results)){
  887. $ids[$count] = $id->feature_id;
  888. $count++;
  889. }
  890. $interval = intval($count * 0.01);
  891. foreach($ids as $feature_id){
  892. // update the job status every 1% features
  893. if($job_id and $i % $interval == 0){
  894. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  895. }
  896. tripal_feature_sync_feature ($feature_id);
  897. $i++;
  898. }
  899. }
  900. /*******************************************************************************
  901. *
  902. */
  903. function tripal_organism_taxonify_features ($organism_id = NULL, $job_id = NULL){
  904. $i = 0;
  905. if(!$organism_id){
  906. return;
  907. }
  908. $sql = "SELECT * ".
  909. "FROM {feature} ".
  910. "WHERE organism_id = $organism_id ".
  911. "ORDER BY feature_id";
  912. $previous_db = tripal_db_set_active('chado'); // use chado database
  913. $results = db_query($sql);
  914. tripal_db_set_active($previous_db); // now use drupal database
  915. // load into ids array
  916. $count = 0;
  917. $ids = array();
  918. while($id = db_fetch_object($results)){
  919. $ids[$count] = $id->feature_id;
  920. $count++;
  921. }
  922. // make sure our vocabularies are set before proceeding
  923. tripal_feature_set_vocabulary();
  924. // use this SQL for getting the nodes
  925. $nsql = "SELECT * FROM {chado_feature} CF ".
  926. " INNER JOIN {node} N ON N.nid = CF.nid ".
  927. "WHERE feature_id = %d";
  928. // iterate through the features and set the taxonomy
  929. $interval = intval($count * 0.01);
  930. foreach($ids as $feature_id){
  931. // update the job status every 1% features
  932. if($job_id and $i % $interval == 0){
  933. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  934. }
  935. $node = db_fetch_object(db_query($nsql,$feature_id));
  936. tripal_feature_set_taxonomy($node,$feature_id);
  937. $i++;
  938. }
  939. }
  940. /*******************************************************************************
  941. * Returns a list of organisms that are currently synced with Drupal
  942. */
  943. function tripal_organism_get_synced() {
  944. // use this SQL for getting synced organisms
  945. $dsql = "SELECT * FROM {chado_organism}";
  946. $orgs = db_query($dsql);
  947. // use this SQL statement for getting the organisms
  948. $csql = "SELECT * FROM {Organism} ".
  949. "WHERE organism_id = %d";
  950. $org_list = array();
  951. // iterate through the organisms and build an array of those that are synced
  952. while($org = db_fetch_object($orgs)){
  953. $previous_db = tripal_db_set_active('chado'); // use chado database
  954. $info = db_fetch_object(db_query($csql,$org->organism_id));
  955. tripal_db_set_active($previous_db); // now use drupal database
  956. $org_list[] = $info;
  957. }
  958. return $org_list;
  959. }
  960. /************************************************************************
  961. *
  962. */
  963. function tripal_organisms_cleanup($dummy = NULL, $job_id = NULL) {
  964. // build the SQL statments needed to check if nodes point to valid organisms
  965. $dsql = "SELECT * FROM {node} WHERE type = 'chado_organism' order by nid";
  966. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  967. $csql = "SELECT * FROM {chado_organism} where nid = %d ";
  968. $cosql= "SELECT * FROM {chado_organism}";
  969. $tsql = "SELECT * FROM {Organism} O ".
  970. "WHERE organism_id = %d";
  971. // load into nodes array
  972. $results = db_query($dsql);
  973. $count = 0;
  974. $nodes = array();
  975. while($node = db_fetch_object($results)){
  976. $nodes[$count] = $node;
  977. $count++;
  978. }
  979. // load the chado_organisms into an array
  980. $results = db_query($cosql);
  981. $cnodes = array();
  982. while($node = db_fetch_object($results)){
  983. $cnodes[$count] = $node;
  984. $count++;
  985. }
  986. $interval = intval($count * 0.01);
  987. // iterate through all of the chado_organism nodes and delete those that aren't valid
  988. foreach($nodes as $nid){
  989. // update the job status every 1% organisms
  990. if($job_id and $i % $interval == 0){
  991. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  992. }
  993. // first check to see if the node has a corresponding entry
  994. // in the chado_organism table. If not then delete the node.
  995. $organism = db_fetch_object(db_query($csql,$nid->nid));
  996. if(!$organism){
  997. node_delete($nid->nid);
  998. $message = "Missing in chado_organism table.... DELETING: $nid->nid\n";
  999. watchdog('tripal_organism',$message,array(),WATCHDOG_WARNING);
  1000. continue;
  1001. }
  1002. $i++;
  1003. }
  1004. // iterate through all of the chado_organism nodes and delete those that aren't valid
  1005. foreach($cnodes as $nid){
  1006. // update the job status every 1% organisms
  1007. if($job_id and $i % $interval == 0){
  1008. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1009. }
  1010. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1011. if(!$node){
  1012. db_query("DELETE FROM {chado_organism} WHERE nid = $nid->nid");
  1013. $message = "chado_organism missing node.... DELETING: $nid->nid\n";
  1014. watchdog('tripal_organism',$message,array(),WATCHDOG_WARNING);
  1015. }
  1016. $i++;
  1017. }
  1018. return '';
  1019. }
  1020. /*************************************************************************
  1021. * Implements hook_views_api()
  1022. * Purpose: Essentially this hook tells drupal that there is views support for
  1023. * for this module which then includes tripal_db.views.inc where all the
  1024. * views integration code is
  1025. */
  1026. function tripal_organism_views_api() {
  1027. return array(
  1028. 'api' => 2.0,
  1029. );
  1030. }