tripal_organism.module 40 KB

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