pub_importers.inc 19 KB

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