pub_importers.inc 18 KB

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