tripal_pub.pub_importers.inc 30 KB

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