tripal_organism.module 39 KB

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