pub_importers.inc 18 KB

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