tripal_organism.module 46 KB

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