tripal_organism.module 39 KB

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