tripal_pub.api.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Pub API
  5. *
  6. * @defgroup tripal_pub_api Publication Module API
  7. * @ingroup tripal_api
  8. */
  9. /*
  10. * Retrieves a list of publications as an associated array where
  11. * keys correspond directly with Tripal Pub CV terms.
  12. *
  13. * @param remote_db
  14. * The name of the remote publication database to query. These names should
  15. * match the name of the databases in the Chado 'db' table. Currently
  16. * supported databass include
  17. * 'PMID': PubMed
  18. *
  19. * @param search_array
  20. * An associate array containing the search criteria. The following key
  21. * are expected
  22. * 'remote_db': Specifies the name of the remote publication database
  23. * 'num_criteria': Specifies the number of criteria present in the search array
  24. * 'days': The number of days to include in the search starting from today
  25. * 'criteria': An associate array containing the search critiera. There should
  26. * be no less than 'num_criteria' elements in this array.
  27. *
  28. * The following keys are expected in the 'criteria' array
  29. * 'search_terms': A list of terms to search on, separated by spaces.
  30. * 'scope': The fields to search in the remote database. Valid values
  31. * include: 'title', 'abstract', 'author' and 'any'
  32. * 'operation': The logical operation to use for this criteria. Valid
  33. * values include: 'AND', 'OR' and 'NOT'.
  34. * @param $num_to_retrieve
  35. * The number of records to retrieve. In cases with large numbers of
  36. * records to retrieve, the remote database may limit the size of each
  37. * retrieval.
  38. * @param $pager_id
  39. * Optional. This function uses the 'tripal_pager_callback' function
  40. * to page a set of results. This is helpful when generating results to
  41. * be view online. The pager works identical to the pager_query function
  42. * of drupal. Simply provide a unique integer value for this argument. Each
  43. * form on a single page should have a unique $pager_id.
  44. * @param $page
  45. * Optional. If this function is called where the
  46. * page for the pager cannot be set using the $_GET variable, use this
  47. * argument to specify the page to retrieve.
  48. *
  49. * @return
  50. * Returns an array of pubs where each element is
  51. * an associative array where the keys are Tripal Pub CV terms.
  52. *
  53. * @ingroup tripal_pub_api
  54. */
  55. function tripal_pub_get_remote_search_results($remote_db, $search_array,
  56. $num_to_retrieve, $pager_id = 0, $page = 0) {
  57. // construct the callback function using the remote database name
  58. $callback = 'tripal_pub_remote_search_' . strtolower($remote_db);
  59. // manually set the $_GET['page'] parameter to trick the pager
  60. // into giving us the requested page
  61. if (is_int($page) and $page > 0) {
  62. $_GET['page'] = $page;
  63. }
  64. // now call the callback function to get the rsults
  65. $pubs = array();
  66. if (function_exists($callback)) {
  67. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $pager_id);
  68. }
  69. return $pubs;
  70. }
  71. /*
  72. * @ingroup tripal_pub_api
  73. */
  74. function tripal_pub_update_publications($do_contact = FALSE) {
  75. // get a persistent connection
  76. $connection = tripal_db_persistent_chado();
  77. if (!$connection) {
  78. print "A persistant connection was not obtained. Loading will be slow\n";
  79. }
  80. // if we cannot get a connection then let the user know the loading will be slow
  81. tripal_db_start_transaction();
  82. if ($connection) {
  83. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  84. "If the load fails or is terminated prematurely then the entire set of \n" .
  85. "insertions/updates is rolled back and will not be found in the database\n\n";
  86. }
  87. // get a list of all publications that have
  88. // supported databases
  89. $sql = "
  90. SELECT DB.name as db_name, DBX.accession
  91. FROM pub P
  92. INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
  93. INNER JOIN dbxref DBX ON DBX.dbxref_id = PDBX.dbxref_id
  94. INNER JOIN db DB ON DB.db_id = DBX.db_id
  95. ORDER BY DB.name, P.pub_id
  96. ";
  97. $results = chado_query($sql);
  98. $num_to_retrieve = 100;
  99. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query
  100. $curr_db = ''; // keeps track of the current current database
  101. $ids = array(); // the list of IDs for the database
  102. $search = array(); // the search array passed to the search function
  103. // iterate through the pub IDs
  104. while ($pub = db_fetch_object($results)) {
  105. $accession = $pub->accession;
  106. $remote_db = $pub->db_name;
  107. // here we need to only update publications for databases we support
  108. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  109. if(!in_array($remote_db, $supported_dbs)) {
  110. continue;
  111. }
  112. // if we're switching databases then reset the search array
  113. if($remote_db != $curr_db) {
  114. // if we had a previous DB then do the update.
  115. if ($curr_db) {
  116. $search['num_criteria'] = $i - 1;
  117. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
  118. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  119. }
  120. $curr_db = $remote_db;
  121. $search = array(
  122. 'remote_db' => $remote_db,
  123. 'criteria' => array(),
  124. );
  125. $ids = array();
  126. $i = 0;
  127. }
  128. // if we've hit the maximum number to retrieve then do the search
  129. if($i == $num_to_retrieve) {
  130. $search['num_criteria'] = $i - 1;
  131. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
  132. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  133. $search['criteria'] = array();
  134. $i = 0;
  135. }
  136. // add each accession to the search criteria
  137. $search['criteria'][] = array(
  138. 'search_terms' => $accession,
  139. 'scope' => 'id',
  140. 'operation' => 'OR'
  141. );
  142. $i++;
  143. }
  144. // now update any remaining in the search criteria array
  145. $search['num_criteria'] = $i - 1;
  146. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
  147. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  148. // transaction is complete
  149. tripal_db_commit_transaction();
  150. print "Transaction Complete\n";
  151. // sync the newly added publications with Drupal
  152. print "Syncing publications with Drupal...\n";
  153. tripal_pub_sync_pubs();
  154. // if the caller wants to create contacts then we should sync them
  155. if ($do_contact) {
  156. print "Syncing contacts with Drupal...\n";
  157. tripal_contact_sync_contacts();
  158. }
  159. print "Done.\n";
  160. }
  161. /*
  162. * @ingroup tripal_pub_api
  163. */
  164. function tripal_pub_import_publications() {
  165. $num_to_retrieve = 100;
  166. $pager_id = 0;
  167. $page = 0;
  168. $num_pubs = 0;
  169. // get a persistent connection
  170. $connection = tripal_db_persistent_chado();
  171. if (!$connection) {
  172. print "A persistant connection was not obtained. Loading will be slow\n";
  173. }
  174. // if we cannot get a connection then let the user know the loading will be slow
  175. tripal_db_start_transaction();
  176. if ($connection) {
  177. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  178. "If the load fails or is terminated prematurely then the entire set of \n" .
  179. "insertions/updates is rolled back and will not be found in the database\n\n";
  180. }
  181. // get all of the loaders
  182. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0";
  183. $results = db_query($sql);
  184. $do_contact = FALSE;
  185. while ($import = db_fetch_object($results)) {
  186. print "Importing: " . $import->name . "\n";
  187. // keep track if any of the importers want to create contacts from authors
  188. if ($import->do_contact == 1) {
  189. $do_contact = TRUE;
  190. }
  191. $criteria = unserialize($import->criteria);
  192. $remote_db = $criteria['remote_db'];
  193. do {
  194. // retrieve the pubs for this page. We'll retreive 10 at a time
  195. $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
  196. tripal_pub_add_publications($pubs, $import->do_contact);
  197. $page++;
  198. }
  199. // continue looping until we have a $pubs array that does not have
  200. // our requested numer of records. This means we've hit the end
  201. while (count($pubs) == $num_to_retrieve);
  202. }
  203. // transaction is complete
  204. tripal_db_commit_transaction();
  205. print "Transaction Complete\n";
  206. // sync the newly added publications with Drupal
  207. print "Syncing publications with Drupal...\n";
  208. tripal_pub_sync_pubs();
  209. // if any of the importers wanted to create contacts from the authors then sync them
  210. if($do_contact) {
  211. print "Syncing contacts with Drupal...\n";
  212. tripal_contact_sync_contacts();
  213. }
  214. print "Done.\n";
  215. }
  216. /*
  217. *
  218. */
  219. function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
  220. // iterate through the publications and add each one
  221. foreach ($pubs as $pub) {
  222. // add the publication to Chado and sync it with Chado
  223. $pub_id = tripal_pub_add_publication($pub, $do_contact, $update);
  224. if (is_numeric($pub_id)){
  225. // add the publication cross reference (e.g. to PubMed)
  226. if ($pub_id and $pub['Publication Dbxref']) {
  227. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub['Publication Dbxref']);
  228. }
  229. $num_pubs++;
  230. print "Success: " . $pub['Publication Dbxref'] . "\n";
  231. }
  232. elseif($pub_id) {
  233. print "Skipped: " . $pub['Publication Dbxref'] . "\n";
  234. }
  235. else {
  236. print "Failed: " . $pub['Publication Dbxref'] . "\n";
  237. }
  238. }
  239. }
  240. /*
  241. *
  242. */
  243. function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
  244. // break apart the dbxref
  245. $dbname = '';
  246. $accession = '';
  247. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  248. $dbname = $matches[1];
  249. $accession = $matches[2];
  250. }
  251. else {
  252. return FALSE;
  253. }
  254. // check to see if the pub_dbxref record already exist
  255. $values = array(
  256. 'dbxref_id' => array(
  257. 'accession' => $accession,
  258. 'db_id' => array(
  259. 'name' => $dbname,
  260. ),
  261. ),
  262. 'pub_id' => $pub_id,
  263. );
  264. $options = array('statement_name' => 'sel_pubdbxref_dbpu');
  265. $results = tripal_core_chado_select('pub_dbxref', array('*'), $values, $options);
  266. // if the pub_dbxref record exist then we don't need to re-add it.
  267. if(count($results) > 0) {
  268. return $results[0];
  269. }
  270. // make sure our database already exists
  271. $db = tripal_db_add_db($dbname);
  272. // get the database cross-reference
  273. $dbxvalues = array(
  274. 'accession' => $accession,
  275. 'db_id' => $db->db_id,
  276. );
  277. $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
  278. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
  279. // if the accession doesn't exist then add it
  280. if(count($results) == 0){
  281. $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
  282. }
  283. else {
  284. $dbxref = $results[0];
  285. }
  286. // now add the record
  287. $options = array('statement_name' => 'ins_pubdbxref_dbpu');
  288. $results = tripal_core_chado_insert('pub_dbxref', $values, $options);
  289. if (!$results) {
  290. watchdog('tripal_pub', "Cannot add publication dbxref: %db:%accession.",
  291. array('%db' => $dbname, '%accession' => $accession). WATCHDOG_ERROR);
  292. return FALSE;
  293. }
  294. return $results;
  295. }
  296. /**
  297. * Returns the list of publications that are assigned the database
  298. * cross-reference provided
  299. *
  300. * @param $pub_dbxref
  301. * The database cross reference accession. It should be in the form
  302. * DB:ACCESSION, where DB is the database name and ACCESSION is the
  303. * unique publication identifier (e.g. PMID:4382934)
  304. *
  305. * @return
  306. * Returns an array of all the publications that have the provided
  307. * cross reference. If no publications match, then an empty array
  308. * is returned.
  309. *
  310. * @ingroup tripal_pub_api
  311. *
  312. */
  313. function tripal_pub_get_pubs_by_dbxref($pub_dbxref) {
  314. $return = array();
  315. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  316. $dbname = $matches[1];
  317. $accession = $matches[2];
  318. $values = array(
  319. 'dbxref_id' => array (
  320. 'accession' => $accession,
  321. 'db_id' => array(
  322. 'name' => $dbname
  323. ),
  324. ),
  325. );
  326. $options = array('statement_name' => 'sel_pubdbxref_db');
  327. $results = tripal_core_chado_select('pub_dbxref', array('pub_id'), $values, $options);
  328. foreach ($results as $index => $pub) {
  329. $return[] = $pub->pub_id;
  330. }
  331. }
  332. return $return;
  333. }
  334. /**
  335. * Returns the list of publications that match a given title and year
  336. *
  337. * @param title
  338. * The title of the publication to look for
  339. * @param type
  340. * The publication type. The value of this field should come from
  341. * the Tripal Pub vocabulary
  342. * @param year
  343. * Optional. The year the publication was published.
  344. *
  345. * @return
  346. * Returns an array of all the publications that have the provided
  347. * cross reference. If no publications match, then an empty array
  348. * is returned.
  349. *
  350. * @ingroup tripal_pub_api
  351. *
  352. */
  353. function tripal_pub_get_pubs_by_title_type_pyear($title, $type, $pyear = '') {
  354. $return = array();
  355. // build the values array for the query. The $pyear is not required.
  356. $values = array(
  357. 'title' => $title,
  358. 'type_id' => array(
  359. 'name' => $type,
  360. 'cv_id' => array(
  361. 'name' => 'tripal_pub'
  362. )
  363. )
  364. );
  365. $stmnt_suffix = 'tity';
  366. if ($pub_details['Year']) {
  367. $values['pyear'] = $pyear;
  368. $stmnt_suffix .= 'py';
  369. }
  370. $options = array('statement_name' => 'sel_pub_' . $stmnt_suffix);
  371. $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
  372. // iterate through any matches and pull out the pub_id
  373. foreach ($results as $index => $pub) {
  374. $return[] = $pub->pub_id;
  375. }
  376. return $return;
  377. }
  378. /**
  379. * Adds a new publication to the Chado, along with all properties and
  380. * database cross-references. If the publication does not already exist
  381. * in Chado then it is added. If it does exist nothing is done. If
  382. * the $update parameter is TRUE then the publication is updated if it exists.
  383. *
  384. * @param $pub_details
  385. * An associative array containing all of the details about the publication.
  386. * @param $do_contact
  387. * Optional. Set to TRUE if a contact entry should be added to the Chado contact table
  388. * for authors of the publication.
  389. * @param $update
  390. * Optional. If the publication already exists then this function will return
  391. * without adding a new publication. However, set this value to TRUE to force
  392. * the function to pudate the publication using the $pub_details that are provided.
  393. *
  394. * @return
  395. * On successful addition of the publication, the new publication ID is returned. If
  396. * the publication already exists but $update is FALSE then TRUE is returned indicating
  397. * that the publication is there already. If $update is TRUE and the publication
  398. * exists then the publication ID is returned.
  399. *
  400. */
  401. function tripal_pub_add_publication($pub_details, $do_contact = FALSE, $update = FALSE) {
  402. $pub_id = 0;
  403. // first try to find the publication using the accession number. It will have
  404. // one if the pub has already been loaded for the publication database
  405. if ($pub_details['Publication Dbxref']) {
  406. $results = tripal_pub_get_pubs_by_dbxref($pub_details['Publication Dbxref']);
  407. if(count($results) == 1) {
  408. $pub_id = $results[0];
  409. }
  410. elseif(count($results) > 1) {
  411. watchdog('tripal_pub', "There are two publications with this accession: %db:%accession. Cannot determine which to update.",
  412. array('%db' => $dbname, '%accession' => $accession), WATCHDOG_ERROR);
  413. return FALSE;
  414. }
  415. // If we found the publication and we do not want to do the update then
  416. // return true to indicate the publication has been added
  417. if (!$update and $pub_id) {
  418. return TRUE;
  419. }
  420. }
  421. // if we couldn't find a publication by the accession (which means it doesn't
  422. // yet exist or it has been added using a different publication database) then
  423. // try to find it using the title and publication year.
  424. elseif ($pub_details['Title']) {
  425. $results = tripal_pub_get_pubs_by_title_type_pyear($pub_details['Title'], $pub_details['Publication Type'], $pub_details['Year']);
  426. if (count($results) == 1) {
  427. $pub_id = $results[0];
  428. }
  429. elseif (count($results) > 1) {
  430. watchdog('tripal_pub', "The publication with the same title, type and year is present multiple times. Cannot ".
  431. "determine which to use. Title: '%title'. Type: '%type'. Year: '%year'",
  432. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_ERROR);
  433. return FALSE;
  434. }
  435. // If we found the publication and we do not want to do the update then
  436. // return true to indicate the publication has been added
  437. if (!$update and $pub_id) {
  438. return TRUE;
  439. }
  440. }
  441. // if we couldn't find the publication using the database accession or the title/year then add it
  442. if(!$pub_id) {
  443. // get the publication type (use the first publication type, any others will get stored as properties)
  444. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
  445. if (!$pub_type) {
  446. watchdog('tripal_pub', "Cannot find publication type: '%type'",
  447. array('%type' => $pub_details['Publication Type'][0]), WATCHDOG_ERROR);
  448. return FALSE;
  449. }
  450. // if the publication does not exist then create it.
  451. $values = array(
  452. 'title' => $pub_details['Title'],
  453. 'volume' => $pub_details['Volume'],
  454. 'series_name' => $pub_details['Journal Name'],
  455. 'issue' => $pub_details['Issue'],
  456. 'pyear' => $pub_details['Year'],
  457. 'pages' => $pub_details['Pages'],
  458. 'uniquename' => $pub_details['Citation'],
  459. 'type_id' => $pub_type->cvterm_id,
  460. );
  461. $options = array('statement_name' => 'ins_pub_tivoseispypaunty');
  462. $pub = tripal_core_chado_insert('pub', $values, $options);
  463. if (!$pub) {
  464. watchdog('tripal_pub', "Cannot insert the publication with title: %title",
  465. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  466. return FALSE;
  467. }
  468. $pub_id = $pub['pub_id'];
  469. }
  470. // before we add any new properties we need to remove those that are there if this
  471. // is an update. The only thing we don't want to remove are the 'Publication Dbxref'
  472. if($update) {
  473. $sql = "
  474. DELETE FROM {pubprop}
  475. WHERE
  476. pub_id = %d AND
  477. NOT type_id in (
  478. SELECT cvterm_id
  479. FROM {cvterm}
  480. WHERE name = 'Publication Dbxref'
  481. )
  482. ";
  483. chado_query($sql, $pub_id);
  484. }
  485. foreach ($pub_details as $key => $value) {
  486. // get the cvterm by name or synonym
  487. $cvterm = tripal_cv_get_cvterm_by_name($key, NULL, 'tripal_pub');
  488. if (!$cvterm) {
  489. $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
  490. }
  491. if (!$cvterm) {
  492. watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", array('%prop' => $key), WATCHDOG_ERROR);
  493. continue;
  494. }
  495. // skip details that won't be stored as properties
  496. if ($key == 'Author List') {
  497. tripal_pub_add_authors($pub_id, $value, $do_contact);
  498. continue;
  499. }
  500. if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
  501. $key == 'Year' or $key == 'Pages') {
  502. continue;
  503. }
  504. $success = 0;
  505. if (is_array($value)) {
  506. foreach ($value as $subkey => $subvalue) {
  507. // if the key is an integer then this array is a simple list and
  508. // we will insert using the primary key. Otheriwse, use the new key
  509. if(is_int($subkey)) {
  510. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $subvalue, FALSE);
  511. }
  512. else {
  513. $success = tripal_core_insert_property('pub', $pub_id, $subkey, 'tripal_pub', $subvalue, FALSE);
  514. }
  515. }
  516. }
  517. else {
  518. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
  519. }
  520. if (!$success) {
  521. watchdog('tripal_pub', "Cannot add property '%prop' to publication. Skipping.",
  522. array('%prop' => $key), WATCHDOG_ERROR);
  523. continue;
  524. }
  525. }
  526. return $pub_id;
  527. }
  528. /*
  529. *
  530. */
  531. function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
  532. $rank = 0;
  533. // first remove any of the existing pubauthor entires
  534. $sql = "DELETE FROM {pubauthor} WHERE pub_id = %d";
  535. chado_query($sql, $pub_id);
  536. // iterate through the authors and add them to the pubauthors and contact
  537. // tables of chado, then link them through the custom pubauthors_contact table
  538. foreach ($authors as $author) {
  539. // skip invalid author entires
  540. if ($author['valid'] == 'N') {
  541. continue;
  542. }
  543. // remove the 'valid' property as we don't have a CV term for it
  544. unset($author['valid']);
  545. // construct the contact.name field using the author information
  546. $name = '';
  547. $type = 'Person';
  548. if ($author['Given Name']) {
  549. $name .= $author['Given Name'];
  550. }
  551. if ($author['Surname']) {
  552. $name .= ' ' . $author['Surname'];
  553. }
  554. if ($author['Suffix']) {
  555. $name .= ' ' . $author['Suffix'];
  556. }
  557. if ($author['Collective']) {
  558. $name = $author['Collective'];
  559. $type = 'Collective';
  560. }
  561. $name = trim($name);
  562. // add an entry to the pubauthors table
  563. $values = array(
  564. 'pub_id' => $pub_id,
  565. 'rank' => $rank,
  566. 'surname' => $author['Surname'] ? $author['Surname'] : $author['Collective'],
  567. 'givennames' => $author['Given Name'],
  568. 'suffix' => $author['Suffix'],
  569. );
  570. $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
  571. $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
  572. // if the user wants us to create a contact for each author then do it.
  573. if ($do_contact) {
  574. // Add the contact
  575. $contact = tripal_contact_add_contact($name, '', $type, $author);
  576. // if we have succesfully added the contact and the pubauthor entries then we want to
  577. // link them together
  578. if ($contact and $pubauthor) {
  579. // link the pubauthor entry to the contact
  580. $values = array(
  581. 'pubauthor_id' => $pubauthor['pubauthor_id'],
  582. 'contact_id' => $contact['contact_id'],
  583. );
  584. $options = array('statement_name' => 'ins_pubauthorcontact_puco');
  585. $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
  586. if (!$pubauthor_contact) {
  587. watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
  588. }
  589. }
  590. }
  591. $rank++;
  592. }
  593. }
  594. /**
  595. * Retrieve properties of a given type for a given pub
  596. *
  597. * @param $pub_id
  598. * The pub_id of the properties you would like to retrieve
  599. * @param $property
  600. * The cvterm name of the properties to retrieve
  601. *
  602. * @return
  603. * An pub chado variable with the specified properties expanded
  604. *
  605. * @ingroup tripal_pub_api
  606. */
  607. function tripal_pub_get_property($pub_id, $property) {
  608. return tripal_core_get_property('pub', $pub_id, $property, 'tripal');
  609. }
  610. /**
  611. * Insert a given property
  612. *
  613. * @param $pub_id
  614. * The pub_id of the property to insert
  615. * @param $property
  616. * The cvterm name of the property to insert
  617. * @param $value
  618. * The value of the property to insert
  619. * @param $update_if_present
  620. * A boolean indicated whether to update the record if it's already present
  621. *
  622. * @return
  623. * True of success, False otherwise
  624. *
  625. * @ingroup tripal_pub_api
  626. */
  627. function tripal_pub_insert_property($pub_id, $property, $value, $update_if_present = 0) {
  628. return tripal_core_insert_property('pub', $pub_id, $property, 'tripal_pub', $value, $update_if_present);
  629. }
  630. /**
  631. * Update a given property
  632. *
  633. * @param $pub_id
  634. * The pub_id of the property to update
  635. * @param $property
  636. * The cvterm name of the property to update
  637. * @param $value
  638. * The value of the property to update
  639. * @param $insert_if_missing
  640. * A boolean indicated whether to insert the record if it's absent
  641. *
  642. * Note: The property will be identified using the unique combination of the $pub_id and $property
  643. * and then it will be updated with the supplied value
  644. *
  645. * @return
  646. * True of success, False otherwise
  647. *
  648. * @ingroup tripal_pub_api
  649. */
  650. function tripal_pub_update_property($pub_id, $property, $value, $insert_if_missing = 0) {
  651. return tripal_core_update_property('pub', $pub_id, $property, 'tripal_pub', $value, $insert_if_missing);
  652. }
  653. /**
  654. * Delete a given property
  655. *
  656. * @param $pub_id
  657. * The pub_id of the property to delete
  658. * @param $property
  659. * The cvterm name of the property to delete
  660. *
  661. * Note: The property will be identified using the unique combination of the $pub_id and $property
  662. * and then it will be deleted
  663. *
  664. * @return
  665. * True of success, False otherwise
  666. *
  667. * @ingroup tripal_pub_api
  668. */
  669. function tripal_pub_delete_property($pub_id, $property) {
  670. return tripal_core_delete_property('pub', $pub_id, $property, 'tripal');
  671. }