tripal_pub.pub_importers.inc 31 KB

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