tripal_chado.pub_importers.inc 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. <?php
  2. /**
  3. * @file
  4. * Management of importers
  5. */
  6. require_once('tripal_chado.pub_importer_AGL.inc');
  7. require_once('tripal_chado.pub_importer_PMID.inc');
  8. /**
  9. * A function to generate a table containing the list of publication importers
  10. *
  11. * @ingroup tripal_pub
  12. */
  13. function tripal_pub_importers_list() {
  14. // Check to make sure that the tripal_pub vocabulary is loaded. If not, then
  15. // warn the user that they should load it before continuing.
  16. $pub_cv = chado_select_record('cv', ['cv_id'], ['name' => 'tripal_pub']);
  17. if (count($pub_cv) == 0) {
  18. drupal_set_message(t('The Tripal Pub vocabulary is currently not loaded. ' .
  19. 'This vocabulary is required to be loaded before importing of ' .
  20. 'publications. <br>Please !import',
  21. ['!import' => l('load the Tripal Publication vocabulary', 'aadmin/tripal/loaders/chado_vocabs/obo_loader')]), 'warning');
  22. }
  23. // clear out the session variable when we view the list.
  24. unset($_SESSION['tripal_pub_import']);
  25. $headers = [
  26. '',
  27. 'Importer Name',
  28. 'Database',
  29. 'Search String',
  30. 'Disabled',
  31. 'Create Contact',
  32. '',
  33. ];
  34. $rows = [];
  35. $importers = db_query("SELECT * FROM {tripal_pub_import} ORDER BY name");
  36. while ($importer = $importers->fetchObject()) {
  37. $criteria = unserialize($importer->criteria);
  38. $num_criteria = $criteria['num_criteria'];
  39. $criteria_str = '';
  40. for ($i = 1; $i <= $num_criteria; $i++) {
  41. $search_terms = $criteria['criteria'][$i]['search_terms'];
  42. $scope = $criteria['criteria'][$i]['scope'];
  43. $is_phrase = $criteria['criteria'][$i]['is_phrase'];
  44. $operation = $criteria['criteria'][$i]['operation'];
  45. $criteria_str .= "$operation ($scope: $search_terms) ";
  46. }
  47. $rows[] = [
  48. [
  49. 'data' => l(t('Edit/Test'), "admin/tripal/loaders/pub/edit/$importer->pub_import_id") . '<br>' .
  50. l(t('Import Pubs'), "admin/tripal/loaders/pub/submit/$importer->pub_import_id"),
  51. 'nowrap' => 'nowrap',
  52. ],
  53. $importer->name,
  54. $criteria['remote_db'],
  55. $criteria_str,
  56. $importer->disabled ? 'Yes' : 'No',
  57. $importer->do_contact ? 'Yes' : 'No',
  58. l(t('Delete'), "admin/tripal/loaders/pub/delete/$importer->pub_import_id"),
  59. ];
  60. }
  61. $page = "<ul class='action-links'>";
  62. $page .= ' <li>' . l('New Importer', 'admin/tripal/loaders/pub/new') . '</li>';
  63. $page .= '</ul>';
  64. $page .= '<p>' . t(
  65. "A publication importer is used to create a set of search criteria that can be used
  66. to query a remote database, find publications that match the specified criteria
  67. and then import those publications into the Chado database. An example use case would
  68. be to peridocially add new publications to this Tripal site that have appeared in PubMed
  69. in the last 30 days. You can import publications in one of two ways:
  70. <ol>
  71. <li>Create a new importer by clicking the 'New Importer' link above, and after saving it should appear in the list below. Click the
  72. link labeled 'Import Pubs' to schedule a job to import the publications</li>
  73. <li>The first method only performs the import once. However, you can schedule the
  74. importer to run peridically by adding a cron job. </li>
  75. </ol><br>");
  76. $form = drupal_get_form('tripal_pub_importer_ncbi_api_key_form');
  77. $page .= drupal_render($form);
  78. $table = [
  79. 'header' => $headers,
  80. 'rows' => $rows,
  81. 'attributes' => [
  82. ],
  83. 'caption' => '',
  84. 'sticky' => TRUE,
  85. 'colgroups' => [],
  86. 'empty' => 'There are no currently importers',
  87. ];
  88. $page .= theme_table($table);
  89. return $page;
  90. }
  91. /**
  92. * Creates the page that contains the publication importer setup form and
  93. * test results.
  94. *
  95. * @param $action
  96. * The action to perform
  97. * @param $pub_import_id
  98. * The importer ID
  99. *
  100. * @return
  101. * The HTML for the importer setup page
  102. *
  103. * @ingroup tripal_pub
  104. */
  105. function tripal_pub_importer_setup_page($action = 'new', $pub_import_id = NULL) {
  106. global $base_path;
  107. // make sure the tripal_pub and tripal_contact ontologies are loaded
  108. $values = ['name' => 'tripal_pub'];
  109. $tpub_cv = chado_select_record('cv', ['cv_id'], $values);
  110. if (count($tpub_cv) == 0) {
  111. drupal_set_message(t('Before importing publications you must first ') . l(t('load the Tripal Pub Ontology'), 'admin/tripal/loaders/chado_vocabs/obo_loader'), 'error');
  112. }
  113. $values = ['name' => 'tripal_contact'];
  114. $tpub_cv = chado_select_record('cv', ['cv_id'], $values);
  115. if (count($tpub_cv) == 0) {
  116. 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/loaders/chado_vocabs/obo_loader'), 'error');
  117. }
  118. /**
  119. * Commenting the AGL message out until we get AGL working again.
  120. * spf 6/22/2018
  121. * if(!extension_loaded ('yaz')){
  122. * 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/legacy/tripal_pub/help') . ' for assistance. If you do not want to use AGL you can ignore this warning.', 'warning');
  123. * }
  124. */
  125. // generate the search form
  126. $form = drupal_get_form('tripal_pub_importer_setup_form', $pub_import_id, $action);
  127. $output = l("Return to publication importers list", "admin/tripal/loaders/pub");
  128. $output .= drupal_render($form);
  129. // retrieve any results
  130. if (array_key_exists('tripal_pub_import', $_SESSION)) {
  131. $remote_db = array_key_exists('remote_db', $_SESSION['tripal_pub_import']) ? $_SESSION['tripal_pub_import']['remote_db'] : '';
  132. $num_criteria = array_key_exists('num_criteria', $_SESSION['tripal_pub_import']) ? $_SESSION['tripal_pub_import']['num_criteria'] : '';
  133. $days = array_key_exists('days', $_SESSION['tripal_pub_import']) ? $_SESSION['tripal_pub_import']['days'] : '';
  134. $search_array = [];
  135. $search_array['remote_db'] = $remote_db;
  136. $search_array['num_criteria'] = $num_criteria;
  137. $search_array['days'] = $days;
  138. for ($i = 1; $i <= $num_criteria; $i++) {
  139. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'];
  140. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_import']['criteria'][$i]['scope'];
  141. $search_array['criteria'][$i]['is_phrase'] = $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'];
  142. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_import']['criteria'][$i]['operation'];
  143. }
  144. // if the form has been submitted with the 'test' button then get the results
  145. if ($_SESSION['tripal_pub_import']['perform_search']) {
  146. $limit = 25;
  147. // get the list of publications from the remote database using the search criteria.
  148. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  149. $results = tripal_get_remote_pubs($remote_db, $search_array, $limit, $page);
  150. $total_records = $results['total_records'];
  151. $search_str = $results['search_str'];
  152. $pubs = $results['pubs'];
  153. // iterate through the results and construct the table displaying the publications
  154. $rows = [];
  155. $i = $page * $limit + 1;
  156. if (count($pubs) > 0) {
  157. foreach ($pubs as $pub) {
  158. $citation = array_key_exists('Citation', $pub) ? htmlspecialchars($pub['Citation']) : 'Unable to generate citation';
  159. $raw_link = '';
  160. if (array_key_exists('Publication Dbxref', $pub) and $pub['Publication Dbxref']) {
  161. $raw_link = l('raw', 'admin/tripal/loaders/pub/raw/' . $pub['Publication Dbxref'], ['attributes' => ['target' => '_blank']]);
  162. }
  163. $rows[] = [
  164. number_format($i),
  165. $citation,
  166. $raw_link,
  167. ];
  168. $i++;
  169. }
  170. }
  171. if (count($rows) == 0) {
  172. $rows[] = [
  173. [
  174. 'data' => 'No results found',
  175. 'colspan' => 3,
  176. ],
  177. ];
  178. }
  179. $headers = ['', 'Publication', 'Raw Results'];
  180. $table = [
  181. 'header' => $headers,
  182. 'rows' => $rows,
  183. 'attributes' => [
  184. 'id' => 'tripal_pub-importer-test',
  185. 'class' => 'tripal-data-table',
  186. ],
  187. 'sticky' => FALSE,
  188. 'caption' => '',
  189. 'colgroups' => [],
  190. 'empty' => '',
  191. ];
  192. // once we have our table array structure defined, we call Drupal's theme_table()
  193. // function to generate the table.
  194. $table = theme_table($table);
  195. // generate the pager
  196. pager_default_initialize($total_records, $limit);
  197. $pager = [
  198. 'tags' => [],
  199. 'element' => 0,
  200. 'parameters' => [],
  201. 'quantity' => $limit,
  202. ];
  203. $pager = theme_pager($pager);
  204. // because this is an ajax callback, the theme_pager will set the URL to be
  205. // "system/ajax", so we need to reset that
  206. $pager = str_replace($base_path . "system/ajax", "", $pager);
  207. // join all to form the results
  208. $total_pages = (int) ($total_records / $limit) + 1;
  209. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  210. $output .= "$pager<br><b>Found " . number_format($total_records) . " publications. Page " . ($page + 1) . " of $total_pages.</b> " .
  211. "<br>$remote_db Search String: $search_str $table<br>$pager";
  212. }
  213. }
  214. return $output;
  215. }
  216. /**
  217. * The form used for creating publication importers.
  218. *
  219. * @param $form
  220. * The Drupal form
  221. * @param $form_state
  222. * The form state
  223. * @param $pub_import_id
  224. * The publication importer ID
  225. * @param $action
  226. * The action to perform
  227. *
  228. * @return
  229. * A form array
  230. *
  231. * @ingroup tripal_pub
  232. */
  233. function tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_id = NULL, $action = 'new') {
  234. // Default values can come in the following ways:
  235. //
  236. // 1) as elements of the $pub_importer object. This occurs when editing an existing importer
  237. // 2) in the $form_state['values'] array which occurs on a failed validation or
  238. // ajax callbacks from non submit form elements
  239. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  240. // form elements and the form is being rebuilt
  241. //
  242. // set form field defaults
  243. // Set the default values. If the pub_import_id isn't already defined by the form values
  244. // and one is provided then look it up in the database
  245. $criteria = NULL;
  246. $remote_db = '';
  247. $days = '';
  248. $disabled = '';
  249. $do_contact = '';
  250. $num_criteria = 1;
  251. $loader_name = '';
  252. // if this is an edit the we are pulling an import object from the database
  253. if ($action == "edit") {
  254. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  255. $importer = db_query($sql, [':pub_import_id' => $pub_import_id])->fetchObject();
  256. $criteria = unserialize($importer->criteria);
  257. $remote_db = $criteria['remote_db'];
  258. $days = $criteria['days'];
  259. $disabled = $criteria['disabled'];
  260. $do_contact = $criteria['do_contact'];
  261. $num_criteria = $criteria['num_criteria'];
  262. $loader_name = $criteria['loader_name'];
  263. }
  264. // if there are any session variables then use those
  265. if (array_key_exists('tripal_pub_import', $_SESSION)) {
  266. $remote_db = $_SESSION['tripal_pub_import']['remote_db'];
  267. $days = $_SESSION['tripal_pub_import']['days'];
  268. $disabled = $_SESSION['tripal_pub_import']['disabled'];
  269. $do_contact = $_SESSION['tripal_pub_import']['do_contact'];
  270. $num_criteria = $_SESSION['tripal_pub_import']['num_criteria'];
  271. $loader_name = $_SESSION['tripal_pub_import']['loader_name'];
  272. // check if the pub_import_id in the session variable is not the same as the one we've been provided
  273. // if so, then clear the session variable
  274. if ($pub_import_id and $pub_import_id != $_SESSION['tripal_pub_import']['pub_import_id']) {
  275. unset($_SESSION['tripal_pub_import']);
  276. }
  277. }
  278. // if we are re constructing the form from a failed validation or ajax callback
  279. // then use the $form_state['values'] values
  280. if (array_key_exists('values', $form_state)) {
  281. $remote_db = $form_state['values']['remote_db'];
  282. $days = $form_state['values']['days'];
  283. $disabled = $form_state['values']['disabled'];
  284. $do_contact = $form_state['values']['do_contact'];
  285. $num_criteria = $form_state['values']['num_criteria'];
  286. $loader_name = $form_state['values']['loader_name'];
  287. }
  288. // if we are re building the form from after submission (from ajax call) then
  289. // the values are in the $form_state['input'] array
  290. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  291. $remote_db = $form_state['input']['remote_db'];
  292. $days = $form_state['input']['days'];
  293. $disabled = $form_state['input']['disabled'];
  294. $do_contact = $form_state['input']['do_contact'];
  295. $loader_name = $form_state['input']['loader_name'];
  296. // because the num_criteria is a value and not a visible or hidden form
  297. // element it is not part of the ['input'] array, so we need to get it from the form
  298. $num_criteria = $form_state['complete form']['num_criteria']['#value'];
  299. }
  300. if (array_key_exists('triggering_element', $form_state) and
  301. $form_state['triggering_element']['#name'] == 'add') {
  302. $num_criteria++;
  303. }
  304. if (array_key_exists('triggering_element', $form_state) and
  305. $form_state['triggering_element']['#name'] == 'remove') {
  306. $num_criteria--;
  307. }
  308. // set the values we need for later but that should not be shown on the form
  309. $form['num_criteria'] = [
  310. '#type' => 'value',
  311. '#value' => $num_criteria,
  312. ];
  313. $form['pub_import_id'] = [
  314. '#type' => 'value',
  315. '#value' => $pub_import_id,
  316. ];
  317. $form['action'] = [
  318. '#type' => 'value',
  319. '#value' => $action,
  320. ];
  321. // add in the elements that will be organized via a theme function
  322. $form['themed_element']['loader_name'] = [
  323. '#type' => 'textfield',
  324. '#title' => t('Loader Name'),
  325. '#description' => t('Please provide a name for this loader setup.'),
  326. '#default_value' => $loader_name,
  327. '#required' => TRUE,
  328. ];
  329. $supported_dbs = variable_get('tripal_pub_supported_dbs', ['PMID']);
  330. $remote_dbs = [];
  331. $values = [
  332. 'name' => $supported_dbs,
  333. ];
  334. $dbs = chado_select_record('db', ['*'], $values);
  335. foreach ($dbs as $index => $db) {
  336. $remote_dbs[$db->name] = $db->description ? $db->description : $db->name;
  337. };
  338. // use PubMed as the default
  339. if (!$remote_db) {
  340. $remote_db = 'PMID';
  341. }
  342. /**
  343. * Removing AGL option until we get it fixed
  344. * spf 6/22/2018
  345. */
  346. unset($remote_dbs['AGL']);
  347. $form['themed_element']['remote_db'] = [
  348. '#title' => t('Source'),
  349. '#type' => 'select',
  350. '#options' => $remote_dbs,
  351. '#default_value' => $remote_db,
  352. '#ajax' => [
  353. 'callback' => "tripal_pubs_setup_form_ajax_update",
  354. 'wrapper' => 'tripal-pubs-importer-setup',
  355. 'effect' => 'fade',
  356. 'method' => 'replace',
  357. ],
  358. ];
  359. $form['themed_element']['days'] = [
  360. '#type' => 'textfield',
  361. '#title' => t('Days since record modified'),
  362. '#description' => t('Limit the search to include pubs that have been added no more than this many days before today.'),
  363. '#default_value' => $days,
  364. '#size' => 5,
  365. ];
  366. $form['themed_element']['disabled'] = [
  367. '#type' => 'checkbox',
  368. '#title' => t('Disabled'),
  369. '#description' => t('Check to disable this importer.'),
  370. '#default_value' => $disabled,
  371. ];
  372. $form['themed_element']['do_contact'] = [
  373. '#type' => 'checkbox',
  374. '#title' => t('Create Contact'),
  375. '#description' => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of
  376. additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
  377. '#default_value' => $do_contact,
  378. ];
  379. // add in the form for the criteria
  380. tripal_pub_importer_setup_add_criteria_fields($form, $form_state, $num_criteria, $criteria);
  381. // add in the buttons
  382. $form['save'] = [
  383. '#type' => 'submit',
  384. '#value' => t('Save Importer'),
  385. ];
  386. $form['test'] = [
  387. '#type' => 'submit',
  388. '#value' => t('Test Importer'),
  389. ];
  390. $form['delete'] = [
  391. '#type' => 'submit',
  392. '#value' => t('Delete Importer'),
  393. '#attributes' => ['style' => 'float: right;'],
  394. ];
  395. // add in the section where the test results will appear
  396. $form['results'] = [
  397. '#markup' => '<div id="tripal-pub-importer-test-section"></div>',
  398. ];
  399. // allow the selected remote database to make changes to the form if needed
  400. $callback = "tripal_pub_remote_alter_form_$remote_db";
  401. $form = call_user_func($callback, $form, $form_state, $num_criteria);
  402. $form['themed_element']['#theme'] = 'tripal_pub_importer_setup_form_elements';
  403. return $form;
  404. }
  405. /**
  406. * The form used for setting the optional NCBI API key.
  407. *
  408. * @param $form
  409. * The form element to be populated.
  410. * @param $form_state
  411. * The state of the form element to be populated.
  412. *
  413. * @return array
  414. * The populated form element.
  415. *
  416. * @ingroup tripal_pub
  417. */
  418. function tripal_pub_importer_ncbi_api_key_form($form, $form_state) {
  419. $description = t('Tripal imports publications using NCBI\'s ')
  420. . l('EUtils API', 'https://www.ncbi.nlm.nih.gov/books/NBK25500/')
  421. . t(', which limits users and programs to a maximum of 3 requests per second without an API key. '
  422. . 'However, NCBI allows users and programs to an increased maximum of 10 requests per second if '
  423. . 'they provide a valid API key. This is particularly useful in speeding up large publication imports. '
  424. . 'For more information on NCBI API keys, please ')
  425. . l('see here', 'https://www.ncbi.nlm.nih.gov/books/NBK25497/#chapter2.Coming_in_December_2018_API_Key', array(
  426. 'attributes' => array(
  427. 'target' => 'blank',
  428. ),
  429. )) . '.';
  430. $form['ncbi_api_key'] = array(
  431. '#type' => 'textfield',
  432. '#title' => t('(Optional) NCBI API key:'),
  433. '#description' => $description,
  434. '#default_value' => variable_get('tripal_pub_importer_ncbi_api_key', NULL),
  435. '#ajax' => array(
  436. 'callback' => 'tripal_pub_importer_set_ncbi_api_key',
  437. 'wrapper' => 'ncbi_api_key',
  438. ),
  439. '#prefix' => '<div id="ncbi_api_key">',
  440. '#suffix' => '</div>',
  441. );
  442. return $form;
  443. }
  444. /**
  445. * This function saves the NCBI API key to the database.
  446. *
  447. * It is called when the user makes a change to the NCBI API key field and then
  448. * moves their cursor out of the field.
  449. *
  450. * @param $form
  451. * The new form element.
  452. * @param $form_state
  453. * The state of the new form element.
  454. *
  455. * @return array
  456. * The new api key field.
  457. *
  458. * @ingroup tripal_pub
  459. */
  460. function tripal_pub_importer_set_ncbi_api_key($form, $form_state) {
  461. variable_set('tripal_pub_importer_ncbi_api_key', check_plain($form_state['values']['ncbi_api_key']));
  462. drupal_set_message('NCBI API key has been saved successfully!');
  463. return $form['ncbi_api_key'];
  464. }
  465. /**
  466. * A helper function for the importer setup form that adds the criteria to
  467. * the form that belong to the importer.
  468. *
  469. * @param $form
  470. * The form
  471. * @param $form_state
  472. * The form state
  473. * @param $num_criteria
  474. * The number of criteria that exist for the importer
  475. * @param $criteria
  476. * An array containing the criteria
  477. *
  478. * @return
  479. * A form array
  480. *
  481. * @ingroup tripal_pub
  482. */
  483. function tripal_pub_importer_setup_add_criteria_fields(&$form, &$form_state, $num_criteria, $criteria) {
  484. // choices array
  485. $scope_choices = [
  486. 'any' => 'Any Field',
  487. 'abstract' => 'Abstract',
  488. 'author' => 'Author',
  489. 'id' => 'Accession',
  490. 'title' => 'Title',
  491. 'journal' => 'Journal Name',
  492. ];
  493. $first_op_choices = [
  494. '' => '',
  495. 'NOT' => 'NOT',
  496. ];
  497. $op_choices = [
  498. 'AND' => 'AND',
  499. 'OR' => 'OR',
  500. 'NOT' => 'NOT',
  501. ];
  502. for ($i = 1; $i <= $num_criteria; $i++) {
  503. $is_phrase = 1;
  504. $search_terms = '';
  505. $scope = '';
  506. $is_phrase = '';
  507. $operation = '';
  508. // if we have criteria supplied from the database then use that as the initial defaults
  509. if ($criteria) {
  510. $search_terms = $criteria['criteria'][$i]['search_terms'];
  511. $scope = $criteria['criteria'][$i]['scope'];
  512. $is_phrase = $criteria['criteria'][$i]['is_phrase'];
  513. $operation = $criteria['criteria'][$i]['operation'];
  514. }
  515. // if the criteria comes the session
  516. if (array_key_exists('tripal_pub_import', $_SESSION)) {
  517. $search_terms = isset($_SESSION['tripal_pub_import']['criteria'][$i]['search_terms']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'] : $search_terms;
  518. $scope = isset($_SESSION['tripal_pub_import']['criteria'][$i]['scope']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['scope'] : $scope;
  519. $is_phrase = isset($_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'] : $is_phrase;
  520. $operation = isset($_SESSION['tripal_pub_import']['criteria'][$i]['operation']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['operation'] : $operation;
  521. }
  522. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  523. // form is resbumitted using AJAX
  524. if (array_key_exists('values', $form_state)) {
  525. $search_terms = $form_state['values']["search_terms-$i"];
  526. $scope = $form_state['values']["scope-$i"];
  527. $is_phrase = $form_state['values']["is_phrase-$i"];
  528. $operation = $form_state['values']["operation-$i"];
  529. }
  530. $form['themed_element']['criteria'][$i]["scope-$i"] = [
  531. '#type' => 'select',
  532. '#description' => t('Please select the fields to search for this term.'),
  533. '#options' => $scope_choices,
  534. '#default_value' => $scope,
  535. ];
  536. $form['themed_element']['criteria'][$i]["search_terms-$i"] = [
  537. '#type' => 'textfield',
  538. '#description' => t('<span style="white-space: normal">Please provide a list of words for searching. You may use
  539. conjunctions such as "AND" or "OR" to separate words if they are expected in
  540. 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>'),
  541. '#default_value' => $search_terms,
  542. '#required' => TRUE,
  543. '#maxlength' => 2048,
  544. ];
  545. $form['themed_element']['criteria'][$i]["is_phrase-$i"] = [
  546. '#type' => 'checkbox',
  547. '#title' => t('Is Phrase?'),
  548. '#default_value' => $is_phrase,
  549. ];
  550. if ($i == 1) {
  551. /*
  552. $form['criteria'][$i]["operation-$i"] = array(
  553. '#type' => 'select',
  554. '#options' => $first_op_choices,
  555. '#default_value' => $operation,
  556. );*/
  557. }
  558. if ($i > 1) {
  559. $form['themed_element']['criteria'][$i]["operation-$i"] = [
  560. '#type' => 'select',
  561. '#options' => $op_choices,
  562. '#default_value' => $operation,
  563. ];
  564. }
  565. if ($i == $num_criteria) {
  566. if ($i > 1) {
  567. $form['themed_element']['criteria'][$i]["remove-$i"] = [
  568. '#type' => 'button',
  569. '#name' => 'remove',
  570. '#value' => t('Remove'),
  571. '#ajax' => [
  572. 'callback' => "tripal_pubs_setup_form_ajax_update",
  573. 'wrapper' => 'tripal-pubs-importer-setup',
  574. 'effect' => 'fade',
  575. 'method' => 'replace',
  576. 'prevent' => 'click',
  577. ],
  578. // When this button is clicked, the form will be validated and submitted.
  579. // Therefore, we set custom submit and validate functions to override the
  580. // default form submit. In the validate function we set the form_state
  581. // to rebuild the form so the submit function never actually gets called,
  582. // but we need it or Drupal will run the default validate anyway.
  583. // we also set #limit_validation_errors to empty so fields that
  584. // are required that don't have values won't generate warnings.
  585. '#submit' => ['tripal_pub_setup_form_ajax_button_submit'],
  586. '#validate' => ['tripal_pub_setup_form_ajax_button_validate'],
  587. '#limit_validation_errors' => [],
  588. ];
  589. }
  590. $form['themed_element']['criteria'][$i]["add-$i"] = [
  591. '#type' => 'button',
  592. '#name' => 'add',
  593. '#value' => t('Add'),
  594. '#ajax' => [
  595. 'callback' => "tripal_pubs_setup_form_ajax_update",
  596. 'wrapper' => 'tripal-pubs-importer-setup',
  597. 'effect' => 'fade',
  598. 'method' => 'replace',
  599. 'prevent' => 'click',
  600. ],
  601. // When this button is clicked, the form will be validated and submitted.
  602. // Therefore, we set custom submit and validate functions to override the
  603. // default form submit. In the validate function we set the form_state
  604. // to rebuild the form so the submit function never actually gets called,
  605. // but we need it or Drupal will run the default validate anyway.
  606. // we also set #limit_validation_errors to empty so fields that
  607. // are required that don't have values won't generate warnings.
  608. '#submit' => ['tripal_pub_setup_form_ajax_button_submit'],
  609. '#validate' => ['tripal_pub_setup_form_ajax_button_validate'],
  610. '#limit_validation_errors' => [],
  611. ];
  612. }
  613. }
  614. }
  615. /**
  616. * This function is used to rebuild the form if an ajax call is made vai a
  617. * button. The button causes the form to be submitted. We don't want this so we
  618. * override the validate and submit routines on the form button. Therefore,
  619. * this function only needs to tell Drupal to rebuild the form
  620. *
  621. * @ingroup tripal_pub
  622. */
  623. function tripal_pub_setup_form_ajax_button_validate($form, &$form_state) {
  624. $form_state['rebuild'] = TRUE;
  625. }
  626. /**
  627. * This function is just a dummy to override the default form submit on ajax
  628. * calls for buttons
  629. *
  630. * @ingroup tripal_pub
  631. */
  632. function tripal_pub_setup_form_ajax_button_submit($form, &$form_state) {
  633. // do nothing
  634. }
  635. /**
  636. * Validate the tripal_pub_importer_setup_form form
  637. *
  638. * @ingroup tripal_pub
  639. */
  640. function tripal_pub_importer_setup_form_validate($form, &$form_state) {
  641. $num_criteria = $form_state['values']['num_criteria'];
  642. $remote_db = $form_state['values']["remote_db"];
  643. $days = trim($form_state['values']["days"]);
  644. $disabled = $form_state['values']["disabled"];
  645. $do_contact = $form_state['values']["do_contact"];
  646. $loader_name = trim($form_state['values']["loader_name"]);
  647. for ($i = 1; $i <= $num_criteria; $i++) {
  648. $search_terms = trim($form_state['values']["search_terms-$i"]);
  649. $scope = $form_state['values']["scope-$i"];
  650. $is_phrase = $form_state['values']["is_phrase-$i"];
  651. $operation = '';
  652. if ($i > 1) {
  653. $operation = $form_state['values']["operation-$i"];
  654. }
  655. if (!$is_phrase) {
  656. if (preg_match('/\sand\s/i', $search_terms) and preg_match('/\sor\s/i', $search_terms)) {
  657. 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.");
  658. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  659. }
  660. }
  661. }
  662. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  663. form_set_error("days", "Please enter a numeric, non decimal value, for the number of days.");
  664. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  665. }
  666. // allow the selected remote database to validate any changes to the form if needed
  667. $callback = "tripal_pub_remote_validate_form_$remote_db";
  668. $form = call_user_func($callback, $form, $form_state);
  669. }
  670. /**
  671. * Submit the tripal_pub_importer_setup_form form
  672. *
  673. * @ingroup tripal_pub
  674. */
  675. function tripal_pub_importer_setup_form_submit($form, &$form_state) {
  676. $pub_import_id = $form_state['values']['pub_import_id'];
  677. $num_criteria = $form_state['values']['num_criteria'];
  678. $remote_db = $form_state['values']["remote_db"];
  679. $days = trim($form_state['values']["days"]);
  680. $loader_name = trim($form_state['values']["loader_name"]);
  681. $disabled = $form_state['values']["disabled"];
  682. $do_contact = $form_state['values']["do_contact"];
  683. // set the session variables
  684. $_SESSION['tripal_pub_import']['remote_db'] = $remote_db;
  685. $_SESSION['tripal_pub_import']['days'] = $days;
  686. $_SESSION['tripal_pub_import']['num_criteria'] = $num_criteria;
  687. $_SESSION['tripal_pub_import']['loader_name'] = $loader_name;
  688. $_SESSION['tripal_pub_import']['disabled'] = $disabled;
  689. $_SESSION['tripal_pub_import']['do_contact'] = $do_contact;
  690. $_SESSION['tripal_pub_import']['pub_import_id'] = $pub_import_id;
  691. unset($_SESSION['tripal_pub_import']['criteria']);
  692. for ($i = 1; $i <= $num_criteria; $i++) {
  693. $search_terms = trim($form_state['values']["search_terms-$i"]);
  694. $scope = $form_state['values']["scope-$i"];
  695. $is_phrase = $form_state['values']["is_phrase-$i"];
  696. $operation = '';
  697. if ($i > 1) {
  698. $operation = $form_state['values']["operation-$i"];
  699. }
  700. $_SESSION['tripal_pub_import']['criteria'][$i] = [
  701. 'search_terms' => $search_terms,
  702. 'scope' => $scope,
  703. 'is_phrase' => $is_phrase,
  704. 'operation' => $operation,
  705. ];
  706. }
  707. // now perform the appropriate action for the button clicked
  708. if ($form_state['values']['op'] == 'Test Importer') {
  709. $_SESSION['tripal_pub_import']['perform_search'] = 1;
  710. }
  711. if ($form_state['values']['op'] == 'Save Importer' or
  712. $form_state['values']['op'] == 'Save & Import Now') {
  713. $record = [
  714. 'name' => $loader_name,
  715. 'criteria' => serialize($_SESSION['tripal_pub_import']),
  716. 'disabled' => $disabled,
  717. 'do_contact' => $do_contact,
  718. ];
  719. // first check to see if this pub_import_id is already present. If so,
  720. // do an update rather than an insert
  721. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  722. $importer = db_query($sql, [':pub_import_id' => $pub_import_id])->fetchObject();
  723. if ($importer) {
  724. // do the update
  725. $record['pub_import_id'] = $pub_import_id;
  726. if (drupal_write_record('tripal_pub_import', $record, 'pub_import_id')) {
  727. unset($_SESSION['tripal_pub_import']);
  728. drupal_set_message('Publication import settings updated.');
  729. drupal_goto('admin/tripal/loaders/pub');
  730. }
  731. else {
  732. drupal_set_message('Could not update publication import settings.', 'error');
  733. }
  734. }
  735. else {
  736. // do the insert
  737. if (drupal_write_record('tripal_pub_import', $record)) {
  738. unset($_SESSION['tripal_pub_import']);
  739. drupal_set_message('Publication import settings saved.');
  740. // if the user wants to do the import now then do it (may time out
  741. // for long jobs)
  742. if ($form_state['values']['op'] == 'Save & Import Now') {
  743. chado_execute_pub_importer($record['pub_import_id']);
  744. }
  745. drupal_goto('admin/tripal/loaders/pub');
  746. }
  747. else {
  748. drupal_set_message('Could not save publication import settings.', 'error');
  749. }
  750. }
  751. }
  752. if ($form_state['values']['op'] == 'Delete Importer') {
  753. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  754. $success = db_query($sql, [':pub_import_id' => $pub_import_id]);
  755. if ($success) {
  756. drupal_set_message('Publication importer deleted.');
  757. drupal_goto('admin/tripal/loaders/pub');
  758. }
  759. else {
  760. drupal_set_message('Could not delete publication importer.', 'error');
  761. }
  762. }
  763. }
  764. /**
  765. * AJAX callback for updating the form.
  766. *
  767. * @ingroup tripal_pub
  768. */
  769. function tripal_pubs_setup_form_ajax_update($form, $form_state) {
  770. return $form['themed_element'];
  771. }
  772. /**
  773. * Theme the tripal_pub_importer_setup_form form.
  774. *
  775. * @ingroup tripal_pub
  776. */
  777. function theme_tripal_pub_importer_setup_form_elements($variables) {
  778. $form = $variables['form'];
  779. // first render the fields at the top of the form
  780. $markup = '';
  781. $markup .= '<div id="pub-search-form-row0">';
  782. $markup .= ' <div id="pub-search-form-row0-col1" style="float: left">' . drupal_render($form['remote_db']) . '</div>';
  783. $markup .= ' <div id="pub-search-form-row0-col2" style="float: left; margin-left: 10px">' . drupal_render($form['loader_name']) . '</div>';
  784. $markup .= '</div>';
  785. $markup .= '<div id="pub-search-form-row1" style="clear:both">';
  786. $markup .= ' <div id="pub-search-form-row1-col1">' . drupal_render($form['days']) . '</div>';
  787. $markup .= '</div>';
  788. $markup .= '<div id="pub-search-form-row2">' . drupal_render($form['disabled']) . '</div>';
  789. $markup .= '<div id="pub-search-form-row3">' . drupal_render($form['do_contact']) . '</div>';
  790. // next render the criteria fields into a table format
  791. $rows = [];
  792. foreach ($form['criteria'] as $i => $element) {
  793. if (is_numeric($i)) {
  794. $rows[] = [
  795. drupal_render($element["operation-$i"]),
  796. drupal_render($element["scope-$i"]),
  797. drupal_render($element["search_terms-$i"]),
  798. drupal_render($element["is_phrase-$i"]),
  799. drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  800. ];
  801. }
  802. }
  803. $headers = ['Operation', 'Scope', 'Search Terms', '', ''];
  804. $table = [
  805. 'header' => $headers,
  806. 'rows' => $rows,
  807. 'attributes' => [
  808. 'class' => ['tripal-data-table'],
  809. ],
  810. 'sticky' => TRUE,
  811. 'caption' => '',
  812. 'colgroups' => [],
  813. 'empty' => '',
  814. ];
  815. $criteria_table = theme_table($table);
  816. $markup .= $criteria_table;
  817. // add the rendered form
  818. $form = [
  819. '#markup' => $markup,
  820. '#prefix' => '<div id="tripal-pubs-importer-setup">',
  821. '#suffix' => '</div>',
  822. ];
  823. return drupal_render($form);
  824. }
  825. /**
  826. * Add a job to import publications
  827. *
  828. * @param $pub_importer_id
  829. * The id of the importer to submit a job to update
  830. *
  831. * @ingroup tripal_pub
  832. */
  833. function tripal_pub_importer_submit_job($import_id) {
  834. global $user;
  835. // get all of the loaders
  836. $args = [':import_id' => $import_id];
  837. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :import_id ";
  838. $import = db_query($sql, $args)->fetchObject();
  839. $args = [$import_id, TRUE, FALSE];
  840. $includes = [];
  841. $includes[] = module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
  842. tripal_add_job("Import publications $import->name", 'tripal_chado',
  843. 'chado_execute_pub_importer', $args, $user->uid, 10, $includes);
  844. drupal_goto('admin/tripal/loaders/pub');
  845. }
  846. /**
  847. * Deletes a publication importer.
  848. *
  849. */
  850. function tripal_pub_importer_delete($import_id) {
  851. $args = [':import_id' => $import_id];
  852. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = :import_id";
  853. $success = db_query($sql, $args);
  854. if ($success) {
  855. drupal_set_message('Publication importer deleted.');
  856. drupal_goto('admin/tripal/loaders/pub');
  857. }
  858. else {
  859. drupal_set_message('Could not delete publication importer.', 'error');
  860. }
  861. }
  862. /**
  863. * Adds publications that have been retrieved from a remote database and
  864. * consolidated into an array of details.
  865. *
  866. * @param $pubs
  867. * An array containing a list of publications to add to Chado. The
  868. * array contains a set of details for the publication.
  869. * @param $do_contact
  870. * Set to TRUE if authors should automatically have a contact record added
  871. * to Chado.
  872. * @param $update
  873. * If set to TRUE then publications that already exist in the Chado database
  874. * will be updated, whereas if FALSE only new publications will be added
  875. * @param $job
  876. * The jobs management object for the job if this function is run as a job.
  877. * This argument is added by Tripal during a job run and is not needed if
  878. * this function is run directly.
  879. *
  880. * @return
  881. * Returns an array containing the number of publications that were
  882. * inserted, updated, skipped and which had an error during import.
  883. *
  884. * @ingroup tripal_pub
  885. */
  886. function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE, $job = NULL) {
  887. // These are options for the tripal_report_error function. We do not
  888. // want to log messages to the watchdog but we do for the job and to
  889. // the terminal
  890. $message_type = 'pub_import';
  891. $message_opts = [
  892. 'watchdog' == TRUE,
  893. 'job' => $job,
  894. 'print' => TRUE,
  895. ];
  896. $report = [];
  897. $report['error'] = [];
  898. $report['inserted'] = [];
  899. $report['skipped'] = [];
  900. $report['updated'] = [];
  901. $total_pubs = count($pubs);
  902. // iterate through the publications and add each one
  903. $i = 1;
  904. foreach ($pubs as $pub) {
  905. $memory = number_format(memory_get_usage()) . " bytes";
  906. print "Processing $i of $total_pubs. Memory usage: $memory.\r";
  907. // add the publication to Chado
  908. $action = '';
  909. $pub_id = tripal_pub_add_publication($pub, $action, $do_contact, $update, $job);
  910. if ($pub_id) {
  911. // add the publication cross reference (e.g. to PubMed)
  912. if ($pub_id and $pub['Publication Dbxref']) {
  913. $dbxref = [];
  914. if (preg_match('/^(.*?):(.*?)$/', trim($pub['Publication Dbxref']), $matches)) {
  915. $dbxref['db_name'] = $matches[1];
  916. $dbxref['accession'] = $matches[2];
  917. }
  918. else {
  919. tripal_report_error($message_type, TRIPAL_ERROR,
  920. 'Unable to extract the dbxref to be associated with the publication (pub ID=@pub_id) from @dbxref. This reference should be [database-name]:[accession]',
  921. [
  922. '@pub_id' => $pub_id,
  923. '@dbxref' => $pub['Publication Dbxref'],
  924. $message_opts,
  925. ]
  926. );
  927. }
  928. $pub_dbxref = tripal_associate_dbxref('pub', $pub_id, $dbxref);
  929. }
  930. $pub['pub_id'] = $pub_id;
  931. }
  932. switch ($action) {
  933. case 'error':
  934. $report['error'][] = $pub['Citation'];
  935. break;
  936. case 'inserted':
  937. $report['inserted'][] = $pub['Citation'];
  938. break;
  939. case 'updated':
  940. $report['updated'][] = $pub['Citation'];
  941. break;
  942. case 'skipped':
  943. $report['skipped'][] = $pub['Citation'];
  944. break;
  945. }
  946. $i++;
  947. }
  948. return $report;
  949. }
  950. /**
  951. * Adds a new publication to Chado.
  952. *
  953. * In addition, all properties and
  954. * database cross-references. If the publication does not already exist
  955. * in Chado then it is added. If it does exist nothing is done. If
  956. * the $update parameter is TRUE then the publication is updated if it exists.
  957. *
  958. * @param $pub_details
  959. * An associative array containing all of the details about the publication.
  960. * @param $action
  961. * This variable will get set to a text value indicating the action that was
  962. * performed. The values include 'skipped', 'inserted', 'updated' or 'error'.
  963. * @param $do_contact
  964. * Optional. Set to TRUE if a contact entry should be added to the Chado
  965. * contact table for authors of the publication.
  966. * @param $update_if_exists
  967. * Optional. If the publication already exists then this function will
  968. * return without adding a new publication. However, set this value to
  969. * TRUE to force the function to pudate the publication using the
  970. * $pub_details that are provided.
  971. * @param $job
  972. * The jobs management object for the job if this function is run as a job.
  973. * This argument is added by Tripal during a job run and is not needed if
  974. * this function is run directly.
  975. *
  976. * @return
  977. * If the publication already exists, is inserted or updated then the
  978. * publication ID is returned, otherwise FALSE is returned. If the
  979. * publication already exists and $update_if_exists is not TRUE then the
  980. * $action variable is set to 'skipped'. If the publication already exists
  981. * and $update_if_exists is TRUE and if the update was successful then
  982. * $action is set to 'updated'. Otherwise on successful insert the
  983. * $action variable is set to 'inserted'. If the function failes then the
  984. * $action variable is set to 'error'
  985. *
  986. * @ingroup tripal_pub
  987. */
  988. function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE, $update_if_exists = FALSE, $job = NULL) {
  989. $pub_id = 0;
  990. // These are options for the tripal_report_error function. We do not
  991. // want to log messages to the watchdog except for errors and to the job and
  992. // to the terminal
  993. $message_type = 'pub_import';
  994. $message_opts = [
  995. 'watchdog' == FALSE,
  996. 'job' => $job,
  997. 'print' => TRUE,
  998. ];
  999. $error_opts = [
  1000. 'watchdog' == TRUE,
  1001. 'job' => $job,
  1002. 'print' => TRUE,
  1003. ];
  1004. if (!is_array($pub_details)) {
  1005. return FALSE;
  1006. }
  1007. // Before proceeding check to see if the publication already exists. If there
  1008. // is only one match and the $update_if_exists is NOT set then return FALSE.
  1009. $pub_ids = chado_publication_exists($pub_details);
  1010. if (count($pub_ids) == 1 and !$update_if_exists) {
  1011. tripal_report_error($message_type, TRIPAL_NOTICE,
  1012. "The following publication already exists on this site: %title %dbxref (Matching Pub id: %ids). Skipping.",
  1013. [
  1014. '%title' => $pub_details['Citation'],
  1015. '%dbxref' => $pub_details['Publication Dbxref'],
  1016. '%ids' => implode(",", $pub_ids),
  1017. ],
  1018. $message_opts
  1019. );
  1020. $action = 'skipped';
  1021. return FALSE;
  1022. }
  1023. // If we have more than one matching pub then return an error as we don't
  1024. // know which to update even if update_if_exists is set to TRUE.
  1025. if (count($pub_ids) > 1) {
  1026. tripal_report_error($message_type, TRIPAL_NOTICE,
  1027. "The following publication exists %num times on this site: %title %dbxref (Matching Pub id: %ids). Skipping.",
  1028. [
  1029. '%num' => count($pub_ids),
  1030. '%title' => $pub_details['Citation'],
  1031. '%dbxref' => $pub_details['Publication Dbxref'],
  1032. '%ids' => implode(",", $pub_ids),
  1033. ],
  1034. $message_opts
  1035. );
  1036. $action = 'skipped';
  1037. return FALSE;
  1038. }
  1039. if (count($pub_ids) == 1 and $update_if_exists) {
  1040. $pub_id = $pub_ids[0];
  1041. }
  1042. // Get the publication type (use the first publication type).
  1043. if (array_key_exists('Publication Type', $pub_details)) {
  1044. $pub_type = '';
  1045. if (is_array($pub_details['Publication Type'])) {
  1046. $pub_type = $pub_details['Publication Type'][0];
  1047. }
  1048. else {
  1049. $pub_type = $pub_details['Publication Type'];
  1050. }
  1051. $identifiers = [
  1052. 'name' => $pub_type,
  1053. 'cv_id' => [
  1054. 'name' => 'tripal_pub',
  1055. ],
  1056. ];
  1057. $pub_type = chado_get_cvterm($identifiers);
  1058. }
  1059. else {
  1060. tripal_report_error($message_type, TRIPAL_ERROR,
  1061. "The Publication Type is a required property but is missing", [], $error_opts);
  1062. $action = 'error';
  1063. return FALSE;
  1064. }
  1065. if (!$pub_type) {
  1066. tripal_report_error($message_type, TRIPAL_ERROR,
  1067. "Cannot find publication type: '%type'",
  1068. ['%type' => $pub_details['Publication Type'][0]], $error_opts);
  1069. $action = 'error';
  1070. return FALSE;
  1071. }
  1072. // The series name field in the pub table is only 255 characters, so we
  1073. // should trim just in case.
  1074. $series_name = '';
  1075. if (array_key_exists('Series_Name', $pub_details)) {
  1076. $series_name = substr($pub_details['Series Name'], 0, 255);
  1077. }
  1078. if (array_key_exists('Journal Name', $pub_details)) {
  1079. $series_name = substr($pub_details['Journal Name'], 0, 255);
  1080. }
  1081. // Build the values array for inserting or updating.
  1082. $values = [
  1083. 'title' => $pub_details['Title'],
  1084. 'volume' => (isset($pub_details['Volume'])) ? $pub_details['Volume'] : '',
  1085. 'series_name' => $series_name,
  1086. 'issue' => (isset($pub_details['Issue'])) ? $pub_details['Issue'] : '',
  1087. 'pyear' => (isset($pub_details['Year'])) ? $pub_details['Year'] : '',
  1088. 'pages' => (isset($pub_details['Pages'])) ? $pub_details['Pages'] : '',
  1089. 'uniquename' => $pub_details['Citation'],
  1090. 'type_id' => $pub_type->cvterm_id,
  1091. ];
  1092. // If there is no pub_id then we need to do an insert.
  1093. if (!$pub_id) {
  1094. $options = ['statement_name' => 'ins_pub_tivoseispypaunty'];
  1095. $pub = chado_insert_record('pub', $values, $options);
  1096. if (!$pub) {
  1097. tripal_report_error($message_type, TRIPAL_ERROR,
  1098. "Cannot insert the publication with title: %title",
  1099. ['%title' => $pub_details['Title']], $error_opts);
  1100. $action = 'error';
  1101. return FALSE;
  1102. }
  1103. $pub_id = $pub['pub_id'];
  1104. $action = 'inserted';
  1105. }
  1106. // If there is a pub_id and we've been told to update, then do the update.
  1107. else {
  1108. if ($pub_id and $update_if_exists) {
  1109. $match = ['pub_id' => $pub_id];
  1110. $options = ['statement_name' => 'up_pub_tivoseispypaunty'];
  1111. $success = chado_update_record('pub', $match, $values, $options);
  1112. if (!$success) {
  1113. tripal_report_error($message_type, TRIPAL_ERROR,
  1114. "Cannot update the publication with title: %title",
  1115. ['%title' => $pub_details['Title']], $error_opts);
  1116. $action = 'error';
  1117. return FALSE;
  1118. }
  1119. $action = 'updated';
  1120. }
  1121. }
  1122. // Before we add any new properties we need to remove those that are there
  1123. // if this is an update. The only thing we don't want to remove are the
  1124. // 'Publication Dbxref'.
  1125. if ($update_if_exists) {
  1126. $sql = "
  1127. DELETE FROM {pubprop}
  1128. WHERE
  1129. pub_id = :pub_id AND
  1130. NOT type_id in (
  1131. SELECT cvterm_id
  1132. FROM {cvterm}
  1133. WHERE name = 'Publication Dbxref'
  1134. )
  1135. ";
  1136. chado_query($sql, [':pub_id' => $pub_id]);
  1137. }
  1138. // Iterate through the properties and add them.
  1139. foreach ($pub_details as $key => $value) {
  1140. // The pub_details may have the raw search data (e.g. in XML from PubMed.
  1141. // We'll irgnore this for now.
  1142. if ($key == 'raw') {
  1143. continue;
  1144. }
  1145. // Since we're not updating the 'Publication Dbxref' on an update
  1146. // skip this property.
  1147. if ($update_if_exists and $key == 'Publication Dbxref') {
  1148. continue;
  1149. }
  1150. // Get the cvterm by name.
  1151. $identifiers = [
  1152. 'name' => $key,
  1153. 'cv_id' => [
  1154. 'name' => 'tripal_pub',
  1155. ],
  1156. ];
  1157. $cvterm = chado_get_cvterm($identifiers);
  1158. // If we could not find the cvterm by name then try by synonym.
  1159. if (!$cvterm) {
  1160. $identifiers = [
  1161. 'synonym' => [
  1162. 'name' => $key,
  1163. 'cv_name' => 'tripal_pub',
  1164. ],
  1165. ];
  1166. $cvterm = chado_get_cvterm($identifiers);
  1167. }
  1168. if (!$cvterm) {
  1169. tripal_report_error($message_type, TRIPAL_ERROR,
  1170. "Cannot find term: '%prop'. Skipping.", ['%prop' => $key], $error_opts);
  1171. continue;
  1172. }
  1173. // Skip details that won't be stored as properties.
  1174. if ($key == 'Author List') {
  1175. tripal_pub_add_authors($pub_id, $value, $do_contact);
  1176. continue;
  1177. }
  1178. if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
  1179. $key == 'Year' or $key == 'Pages') {
  1180. continue;
  1181. }
  1182. $success = 0;
  1183. if (is_array($value)) {
  1184. foreach ($value as $subkey => $subvalue) {
  1185. // If the key is an integer then this array is a simple list and
  1186. // we will insert using the primary key. Otheriwse, use the new key.
  1187. if (is_int($subkey)) {
  1188. $success = chado_insert_property(
  1189. ['table' => 'pub', 'id' => $pub_id],
  1190. [
  1191. 'type_name' => $key,
  1192. 'cv_name' => 'tripal_pub',
  1193. 'value' => $subvalue,
  1194. ]
  1195. );
  1196. }
  1197. else {
  1198. $success = chado_insert_property(
  1199. ['table' => 'pub', 'id' => $pub_id],
  1200. [
  1201. 'type_name' => $subkey,
  1202. 'cv_name' => 'tripal_pub',
  1203. 'value' => $subvalue,
  1204. ]
  1205. );
  1206. }
  1207. }
  1208. }
  1209. else {
  1210. $success = chado_insert_property(
  1211. ['table' => 'pub', 'id' => $pub_id],
  1212. ['type_name' => $key, 'cv_name' => 'tripal_pub', 'value' => $value],
  1213. ['update_if_present' => TRUE]
  1214. );
  1215. }
  1216. if (!$success) {
  1217. tripal_report_error($message_type, TRIPAL_ERROR,
  1218. "Cannot add property '%prop' to pubprop table. Skipping.",
  1219. ['%prop' => $key], $error_opts);
  1220. continue;
  1221. }
  1222. }
  1223. return $pub_id;
  1224. }
  1225. /**
  1226. * Add one or more authors to a publication
  1227. *
  1228. * @param $pub_id
  1229. * The publication ID of the pub in Chado.
  1230. * @param $authors
  1231. * An array of authors. Each author should have a set of keys/value pairs
  1232. * describing the author.
  1233. * @param $do_contact
  1234. * Optional. Set to TRUE if a contact entry should be added to the Chado
  1235. * contact table for authors of the publication.
  1236. *
  1237. * @ingroup tripal_pub
  1238. */
  1239. function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
  1240. $rank = 0;
  1241. // First remove any of the existing pubauthor entires.
  1242. $sql = "DELETE FROM {pubauthor} WHERE pub_id = :pub_id";
  1243. chado_query($sql, [':pub_id' => $pub_id]);
  1244. // Iterate through the authors and add them to the pubauthors and contact
  1245. // tables of chado, then link them through the custom pubauthors_contact
  1246. // table.
  1247. foreach ($authors as $author) {
  1248. // Skip invalid author entires.
  1249. if (isset($author['valid']) AND $author['valid'] == 'N') {
  1250. continue;
  1251. }
  1252. // remove the 'valid' property as we don't have a CV term for it
  1253. unset($author['valid']);
  1254. $values = [
  1255. 'pub_id' => $pub_id,
  1256. 'rank' => $rank,
  1257. ];
  1258. // construct the contact.name field using the author information
  1259. $name = '';
  1260. $type = 'Person';
  1261. if (isset($author['Given Name'])) {
  1262. $name .= $author['Given Name'];
  1263. $values['givennames'] = $author['Given Name'];
  1264. }
  1265. if (isset($author['Surname'])) {
  1266. $name .= ' ' . $author['Surname'];
  1267. $values['surname'] = substr($author['Surname'], 0, 100);
  1268. }
  1269. if (isset($author['Suffix'])) {
  1270. $name .= ' ' . $author['Suffix'];
  1271. $values['suffix'] = $author['Suffix'];
  1272. }
  1273. if (isset($author['Collective'])) {
  1274. $name = $author['Collective'];
  1275. $type = 'Collective';
  1276. if (!isset($author['Surname'])) {
  1277. $values['surname'] = substr($author['Collective'], 0, 100);
  1278. }
  1279. }
  1280. $name = trim($name);
  1281. // add an entry to the pubauthors table
  1282. $options = ['statement_name' => 'ins_pubauthor_idrasugisu'];
  1283. $pubauthor = chado_insert_record('pubauthor', $values, $options);
  1284. // if the user wants us to create a contact for each author then do it.
  1285. if ($do_contact) {
  1286. // Add the contact
  1287. $contact = chado_insert_contact([
  1288. 'name' => $name,
  1289. 'description' => '',
  1290. 'type_name' => $type,
  1291. 'properties' => $author,
  1292. ]);
  1293. // if we have succesfully added the contact and the pubauthor entries then we want to
  1294. // link them together
  1295. if ($contact and $pubauthor) {
  1296. // link the pubauthor entry to the contact
  1297. $values = [
  1298. 'pubauthor_id' => $pubauthor['pubauthor_id'],
  1299. 'contact_id' => $contact['contact_id'],
  1300. ];
  1301. $options = ['statement_name' => 'ins_pubauthorcontact_puco'];
  1302. $pubauthor_contact = chado_insert_record('pubauthor_contact', $values, $options);
  1303. if (!$pubauthor_contact) {
  1304. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot link pub authro and contact.", []);
  1305. }
  1306. }
  1307. }
  1308. $rank++;
  1309. }
  1310. }
  1311. /**
  1312. * This function generates an array suitable for use with the
  1313. * tripal_pub_create_citation function for any publication
  1314. * already stored in the Chado tables.
  1315. *
  1316. * @param $pub_id
  1317. * The publication ID
  1318. * @param $skip_existing
  1319. * Set to TRUE to skip publications that already have a citation
  1320. * in the pubprop table. Set to FALSE to generate a citation
  1321. * regardless if the citation already exists.
  1322. *
  1323. * @return
  1324. * An array suitable for the trpial_pub_create_citation function. On
  1325. * failure returns FALSE.
  1326. *
  1327. * @ingroup tripal_pub
  1328. */
  1329. function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
  1330. $options = ['return_array' => 1];
  1331. // ---------------------------------
  1332. // get the publication
  1333. // ---------------------------------
  1334. $values = ['pub_id' => $pub_id];
  1335. $pub = chado_generate_var('pub', $values);
  1336. // expand the title
  1337. $pub = chado_expand_var($pub, 'field', 'pub.title');
  1338. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  1339. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  1340. $pub_array = [];
  1341. if (trim($pub->title)) {
  1342. $pub_array['Title'] = $pub->title;
  1343. }
  1344. if (trim($pub->volumetitle)) {
  1345. $pub_array['Volume Title'] = $pub->volumetitle;
  1346. }
  1347. if (trim($pub->volume)) {
  1348. $pub_array['Volume'] = $pub->volume;
  1349. }
  1350. if (trim($pub->series_name)) {
  1351. $pub_array['Series Name'] = $pub->series_name;
  1352. }
  1353. if (trim($pub->issue)) {
  1354. $pub_array['Issue'] = $pub->issue;
  1355. }
  1356. if (trim($pub->pyear)) {
  1357. $pub_array['Year'] = $pub->pyear;
  1358. }
  1359. if (trim($pub->pages)) {
  1360. $pub_array['Pages'] = $pub->pages;
  1361. }
  1362. if (trim($pub->miniref)) {
  1363. $pub_array['Mini Ref'] = $pub->miniref;
  1364. }
  1365. if (trim($pub->uniquename)) {
  1366. $pub_array['Uniquename'] = $pub->uniquename;
  1367. }
  1368. $pub_array['Publication Type'][] = $pub->type_id->name;
  1369. // ---------------------------------
  1370. // get the citation
  1371. // ---------------------------------
  1372. $values = [
  1373. 'pub_id' => $pub->pub_id,
  1374. 'type_id' => [
  1375. 'name' => 'Citation',
  1376. ],
  1377. ];
  1378. $citation = chado_generate_var('pubprop', $values);
  1379. if ($citation) {
  1380. $citation = chado_expand_var($citation, 'field', 'pubprop.value', $options);
  1381. if (count($citation) > 1) {
  1382. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Publication has multiple citations already: %pub_id",
  1383. ['%pub_id' => $pubid]);
  1384. return FALSE;
  1385. }
  1386. elseif (count($citation) == 1 and $skip_existing == TRUE) {
  1387. // skip this publication, it already has a citation
  1388. return FALSE;
  1389. }
  1390. }
  1391. // ---------------------------------
  1392. // get the publication types
  1393. // ---------------------------------
  1394. $values = [
  1395. 'pub_id' => $pub->pub_id,
  1396. 'type_id' => [
  1397. 'name' => 'Publication Type',
  1398. ],
  1399. ];
  1400. $ptypes = chado_generate_var('pubprop', $values, $options);
  1401. if ($ptypes) {
  1402. $ptypes = chado_expand_var($ptypes, 'field', 'pubprop.value', $options);
  1403. foreach ($ptypes as $ptype) {
  1404. $pub_array['Publication Type'][] = $ptype->value;
  1405. }
  1406. }
  1407. // ---------------------------------
  1408. // get the authors list
  1409. // ---------------------------------
  1410. $values = [
  1411. 'pub_id' => $pub->pub_id,
  1412. 'type_id' => [
  1413. 'name' => 'Authors',
  1414. ],
  1415. ];
  1416. $authors = chado_generate_var('pubprop', $values);
  1417. $authors = chado_expand_var($authors, 'field', 'pubprop.value', $options);
  1418. if (count($authors) > 1) {
  1419. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Publication has multiple author lists. It should have only one list: %pub_id",
  1420. ['%pub_id' => $pubid]);
  1421. return FALSE;
  1422. }
  1423. else {
  1424. if (trim($authors->value)) {
  1425. $pub_array['Authors'] = $authors->value;
  1426. }
  1427. // if there is no 'Author's property then try to retreive authors from the pubauthor table
  1428. else {
  1429. $sql = "
  1430. SELECT string_agg(surname || ' ' || givennames, ', ')
  1431. FROM {pubauthor}
  1432. WHERE pub_id = :pub_id
  1433. GROUP BY pub_id
  1434. ";
  1435. $au = chado_query($sql, [':pub_id' => $pub_id])->fetchField();
  1436. if ($au) {
  1437. $pub_array['Authors'] = $au;
  1438. }
  1439. }
  1440. }
  1441. //Get other props
  1442. $props = [
  1443. 'Journal Abbreviation',
  1444. 'Elocation',
  1445. 'Media Code',
  1446. 'Conference Name',
  1447. 'Keywords',
  1448. 'Series Name',
  1449. 'pISSN',
  1450. 'Publication Date',
  1451. 'Journal Code',
  1452. 'Journal Alias',
  1453. 'Journal Country',
  1454. 'Published Location',
  1455. 'Publication Model',
  1456. 'Language Abbr',
  1457. 'Alias',
  1458. 'Publication Dbxref',
  1459. 'Copyright',
  1460. 'Abstract',
  1461. 'Notes',
  1462. 'Citation',
  1463. 'Language',
  1464. 'URL',
  1465. 'eISSN',
  1466. 'DOI',
  1467. 'ISSN',
  1468. 'Publication Code',
  1469. 'Comments',
  1470. 'Publisher',
  1471. 'Media Alias',
  1472. 'Original Title',
  1473. ];
  1474. foreach ($props AS $prop) {
  1475. $sql =
  1476. "SELECT value FROM {pubprop}
  1477. WHERE type_id =
  1478. (SELECT cvterm_id
  1479. FROM {cvterm}
  1480. WHERE name = :cvtname AND cv_id =
  1481. (SELECT cv_id
  1482. FROM {cv}
  1483. WHERE name = 'tripal_pub'
  1484. )
  1485. )
  1486. AND pub_id = :pub_id
  1487. ";
  1488. $val = trim(chado_query($sql, [
  1489. ':cvtname' => $prop,
  1490. ':pub_id' => $pub->pub_id,
  1491. ])->fetchField());
  1492. if ($val) {
  1493. $pub_array[$prop] = $val;
  1494. }
  1495. }
  1496. return $pub_array;
  1497. }
  1498. /**
  1499. * This function is used to perfom a query using one of the supported databases
  1500. * and return the raw query results. This may be XML or some other format
  1501. * as provided by the database.
  1502. *
  1503. * @param $dbxref
  1504. * The unique database ID for the record to retrieve. This value must
  1505. * be of the format DB_NAME:ACCESSION where DB_NAME is the name of the
  1506. * database (e.g. PMID or AGL) and the ACCESSION is the unique identifier
  1507. * for the record in the database.
  1508. *
  1509. * @return
  1510. * Returns the publication array or FALSE if a problem occurs
  1511. *
  1512. * @ingroup tripal_pub
  1513. */
  1514. function tripal_get_remote_pub($dbxref) {
  1515. if (preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  1516. $remote_db = $matches[1];
  1517. $accession = $matches[2];
  1518. // check that the database is supported
  1519. $supported_dbs = variable_get('tripal_pub_supported_dbs', ['PMID']);
  1520. if (!in_array($remote_db, $supported_dbs)) {
  1521. return FALSE;
  1522. }
  1523. $search = [
  1524. 'num_criteria' => 1,
  1525. 'remote_db' => $remote_db,
  1526. 'criteria' => [
  1527. '1' => [
  1528. 'search_terms' => "$remote_db:$accession",
  1529. 'scope' => 'id',
  1530. 'operation' => '',
  1531. 'is_phrase' => 0,
  1532. ],
  1533. ],
  1534. ];
  1535. $pubs = tripal_get_remote_pubs($remote_db, $search, 1, 0);
  1536. return $pubs['pubs'][0];
  1537. }
  1538. return FALSE;
  1539. }
  1540. /**
  1541. * Retrieves a list of publications as an associated array where
  1542. * keys correspond directly with Tripal Pub CV terms.
  1543. *
  1544. * @param remote_db
  1545. * The name of the remote publication database to query. These names should
  1546. * match the name of the databases in the Chado 'db' table. Currently
  1547. * supported databass include
  1548. * 'PMID': PubMed
  1549. *
  1550. * @param search_array
  1551. * An associate array containing the search criteria. The following key
  1552. * are expected
  1553. * 'remote_db': Specifies the name of the remote publication database
  1554. * 'num_criteria': Specifies the number of criteria present in the search
  1555. * array
  1556. * 'days': The number of days to include in the search starting
  1557. * from today
  1558. * 'criteria': An associate array containing the search critiera.
  1559. * There should be no less than 'num_criteria' elements in this array.
  1560. *
  1561. * The following keys are expected in the 'criteria' array
  1562. * 'search_terms': A list of terms to search on, separated by spaces.
  1563. * 'scope': The fields to search in the remote database. Valid
  1564. * values include: 'title', 'abstract', 'author' and 'any'
  1565. * 'operation': The logical operation to use for this criteria. Valid
  1566. * values include: 'AND', 'OR' and 'NOT'.
  1567. * @param $num_to_retrieve
  1568. * The number of records to retrieve. In cases with large numbers of
  1569. * records to retrieve, the remote database may limit the size of each
  1570. * retrieval.
  1571. * @param $page
  1572. * Optional. If this function is called where the
  1573. * page for the pager cannot be set using the $_GET variable, use this
  1574. * argument to specify the page to retrieve.
  1575. *
  1576. * @return
  1577. * Returns an array of pubs where each element is
  1578. * an associative array where the keys are Tripal Pub CV terms.
  1579. *
  1580. * @ingroup tripal_pub
  1581. */
  1582. function tripal_get_remote_pubs($remote_db, $search_array, $num_to_retrieve, $page = 0) {
  1583. // now call the callback function to get the results
  1584. $callback = "tripal_pub_remote_search_$remote_db";
  1585. $pubs = [
  1586. 'total_records' => 0,
  1587. 'search_str' => '',
  1588. 'pubs' => [],
  1589. ];
  1590. if (function_exists($callback)) {
  1591. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $page);
  1592. }
  1593. return $pubs;
  1594. }
  1595. /**
  1596. * The admin form for submitting job to create citations
  1597. *
  1598. * @param $form_state
  1599. *
  1600. * @ingroup tripal_pub
  1601. */
  1602. function tripal_pub_citation_form($form, &$form_state) {
  1603. $form['instructions'] = [
  1604. '#markup' => '<p>Use this form to unify publication citations. Citations are created automtically when
  1605. importing publications but citations are set by the user when publications are added manually.
  1606. Or publications added to the Chado database by tools other than the Tripal Publication Importer may
  1607. not have citations set. If you are certain that all necessary information for all publications is present (e.g.
  1608. authors, volume, issue, page numbers, etc.) but citations are not consistent, then you can
  1609. choose to update all citations for all publications using the form below. Alternatively, you
  1610. can update citations only for publication that do not already have one.</p>',
  1611. ];
  1612. $form['options'] = [
  1613. '#type' => 'radios',
  1614. '#options' => [
  1615. 'all' => 'Create citation for all publications. Replace the existing citation if it exists.',
  1616. 'new' => 'Create citation for publication only if it does not already have one.',
  1617. ],
  1618. '#default_value' => 'all',
  1619. ];
  1620. $form['submit'] = [
  1621. '#type' => 'submit',
  1622. '#value' => t('Submit'),
  1623. ];
  1624. return $form;
  1625. }
  1626. /**
  1627. * Submit form. Create Tripal job for citations
  1628. *
  1629. * @param $form_state
  1630. *
  1631. * @ingroup tripal_pub
  1632. */
  1633. function tripal_pub_citation_form_submit(&$form_state) {
  1634. $options [0] = $form_state['options']['#value'];
  1635. tripal_add_job("Create citations ($options[0])", 'tripal_pub', 'chado_pub_create_citations', $options, $user->uid);
  1636. }