tripal_organism.module 50 KB

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