tripal_organism.module 39 KB

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