pub_importers.inc 19 KB

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