pub_importers.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /**
  3. * A function to render a listing of all publication importers
  4. *
  5. * @ingroup tripal_pub
  6. */
  7. function tripal_pub_importers_list() {
  8. $header = array('', 'Importer Name', 'Database', 'Search String', 'Disabled', 'Create Contact', '');
  9. $rows = array();
  10. $importers = db_query("SELECT * FROM {tripal_pub_import} ORDER BY name");
  11. while ($importer = db_fetch_object($importers)) {
  12. $criteria = unserialize($importer->criteria);
  13. $num_criteria = $criteria['num_criteria'];
  14. $criteria_str = '';
  15. for ($i = 0; $i <= $num_criteria; $i++) {
  16. $search_terms = $criteria['criteria'][$i]['search_terms'];
  17. $scope = $criteria['criteria'][$i]['scope'];
  18. $operation = $criteria['criteria'][$i]['operation'];
  19. $criteria_str .= "$operation ($scope: $search_terms) ";
  20. }
  21. $rows[] = array(
  22. l(t('Edit/Test'), "admin/tripal/tripal_pub/import/edit/$importer->pub_import_id"),
  23. $importer->name,
  24. $criteria['remote_db'],
  25. $criteria_str,
  26. $importer->disabled ? 'Yes' : 'No',
  27. $importer->do_contact ? 'Yes' : 'No',
  28. l(t('Delete'), "admin/tripal/tripal_pub/import/delete/$importer->pub_import_id"),
  29. );
  30. }
  31. $rows[] = array(
  32. 'data' => array(
  33. array('data' => l(t('Create a new publication importer.'), "admin/tripal/tripal_pub/import/new"),
  34. 'colspan' => 7),
  35. )
  36. );
  37. $page = theme('table', $header, $rows);
  38. return $page;
  39. }
  40. /*
  41. *
  42. */
  43. function tripal_pub_importer_setup($action = 'new', $pub_import_id = NULL) {
  44. global $pager_total, $pager_total_items;
  45. $pager_id = 0;
  46. $limit = 10;
  47. // generate the search form
  48. $form = drupal_get_form('tripal_pub_importer_setup_form', $pub_import_id, $action);
  49. $output = l("Return to publication importers list", "admin/tripal/tripal_pub/import_list");
  50. $output .= $form;
  51. // retrieve any results
  52. $remote_db = $_SESSION['tripal_pub_search']['remote_db'];
  53. $num_criteria = $_SESSION['tripal_pub_search']['num_criteria'];
  54. $days = $_SESSION['tripal_pub_search']['days'];
  55. $search_array = array();
  56. $search_array['remote_db'] = $remote_db;
  57. $search_array['num_criteria'] = $num_criteria;
  58. $search_array['days'] = $days;
  59. for ($i = 0; $i <= $num_criteria; $i++) {
  60. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search']['criteria'][$i]['search_terms'];
  61. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search']['criteria'][$i]['scope'];
  62. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search']['criteria'][$i]['operation'];
  63. }
  64. if ($_SESSION['tripal_pub_search']['perform_search']) {
  65. // get the list of publications from the remote database using the search criteria.
  66. $pubs = tripal_pub_get_remote_search_results($remote_db, $search_array, $limit, $pager_id);
  67. // generate the pager
  68. $total_pages = $pager_total[$pager_id];
  69. $total_items = $pager_total_items[$pager_id];
  70. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  71. $pager = theme('pager');
  72. // iterate through the results and construct the table displaying the publications
  73. $rows = array();
  74. $i = $page * $limit + 1;
  75. if (count($pubs) > 0) {
  76. foreach ($pubs as $pub) {
  77. $raw_link = '';
  78. if($pub['Publication Dbxref']) {
  79. $raw_link = l('raw', 'admin/tripal/tripal_pub/import/raw/' . $pub['Publication Dbxref'], array('attributes' => array('target' => '_blank')));
  80. }
  81. $rows[] = array(
  82. number_format($i),
  83. $pub['Citation'],
  84. $raw_link,
  85. );
  86. $i++;
  87. }
  88. }
  89. $headers = array('', 'Publication', '');
  90. $table = theme('table', $headers, $rows);
  91. // join all to form the results
  92. $output .= "<br>Search String: $search_str<br><p><b>Found " . number_format($total_items) .
  93. ". Page " . ($page + 1) . " of $total_pages. " .
  94. " Results</b></br>" . $table . '</p>' . $pager;
  95. }
  96. return $output;
  97. }
  98. /*
  99. *
  100. */
  101. function theme_tripal_pub_importer_setup_form($form) {
  102. $rows = array();
  103. foreach ($form['criteria'] as $i => $element) {
  104. if(is_numeric($i)) {
  105. $rows[] = array(
  106. array('data' => drupal_render($element["operation-$i"]), 'width' => '10%'),
  107. array('data' => drupal_render($element["scope-$i"]), 'width' => '10%'),
  108. drupal_render($element["search_terms-$i"]),
  109. array('data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]), 'width' => '5%'),
  110. );
  111. }
  112. }
  113. $headers = array('Operation','Scope', 'Search Terms', '');
  114. $markup = '<div id="pub-search-form-row1">';
  115. $markup .= ' <div id="pub-search-form-col1">' . drupal_render($form['loader_name']) . '</div>';
  116. $markup .= ' <div id="pub-search-form-col2">' . drupal_render($form['remote_db']) . '</div>';
  117. $markup .= ' <div id="pub-search-form-col3">' . drupal_render($form['days']) . '</div>';
  118. $markup .= '</div>';
  119. $markup .= '<div id="pub-search-form-row2">' . drupal_render($form['disabled']) . '</div>';
  120. $markup .= '<div id="pub-search-form-row3">' . drupal_render($form['do_contact']) . '</div>';
  121. $markup .= theme('table', $headers, $rows, array('id' => 'tripal-pub-importer-table'));
  122. $form['criteria'] = array(
  123. '#type' => 'markup',
  124. '#value' => $markup,
  125. '#weight' => -10,
  126. );
  127. return drupal_render($form);
  128. }
  129. /**
  130. * Purpose: Provides the form to search pubmed
  131. *
  132. * @ingroup tripal_pub
  133. */
  134. function tripal_pub_importer_setup_form(&$form_state = NULL, $pub_import_id = NULL, $action = 'new') {
  135. tripal_core_ahah_init_form();
  136. // Set the default values. If the pub_import_id isn't already defined by the form values
  137. // and one is provided then look it up in the database
  138. $criteria = NULL;
  139. if ($action == "edit" and !$form_state['values']) {
  140. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = %d";
  141. $importer = db_fetch_object(db_query($sql, $pub_import_id));
  142. $criteria = unserialize($importer->criteria);
  143. $remote_db = $criteria['remote_db'];
  144. $days = $criteria['days'];
  145. $disabled = $criteria['disabled'];
  146. $do_contact = $criteria['do_contact'];
  147. $num_criteria = $criteria['num_criteria'];
  148. $loader_name = $criteria['loader_name'];
  149. }
  150. // if the session has variables then use those. This should only happen when
  151. // the 'Test Criteria' button is clicked.
  152. $num_criteria = $_SESSION['tripal_pub_search']['num_criteria'] ? $_SESSION['tripal_pub_search']['num_criteria'] : $num_criteria;
  153. $loader_name = $_SESSION['tripal_pub_search']['loader_name'] ? $_SESSION['tripal_pub_search']['loader_name'] : $loader_name;
  154. $remote_db = $_SESSION['tripal_pub_search']['remote_db'] ? $_SESSION['tripal_pub_search']['remote_db'] : $remote_db;
  155. $disabled = $_SESSION['tripal_pub_search']['disabled'] ? $_SESSION['tripal_pub_search']['disabled'] : $disabled;
  156. $do_contact = $_SESSION['tripal_pub_search']['do_contact'] ? $_SESSION['tripal_pub_search']['do_contact'] : $do_contact;
  157. $days = $_SESSION['tripal_pub_search']['days'] ? $_SESSION['tripal_pub_search']['days'] : $days;
  158. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  159. // form is resbumitted using AJAX
  160. $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $num_criteria;
  161. $loader_name = $form_state['values']['loader_name'] ? $form_state['values']['loader_name'] : $loader_name;
  162. $remote_db = $form_state['values']['remote_db'] ? $form_state['values']['remote_db'] : $remote_db;
  163. $disabled = $form_state['values']['disabled'] ? $form_state['values']['disabled'] : $disabled;
  164. $do_contact = $form_state['values']['do_contact'] ? $form_state['values']['do_contact'] : $do_contact;
  165. $days = $form_state['values']['days'] ? $form_state['values']['days'] : $days;
  166. // change the number of criteria based on form_state post data.
  167. if (!$num_criteria) {
  168. $num_criteria = 0;
  169. }
  170. if($form_state['post']["add-$num_criteria"]) {
  171. $num_criteria++;
  172. }
  173. if($form_state['post']["remove-$num_criteria"]) {
  174. $num_criteria--;
  175. }
  176. $form['pub_import_id'] = array(
  177. '#type' => 'hidden',
  178. '#value' => $pub_import_id,
  179. '#required' => TRUE,
  180. );
  181. $form['action'] = array(
  182. '#type' => 'hidden',
  183. '#value' => $action,
  184. '#required' => TRUE,
  185. );
  186. $form['loader_name'] = array(
  187. '#type' => 'textfield',
  188. '#title' => t('Loader Name'),
  189. '#description' => t('Please provide a name for this loader setup..'),
  190. '#default_value' => $loader_name,
  191. '#required' => TRUE,
  192. );
  193. $remote_dbs = array(
  194. 'PMID' => 'Pubmed',
  195. 'AGRICOLA' => 'AGRICOLA',
  196. );
  197. $form['remote_db'] = array(
  198. '#title' => t('Remote Database'),
  199. '#type' => 'select',
  200. '#options' => $remote_dbs,
  201. '#default_value' => $remote_db,
  202. );
  203. $form['num_criteria']= array(
  204. '#type' => 'hidden',
  205. '#default_value' => $num_criteria,
  206. );
  207. $form['pub_import_id']= array(
  208. '#type' => 'hidden',
  209. '#default_value' => $pub_import_id,
  210. );
  211. $form['days'] = array(
  212. '#type' => 'textfield',
  213. '#title' => t('Days'),
  214. '#description' => t('The number of days from today to search.'),
  215. '#default_value' => $days,
  216. '#size' => 5,
  217. );
  218. $form['disabled'] = array(
  219. '#type' => 'checkbox',
  220. '#title' => t('Disabled'),
  221. '#description' => t('Check to disable this importer.'),
  222. '#default_value' => $disabled,
  223. );
  224. $form['do_contact'] = array(
  225. '#type' => 'checkbox',
  226. '#title' => t('Create Contact'),
  227. '#description' => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of
  228. additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
  229. '#default_value' => $do_contact,
  230. );
  231. for($i = 0; $i <= $num_criteria; $i++) {
  232. // if we have criteria supplied from the database then use that, othrewise look from the form_state or the session
  233. if ($criteria) {
  234. $search_terms = $criteria['criteria'][$i]['search_terms'];
  235. $scope = $criteria['criteria'][$i]['scope'];
  236. $operation = $criteria['criteria'][$i]['operation'];
  237. }
  238. // first populate defaults using any values in the SESSION variable
  239. $search_terms = $_SESSION['tripal_pub_search']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search']['criteria'][$i]['search_terms'] : $search_terms;
  240. $scope = $_SESSION['tripal_pub_search']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search']['criteria'][$i]['scope'] : $scope;
  241. $operation = $_SESSION['tripal_pub_search']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search']['criteria'][$i]['operation'] : $operation;
  242. // next populate defaults using any form values
  243. $search_terms = $form_state['values']["search_terms-$i"] ? $form_state['values']["search_terms-$i"] : $search_terms;
  244. $scope = $form_state['values']["scope-$i"] ? $form_state['values']["scope-$i"] : $scope;
  245. $operation = $form_state['values']["operation-$i"] ? $form_state['values']["operation-$i"] : $operation;
  246. // default to searching the title and abstract
  247. if (!$scope) {
  248. $scope = 'abstract';
  249. }
  250. $form['criteria'][$i]["search_terms-$i"] = array(
  251. '#type' => 'textfield',
  252. '#description' => t('Please provide a list of words for searching. You may use conjunctions such as "AND" or "OR" to separate words if they are expected in the same scope"'),
  253. '#default_value' => $search_terms,
  254. '#required' => TRUE,
  255. );
  256. $form['criteria'][$i]["scope-$i"] = array(
  257. '#type' => 'select',
  258. '#description' => t('Please select the fields to search for this term.'),
  259. '#options' => array(
  260. 'any' => 'Any Field',
  261. 'title' => 'Title',
  262. 'abstract' => 'Title/Abstract',
  263. 'author' => 'Author'),
  264. '#default_value' => $scope,
  265. );
  266. if ($i > 0) {
  267. $form['criteria'][$i]["operation-$i"] = array(
  268. '#type' => 'select',
  269. '#options' => array(
  270. 'AND' => 'AND',
  271. 'OR' => 'OR',
  272. 'NOT' => 'NOT'),
  273. '#default_value' => $operation,
  274. );
  275. }
  276. if ($i == $num_criteria) {
  277. if($i > 0) {
  278. $form['criteria'][$i]["remove-$i"] = array(
  279. '#type' => 'image_button',
  280. '#value' => t('Remove'),
  281. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  282. '#ahah' => array(
  283. 'path' => "admin/tripal/tripal_pub/import/criteria/minus/$i",
  284. 'wrapper' => 'tripal-pub-importer-setup-form',
  285. 'event' => 'click',
  286. 'method' => 'replace',
  287. ),
  288. '#attributes' => array('onClick' => 'return false;'),
  289. );
  290. }
  291. $form['criteria'][$i]["add-$i"] = array(
  292. '#type' => 'image_button',
  293. '#value' => t('Add'),
  294. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  295. '#ahah' => array(
  296. 'path' => "admin/tripal/tripal_pub/import/criteria/add/$i",
  297. 'wrapper' => 'tripal-pub-importer-setup-form',
  298. 'event' => 'click',
  299. 'method' => 'replace',
  300. ),
  301. '#attributes' => array('onClick' => 'return false;'),
  302. );
  303. }
  304. }
  305. $form['test'] = array(
  306. '#type' => 'submit',
  307. '#value' => t('Test Importer'),
  308. );
  309. $form['save'] = array(
  310. '#type' => 'submit',
  311. '#value' => t('Save Importer'),
  312. );
  313. /*
  314. $form['import'] = array(
  315. '#type' => 'submit',
  316. '#value' => t('Save & Import Now'),
  317. );*/
  318. $form['delete'] = array(
  319. '#type' => 'submit',
  320. '#value' => t('Delete Importer'),
  321. );
  322. return $form;
  323. }
  324. /**
  325. *
  326. */
  327. function tripal_pub_importer_setup_form_validate($form, &$form_state) {
  328. $num_criteria = $form_state['values']['num_criteria'];
  329. $remote_db = $form_state['values']["remote_db"];
  330. $days = trim($form_state['values']["days"]);
  331. $disabled = $form_state['values']["disabled"];
  332. $do_contact = $form_state['values']["do_contact"];
  333. $loader_name = trim($form_state['values']["loader_name"]);
  334. for ($i = 0; $i <= $num_criteria; $i++) {
  335. $search_terms = trim($form_state['values']["search_terms-$i"]);
  336. $scope = $form_state['values']["scope-$i"];
  337. $operation = $form_state['values']["operation-$i"];
  338. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  339. form_set_error("days-$i", "Please enter a numeric, non decimal value, for the number of days.");
  340. }
  341. }
  342. }
  343. /**
  344. *
  345. */
  346. function tripal_pub_importer_setup_form_submit($form, &$form_state) {
  347. $pub_import_id = $form_state['values']['pub_import_id'];
  348. $num_criteria = $form_state['values']['num_criteria'];
  349. $remote_db = $form_state['values']["remote_db"];
  350. $days = trim($form_state['values']["days"]);
  351. $loader_name = trim($form_state['values']["loader_name"]);
  352. $disabled = $form_state['values']["disabled"];
  353. $do_contact = $form_state['values']["do_contact"];
  354. // set the session variables
  355. $_SESSION['tripal_pub_search']['remote_db'] = $remote_db;
  356. $_SESSION['tripal_pub_search']['days'] = $days;
  357. $_SESSION['tripal_pub_search']['num_criteria'] = $num_criteria;
  358. $_SESSION['tripal_pub_search']['loader_name'] = $loader_name;
  359. $_SESSION['tripal_pub_search']['disabled'] = $disabled;
  360. $_SESSION['tripal_pub_search']['do_contact'] = $do_contact;
  361. unset($_SESSION['tripal_pub_search']['criteria']);
  362. for ($i = 0; $i <= $num_criteria; $i++) {
  363. $search_terms = trim($form_state['values']["search_terms-$i"]);
  364. $scope = $form_state['values']["scope-$i"];
  365. $operation = $form_state['values']["operation-$i"];
  366. $_SESSION['tripal_pub_search']['criteria'][$i] = array(
  367. 'search_terms' => $search_terms,
  368. 'scope' => $scope,
  369. 'operation' => $operation
  370. );
  371. }
  372. // now perform the appropriate action for the button clicked
  373. if ($form_state['values']['op'] == 'Test Importer') {
  374. $_SESSION['tripal_pub_search']['perform_search'] = 1;
  375. }
  376. if ($form_state['values']['op'] == 'Save Importer' or
  377. $form_state['values']['op'] == 'Save & Import Now') {
  378. $record = array(
  379. 'name' => $loader_name,
  380. 'criteria' => serialize($_SESSION['tripal_pub_search']),
  381. 'disabled' => $disabled,
  382. 'do_contact' => $do_contact
  383. );
  384. // first check to see if this pub_import_id is already present. If so,
  385. // do an update rather than an insert
  386. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = %d";
  387. $importer = db_fetch_object(db_query($sql, $pub_import_id));
  388. if($importer) {
  389. // do the update
  390. $record['pub_import_id'] = $pub_import_id;
  391. if(drupal_write_record('tripal_pub_import', $record, 'pub_import_id')){
  392. unset($_SESSION['tripal_pub_search']);
  393. drupal_set_message('Publication import settings updated.');
  394. drupal_goto('admin/tripal/tripal_pub/import_list');
  395. }
  396. else {
  397. drupal_set_message('Could not update publication import settings.', 'error');
  398. }
  399. }
  400. else {
  401. // do the insert
  402. if(drupal_write_record('tripal_pub_import', $record)){
  403. unset($_SESSION['tripal_pub_search']);
  404. drupal_set_message('Publication import settings saved.');
  405. // if the user wants to do the import now then do it (may time out
  406. // for long jobs)
  407. if ($form_state['values']['op'] == 'Save & Import Now') {
  408. tripal_pub_import_publications($record['pub_import_id']);
  409. }
  410. drupal_goto('admin/tripal/tripal_pub/import_list');
  411. }
  412. else {
  413. drupal_set_message('Could not save publication import settings.', 'error');
  414. }
  415. }
  416. }
  417. if ($form_state['values']['op'] == 'Delete Importer') {
  418. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = %d";
  419. $success = db_query($sql, $pub_import_id);
  420. if ($success) {
  421. drupal_set_message('Publication importer deleted.');
  422. drupal_goto('admin/tripal/tripal_pub/import_list');
  423. }
  424. else {
  425. drupal_set_message('Could not delete publication importer.', 'error');
  426. }
  427. }
  428. }
  429. /*
  430. *
  431. */
  432. function tripal_pub_importer_delete($pub_import_id) {
  433. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = %d";
  434. $success = db_query($sql, $pub_import_id);
  435. if ($success) {
  436. drupal_set_message('Publication importer deleted.');
  437. drupal_goto('admin/tripal/tripal_pub/import_list');
  438. }
  439. else {
  440. drupal_set_message('Could not delete publication importer.', 'error');
  441. }
  442. }
  443. /*
  444. * AHAH callback
  445. */
  446. function tripal_pub_importer_setup_page_update_criteria($action, $i) {
  447. $status = TRUE;
  448. // prepare and render the form
  449. $form = tripal_core_ahah_prepare_form();
  450. $data = theme('tripal_pub_importer_setup_form', $form);
  451. // bind javascript events to the new objects that will be returned
  452. // so that AHAH enabled elements will work.
  453. $settings = tripal_core_ahah_bind_events();
  454. // return the updated JSON
  455. drupal_json(
  456. array(
  457. 'status' => $status,
  458. 'data' => $data,
  459. 'settings' => $settings,
  460. )
  461. );
  462. }