tripal_library.module 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. <?php
  2. require('tripal_library.api.inc');
  3. /*******************************************************************************
  4. * Display help and module information
  5. * @param path which path of the site we're displaying help
  6. * @param arg array that holds the current path as would be returned from arg()
  7. * function
  8. * @return help text for the path
  9. */
  10. function tripal_library_help($path, $arg) {
  11. $output = '';
  12. switch ($path) {
  13. case "admin/help#tripal_library":
  14. $output = '<p>'.
  15. t("Displays links to nodes created on this date").
  16. '</p>';
  17. break;
  18. }
  19. return $output;
  20. }
  21. /*******************************************************************************
  22. * Provide information to drupal about the node types that we're creating
  23. * in this module
  24. */
  25. function tripal_library_node_info() {
  26. $nodes = array();
  27. $nodes['chado_library'] = array(
  28. 'name' => t('Library'),
  29. 'module' => 'chado_library',
  30. 'description' => t('A library from the chado database'),
  31. 'has_title' => FALSE,
  32. 'title_label' => t('Library'),
  33. 'has_body' => FALSE,
  34. 'body_label' => t('Library Description'),
  35. 'locked' => TRUE
  36. );
  37. return $nodes;
  38. }
  39. /*******************************************************************************
  40. * Set the permission types that the chado module uses. Essentially we
  41. * want permissionis that protect creation, editing and deleting of chado
  42. * data objects
  43. */
  44. function tripal_library_perm(){
  45. return array(
  46. 'access chado_library content',
  47. 'create chado_library content',
  48. 'delete chado_library content',
  49. 'edit chado_library content',
  50. );
  51. }
  52. /************************************************************************
  53. * Set the permission types that the module uses.
  54. */
  55. function chado_library_access($op, $node, $account) {
  56. if ($op == 'create') {
  57. return user_access('create chado_library content', $account);
  58. }
  59. if ($op == 'update') {
  60. if (user_access('edit chado_library content', $account)) {
  61. return TRUE;
  62. }
  63. }
  64. if ($op == 'delete') {
  65. if (user_access('delete chado_library content', $account)) {
  66. return TRUE;
  67. }
  68. }
  69. if ($op == 'view') {
  70. if (user_access('access chado_library content', $account)) {
  71. return TRUE;
  72. }
  73. }
  74. return FALSE;
  75. }
  76. /*******************************************************************************
  77. * Menu items are automatically added for the new node types created
  78. * by this module to the 'Create Content' Navigation menu item. This function
  79. * adds more menu items needed for this module.
  80. */
  81. function tripal_library_menu() {
  82. $items = array();
  83. // The administative settings menu
  84. $items['admin/tripal/tripal_library'] = array(
  85. 'title' => 'Libraries',
  86. 'description' => 'Basic Description of Tripal Library Module Functionality',
  87. 'page callback' => 'tripal_library_module_description_page',
  88. 'access arguments' => array('access administration pages'),
  89. 'type' => MENU_NORMAL_ITEM,
  90. );
  91. $items['admin/tripal/tripal_library/configuration'] = array(
  92. 'title' => 'Configuration',
  93. 'description' => 'Manage integration of Chado libraries including associated features.',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('tripal_library_admin'),
  96. 'access arguments' => array('access administration pages'),
  97. 'type' => MENU_NORMAL_ITEM,
  98. );
  99. // Synchronizing libraries from Chado to Drupal
  100. $items['chado_sync_libraries'] = array(
  101. 'title' => t('Sync Data'),
  102. 'page callback' => 'tripal_library_sync_libraries',
  103. 'access arguments' => array('access administration pages'),
  104. 'type' => MENU_CALLBACK
  105. );
  106. // Displaying libraries
  107. $items['libraries'] = array(
  108. 'menu_name' => ('primary-links'), //Enable the 'Library' primary link
  109. 'title' => t('DNA Libraries'),
  110. 'page callback' => 'tripal_library_show_libraries',
  111. 'access arguments' => array('access chado_library content'),
  112. 'type' => MENU_NORMAL_ITEM
  113. );
  114. return $items;
  115. }
  116. /*************************************************************************
  117. * Implements hook_views_api()
  118. * Purpose: Essentially this hook tells drupal that there is views support for
  119. * for this module which then includes tripal_db.views.inc where all the
  120. * views integration code is
  121. */
  122. function tripal_library_views_api() {
  123. return array(
  124. 'api' => 2.0,
  125. );
  126. }
  127. /*************************************************************************
  128. * Purpose: Provide Guidance to new Tripal Admin
  129. *
  130. * @return HTML Formatted text
  131. */
  132. function tripal_library_module_description_page() {
  133. $text = '';
  134. $text .= '<h3>Description:</h3>';
  135. $text .= '<p>TODO: Basic Description of this module including mention/link to the chado module</p>';
  136. $text .= '<h3>Post Installation Instructions:</h3>';
  137. $text .= '<p>TODO: Describe any post installation intructions here. You shouldalways include setting user permissions.</p>';
  138. $text .= '<h3>Features of this Module:</h3>';
  139. $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>';
  140. return $text;
  141. }
  142. /*******************************************************************************
  143. * Administrative settings form
  144. */
  145. function tripal_library_admin () {
  146. $form = array();
  147. // before proceeding check to see if we have any
  148. // currently processing jobs. If so, we don't want
  149. // to give the opportunity to sync libraries
  150. $active_jobs = FALSE;
  151. if(tripal_get_module_active_jobs('tripal_library')){
  152. $active_jobs = TRUE;
  153. }
  154. // add the field set for syncing libraries
  155. if(!$active_jobs){
  156. get_tripal_library_admin_form_sync_set ($form);
  157. get_tripal_library_admin_form_reindex_set($form);
  158. get_tripal_library_admin_form_taxonomy_set($form);
  159. get_tripal_library_admin_form_cleanup_set($form);
  160. get_tripal_library_admin_form_menu_set($form);
  161. } else {
  162. $form['notice'] = array(
  163. '#type' => 'fieldset',
  164. '#title' => t('Library Management Temporarily Unavailable')
  165. );
  166. $form['notice']['message'] = array(
  167. '#value' => t('Currently, library 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.'),
  168. );
  169. }
  170. return system_settings_form($form);
  171. }
  172. /*******************************************************************************
  173. * HOOK: Implementation of hook_nodeapi()
  174. * Display library information for associated features or organisms
  175. * This function also provides contents for indexing
  176. */
  177. function tripal_library_nodeapi(&$node, $op, $teaser, $page) {
  178. switch ($op) {
  179. // Note that this function only adds library view to an organism/feature
  180. // node.
  181. case 'view':
  182. // add the library to the organism/feature search indexing
  183. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  184. $node->content['tripal_library_index_version'] = array(
  185. '#value' => theme('tripal_library_search_index',$node),
  186. );
  187. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  188. $node->content['tripal_library_index_version'] = array(
  189. '#value' => theme('tripal_library_search_result',$node),
  190. );
  191. } else {
  192. switch($node->type){
  193. case 'chado_organism':
  194. // Show library if the organism/feature is not at teaser view
  195. $node->content['tripal_organism_libraries'] = array(
  196. '#value' => theme('tripal_organism_libraries', $node),
  197. );
  198. break;
  199. case 'chado_feature':
  200. // Show library if the organism/feature is not at teaser view
  201. $node->content['tripal_feature_libraries'] = array(
  202. '#value' => theme('tripal_feature_libraries', $node),
  203. );
  204. break;
  205. }
  206. }
  207. break;
  208. }
  209. }
  210. /************************************************************************
  211. * We need to let drupal know about our theme functions and their arguments.
  212. * We create theme functions to allow users of the module to customize the
  213. * look and feel of the output generated in this module
  214. */
  215. function tripal_library_theme () {
  216. return array(
  217. 'tripal_library_library_table' => array (
  218. 'arguments' => array('libraries'),
  219. ),
  220. 'tripal_library_search_index' => array (
  221. 'arguments' => array('node'),
  222. ),
  223. 'tripal_library_search_result' => array (
  224. 'arguments' => array('node'),
  225. ),
  226. 'tripal_organism_libraries' => array (
  227. 'arguments' => array('node'=> null),
  228. 'template' => 'tripal_organism_libraries',
  229. ),
  230. 'tripal_feature_libraries' => array (
  231. 'arguments' => array('node'=> null),
  232. 'template' => 'tripal_feature_libraries',
  233. )
  234. );
  235. }
  236. /************************************************************************
  237. * This function is an extension of the chado_feature_view and
  238. * chado_organism_view by providing the markup for the library object
  239. * THAT WILL BE INDEXED.
  240. */
  241. function theme_tripal_library_search_index ($node) {
  242. if ($node->type == 'chado_organism') {
  243. $content = "";
  244. // get the libraries for the organism
  245. $previous_db = tripal_db_set_active('chado');
  246. $sql = "SELECT * FROM {library} L ".
  247. "WHERE L.Organism_id = $node->organism_id";
  248. $libraries = array();
  249. $results = db_query($sql);
  250. while($library = db_fetch_object($results)){
  251. // get the description
  252. $sql = "SELECT * FROM {libraryprop} LP ".
  253. " INNER JOIN CVTerm CVT ON CVT.cvterm_id = LP.type_id ".
  254. "WHERE LP.library_id = $library->library_id ".
  255. " AND CVT.name = 'library_description'";
  256. $desc = db_fetch_object(db_query($sql));
  257. $library->description = $desc->value;
  258. $libraries[] = $library;
  259. }
  260. tripal_db_set_active($previous_db);
  261. if(count($libraries) > 0){
  262. foreach ($libraries as $library){
  263. $content .= "$library->name ";
  264. $content .= "$library->description";
  265. };
  266. }
  267. // Provide library names to show in a feature page
  268. } else if ($node->type == 'chado_feature') {
  269. $content = "";
  270. $organism_id = $node->feature->organism_id;
  271. $previous_db = tripal_db_set_active('chado');
  272. $sql = "SELECT * FROM {library} L ".
  273. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  274. "WHERE LF.feature_id = " . $node->feature->feature_id;
  275. $libraries = array();
  276. $results = db_query($sql);
  277. while($library = db_fetch_object($results)){
  278. $libraries[] = $library;
  279. }
  280. tripal_db_set_active($previous_db);
  281. if(count($libraries) > 0){
  282. $lib_additions = array();
  283. foreach ($libraries as $library){
  284. $content .= $library->name;
  285. };
  286. }
  287. }
  288. return $content;
  289. }
  290. /*******************************************************************************
  291. * This function shows library information on an organism/feature node
  292. */
  293. function theme_tripal_library_node_libraries($node) {
  294. $content = "";
  295. // Show library information in a expandable box for a organism page.
  296. // Make sure we have $node->organism_id. In the case of creating a new
  297. // organism, the organism_id is not created until we save. This will cause
  298. // an error when users preview the creation without a $node->organism_id
  299. if ($node->type == 'chado_organism' && $node->organism_id) {
  300. $box_status = variable_get("tripal_library-box-libraries","menu_off");
  301. if(strcmp($box_status,"menu_off")==0){
  302. return get_tripal_library_organism_libraries($node->nid);
  303. }
  304. }
  305. // Provide library names to show in a feature page.
  306. // Make sure we have $node->feature->feature_id or there will be an error
  307. // when a feature is previewed at its creation
  308. else if ($node->type == 'chado_feature' && $node->feature->feature_id) {
  309. $organism_id = $node->feature->organism_id;
  310. $previous_db = tripal_db_set_active('chado');
  311. $sql = "SELECT * FROM {library} L ".
  312. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  313. "WHERE LF.feature_id = " . $node->feature->feature_id;
  314. $libraries = array();
  315. $results = db_query($sql);
  316. while($library = db_fetch_object($results)){
  317. $libraries[] = $library;
  318. }
  319. tripal_db_set_active($previous_db);
  320. if(count($libraries) > 0){
  321. $lib_additions = array();
  322. foreach ($libraries as $library){
  323. $sql = "SELECT nid FROM {chado_library} WHERE library_id = %d";
  324. $lib_nid = db_result(db_query($sql, $library->library_id));
  325. if ($lib_nid) {
  326. $lib_url = url("node/$lib_nid");
  327. }
  328. $lib_additions[$lib_url] = $library->name;
  329. };
  330. $node->lib_additions = $lib_additions;
  331. }
  332. }
  333. return $content;
  334. }
  335. /************************************************************************
  336. *
  337. */
  338. function get_tripal_library_admin_form_menu_set(&$form){
  339. $form['menu'] = array(
  340. '#type' => 'fieldset',
  341. '#title' => t('Set Menu')
  342. );
  343. $types = array(
  344. 'tripal_organism' => t('Organism Page'),
  345. 'tripal_feature' => t('Feature Page'),
  346. );
  347. $defaults = variable_get('tripal_lib_menu_node_types','');
  348. $form['menu']['tripal_lib_menu_node_types'] = array(
  349. '#title' => 'Page Types',
  350. '#type' => 'checkboxes',
  351. '#description' => t("Libraries can be associated with other data types ".
  352. "such as organisms, features, analyses, etc. The library module ".
  353. "will add to the respective pages a box or menu item with a ".
  354. "list of libraries associated with the data type. This list ".
  355. "will appear in a box on the page by default. To place this ".
  356. "list as a menu item rather than a box, check the appropriate boxes ".
  357. "above"),
  358. '#options' => $types,
  359. '#default_value' => $defaults,
  360. '#weight' => 1,
  361. );
  362. }
  363. /************************************************************************
  364. *
  365. */
  366. function get_tripal_library_admin_form_cleanup_set(&$form) {
  367. $form['cleanup'] = array(
  368. '#type' => 'fieldset',
  369. '#title' => t('Clean Up')
  370. );
  371. $form['cleanup']['description'] = array(
  372. '#type' => 'item',
  373. '#value' => t("With Drupal and chado residing in different databases ".
  374. "it is possible that nodes in Drupal and libraries in Chado become ".
  375. "\"orphaned\". This can occur if an library node in Drupal is ".
  376. "deleted but the corresponding chado library is not and/or vice ".
  377. "versa. Click the button below to resolve these discrepancies."),
  378. '#weight' => 1,
  379. );
  380. $form['cleanup']['button'] = array(
  381. '#type' => 'submit',
  382. '#value' => t('Clean up orphaned libraries'),
  383. '#weight' => 2,
  384. );
  385. }
  386. /************************************************************************
  387. *
  388. */
  389. function get_tripal_library_admin_form_taxonomy_set(&$form) {
  390. $form['taxonify'] = array(
  391. '#type' => 'fieldset',
  392. '#title' => t('Assign Drupal Taxonomy to Library Features')
  393. );
  394. // get the list of libraries
  395. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  396. $previous_db = tripal_db_set_active('chado'); // use chado database
  397. $lib_rset = db_query($sql);
  398. tripal_db_set_active($previous_db); // now use drupal database
  399. // iterate through all of the libraries
  400. $lib_boxes = array();
  401. while($library = db_fetch_object($lib_rset)){
  402. $lib_boxes[$library->library_id] = "$library->name";
  403. }
  404. $form['taxonify']['description'] = array(
  405. '#type' => 'item',
  406. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  407. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  408. "for setting taxonomy only for features that belong to the selected libraries below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  409. '#weight' => 1,
  410. );
  411. $form['taxonify']['tx-libraries'] = array (
  412. '#title' => t('Libraries'),
  413. '#type' => t('checkboxes'),
  414. '#description' => t("Check the libraries whose features you want to reset taxonomy. Note: this list contains all libraries, even those that may not be synced."),
  415. '#required' => FALSE,
  416. '#prefix' => '<div id="lib_boxes">',
  417. '#suffix' => '</div>',
  418. '#options' => $lib_boxes,
  419. '#weight' => 2
  420. );
  421. $form['taxonify']['tx-button'] = array(
  422. '#type' => 'submit',
  423. '#value' => t('Set Feature Taxonomy'),
  424. '#weight' => 3
  425. );
  426. }
  427. /************************************************************************
  428. *
  429. */
  430. function get_tripal_library_admin_form_reindex_set(&$form) {
  431. // define the fieldsets
  432. $form['reindex'] = array(
  433. '#type' => 'fieldset',
  434. '#title' => t('Reindex Library Features')
  435. );
  436. // get the list of libraries
  437. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  438. $previous_db = tripal_db_set_active('chado'); // use chado database
  439. $lib_rset = db_query($sql);
  440. tripal_db_set_active($previous_db); // now use drupal database
  441. // iterate through all of the libraries
  442. $lib_boxes = array();
  443. while($library = db_fetch_object($lib_rset)){
  444. $lib_boxes[$library->library_id] = "$library->name";
  445. }
  446. $form['reindex']['description'] = array(
  447. '#type' => 'item',
  448. '#value' => t("This option allows for reindexing of only those features that belong to the selected libraries below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
  449. '#weight' => 1,
  450. );
  451. $form['reindex']['re-libraries'] = array (
  452. '#title' => t('Libraries'),
  453. '#type' => t('checkboxes'),
  454. '#description' => t("Check the libraries whoee features you want to reindex. Note: this list contains all libraries, even those that may not be synced."),
  455. '#required' => FALSE,
  456. '#prefix' => '<div id="lib_boxes">',
  457. '#suffix' => '</div>',
  458. '#options' => $lib_boxes,
  459. '#weight' => 2,
  460. );
  461. $form['reindex']['re-button'] = array(
  462. '#type' => 'submit',
  463. '#value' => t('Reindex Features'),
  464. '#weight' => 3,
  465. );
  466. }
  467. /************************************************************************
  468. *
  469. */
  470. function get_tripal_library_admin_form_sync_set (&$form) {
  471. // define the fieldsets
  472. $form['sync'] = array(
  473. '#type' => 'fieldset',
  474. '#title' => t('Sync Libraries')
  475. );
  476. // get the list of libraries
  477. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  478. $previous_db = tripal_db_set_active('chado'); // use chado database
  479. $lib_rset = db_query($sql);
  480. tripal_db_set_active($previous_db); // now use drupal database
  481. // if we've added any libraries to the list that can be synced
  482. // then we want to build the form components to allow the user
  483. // to select one or all of them. Otherwise, just present
  484. // a message stating that all libraries are currently synced.
  485. $lib_boxes = array();
  486. $added = 0;
  487. while($library = db_fetch_object($lib_rset)){
  488. // check to see if the library is already present as a node in drupal.
  489. // if so, then skip it.
  490. $sql = "SELECT * FROM {chado_library} WHERE library_id = %d";
  491. if(!db_fetch_object(db_query($sql,$library->library_id))){
  492. $lib_boxes[$library->library_id] = "$library->name";
  493. $added++;
  494. }
  495. }
  496. // if we have libraries we need to add to the checkbox then
  497. // build that form element
  498. if($added > 0){
  499. $lib_boxes['all'] = "All Libraries";
  500. $form['reindex']['description'] = array(
  501. '#type' => 'item',
  502. '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
  503. '#weight' => 1,
  504. );
  505. $form['sync']['libraries'] = array (
  506. '#title' => t('Available Libraries'),
  507. '#type' => t('checkboxes'),
  508. '#description' => t("Check the libraries you want to sync. Drupal content will be created for each of the libraries listed above. Select 'All Libraries' to sync all of them."),
  509. '#required' => FALSE,
  510. '#prefix' => '<div id="lib_boxes">',
  511. '#suffix' => '</div>',
  512. '#options' => $lib_boxes,
  513. '#weight' => 2,
  514. );
  515. $form['sync']['button'] = array(
  516. '#type' => 'submit',
  517. '#value' => t('Sync Libraries'),
  518. '#weight' => 3,
  519. );
  520. }
  521. // we don't have any libraries to select from
  522. else {
  523. $form['sync']['value'] = array(
  524. '#value' => t('All libraries in Chado are currently synced with Drupal.')
  525. );
  526. }
  527. }
  528. /************************************************************************
  529. *
  530. */
  531. function tripal_library_admin_validate($form, &$form_state) {
  532. global $user; // we need access to the user info
  533. $job_args = array();
  534. // Submit the Sync Job if selected
  535. if ($form_state['values']['op'] == t('Sync Libraries')) {
  536. // check to see if the user wants to sync chado and drupal. If
  537. // so then we need to register a job to do so with tripal
  538. $libraries = $form_state['values']['libraries'];
  539. $do_all = FALSE;
  540. $to_sync = array();
  541. foreach ($libraries as $library_id){
  542. if(preg_match("/^all$/i",$library_id)){
  543. $do_all = TRUE;
  544. }
  545. if($library_id and preg_match("/^\d+$/i",$library_id)){
  546. // get the library info
  547. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  548. $previous_db = tripal_db_set_active('chado'); // use chado database
  549. $library = db_fetch_object(db_query($sql,$library_id));
  550. tripal_db_set_active($previous_db); // now use drupal database
  551. $to_sync[$library_id] = $library->name;
  552. }
  553. }
  554. // submit the job to the tripal job manager
  555. if($do_all){
  556. tripal_add_job('Sync all libraries','tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
  557. }
  558. else{
  559. foreach($to_sync as $library_id => $name){
  560. $job_args[0] = $library_id;
  561. tripal_add_job("Sync library: $name",'tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
  562. }
  563. }
  564. }
  565. // -------------------------------------
  566. // Submit the Reindex Job if selected
  567. if ($form_state['values']['op'] == t('Reindex Features')) {
  568. $libraries = $form_state['values']['re-libraries'];
  569. foreach ($libraries as $library_id){
  570. if($library_id and preg_match("/^\d+$/i",$library_id)){
  571. // get the library info
  572. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  573. $previous_db = tripal_db_set_active('chado'); // use chado database
  574. $library = db_fetch_object(db_query($sql,$library_id));
  575. tripal_db_set_active($previous_db); // now use drupal database
  576. $job_args[0] = $library_id;
  577. tripal_add_job("Reindex features for library: $library->name",'tripal_library',
  578. 'tripal_library_reindex_features',$job_args,$user->uid);
  579. }
  580. }
  581. }
  582. // -------------------------------------
  583. // Submit the Taxonomy Job if selected
  584. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  585. $libraries = $form_state['values']['tx-libraries'];
  586. foreach ($libraries as $library_id){
  587. if($library_id and preg_match("/^\d+$/i",$library_id)){
  588. // get the library info
  589. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  590. $previous_db = tripal_db_set_active('chado'); // use chado database
  591. $library = db_fetch_object(db_query($sql,$library_id));
  592. tripal_db_set_active($previous_db); // now use drupal database
  593. $job_args[0] = $library_id;
  594. tripal_add_job("Set taxonomy for features in library: $library->name",'tripal_library',
  595. 'tripal_library_taxonify_features',$job_args,$user->uid);
  596. }
  597. }
  598. }
  599. // -------------------------------------
  600. // Submit the Cleanup Job if selected
  601. if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
  602. tripal_add_job('Cleanup orphaned libraries','tripal_library',
  603. 'tripal_library_cleanup',$job_args,$user->uid);
  604. }
  605. }
  606. /*******************************************************************************
  607. *
  608. */
  609. function tripal_library_show_libraries (){
  610. // Show libraries stored in Drupal's {chado_library} table
  611. $sql = "SELECT COUNT(library_id) FROM {chado_library}";
  612. $no_libs = db_result(db_query ($sql));
  613. if($no_libs != 0) {
  614. $libraries = get_chado_libraries ();
  615. if($no_libs != count($libraries)) {
  616. drupal_set_message("Synchronization needed.");
  617. }
  618. return theme('tripal_library_library_table', &$libraries);
  619. } else {
  620. return t("No library exists. Please contact administrators to ".
  621. "synchronize libraries.");
  622. }
  623. }
  624. /*******************************************************************************
  625. *
  626. */
  627. function tripal_library_cron (){
  628. }
  629. /*******************************************************************************
  630. *
  631. * CHADO_LIBRARY NODE FUNCTIONS
  632. *
  633. * The following function proves access control for users trying to
  634. * perform actions on data managed by this module
  635. */
  636. function tripal_library_library_access($op, $node, $account){
  637. if ($op == 'create') {
  638. return user_access('create chado_library content', $account);
  639. }
  640. if ($op == 'update') {
  641. if (user_access('edit any chado_library content', $account) ||
  642. (user_access('edit own chado_library content', $account) &&
  643. ($account->uid == $node->uid))){
  644. return TRUE;
  645. }
  646. }
  647. if ($op == 'delete') {
  648. if (user_access('delete any chado_library content', $account) ||
  649. (user_access('delete own chado_library content', $account) &&
  650. ($account->uid == $node->uid))) {
  651. return TRUE;
  652. }
  653. }
  654. }
  655. /*******************************************************************************
  656. * validates submission of form when adding or updating a library node
  657. */
  658. function chado_library_validate($node){
  659. $lib = 0;
  660. // check to make sure the unique name on the library is unique
  661. // before we try to insert into chado.
  662. if($node->library_id){
  663. $sql = "SELECT * FROM ".
  664. "{Library} WHERE ".
  665. "uniquename = '%s' ".
  666. "AND NOT library_id = %d";
  667. $previous_db = tripal_db_set_active('chado');
  668. $lib = db_fetch_object(db_query($sql, $node->uniquename,
  669. $node->library_id));
  670. tripal_db_set_active($previous_db);
  671. } else {
  672. $sql = "SELECT * FROM ".
  673. "{Library} ".
  674. "WHERE uniquename = '%s'";
  675. $previous_db = tripal_db_set_active('chado');
  676. $lib = db_fetch_object(db_query($sql, $node->uniquename));
  677. tripal_db_set_active($previous_db);
  678. }
  679. if($lib){
  680. form_set_error('uniquename',t('The unique library name already exists. '.
  681. 'Please choose another'));
  682. }
  683. }
  684. /*******************************************************************************
  685. * When a new chado_library node is created we also need to add information
  686. * to our chado_library table. This function is called on insert of a new node
  687. * of type 'chado_library' and inserts the necessary information.
  688. */
  689. function chado_library_insert($node){
  690. // get the library type id
  691. $values = array(
  692. 'cv_id' => array(
  693. 'name' => 'tripal'
  694. ),
  695. 'name' => $node->library_type
  696. );
  697. $type = tripal_core_chado_select('cvterm',array('cvterm_id'),$values);
  698. $values = array(
  699. 'name' => $node->title,
  700. 'uniquename' => $node->uniquename,
  701. 'organism_id' => $node->organism_id,
  702. 'type_id' => $type[0]->cvterm_id
  703. );
  704. $library = tripal_core_chado_insert('library', $values);
  705. if ($library) {
  706. // add the description property
  707. tripal_library_insert_property($library['library_id'],'library_description',$node->library_description);
  708. // make sure the entry for this feature doesn't already exist in the chado_feature table
  709. // if it doesn't exist then we want to add it.
  710. $library_id = chado_get_id_for_node('library',$node) ;
  711. if(!$library_id){
  712. // next add the item to the drupal table
  713. $sql = "INSERT INTO {chado_library} (nid, vid, library_id) ".
  714. "VALUES (%d, %d, %d)";
  715. db_query($sql,$node->nid,$node->vid,$library['library_id']);
  716. }
  717. } else {
  718. drupal_set_message('Unable to add library.', 'warning');
  719. watchdog('tripal_library',
  720. 'Insert feature: Unable to create library where values: %values',
  721. array('%values' => print_r($values, TRUE)),
  722. WATCHDOG_WARNING
  723. );
  724. }
  725. }
  726. /*******************************************************************************
  727. * Update nodes
  728. */
  729. function chado_library_update($node){
  730. if($node->revision){
  731. // TODO -- decide what to do about revisions
  732. } else {
  733. $library_id = chado_get_id_for_node('library',$node) ;
  734. // get the library type id
  735. $values = array(
  736. 'cv_id' => array(
  737. 'name' => 'tripal'
  738. ),
  739. 'name' => $node->library_type
  740. );
  741. $type = tripal_core_chado_select('cvterm',array('cvterm_id'),$values);
  742. // update the library record
  743. $match = array(
  744. 'library_id' => $library_id,
  745. );
  746. $values = array(
  747. 'name' => $node->title,
  748. 'uniquename' => $node->uniquename,
  749. 'organism_id' => $node->organism_id,
  750. 'type_id' => $type[0]->cvterm_id
  751. );
  752. $status = tripal_core_chado_update('library', $match,$values);
  753. tripal_library_update_property($library_id,'library_description',$node->library_description);
  754. }
  755. }
  756. /*******************************************************************************
  757. * Add the library as a taxonomy term for associating with library_features
  758. */
  759. function tripal_library_add_taxonomy ($node,$library_id){
  760. //include the file containing the required functions. We only have to
  761. // do this because Drupal 6 fails to do this globally for us and
  762. // the drupal_execute function below won't work
  763. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  764. /* // get the vocabulary id
  765. $vocabularies = taxonomy_get_vocabularies();
  766. $vid = NULL;
  767. foreach($vocabularies as $vocab){
  768. if($vocab->name == 'DNA Libraries'){
  769. $vid = $vocab->vid;
  770. }
  771. }
  772. if(!$vid){ */
  773. // add the vocabulary
  774. $vocab_form['values']['name'] = 'DNA Libraries';
  775. $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
  776. $vocab_form['values']['help'] = '';
  777. $vocab_form['values']['module'] = 'taxonomy';
  778. drupal_execute('taxonomy_form_vocabulary',$vocab_form);
  779. return;
  780. // }
  781. // make sure this term doesn't already exist. If it doesn't then add it
  782. if($vid){
  783. $tree = taxonomy_get_tree($vid);
  784. $found = 0;
  785. foreach ($tree as $term) {
  786. if($term->name == $node->title){
  787. $found = 1;
  788. }
  789. }
  790. // add the term to the vocabulary
  791. if(!$found){
  792. $form_state = array();
  793. $form_state['values']['name'] = $node->title;
  794. $form_state['values']['description'] = $library_id;
  795. drupal_execute('taxonomy_form_term',$form_state,$vid);
  796. }
  797. }
  798. }
  799. /*******************************************************************************
  800. * When editing or creating a new node of type 'chado_library' we need
  801. * a form. This function creates the form that will be used for this.
  802. */
  803. function chado_library_form ($node){
  804. $form = array();
  805. $library = $node->library;
  806. // get the default values
  807. $uniquename = $node->uniquename;
  808. if(!$uniquename){
  809. $uniquename = $library->uniquename;
  810. }
  811. $library_type = $node->library_type;
  812. if(!$library_type){
  813. $library_type = $library->type_id->name;
  814. }
  815. $organism_id = $node->organism_id;
  816. if(!$organism_id){
  817. $organism_id = $library->organism_id->organism_id;
  818. }
  819. $library_description = $node->library_description;
  820. if(!$library_description){
  821. $libprop = tripal_library_get_property($library->library_id,'library_description');
  822. $library_description = $libprop->value;
  823. }
  824. // keep track of the library id if we have. If we do have one then
  825. // this is an update as opposed to an insert.
  826. $form['library_id'] = array(
  827. '#type' => 'value',
  828. '#value' => $library->library_id,
  829. );
  830. $form['title']= array(
  831. '#type' => 'textfield',
  832. '#title' => t('Library Title'),
  833. '#description' => t('Please enter the title for this library. '.
  834. 'This appears at the top of the library page.'),
  835. '#required' => TRUE,
  836. '#default_value' => $node->title,
  837. '#weight' => 1
  838. );
  839. $form['uniquename']= array(
  840. '#type' => 'textfield',
  841. '#title' => t('Unique Library Name'),
  842. '#description' => t('Please enter a unique name for this library'),
  843. '#required' => TRUE,
  844. '#default_value' => $uniquename,
  845. '#weight' => 2
  846. );
  847. // These library types should not be hard coded, but for now the are...
  848. $library_types = array();
  849. $library_types[''] = '';
  850. $library_types['cdna_library'] = 'cDNA Library';
  851. $library_types['bac_library'] = 'BAC Library';
  852. $library_types['fosmid_library'] = 'FOSMID Library';
  853. $library_types['cosmid_library'] = 'COSMID Library';
  854. $library_types['yac_library'] = 'YAC Library';
  855. $form['library_type'] = array (
  856. '#title' => t('Library Type'),
  857. '#type' => t('select'),
  858. '#description' => t("Choose the library type."),
  859. '#required' => TRUE,
  860. '#default_value' => $library_type,
  861. '#options' => $library_types,
  862. '#weight' => 3
  863. );
  864. // get the list of organisms
  865. $sql = "SELECT * FROM {Organism}";
  866. $previous_db = tripal_db_set_active('chado'); // use chado database
  867. $org_rset = db_query($sql);
  868. tripal_db_set_active($previous_db); // now use drupal database
  869. $organisms = array();
  870. $organisms[''] = '';
  871. while($organism = db_fetch_object($org_rset)){
  872. $organisms[$organism->organism_id] =
  873. "$organism->genus $organism->species ($organism->common_name)";
  874. }
  875. $form['organism_id'] = array (
  876. '#title' => t('Organism'),
  877. '#type' => t('select'),
  878. '#description' => t("Choose the organism with which this library is ".
  879. "associated."),
  880. '#required' => TRUE,
  881. '#default_value' => $organism_id,
  882. '#options' => $organisms,
  883. '#weight' => 4,
  884. );
  885. $form['library_description']= array(
  886. '#type' => 'textarea',
  887. '#title' => t('Library Description'),
  888. '#description' => t('A brief description of the library'),
  889. '#required' => TRUE,
  890. '#default_value' => $library_description,
  891. '#weight' => 5
  892. );
  893. return $form;
  894. }
  895. /************************************************************************
  896. *
  897. */
  898. function tripal_library_sync_libraries ($library_id = NULL, $job_id = NULL){
  899. global $user;
  900. $page_content = '';
  901. // get the list of libraries and create new nodes
  902. if(!$library_id){
  903. $sql = "SELECT * FROM {Library} L";
  904. $previous_db = tripal_db_set_active('chado'); // use chado database
  905. $results = db_query($sql);
  906. tripal_db_set_active($previous_db); // now use drupal database
  907. } else {
  908. $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
  909. $previous_db = tripal_db_set_active('chado'); // use chado database
  910. $results = db_query($sql,$library_id);
  911. tripal_db_set_active($previous_db); // now use drupal database
  912. }
  913. // We'll use the following SQL statement for checking if the library
  914. // already exists as a drupal node.
  915. $sql = "SELECT * FROM {chado_library} ".
  916. "WHERE library_id = %d";
  917. while($library = db_fetch_object($results)){
  918. // check if this library already exists in the drupal database. if it
  919. // does then skip this library and go to the next one.
  920. if(!db_fetch_object(db_query($sql,$library->library_id))){
  921. $new_node = new stdClass();
  922. $new_node->type = 'chado_library';
  923. $new_node->uid = $user->uid;
  924. $new_node->title = "$library->name";
  925. $new_node->library_id = $library->library_id;
  926. $new_node->organism_id = $library->organism_id;
  927. $new_node->uniquename = $library->uniquename;
  928. $new_node->type_id = $library->type_id;
  929. node_validate($new_node);
  930. if(!form_get_errors()){
  931. $node = node_submit($new_node);
  932. node_save($node);
  933. if($node->nid){
  934. $page_content .= "Added $library->name<br>";
  935. }
  936. }
  937. } else {
  938. $page_content .= "Skipped $library->name<br>";
  939. }
  940. }
  941. return $page_content;
  942. }
  943. /************************************************************************
  944. * When a node is requested by the user this function is called to allow us
  945. * to add auxiliary data to the node object.
  946. */
  947. function chado_library_load($node){
  948. // get the feature details from chado
  949. $library_id = chado_get_id_for_node('library',$node);
  950. $values = array('library_id' => $library_id);
  951. $library = tripal_core_generate_chado_var('library',$values);
  952. $additions->library = $library;
  953. return $additions;
  954. }
  955. /*******************************************************************************
  956. * This function customizes the view of the chado_library node. It allows
  957. * us to generate the markup. This function is required for node [Preview]
  958. */
  959. function chado_library_view ($node, $teaser = FALSE, $page = FALSE) {
  960. // use drupal's default node view:
  961. if (!$teaser) {
  962. $node = node_prepare($node, $teaser);
  963. // If Hook_view() is called by Hook_form(), we'll only have orgnism_id
  964. // but not genus/species/common_name. We need to get those from chado
  965. // database so they will show up in preview
  966. if(!$node->genus) {
  967. $previous_db = tripal_db_set_active('chado');
  968. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  969. $data = db_fetch_object(db_query($sql, $node->organism_id));
  970. $node->genus = $data->genus;
  971. $node->species = $data->species;
  972. $node->common_name = $data->common_name;
  973. tripal_db_set_active($previous_db);
  974. }
  975. }
  976. return $node;
  977. }
  978. /************************************************************************
  979. ** This function creates the html markup for the library table.
  980. ** It can be overridden in the theme for the site by adding a php
  981. ** method in the template.php file named
  982. ** <theme_name>_tripal_library_library_table(&$libraries)
  983. */
  984. function theme_tripal_library_library_table ($libraries) {
  985. // cycle through the libraries and build the libraries page
  986. $output = "<div id=\"libraries\">";
  987. $output .= '<table>';
  988. $output .= "<tr>";
  989. $output .= "<th>Name</th>";
  990. $output .= "<th>Type</th>";
  991. $output .= "<th>Organism</th>";
  992. $output .= "<th>Description</th>";
  993. $output .= "</tr>";
  994. foreach($libraries as $library){
  995. $lib_url = url("node/$library->node_id");
  996. $output .= "<tr>";
  997. $output .= "<td>".
  998. " <a href=\"$lib_url\">$library->name</a>".
  999. "</td>";
  1000. if(strcasecmp($library->type_name, 'bac_library') == 0){
  1001. $output .= "<td>BAC</td>";
  1002. }
  1003. elseif(strcasecmp($library->type_name, 'cdna_library') == 0){
  1004. $output .= "<td>cDNA</td>";
  1005. }
  1006. else{
  1007. $output .= "<td>$library->type_name</td>";
  1008. }
  1009. $output .= "<td nowrap>".
  1010. " $library->common_name".
  1011. "</td>";
  1012. $description = $library->library_description;
  1013. $output .= "<td>$description</td>";
  1014. $output .= "</tr>";
  1015. }
  1016. $output .= "</table>";
  1017. $output .= "</div>";
  1018. return $output;
  1019. }
  1020. /************************************************************************
  1021. *
  1022. */
  1023. function tripal_library_feature_set_taxonomy($library_id = NULL){
  1024. //TO DO : return usable error if vocabs don't exist
  1025. // get the list of vocabularies and find our two vocabularies of interest
  1026. $vocabularies = taxonomy_get_vocabularies();
  1027. $vid = NULL;
  1028. foreach($vocabularies as $vocab){
  1029. if($vocab->name == 'Library'){
  1030. $vid = $vocab->vid;
  1031. }
  1032. }
  1033. if(!$vid){
  1034. return;
  1035. }
  1036. // We'll use the following SQL statement for getting the node info
  1037. if($library_id){
  1038. print "Finding features for library with ID: $library_id\n";
  1039. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1040. "FROM {library_feature} LF ".
  1041. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1042. "WHERE L.library_id = $library_id ".
  1043. "ORDER BY LF.feature_id";
  1044. $previous_db = tripal_db_set_active('chado'); // use chado database
  1045. $features = db_query($sql);
  1046. tripal_db_set_active($previous_db); // now use drupal database
  1047. } else {
  1048. print "Finding features for all libraries\n";
  1049. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1050. "FROM {library_feature} LF ".
  1051. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1052. "ORDER BY LF.feature_id";
  1053. $previous_db = tripal_db_set_active('chado'); // use chado database
  1054. $features = db_query($sql);
  1055. tripal_db_set_active($previous_db); // now use drupal database
  1056. }
  1057. $node_sql = "SELECT * FROM {chado_feature} CF ".
  1058. " INNER JOIN {node} N ON CF.nid = N.nid ".
  1059. "WHERE feature_id = %d";
  1060. // iterate through the features and add the taxonomy
  1061. while($feature = db_fetch_object($features)){
  1062. $node = db_fetch_object(db_query($node_sql,$feature->feature_id));
  1063. $tags["$vid"] = $feature->libname;
  1064. $terms['tags'] = $tags;
  1065. taxonomy_node_save($node,$terms);
  1066. print "Updated $feature->feature_id as $feature->libname\n";
  1067. }
  1068. }
  1069. /*******************************************************************************
  1070. *
  1071. */
  1072. function tripal_library_reindex_features ($library_id = NULL, $job_id = NULL){
  1073. $i = 0;
  1074. // if the caller provided a library_id then get all of the features
  1075. // associated with the library. Otherwise get all sequences assoicated
  1076. // with all libraries.
  1077. if($library_id){
  1078. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1079. " FROM {library_feature} LF ".
  1080. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1081. "WHERE L.library_id = $library_id ".
  1082. "ORDER BY LF.feature_id";
  1083. $previous_db = tripal_db_set_active('chado'); // use chado database
  1084. $results = db_query($sql);
  1085. tripal_db_set_active($previous_db); // now use drupal database
  1086. }
  1087. else {
  1088. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1089. " FROM {library_feature} LF ".
  1090. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1091. "ORDER BY LF.feature_id";
  1092. $previous_db = tripal_db_set_active('chado'); // use chado database
  1093. $results = db_query($sql);
  1094. tripal_db_set_active($previous_db); // now use drupal database
  1095. }
  1096. // load into ids array
  1097. $count = 0;
  1098. $ids = array();
  1099. while($id = db_fetch_object($results)){
  1100. $ids[$count] = $id->feature_id;
  1101. $count++;
  1102. }
  1103. $interval = intval($count * 0.01);
  1104. foreach($ids as $feature_id){
  1105. // update the job status every 1% features
  1106. if($job_id and $i % interval == 0){
  1107. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1108. }
  1109. tripal_feature_sync_feature ($feature_id);
  1110. $i++;
  1111. }
  1112. }
  1113. /*******************************************************************************
  1114. *
  1115. */
  1116. function tripal_library_taxonify_features ($library_id = NULL, $job_id = NULL){
  1117. $i = 0;
  1118. // if the caller provided a library_id then get all of the features
  1119. // associated with the library. Otherwise get all sequences assoicated
  1120. // with all libraries.
  1121. if($library_id){
  1122. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1123. " FROM {library_feature} LF ".
  1124. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1125. "WHERE L.library_id = $library_id ".
  1126. "ORDER BY LF.feature_id";
  1127. $previous_db = tripal_db_set_active('chado'); // use chado database
  1128. $results = db_query($sql);
  1129. tripal_db_set_active($previous_db); // now use drupal database
  1130. }
  1131. else {
  1132. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1133. " FROM {library_feature} LF ".
  1134. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1135. "ORDER BY LF.feature_id";
  1136. $previous_db = tripal_db_set_active('chado'); // use chado database
  1137. $results = db_query($sql);
  1138. tripal_db_set_active($previous_db); // now use drupal database
  1139. }
  1140. // load into ids array
  1141. $count = 0;
  1142. $ids = array();
  1143. while($id = db_fetch_object($results)){
  1144. $ids[$count] = $id->feature_id;
  1145. $count++;
  1146. }
  1147. // make sure our vocabularies are set before proceeding
  1148. tripal_feature_set_vocabulary();
  1149. // use this SQL for getting the nodes
  1150. $nsql = "SELECT * FROM {chado_feature} CF ".
  1151. " INNER JOIN {node} N ON N.nid = CF.nid ".
  1152. "WHERE feature_id = %d";
  1153. // iterate through the features and set the taxonomy
  1154. $interval = intval($count * 0.01);
  1155. foreach($ids as $feature_id){
  1156. // update the job status every 1% features
  1157. if($job_id and $i % interval == 0){
  1158. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1159. }
  1160. $node = db_fetch_object(db_query($nsql,$feature_id));
  1161. tripal_feature_set_taxonomy($node,$feature_id);
  1162. $i++;
  1163. }
  1164. }
  1165. /*******************************************************************************
  1166. * Delete data from drupal and chado databases when a node is deleted
  1167. */
  1168. function chado_library_delete(&$node){
  1169. // Before removing, get library_id so we can remove it from chado database
  1170. // later
  1171. $sql_drupal = "SELECT library_id ".
  1172. "FROM {chado_library} ".
  1173. "WHERE nid = %d AND vid = %d";
  1174. $library_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  1175. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  1176. // drupal database
  1177. $sql_del = "DELETE FROM {chado_library} ".
  1178. "WHERE nid = %d ".
  1179. "AND vid = %d";
  1180. db_query($sql_del, $node->nid, $node->vid);
  1181. $sql_del = "DELETE FROM {node} ".
  1182. "WHERE nid = %d ".
  1183. "AND vid = %d";
  1184. db_query($sql_del, $node->nid, $node->vid);
  1185. $sql_del = "DELETE FROM {node_revisions} ".
  1186. "WHERE nid = %d ".
  1187. "AND vid = %d";
  1188. db_query($sql_del, $node->nid, $node->vid);
  1189. // Remove data from library and libraryprop tables of chado database as well
  1190. $previous_db = tripal_db_set_active('chado');
  1191. db_query("DELETE FROM {library} WHERE library_id = %d", $library_id);
  1192. db_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
  1193. tripal_db_set_active($previous_db);
  1194. }
  1195. /************************************************************************
  1196. * Display block with libraries
  1197. * @param op - parameter to define the phase being called for the block
  1198. * @param delta - id of the block to return (ignored when op is list)
  1199. * @param edit - when op is save, contains the submitted form data
  1200. */
  1201. function tripal_library_block($op = 'list', $delta = '0', $edit = array()){
  1202. switch($op){
  1203. case 'list':
  1204. $blocks[0]['info'] = t('Libraries');
  1205. return $blocks;
  1206. case 'view':
  1207. if(user_access('access chado_library content')){
  1208. // Show libraries stored in Drupal's {chado_library} table
  1209. $sql = "SELECT COUNT(library_id) FROM {chado_library}";
  1210. $no_libs = db_result(db_query ($sql));
  1211. if($no_libs != 0) {
  1212. $libraries = get_chado_libraries();
  1213. foreach($libraries as $library){
  1214. // get the node id for this organism
  1215. $items[] = l($library->name, 'node/' . $library->node_id);
  1216. }
  1217. } else {
  1218. $items[] = t("No library exists.");
  1219. }
  1220. $block['subject'] = t('Libraries');
  1221. //We theme our array of links as an unordered list
  1222. $block['content'] = theme('item_list', $items);
  1223. }
  1224. return $block;
  1225. }
  1226. }
  1227. /*******************************************************************************
  1228. * This function uses library_id's of all drupal library nodes as input and
  1229. * pull the library information (name, uniquename, type, genus, species,
  1230. * common_name, description) from chado database. The return type is an object
  1231. * array that stores sorted $library objects
  1232. */
  1233. function get_chado_libraries() {
  1234. $sql_drupal = "SELECT COUNT (library_id) FROM {chado_library}";
  1235. $no_libs = db_result(db_query($sql_drupal));
  1236. if ($no_libs != 0) {
  1237. // Get library_id's from drupal
  1238. $sql = "SELECT library_id, nid FROM {chado_library}";
  1239. $result = db_query($sql);
  1240. $previous_db = tripal_db_set_active('chado');
  1241. // Get library info from chado's library, organism, and cvterm tables
  1242. $sql_info = "SELECT L.name, uniquename, genus, species, common_name, ".
  1243. " CVT.name as type_name ".
  1244. "FROM {Library} L ".
  1245. "INNER JOIN Organism O ON L.organism_id = O.organism_id ".
  1246. "INNER JOIN CVTerm CVT ON L.type_id = CVT.cvterm_id ".
  1247. "WHERE library_id=%d";
  1248. // Get library description from libraryprop if there is any
  1249. $sql_desc = "SELECT value ".
  1250. "FROM {Libraryprop} LP ".
  1251. "INNER JOIN CVTerm CVT ON LP.type_id = CVT.cvterm_id ".
  1252. "WHERE library_id = %d AND CVT.name = 'library_description'";
  1253. $libraries = array();
  1254. while ($data = db_fetch_object($result)) {
  1255. $library = db_fetch_object(db_query($sql_info, $data->library_id));
  1256. $library->node_id = $data->nid;
  1257. $desc = db_fetch_object(db_query($sql_desc, $data->library_id))->value;
  1258. if ($desc) {
  1259. $library->library_description = $desc;
  1260. } else {
  1261. $library->library_description = "NA";
  1262. }
  1263. // Use uniquename as the key so we can sort by uniquename later
  1264. $key = strtolower($library->uniquename);
  1265. $libraries [$key] = $library;
  1266. }
  1267. tripal_db_set_active($previous_db);
  1268. //Sort libraries by uniquename
  1269. ksort($libraries, SORT_STRING);
  1270. return $libraries;
  1271. }
  1272. }
  1273. /************************************************************************
  1274. *
  1275. */
  1276. function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
  1277. // build the SQL statments needed to check if nodes point to valid analyses
  1278. $dsql = "SELECT * FROM {node} WHERE type = 'chado_library' order by nid";
  1279. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1280. $csql = "SELECT * FROM {chado_library} where nid = %d ";
  1281. $cosql= "SELECT * FROM {chado_library}";
  1282. $tsql = "SELECT * FROM {library} L WHERE library_id = %d";
  1283. // load into nodes array
  1284. $results = db_query($dsql);
  1285. $count = 0;
  1286. $nodes = array();
  1287. while($node = db_fetch_object($results)){
  1288. $nodes[$count] = $node;
  1289. $count++;
  1290. }
  1291. // load the chado_analyses into an array
  1292. $results = db_query($cosql);
  1293. $cnodes = array();
  1294. while($node = db_fetch_object($results)){
  1295. $cnodes[$count] = $node;
  1296. $count++;
  1297. }
  1298. $interval = intval($count * 0.01);
  1299. // iterate through all of the chado_library nodes and delete those that aren't valid
  1300. foreach($nodes as $nid){
  1301. // update the job status every 1% analyses
  1302. if($job_id and $i % $interval == 0){
  1303. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1304. }
  1305. // first check to see if the node has a corresponding entry
  1306. // in the chado_library table. If not then delete the node.
  1307. $library = db_fetch_object(db_query($csql,$nid->nid));
  1308. if(!$library){
  1309. node_delete($nid->nid);
  1310. $message = "Missing in chado_library table.... DELETING: $nid->nid\n";
  1311. watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
  1312. continue;
  1313. }
  1314. $i++;
  1315. }
  1316. // iterate through all of the chado_library nodes and delete those that aren't valid
  1317. foreach($cnodes as $nid){
  1318. // update the job status every 1% analyses
  1319. if($job_id and $i % $interval == 0){
  1320. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1321. }
  1322. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1323. if(!$node){
  1324. db_query("DELETE FROM {chado_library} WHERE nid = $nid->nid");
  1325. $message = "chado_library missing node.... DELETING: $nid->nid\n";
  1326. watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
  1327. }
  1328. $i++;
  1329. }
  1330. return '';
  1331. }