tripal_pub.admin.inc 17 KB

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