pub_importers.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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('PMID' => 'Pubmed');
  194. $form['remote_db'] = array(
  195. '#title' => t('Remote Database'),
  196. '#type' => 'select',
  197. '#options' => $remote_dbs,
  198. '#default_value' => $remote_db,
  199. );
  200. $form['num_criteria']= array(
  201. '#type' => 'hidden',
  202. '#default_value' => $num_criteria,
  203. );
  204. $form['pub_import_id']= array(
  205. '#type' => 'hidden',
  206. '#default_value' => $pub_import_id,
  207. );
  208. $form['days'] = array(
  209. '#type' => 'textfield',
  210. '#title' => t('Days'),
  211. '#description' => t('The number of days from today to search.'),
  212. '#default_value' => $days,
  213. '#size' => 5,
  214. );
  215. $form['disabled'] = array(
  216. '#type' => 'checkbox',
  217. '#title' => t('Disabled'),
  218. '#description' => t('Check to disable this importer.'),
  219. '#default_value' => $disabled,
  220. );
  221. $form['do_contact'] = array(
  222. '#type' => 'checkbox',
  223. '#title' => t('Create Contact'),
  224. '#description' => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of
  225. additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
  226. '#default_value' => $do_contact,
  227. );
  228. for($i = 0; $i <= $num_criteria; $i++) {
  229. // if we have criteria supplied from the database then use that, othrewise look from the form_state or the session
  230. if ($criteria) {
  231. $search_terms = $criteria['criteria'][$i]['search_terms'];
  232. $scope = $criteria['criteria'][$i]['scope'];
  233. $operation = $criteria['criteria'][$i]['operation'];
  234. }
  235. // first populate defaults using any values in the SESSION variable
  236. $search_terms = $_SESSION['tripal_pub_search']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search']['criteria'][$i]['search_terms'] : $search_terms;
  237. $scope = $_SESSION['tripal_pub_search']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search']['criteria'][$i]['scope'] : $scope;
  238. $operation = $_SESSION['tripal_pub_search']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search']['criteria'][$i]['operation'] : $operation;
  239. // next populate defaults using any form values
  240. $search_terms = $form_state['values']["search_terms-$i"] ? $form_state['values']["search_terms-$i"] : $search_terms;
  241. $scope = $form_state['values']["scope-$i"] ? $form_state['values']["scope-$i"] : $scope;
  242. $operation = $form_state['values']["operation-$i"] ? $form_state['values']["operation-$i"] : $operation;
  243. // default to searching the title and abstract
  244. if (!$scope) {
  245. $scope = 'abstract';
  246. }
  247. $form['criteria'][$i]["search_terms-$i"] = array(
  248. '#type' => 'textfield',
  249. '#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"'),
  250. '#default_value' => $search_terms,
  251. '#required' => TRUE,
  252. );
  253. $form['criteria'][$i]["scope-$i"] = array(
  254. '#type' => 'select',
  255. '#description' => t('Please select the fields to search for this term.'),
  256. '#options' => array(
  257. 'any' => 'Any Field',
  258. 'title' => 'Title',
  259. 'abstract' => 'Title/Abstract',
  260. 'author' => 'Author'),
  261. '#default_value' => $scope,
  262. );
  263. if ($i > 0) {
  264. $form['criteria'][$i]["operation-$i"] = array(
  265. '#type' => 'select',
  266. '#options' => array(
  267. 'AND' => 'AND',
  268. 'OR' => 'OR',
  269. 'NOT' => 'NOT'),
  270. '#default_value' => $operation,
  271. );
  272. }
  273. if ($i == $num_criteria) {
  274. if($i > 0) {
  275. $form['criteria'][$i]["remove-$i"] = array(
  276. '#type' => 'image_button',
  277. '#value' => t('Remove'),
  278. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  279. '#ahah' => array(
  280. 'path' => "admin/tripal/tripal_pub/import/criteria/minus/$i",
  281. 'wrapper' => 'tripal-pub-importer-setup-form',
  282. 'event' => 'click',
  283. 'method' => 'replace',
  284. ),
  285. '#attributes' => array('onClick' => 'return false;'),
  286. );
  287. }
  288. $form['criteria'][$i]["add-$i"] = array(
  289. '#type' => 'image_button',
  290. '#value' => t('Add'),
  291. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  292. '#ahah' => array(
  293. 'path' => "admin/tripal/tripal_pub/import/criteria/add/$i",
  294. 'wrapper' => 'tripal-pub-importer-setup-form',
  295. 'event' => 'click',
  296. 'method' => 'replace',
  297. ),
  298. '#attributes' => array('onClick' => 'return false;'),
  299. );
  300. }
  301. }
  302. $form['test'] = array(
  303. '#type' => 'submit',
  304. '#value' => t('Test Importer'),
  305. );
  306. $form['save'] = array(
  307. '#type' => 'submit',
  308. '#value' => t('Save Importer'),
  309. );
  310. /*
  311. $form['import'] = array(
  312. '#type' => 'submit',
  313. '#value' => t('Save & Import Now'),
  314. );*/
  315. $form['delete'] = array(
  316. '#type' => 'submit',
  317. '#value' => t('Delete Importer'),
  318. );
  319. return $form;
  320. }
  321. /**
  322. *
  323. */
  324. function tripal_pub_importer_setup_form_validate($form, &$form_state) {
  325. $num_criteria = $form_state['values']['num_criteria'];
  326. $remote_db = $form_state['values']["remote_db"];
  327. $days = trim($form_state['values']["days"]);
  328. $disabled = $form_state['values']["disabled"];
  329. $do_contact = $form_state['values']["do_contact"];
  330. $loader_name = trim($form_state['values']["loader_name"]);
  331. for ($i = 0; $i <= $num_criteria; $i++) {
  332. $search_terms = trim($form_state['values']["search_terms-$i"]);
  333. $scope = $form_state['values']["scope-$i"];
  334. $operation = $form_state['values']["operation-$i"];
  335. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  336. form_set_error("days-$i", "Please enter a numeric, non decimal value, for the number of days.");
  337. }
  338. }
  339. }
  340. /**
  341. *
  342. */
  343. function tripal_pub_importer_setup_form_submit($form, &$form_state) {
  344. $pub_import_id = $form_state['values']['pub_import_id'];
  345. $num_criteria = $form_state['values']['num_criteria'];
  346. $remote_db = $form_state['values']["remote_db"];
  347. $days = trim($form_state['values']["days"]);
  348. $loader_name = trim($form_state['values']["loader_name"]);
  349. $disabled = $form_state['values']["disabled"];
  350. $do_contact = $form_state['values']["do_contact"];
  351. // set the session variables
  352. $_SESSION['tripal_pub_search']['remote_db'] = $remote_db;
  353. $_SESSION['tripal_pub_search']['days'] = $days;
  354. $_SESSION['tripal_pub_search']['num_criteria'] = $num_criteria;
  355. $_SESSION['tripal_pub_search']['loader_name'] = $loader_name;
  356. $_SESSION['tripal_pub_search']['disabled'] = $disabled;
  357. $_SESSION['tripal_pub_search']['do_contact'] = $do_contact;
  358. unset($_SESSION['tripal_pub_search']['criteria']);
  359. for ($i = 0; $i <= $num_criteria; $i++) {
  360. $search_terms = trim($form_state['values']["search_terms-$i"]);
  361. $scope = $form_state['values']["scope-$i"];
  362. $operation = $form_state['values']["operation-$i"];
  363. $_SESSION['tripal_pub_search']['criteria'][$i] = array(
  364. 'search_terms' => $search_terms,
  365. 'scope' => $scope,
  366. 'operation' => $operation
  367. );
  368. }
  369. // now perform the appropriate action for the button clicked
  370. if ($form_state['values']['op'] == 'Test Importer') {
  371. $_SESSION['tripal_pub_search']['perform_search'] = 1;
  372. }
  373. if ($form_state['values']['op'] == 'Save Importer' or
  374. $form_state['values']['op'] == 'Save & Import Now') {
  375. $record = array(
  376. 'name' => $loader_name,
  377. 'criteria' => serialize($_SESSION['tripal_pub_search']),
  378. 'disabled' => $disabled,
  379. 'do_contact' => $do_contact
  380. );
  381. // first check to see if this pub_import_id is already present. If so,
  382. // do an update rather than an insert
  383. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = %d";
  384. $importer = db_fetch_object(db_query($sql, $pub_import_id));
  385. if($importer) {
  386. // do the update
  387. $record['pub_import_id'] = $pub_import_id;
  388. if(drupal_write_record('tripal_pub_import', $record, 'pub_import_id')){
  389. unset($_SESSION['tripal_pub_search']);
  390. drupal_set_message('Publication import settings updated.');
  391. drupal_goto('admin/tripal/tripal_pub/import_list');
  392. }
  393. else {
  394. drupal_set_message('Could not update publication import settings.', 'error');
  395. }
  396. }
  397. else {
  398. // do the insert
  399. if(drupal_write_record('tripal_pub_import', $record)){
  400. unset($_SESSION['tripal_pub_search']);
  401. drupal_set_message('Publication import settings saved.');
  402. // if the user wants to do the import now then do it (may time out
  403. // for long jobs)
  404. if ($form_state['values']['op'] == 'Save & Import Now') {
  405. tripal_pub_import_publications($record['pub_import_id']);
  406. }
  407. drupal_goto('admin/tripal/tripal_pub/import_list');
  408. }
  409. else {
  410. drupal_set_message('Could not save publication import settings.', 'error');
  411. }
  412. }
  413. }
  414. if ($form_state['values']['op'] == 'Delete Importer') {
  415. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = %d";
  416. $success = db_query($sql, $pub_import_id);
  417. if ($success) {
  418. drupal_set_message('Publication importer deleted.');
  419. drupal_goto('admin/tripal/tripal_pub/import_list');
  420. }
  421. else {
  422. drupal_set_message('Could not delete publication importer.', 'error');
  423. }
  424. }
  425. }
  426. /*
  427. *
  428. */
  429. function tripal_pub_importer_delete($pub_import_id) {
  430. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = %d";
  431. $success = db_query($sql, $pub_import_id);
  432. if ($success) {
  433. drupal_set_message('Publication importer deleted.');
  434. drupal_goto('admin/tripal/tripal_pub/import_list');
  435. }
  436. else {
  437. drupal_set_message('Could not delete publication importer.', 'error');
  438. }
  439. }
  440. /*
  441. * AHAH callback
  442. */
  443. function tripal_pub_importer_setup_page_update_criteria($action, $i) {
  444. $status = TRUE;
  445. // prepare and render the form
  446. $form = tripal_core_ahah_prepare_form();
  447. $data = theme('tripal_pub_importer_setup_form', $form);
  448. // bind javascript events to the new objects that will be returned
  449. // so that AHAH enabled elements will work.
  450. $settings = tripal_core_ahah_bind_events();
  451. // return the updated JSON
  452. drupal_json(
  453. array(
  454. 'status' => $status,
  455. 'data' => $data,
  456. 'settings' => $settings,
  457. )
  458. );
  459. }