pub_importers.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <?php
  2. /**
  3. * A function to generate a table containing the list of publication importers
  4. *
  5. * @ingroup tripal_pub
  6. */
  7. function tripal_pub_importers_list() {
  8. // clear out the session variable when we view the list.
  9. unset($_SESSION['tripal_pub_import']);
  10. $header = array('', 'Importer Name', 'Database', 'Search String', 'Disabled', 'Create Contact', '');
  11. $rows = array();
  12. $importers = db_query("SELECT * FROM {tripal_pub_import} ORDER BY name");
  13. while ($importer = $importers->fetchObject()) {
  14. $criteria = unserialize($importer->criteria);
  15. $num_criteria = $criteria['num_criteria'];
  16. $criteria_str = '';
  17. for ($i = 1; $i <= $num_criteria; $i++) {
  18. $search_terms = $criteria['criteria'][$i]['search_terms'];
  19. $scope = $criteria['criteria'][$i]['scope'];
  20. $is_phrase = $criteria['criteria'][$i]['is_phrase'];
  21. $operation = $criteria['criteria'][$i]['operation'];
  22. $criteria_str .= "$operation ($scope: $search_terms) ";
  23. }
  24. $rows[] = array(
  25. l(t('Edit/Test'), "admin/tripal/chado/tripal_pub/import/edit/$importer->pub_import_id"),
  26. $importer->name,
  27. $criteria['remote_db'],
  28. $criteria_str,
  29. $importer->disabled ? 'Yes' : 'No',
  30. $importer->do_contact ? 'Yes' : 'No',
  31. l(t('Delete'), "admin/tripal/chado/tripal_pub/import/delete/$importer->pub_import_id"),
  32. );
  33. }
  34. $rows[] = array(
  35. 'data' => array(
  36. array('data' => l(t('Create a new publication importer.'), "admin/tripal/chado/tripal_pub/import/new"),
  37. 'colspan' => 7),
  38. )
  39. );
  40. $page = "<ul class='action-links'>";
  41. $page .= ' <li>' . l('New Importer', 'admin/tripal/chado/tripal_pub/import/new') . '</li>';
  42. $page .= '</ul>';
  43. $page .= theme('table', array('header' => $header, 'rows' => $rows));
  44. return $page;
  45. }
  46. /*
  47. *
  48. */
  49. function tripal_pub_importer_setup_page($action = 'new', $pub_import_id = NULL) {
  50. global $pager_total, $pager_total_items;
  51. $pager_id = 0;
  52. $limit = 20;
  53. // make sure the tripal_pub and tripal_contact ontologies are loaded
  54. $values = array('name' => 'tripal_pub');
  55. $tpub_cv = tripal_core_chado_select('cv', array('cv_id'), $values);
  56. if (count($tpub_cv) == 0) {
  57. drupal_set_message(t('Before importing publications you must first ') . l(t('load the Tripal Pub Ontology'), 'admin/tripal/tripal_cv/obo_loader'), 'error');
  58. }
  59. $values = array('name' => 'tripal_contact');
  60. $tpub_cv = tripal_core_chado_select('cv', array('cv_id'), $values);
  61. if (count($tpub_cv) == 0) {
  62. drupal_set_message(t('If you want to create contact pages for authors, you must first ') . l(t('load the Tripal Contact Ontology'), 'admin/tripal/tripal_cv/obo_loader'), 'error');
  63. }
  64. // generate the search form
  65. $form = drupal_get_form('tripal_pub_importer_setup_form', $pub_import_id, $action);
  66. $output = l("Return to publication importers list", "admin/tripal/chado/tripal_pub/import_list");
  67. $output .= drupal_render($form);
  68. /*
  69. // retrieve any results
  70. $remote_db = $_SESSION['tripal_pub_import']['remote_db'];
  71. $num_criteria = $_SESSION['tripal_pub_import']['num_criteria'];
  72. $days = $_SESSION['tripal_pub_import']['days'];
  73. $search_array = array();
  74. $search_array['remote_db'] = $remote_db;
  75. $search_array['num_criteria'] = $num_criteria;
  76. $search_array['days'] = $days;
  77. for ($i = 1; $i <= $num_criteria; $i++) {
  78. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'];
  79. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_import']['criteria'][$i]['scope'];
  80. $search_array['criteria'][$i]['is_phrase'] = $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'];
  81. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_import']['criteria'][$i]['operation'];
  82. }
  83. if ($_SESSION['tripal_pub_import']['perform_search']) {
  84. // get the list of publications from the remote database using the search criteria.
  85. $pubs = tripal_pub_get_remote_search_results($remote_db, $search_array, $limit, $pager_id);
  86. //dpm($pubs);
  87. // generate the pager
  88. $total_pages = $pager_total[$pager_id];
  89. $total_items = $pager_total_items[$pager_id];
  90. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  91. $pager = theme('pager');
  92. // iterate through the results and construct the table displaying the publications
  93. $rows = array();
  94. $i = $page * $limit + 1;
  95. if (count($pubs) > 0) {
  96. foreach ($pubs as $pub) {
  97. $citation = htmlspecialchars($pub['Citation']);
  98. $raw_link = '';
  99. if($pub['Publication Dbxref']) {
  100. $raw_link = l('raw', 'admin/tripal/chado/tripal_pub/import/raw/' . $pub['Publication Dbxref'], array('attributes' => array('target' => '_blank')));
  101. }
  102. $rows[] = array(
  103. number_format($i),
  104. $citation,
  105. $raw_link,
  106. );
  107. $i++;
  108. }
  109. }
  110. $headers = array('', 'Publication', '');
  111. $table = theme('table', array('header' => $headers, 'rows' => $rows));
  112. // join all to form the results
  113. $output .= "<br><p><b>Found " . number_format($total_items) .
  114. ". Page " . ($page + 1) . " of $total_pages. " .
  115. " Results</b></br>" . $table . '</p>' . $pager;
  116. }
  117. */
  118. return $output;
  119. }
  120. /**
  121. * Purpose: Provides the form to search pubmed
  122. *
  123. * @ingroup tripal_pub
  124. */
  125. function tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_id = NULL, $action = 'new') {
  126. // Default values can come in the following ways:
  127. //
  128. // 1) as elements of the $pub_importer object. This occurs when editing an existing importer
  129. // 2) in the $form_state['values'] array which occurs on a failed validation or
  130. // ajax callbacks from non submit form elements
  131. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  132. // form elements and the form is being rebuilt
  133. //
  134. // set form field defaults
  135. // Set the default values. If the pub_import_id isn't already defined by the form values
  136. // and one is provided then look it up in the database
  137. $criteria = NULL;
  138. $remote_db = '';
  139. $days = '';
  140. $disabled = '';
  141. $do_contact = '';
  142. $num_criteria = '';
  143. $loader_name = '';
  144. // if this is an edit the we are pulling an import object from the database
  145. if ($action == "edit") {
  146. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  147. $importer = db_query($sql, array(':pub_import_id' => $pub_import_id))->fetchObject();
  148. $criteria = unserialize($importer->criteria);
  149. $remote_db = $criteria['remote_db'];
  150. $days = $criteria['days'];
  151. $disabled = $criteria['disabled'];
  152. $do_contact = $criteria['do_contact'];
  153. $num_criteria = $criteria['num_criteria'];
  154. $loader_name = $criteria['loader_name'];
  155. }
  156. // if we are re constructing the form from a failed validation or ajax callback
  157. // then use the $form_state['values'] values
  158. if (array_key_exists('values', $form_state)) {
  159. $remote_db = $form_state['values']['remote_db'];
  160. $days = $form_state['values']['days'];
  161. $disabled = $form_state['values']['disabled'];
  162. $do_contact = $form_state['values']['do_contact'];
  163. $num_criteria = $form_state['values']['num_criteria'];
  164. $loader_name = $form_state['values']['loader_name'];
  165. }
  166. // if we are re building the form from after submission (from ajax call) then
  167. // the values are in the $form_state['input'] array
  168. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  169. $remote_db = $form_state['input']['remote_db'];
  170. $days = $form_state['input']['days'];
  171. $disabled = $form_state['input']['disabled'];
  172. $do_contact = $form_state['input']['do_contact'];
  173. $num_criteria = $form_state['input']['num_criteria'];
  174. $loader_name = $form_state['input']['loader_name'];
  175. }
  176. // check if the pub_import_id in the session variable is not the same as the one we've been provided
  177. // if so, then clear the session variable
  178. // if ($pub_import_id and $pub_import_id != $_SESSION['tripal_pub_import']['pub_import_id']) {
  179. // unset($_SESSION['tripal_pub_import']);
  180. // }
  181. // change the number of criteria based on form_state post data.
  182. if (!$num_criteria) {
  183. $num_criteria = 1;
  184. }
  185. /*
  186. if($form_state['post']["add-$num_criteria"]) {
  187. $num_criteria++;
  188. }
  189. if($form_state['post']["remove-$num_criteria"]) {
  190. $num_criteria--;
  191. }
  192. */
  193. $form['pub_import_id'] = array(
  194. '#type' => 'hidden',
  195. '#value' => $pub_import_id,
  196. '#required' => TRUE,
  197. );
  198. $form['action'] = array(
  199. '#type' => 'hidden',
  200. '#value' => $action,
  201. '#required' => TRUE,
  202. );
  203. $form['loader_name'] = array(
  204. '#type' => 'textfield',
  205. '#title' => t('Loader Name'),
  206. '#description' => t('Please provide a name for this loader setup..'),
  207. '#default_value' => $loader_name,
  208. '#required' => TRUE,
  209. );
  210. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  211. $remote_dbs = array();
  212. $values = array(
  213. 'name' => $supported_dbs,
  214. );
  215. $dbs = tripal_core_chado_select('db', array('*'), $values);
  216. foreach ($dbs as $index => $db) {
  217. $remote_dbs[$db->name] = $db->description;
  218. };
  219. // use PubMed as the default
  220. if (!$remote_db) {
  221. $remote_db = 'PMID';
  222. }
  223. $form['#prefix'] = '<div id="tripal-pubs-importer-setup">';
  224. $form['#suffix'] = '</div>';
  225. $form['remote_db'] = array(
  226. '#title' => t('Remote Database'),
  227. '#type' => 'select',
  228. '#options' => $remote_dbs,
  229. '#default_value' => $remote_db,
  230. '#ajax' => array(
  231. 'callback' => "tripal_pubs_setup_form_ajax_update",
  232. 'wrapper' => 'tripal-pubs-importer-setup',
  233. 'effect' => 'fade',
  234. 'method' => 'replace',
  235. ),
  236. );
  237. $form['num_criteria']= array(
  238. '#type' => 'hidden',
  239. '#default_value' => $num_criteria,
  240. );
  241. $form['pub_import_id']= array(
  242. '#type' => 'hidden',
  243. '#default_value' => $pub_import_id,
  244. );
  245. $form['days'] = array(
  246. '#type' => 'textfield',
  247. '#title' => t('Days since record modified'),
  248. '#description' => t('Limit the search to include pubs that have been added no more than this many days before today.'),
  249. '#default_value' => $days,
  250. '#size' => 5,
  251. );
  252. $form['disabled'] = array(
  253. '#type' => 'checkbox',
  254. '#title' => t('Disabled'),
  255. '#description' => t('Check to disable this importer.'),
  256. '#default_value' => $disabled,
  257. );
  258. $form['do_contact'] = array(
  259. '#type' => 'checkbox',
  260. '#title' => t('Create Contact'),
  261. '#description' => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of
  262. additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
  263. '#default_value' => $do_contact,
  264. );
  265. // add in the form for the criteria
  266. tripal_pub_importer_setup_add_criteria_fields($form, $form_state, $num_criteria, $criteria);
  267. $form['test'] = array(
  268. '#type' => 'submit',
  269. '#value' => t('Test Importer'),
  270. );
  271. $form['save'] = array(
  272. '#type' => 'submit',
  273. '#value' => t('Save Importer'),
  274. );
  275. /*
  276. $form['import'] = array(
  277. '#type' => 'submit',
  278. '#value' => t('Save & Import Now'),
  279. );
  280. */
  281. $form['delete'] = array(
  282. '#type' => 'submit',
  283. '#value' => t('Delete Importer'),
  284. );
  285. // allow the selected remote database to make changes to the form if needed
  286. $callback = "tripal_pub_remote_alter_form_$remote_db";
  287. $form = call_user_func($callback, $form, $form_state);
  288. $form['#theme'] = 'tripal_pub_importer_setup_form';
  289. return $form;
  290. }
  291. /**
  292. *
  293. * @param $form
  294. * @param $form_state
  295. * @param $num_criteria
  296. * @param $criteria
  297. */
  298. function tripal_pub_importer_setup_add_criteria_fields(&$form, &$form_state, $num_criteria, $criteria){
  299. // choices array
  300. $scope_choices = array(
  301. 'any' => 'Any Field',
  302. 'abstract' => 'Abstract',
  303. 'author' => 'Author',
  304. 'id' => 'Accession',
  305. 'title' => 'Title',
  306. 'journal' => 'Journal Name'
  307. );
  308. $first_op_choices = array(
  309. '' => '',
  310. 'NOT' => 'NOT'
  311. );
  312. $op_choices = array(
  313. 'AND' => 'AND',
  314. 'OR' => 'OR',
  315. 'NOT' => 'NOT'
  316. );
  317. for($i = 1; $i <= $num_criteria; $i++) {
  318. $is_phrase = 1;
  319. $search_terms = '';
  320. $scope = '';
  321. $is_phrase = '';
  322. $operation = '';
  323. // if we have criteria supplied from the database then use that as the initial defaults
  324. if ($criteria) {
  325. $search_terms = $criteria['criteria'][$i]['search_terms'];
  326. $scope = $criteria['criteria'][$i]['scope'];
  327. $is_phrase = $criteria['criteria'][$i]['is_phrase'];
  328. $operation = $criteria['criteria'][$i]['operation'];
  329. }
  330. /*
  331. // if we're here because the form was posted then load from the session variable (we lost the form state)
  332. $search_terms = isset($_SESSION['tripal_pub_import']['criteria'][$i]['search_terms']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'] : $search_terms;
  333. $scope = isset($_SESSION['tripal_pub_import']['criteria'][$i]['scope']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['scope'] : $scope;
  334. $is_phrase = isset($_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'] : $is_phrase;
  335. $operation = isset($_SESSION['tripal_pub_import']['criteria'][$i]['operation']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['operation'] : $operation;
  336. */
  337. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  338. // form is resbumitted using AJAX
  339. if (array_key_exists('values', $form_state)) {
  340. $search_terms = $form_state['values']["search_terms-$i"];
  341. $scope = $form_state['values']["scope-$i"];
  342. $is_phrase = $form_state['values']["is_phrase-$i"];
  343. $operation = $form_state['values']["operation-$i"];
  344. }
  345. $form['criteria'][$i]["scope-$i"] = array(
  346. '#type' => 'select',
  347. '#description' => t('Please select the fields to search for this term.'),
  348. '#options' => $scope_choices,
  349. '#default_value' => $scope,
  350. );
  351. $form['criteria'][$i]["search_terms-$i"] = array(
  352. '#type' => 'textfield',
  353. '#description' => t('<span style="white-space: normal">Please provide a list of words for searching. You may use
  354. conjunctions such as "AND" or "OR" to separate words if they are expected in
  355. the same scope, but do not mix ANDs and ORs. Uncheck the "Is Phrase" checkbox to use conjunctions</span>'),
  356. '#default_value' => $search_terms,
  357. '#required' => TRUE,
  358. '#maxlength' => 2048,
  359. );
  360. $form['criteria'][$i]["is_phrase-$i"] = array(
  361. '#type' => 'checkbox',
  362. '#title' => t('Is Phrase?'),
  363. '#default_value' => $is_phrase,
  364. );
  365. if ($i == 1) {
  366. /*
  367. $form['criteria'][$i]["operation-$i"] = array(
  368. '#type' => 'select',
  369. '#options' => $first_op_choices,
  370. '#default_value' => $operation,
  371. );*/
  372. }
  373. if ($i > 1) {
  374. $form['criteria'][$i]["operation-$i"] = array(
  375. '#type' => 'select',
  376. '#options' => $op_choices,
  377. '#default_value' => $operation,
  378. );
  379. }
  380. if ($i == $num_criteria) {
  381. if($i > 1) {
  382. $form['criteria'][$i]["remove-$i"] = array(
  383. '#type' => 'button',
  384. '#name' => 'remove',
  385. '#value' => t('Remove'),
  386. '#ajax' => array(
  387. 'callback' => "tripal_pubs_setup_form_ajax_update",
  388. 'wrapper' => 'tripal-pubs-importer-setup',
  389. 'effect' => 'fade',
  390. 'method' => 'replace',
  391. 'prevent' => 'click'
  392. ),
  393. // When this button is clicked, the form will be validated and submitted.
  394. // Therefore, we set custom submit and validate functions to override the
  395. // default form submit. In the validate function we set the form_state
  396. // to rebuild the form so the submit function never actually gets called,
  397. // but we need it or Drupal will run the default validate anyway.
  398. // we also set #limit_validation_errors to empty so fields that
  399. // are required that don't have values won't generate warnings.
  400. '#submit' => array('tripal_pub_setup_form_criteria_button_submit'),
  401. '#validate' => array('tripal_pub_setup_form_criteria_button_validate'),
  402. '#limit_validation_errors' => array(),
  403. );
  404. }
  405. $form['criteria'][$i]["add-$i"] = array(
  406. '#type' => 'button',
  407. '#name' => 'add',
  408. '#value' => t('Add'),
  409. '#ajax' => array(
  410. 'callback' => "tripal_pubs_setup_form_ajax_update",
  411. 'wrapper' => 'tripal-pubs-importer-setup',
  412. 'effect' => 'fade',
  413. 'method' => 'replace',
  414. 'prevent' => 'click'
  415. ),
  416. // When this button is clicked, the form will be validated and submitted.
  417. // Therefore, we set custom submit and validate functions to override the
  418. // default form submit. In the validate function we set the form_state
  419. // to rebuild the form so the submit function never actually gets called,
  420. // but we need it or Drupal will run the default validate anyway.
  421. // we also set #limit_validation_errors to empty so fields that
  422. // are required that don't have values won't generate warnings.
  423. '#submit' => array('tripal_pub_setup_form_criteria_button_submit'),
  424. '#validate' => array('tripal_pub_setup_form_criteria_button_validate'),
  425. '#limit_validation_errors' => array(),
  426. );
  427. }
  428. }
  429. }
  430. /**
  431. * This function is used to rebuild the form if an ajax call is made vai a button.
  432. * The button causes the form to be submitted. We don't want this so we override
  433. * the validate and submit routines on the form button. Therefore, this function
  434. * only needs to tell Drupal to rebuild the form
  435. */
  436. function tripal_pub_setup_form_criteria_button_validate($form, &$form_state){
  437. $form_state['rebuild'] = TRUE;
  438. }
  439. /**
  440. * This function is just a dummy to override the default form submit on ajax calls for buttons
  441. */
  442. function tripal_pub_setup_form_criteria_button_submit($form, &$form_state){
  443. // do nothing
  444. }
  445. /**
  446. *
  447. */
  448. function tripal_pub_importer_setup_form_validate($form, &$form_state) {
  449. $num_criteria = $form_state['values']['num_criteria'];
  450. $remote_db = $form_state['values']["remote_db"];
  451. $days = trim($form_state['values']["days"]);
  452. $disabled = $form_state['values']["disabled"];
  453. $do_contact = $form_state['values']["do_contact"];
  454. $loader_name = trim($form_state['values']["loader_name"]);
  455. for ($i = 1; $i <= $num_criteria; $i++) {
  456. $search_terms = trim($form_state['values']["search_terms-$i"]);
  457. $scope = $form_state['values']["scope-$i"];
  458. $is_phrase = $form_state['values']["is_phrase-$i"];
  459. $operation = $form_state['values']["operation-$i"];
  460. if (!$is_phrase) {
  461. if (preg_match('/and/i', $search_terms) and preg_match('/or/i', $search_terms)) {
  462. form_set_error("search_terms-$i", "You may use 'AND' or 'OR' but cannot use both. Add a new entry below with the same scope for the other conunction.");
  463. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  464. }
  465. }
  466. }
  467. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  468. form_set_error("days", "Please enter a numeric, non decimal value, for the number of days.");
  469. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  470. }
  471. // allow the selected remote database to validate any changes to the form if needed
  472. $callback = "tripal_pub_remote_validate_form_$remote_db";
  473. $form = call_user_func($callback, $form, $form_state);
  474. }
  475. /**
  476. *
  477. */
  478. function tripal_pub_importer_setup_form_submit($form, &$form_state) {
  479. $pub_import_id = $form_state['values']['pub_import_id'];
  480. $num_criteria = $form_state['values']['num_criteria'];
  481. $remote_db = $form_state['values']["remote_db"];
  482. $days = trim($form_state['values']["days"]);
  483. $loader_name = trim($form_state['values']["loader_name"]);
  484. $disabled = $form_state['values']["disabled"];
  485. $do_contact = $form_state['values']["do_contact"];
  486. // set the session variables
  487. $_SESSION['tripal_pub_import']['remote_db'] = $remote_db;
  488. $_SESSION['tripal_pub_import']['days'] = $days;
  489. $_SESSION['tripal_pub_import']['num_criteria'] = $num_criteria;
  490. $_SESSION['tripal_pub_import']['loader_name'] = $loader_name;
  491. $_SESSION['tripal_pub_import']['disabled'] = $disabled;
  492. $_SESSION['tripal_pub_import']['do_contact'] = $do_contact;
  493. $_SESSION['tripal_pub_import']['pub_import_id'] = $pub_import_id;
  494. unset($_SESSION['tripal_pub_import']['criteria']);
  495. for ($i = 1; $i <= $num_criteria; $i++) {
  496. $search_terms = trim($form_state['values']["search_terms-$i"]);
  497. $scope = $form_state['values']["scope-$i"];
  498. $is_phrase = $form_state['values']["is_phrase-$i"];
  499. $operation = $form_state['values']["operation-$i"];
  500. $_SESSION['tripal_pub_import']['criteria'][$i] = array(
  501. 'search_terms' => $search_terms,
  502. 'scope' => $scope,
  503. 'is_phrase' => $is_phrase,
  504. 'operation' => $operation
  505. );
  506. }
  507. // now perform the appropriate action for the button clicked
  508. if ($form_state['values']['op'] == 'Test Importer') {
  509. $_SESSION['tripal_pub_import']['perform_search'] = 1;
  510. }
  511. if ($form_state['values']['op'] == 'Save Importer' or
  512. $form_state['values']['op'] == 'Save & Import Now') {
  513. $record = array(
  514. 'name' => $loader_name,
  515. 'criteria' => serialize($_SESSION['tripal_pub_import']),
  516. 'disabled' => $disabled,
  517. 'do_contact' => $do_contact
  518. );
  519. // first check to see if this pub_import_id is already present. If so,
  520. // do an update rather than an insert
  521. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  522. $importer = db_query($sql, array(':pub_import_id' => $pub_import_id))->fetchObject();
  523. if($importer) {
  524. // do the update
  525. $record['pub_import_id'] = $pub_import_id;
  526. if(drupal_write_record('tripal_pub_import', $record, 'pub_import_id')){
  527. unset($_SESSION['tripal_pub_import']);
  528. drupal_set_message('Publication import settings updated.');
  529. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  530. }
  531. else {
  532. drupal_set_message('Could not update publication import settings.', 'error');
  533. }
  534. }
  535. else {
  536. // do the insert
  537. if(drupal_write_record('tripal_pub_import', $record)){
  538. unset($_SESSION['tripal_pub_import']);
  539. drupal_set_message('Publication import settings saved.');
  540. // if the user wants to do the import now then do it (may time out
  541. // for long jobs)
  542. if ($form_state['values']['op'] == 'Save & Import Now') {
  543. tripal_pub_import_publications($record['pub_import_id']);
  544. }
  545. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  546. }
  547. else {
  548. drupal_set_message('Could not save publication import settings.', 'error');
  549. }
  550. }
  551. }
  552. if ($form_state['values']['op'] == 'Delete Importer') {
  553. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  554. $success = db_query($sql, array(':pub_import_id' => $pub_import_id));
  555. if ($success) {
  556. drupal_set_message('Publication importer deleted.');
  557. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  558. }
  559. else {
  560. drupal_set_message('Could not delete publication importer.', 'error');
  561. }
  562. }
  563. }
  564. /**
  565. * AJAX callback for updating the form. Because we are replacing
  566. * the entire form we just need to return it
  567. */
  568. function tripal_pubs_setup_form_ajax_update($form, $form_state) {
  569. dpm('Hi');
  570. return $form;
  571. }
  572. /*
  573. *
  574. */
  575. function tripal_pub_importer_delete($pub_import_id) {
  576. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  577. $success = db_query($sql, array(':pub_import_id' => $pub_import_id));
  578. if ($success) {
  579. drupal_set_message('Publication importer deleted.');
  580. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  581. }
  582. else {
  583. drupal_set_message('Could not delete publication importer.', 'error');
  584. }
  585. }
  586. /*
  587. * AHAH callback
  588. */
  589. function tripal_pub_importer_setup_page_update_remotedb() {
  590. $status = TRUE;
  591. // prepare and render the form
  592. $form = tripal_core_ahah_prepare_form();
  593. $data = theme('tripal_pub_importer_setup_form', $form);
  594. // bind javascript events to the new objects that will be returned
  595. // so that AHAH enabled elements will work.
  596. $settings = tripal_core_ahah_bind_events();
  597. // return the updated JSON
  598. drupal_json(
  599. array(
  600. 'status' => $status,
  601. 'data' => $data,
  602. 'settings' => $settings,
  603. )
  604. );
  605. }
  606. /*
  607. * AHAH callback
  608. */
  609. function tripal_pub_importer_setup_page_update_criteria($action, $i) {
  610. $status = TRUE;
  611. // prepare and render the form
  612. $form = tripal_core_ahah_prepare_form();
  613. $data = theme('tripal_pub_importer_setup_form', $form);
  614. // bind javascript events to the new objects that will be returned
  615. // so that AHAH enabled elements will work.
  616. $settings = tripal_core_ahah_bind_events();
  617. // return the updated JSON
  618. drupal_json(
  619. array(
  620. 'status' => $status,
  621. 'data' => $data,
  622. 'settings' => $settings,
  623. )
  624. );
  625. }
  626. /**
  627. *
  628. * @param $form
  629. */
  630. function theme_tripal_pub_importer_setup_form($variables) {
  631. $form = $variables['form'];
  632. $rows = array();
  633. foreach ($form['criteria'] as $i => $element) {
  634. if(is_numeric($i)) {
  635. $rows[] = array(
  636. drupal_render($element["operation-$i"]),
  637. drupal_render($element["scope-$i"]),
  638. drupal_render($element["search_terms-$i"]),
  639. drupal_render($element["is_phrase-$i"]),
  640. drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  641. );
  642. }
  643. }
  644. // now build the table containing the criteria elements
  645. $headers = array('Operation','Scope', 'Search Terms', '','');
  646. $table = array(
  647. 'header' => $headers,
  648. 'rows' => $rows,
  649. 'attributes' => array(),
  650. 'sticky' => TRUE,
  651. 'caption' => '',
  652. 'colgroups' => array(),
  653. 'empty' => '',
  654. );
  655. $criteria_table = theme_table($table);
  656. $markup = '';
  657. $markup .= '<div>' . drupal_render($form['remote_db']) . '</div>';
  658. $markup .= '<div id="pub-search-form-row1">';
  659. $markup .= ' <div id="pub-search-form-col1">' . drupal_render($form['loader_name']) . '</div>';
  660. $markup .= ' <div id="pub-search-form-col3">' . drupal_render($form['days']) . '</div>';
  661. $markup .= '</div>';
  662. $markup .= '<div id="pub-search-form-row2">' . drupal_render($form['disabled']) . '</div>';
  663. $markup .= '<div id="pub-search-form-row3">' . drupal_render($form['do_contact']) . '</div>';
  664. $markup .= $criteria_table;
  665. $markup .= drupal_render($form['test']);
  666. $markup .= drupal_render($form['save']);
  667. $markup .= drupal_render($form['delete']);
  668. return $markup;
  669. }