tripal_organism.module 47 KB

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