tripal_pub.chado_node.inc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. <?php
  2. /**
  3. * Implementation of hook_tripal_pub_node_info().
  4. *
  5. * This node_info, is a simple node that describes the functionallity of the module.
  6. *
  7. */
  8. function tripal_pub_node_info() {
  9. return array(
  10. 'chado_pub' => array(
  11. 'name' => t('Publication'),
  12. 'base' => 'chado_pub',
  13. 'description' => t('A publication from the Chado database'),
  14. 'title_label' => t('Article Title'),
  15. 'body_label' => t('Abstract'),
  16. 'has_title' => TRUE,
  17. 'has_body' => FALSE,
  18. 'chado_node_api' => array(
  19. 'base_table' => 'pub',
  20. 'hook_prefix' => 'chado_pub',
  21. 'record_type_title' => array(
  22. 'singular' => t('Publication'),
  23. 'plural' => t('Publications')
  24. ),
  25. 'sync_filters' => array(
  26. 'type_id' => FALSE,
  27. 'organism_id' => FALSE
  28. ),
  29. ),
  30. ),
  31. );
  32. }
  33. /**
  34. * This is the chado_pub node form callback. The arguments
  35. * are out of order from a typical form because it's a defined callback
  36. */
  37. function chado_pub_form($node, $form_state) {
  38. $form = array();
  39. // Default values can come in the following ways:
  40. //
  41. // 1) as elements of the $node object. This occurs when editing an existing pub
  42. // 2) in the $form_state['values'] array which occurs on a failed validation or
  43. // ajax callbacks from non submit form elements
  44. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  45. // form elements and the form is being rebuilt
  46. //
  47. // set form field defaults
  48. $pub_id = null;
  49. $title = '';
  50. $pyear = '';
  51. $uniquename = '';
  52. $type_id = '';
  53. $is_obsolete = '';
  54. // some of the fields in the pub table should show up in the properties
  55. // form elements to make the form more seemless. We will add them
  56. // to this array.
  57. $more_props = array();
  58. // if we are editing an existing node then the pub is already part of the node
  59. if (property_exists($node, 'pub')) {
  60. $pub = $node->pub;
  61. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  62. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  63. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  64. $pub_id = $pub->pub_id;
  65. $title = $pub->title;
  66. $pyear = $pub->pyear;
  67. $uniquename = $pub->uniquename;
  68. $type_id = $pub->type_id->cvterm_id;
  69. $is_obsolete = $pub->is_obsolete;
  70. // if the obsolete value is set by the database then it is in the form of
  71. // 't' or 'f', we need to convert to 1 or 0
  72. $is_obsolete = $is_obsolete == 't' ? 1 : $is_obsolete;
  73. $is_obsolete = $is_obsolete == 'f' ? 0 : $is_obsolete;
  74. // set the organism_id in the form
  75. $form['pub_id'] = array(
  76. '#type' => 'value',
  77. '#value' => $pub->pub_id,
  78. );
  79. // get fields from the pub table and convert them to properties. We will add these to the $more_props
  80. // array which gets passed in to the tripal_core_properties_form() API call further down
  81. if ($pub->volumetitle) {
  82. $cvterm = tripal_cv_get_cvterm_by_name('Volume Title', NULL, 'tripal_pub');
  83. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volumetitle);
  84. }
  85. if ($pub->volume) {
  86. $cvterm = tripal_cv_get_cvterm_by_name('Volume', NULL, 'tripal_pub');
  87. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volume);
  88. }
  89. if ($pub->series_name) {
  90. switch ($pub->type_id->name) {
  91. case 'Journal Article':
  92. $cvterm = tripal_cv_get_cvterm_by_name('Journal Name', NULL, 'tripal_pub');
  93. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  94. break;
  95. case 'Conference Proceedings':
  96. $cvterm = tripal_cv_get_cvterm_by_name('Conference Name', NULL, 'tripal_pub');
  97. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  98. break;
  99. default:
  100. $cvterm = tripal_cv_get_cvterm_by_name('Series Name', NULL, 'tripal_pub');
  101. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  102. }
  103. }
  104. if ($pub->issue) {
  105. $cvterm = tripal_cv_get_cvterm_by_name('Issue', NULL, 'tripal_pub');
  106. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->issue);
  107. }
  108. if ($pub->pages) {
  109. $cvterm = tripal_cv_get_cvterm_by_name('Pages', NULL, 'tripal_pub');
  110. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  111. }
  112. if ($pub->miniref) {
  113. // not sure what to do with this one
  114. }
  115. if ($pub->publisher) {
  116. $cvterm = tripal_cv_get_cvterm_by_name('Publisher', NULL, 'tripal_pub');
  117. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->publisher);
  118. }
  119. if ($pub->pubplace) {
  120. $cvterm = tripal_cv_get_cvterm_by_name('Published Location', NULL, 'tripal_pub');
  121. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  122. }
  123. }
  124. // if we are re constructing the form from a failed validation or ajax callback
  125. // then use the $form_state['values'] values
  126. if (array_key_exists('values', $form_state)) {
  127. $title = $form_state['values']['pubtitle'];
  128. $pyear = $form_state['values']['pyear'];
  129. $uniquename = $form_state['values']['uniquename'];
  130. $type_id = $form_state['values']['type_id'];
  131. $is_obsolete = $form_state['values']['is_obsolete'];
  132. }
  133. // if we are re building the form from after submission (from ajax call) then
  134. // the values are in the $form_state['input'] array
  135. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  136. $title = $form_state['input']['pubtitle'];
  137. $uniquename = $form_state['input']['uniquename'];
  138. $type_id = $form_state['input']['type_id'];
  139. $is_obsolete = $form_state['input']['is_obsolete'];
  140. }
  141. // a drupal title can only be 255 characters, but the Chado title can be much longer.
  142. // we use the publication title as the drupal title, but we'll need to truncate it.
  143. $form['title'] = array(
  144. '#type' => 'hidden',
  145. '#value' => substr($title, 0, 255),
  146. );
  147. $form['pubtitle'] = array(
  148. '#type' => 'textarea',
  149. '#title' => t('Publication Title'),
  150. '#default_value' => $title,
  151. '#required' => TRUE,
  152. );
  153. // get the list of publication types. In the Tripal publication
  154. // ontologies these are all grouped under the term 'Publication Type'
  155. // we want the default to be 'Journal Article'
  156. $sql = "
  157. SELECT
  158. CVTS.cvterm_id, CVTS.name
  159. FROM {cvtermpath} CVTP
  160. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  161. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  162. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  163. WHERE
  164. CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
  165. NOT CVTS.is_obsolete = 1
  166. ORDER BY CVTS.name ASC
  167. ";
  168. $results = chado_query($sql);
  169. $pub_types = array();
  170. while ($pub_type = $results->fetchObject()) {
  171. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  172. // if we don't have a default type then set the default to be 'Journal Article'
  173. if (strcmp($pub_type->name,"Journal Article") == 0 and !$type_id) {
  174. $type_id = $pub_type->cvterm_id;
  175. }
  176. }
  177. $form['type_id'] = array(
  178. '#type' => 'select',
  179. '#title' => t('Publication Type'),
  180. '#options' => $pub_types,
  181. '#required' => TRUE,
  182. '#default_value' => $type_id,
  183. );
  184. $form['pyear'] = array(
  185. '#type' => 'textfield',
  186. '#title' => t('Publication Year'),
  187. '#default_value' => $pyear,
  188. '#required' => TRUE,
  189. '#size' => 5,
  190. '#description' => t('Enter the year of publication. Also, if available, please add a <b>Publication Date</b> property to specify the full date of publication.'),
  191. );
  192. $form['uniquename'] = array(
  193. '#type' => 'textarea',
  194. '#title' => t('Citation'),
  195. '#default_value' => $uniquename,
  196. '#description' => t('All publications must have a unique citation.
  197. <b>Please enter the full citation for this publication or leave blank and one will be generated
  198. automatically if possible</b>. For PubMed style citations list
  199. the last name of the author followed by initials. Each author should be separated by a comma. Next comes
  200. the title, followed by the series title (e.g. journal name), publication date (4 digit year, 3 character Month, day), volume, issue and page numbers. You may also use HTML to provide a link in the citation.
  201. Below is an example: <pre>Medeiros PM, Ladio AH, Santos AM, Albuquerque UP. <a href="http://www.ncbi.nlm.nih.gov/pubmed/23462414" target="_blank">Does the selection of medicinal plants by Brazilian local populations
  202. suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
  203. );
  204. $form['is_obsolete'] = array(
  205. '#type' => 'checkbox',
  206. '#title' => t('Is Obsolete? (Check for Yes)'),
  207. '#default_value' => $is_obsolete,
  208. );
  209. // Properties Form
  210. // ----------------------------------
  211. // Need to pass in our own select_options since we use cvtermpath to filter ours
  212. $select_options = array();
  213. $select_options[] = 'Select a Property';
  214. $sql = "
  215. SELECT
  216. DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  217. FROM {cvtermpath} CVTP
  218. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  219. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  220. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  221. WHERE CV.name = 'tripal_pub' and
  222. (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
  223. NOT CVTS.is_obsolete = 1
  224. ORDER BY CVTS.name ASC
  225. ";
  226. $prop_types = chado_query($sql);
  227. while ($prop = $prop_types->fetchObject()) {
  228. // add all properties except the Citation. That property is set via the uniquename field
  229. if ($prop->name != 'Citation') {
  230. $select_options[$prop->cvterm_id] = $prop->name;
  231. }
  232. }
  233. $details = array(
  234. 'property_table' => 'pubprop',
  235. 'base_foreign_key' => 'pub_id',
  236. 'base_key_value' => $pub_id,
  237. 'cv_name' => 'tripal_pub',
  238. 'select_options' => $select_options,
  239. );
  240. chado_node_properties_form($form, $form_state, $details);
  241. // RELATIONSHIPS FORM
  242. //---------------------------------------------
  243. // We want to use the contact_relationship_types cv if there are any terms available
  244. // and if not, to default to the relationship ontology
  245. $cv_result = tripal_core_chado_select('cv',array('cv_id'),array('name' => 'pub_relationship_types'));
  246. $cv_id = $cv_result[0]->cv_id;
  247. $select_options = tripal_cv_get_cvterm_options($cv_id);
  248. if (empty($select_options)) {
  249. $cv_result = tripal_core_chado_select('cv',array('cv_id'),array('name' => 'relationship'));
  250. $cv_id = $cv_result[0]->cv_id;
  251. $select_options = tripal_cv_get_cvterm_options($cv_id);
  252. }
  253. // D7 @TODO: tell tripal admin's about this
  254. $details = array(
  255. 'relationship_table' => 'pub_relationship', // the name of the _relationship table
  256. 'base_table' => 'pub', // the name of your chado base table
  257. 'base_foreign_key' => 'pub_id', // the name of the key in your base chado table
  258. 'base_key_value' => $pub_id, // the value of example_id for this record
  259. 'nodetype' => 'pub', // the human-readable name of your node type
  260. 'cv_name' => 'pub_relationship_types', // the cv.name of the cv governing example_relationship.type_id
  261. 'base_name_field' => 'uniquename', // the base table field you want to be used as the name
  262. 'select_options' => $select_options
  263. );
  264. // Adds the form elements to your current form
  265. chado_node_relationships_form($form, $form_state, $details);
  266. return $form;
  267. }
  268. /*
  269. *
  270. */
  271. function chado_pub_validate($node, $form, &$form_state) {
  272. // get the submitted values
  273. $title = trim($node->pubtitle);
  274. $pyear = trim($node->pyear);
  275. $uniquename = trim($node->uniquename);
  276. $is_obsolete = $node->is_obsolete;
  277. $type_id = $node->type_id;
  278. // if this is a delete then don't validate
  279. if($node->op == 'Delete') {
  280. return;
  281. }
  282. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  283. // need to validate during syncing so just skip it.
  284. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  285. return;
  286. }
  287. $pub = array();
  288. // make sure the year is four digits
  289. if(!preg_match('/^\d{4}$/', $pyear)){
  290. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  291. return;
  292. }
  293. // get the type of publication
  294. $values = array('cvterm_id' => $type_id);
  295. $options = array('statement_name' => 'sel_pub_ty');
  296. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
  297. if (count($cvterm) == 0) {
  298. $message = t('Invalid publication type.');
  299. form_set_error('type_id', $message);
  300. return;
  301. }
  302. // get the media name looking at the properties
  303. $series_name = '';
  304. foreach ($node as $element => $value) {
  305. // if this is an existing property (either previously in the database or
  306. // added via AHAH/AJAX callback)
  307. if (preg_match('/^prop_value-(\d+)-(\d+)$/', $element, $matches)) {
  308. $prop_type_id = $matches[1];
  309. $prop_type = tripal_cv_get_cvterm_by_id($prop_type_id);
  310. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  311. $series_name = $value;
  312. }
  313. if($prop_type->name == 'Citation') {
  314. $uniquename = $value;
  315. }
  316. $pub[$prop_type->name] = $value;
  317. }
  318. // if this is a new property (added by this submit of the form)
  319. elseif ($element == 'new_id') {
  320. $prop_type = tripal_cv_get_cvterm_by_id($value);
  321. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  322. $series_name = $node->new_value;
  323. }
  324. if($prop_type->name == 'Citation') {
  325. $uniquename = $node->new_value;
  326. }
  327. $pub[$prop_type->name] = $node->new_value;
  328. }
  329. }
  330. // if the citation is missing then try to generate one
  331. if (!$uniquename) {
  332. $pub['Title'] = $title;
  333. $pub['Publication Type'][0] = $cvterm[0]->name;
  334. $pub['Year'] = $pyear;
  335. $uniquename = tripal_pub_create_citation($pub);
  336. if (!$uniquename) {
  337. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  338. }
  339. }
  340. $skip_duplicate_check = 0;
  341. // if this publication is a Patent then skip the validation below. Patents can have the title
  342. // name and year but be different
  343. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  344. $skip_duplicate_check = 1;
  345. }
  346. // Validating for an update
  347. if (!is_null($node->nid)) {
  348. $pub_id = $node->pub_id;
  349. // first get the original title, type and year before it was changed
  350. $values = array('pub_id' => $pub_id);
  351. $columns = array('title', 'pyear', 'type_id', 'series_name');
  352. $options = array('statement_name' => 'sel_pub_id');
  353. $pub = tripal_core_chado_select('pub', $columns, $values, $options);
  354. // if the title, type, year or series_name have changed then check the pub
  355. // to see if it is a duplicate of another
  356. if((strcmp(strtolower($pub[0]->title), strtolower($title)) == 0) and
  357. (strcmp(strtolower($pub[0]->series_name), strtolower($series_name)) == 0) and
  358. ($pub[0]->type_id == $type_id) and
  359. ($pub[0]->pyear == $pyear)) {
  360. $skip_duplicate_check = 1;
  361. }
  362. // check to see if a duplicate publication already exists
  363. if (!$skip_duplicate_check) {
  364. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id);
  365. }
  366. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  367. }
  368. // Validating for an insert
  369. else {
  370. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm);
  371. chado_pub_validate_check_uniquename($uniquename);
  372. }
  373. }
  374. /**
  375. *
  376. * @param unknown $uniquename
  377. */
  378. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  379. $results = tripal_pub_get_pub_by_uniquename($uniquename);
  380. // make sure we don't capture our pub_id in the list (remove it)
  381. foreach ($results as $index => $found_pub_id) {
  382. if($found_pub_id == $pub_id){
  383. unset($results[$index]);
  384. }
  385. }
  386. if (count($results) > 0) {
  387. $message = t('A publication with this unique citation already exists.');
  388. form_set_error('uniquename', $message);
  389. }
  390. }
  391. /**
  392. *
  393. */
  394. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  395. // make sure the publication is unique using the prefereed import duplication check
  396. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  397. switch ($import_dups_check) {
  398. case 'title_year':
  399. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, NULL);
  400. // make sure we don't capture our pub_id in the list (remove it)
  401. foreach ($results as $index => $found_pub_id) {
  402. if($found_pub_id == $pub_id){
  403. unset($results[$index]);
  404. }
  405. }
  406. if (count($results) > 0) {
  407. $message = t('A publication with this title and publication year, already exists.');
  408. form_set_error('pyear', $message);
  409. }
  410. break;
  411. case 'title_year_type':
  412. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, $cvterm[0]->name, $pyear, NULL);
  413. // make sure we don't capture our pub_id in the list (remove it)
  414. foreach ($results as $index => $found_pub_id) {
  415. if($found_pub_id == $pub_id){
  416. unset($results[$index]);
  417. }
  418. }
  419. if (count($results) > 0) {
  420. $message = t('A publication with this title, type and publication year, already exists.');
  421. form_set_error('pyear', $message);
  422. }
  423. break;
  424. case 'title_year_media':
  425. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, $series_name);
  426. // make sure we don't capture our pub_id in the list (remove it)
  427. foreach ($results as $index => $found_pub_id) {
  428. if($found_pub_id == $pub_id){
  429. unset($results[$index]);
  430. }
  431. }
  432. if (count($results) > 0) {
  433. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year, already exists.');
  434. form_set_error('pyear', $message);
  435. }
  436. break;
  437. }
  438. }
  439. /**
  440. * Implement hook_access().
  441. *
  442. * This hook allows node modules to limit access to the node types they define.
  443. *
  444. * @param $node
  445. * The node on which the operation is to be performed, or, if it does not yet exist, the
  446. * type of node to be created
  447. *
  448. * @param $op
  449. * The operation to be performed
  450. *
  451. * @param $account
  452. * A user object representing the user for whom the operation is to be performed
  453. *
  454. * @return
  455. * If the permission for the specified operation is not set then return FALSE. If the
  456. * permission is set then return NULL as this allows other modules to disable
  457. * access. The only exception is when the $op == 'create'. We will always
  458. * return TRUE if the permission is set.
  459. *
  460. */
  461. function chado_pub_node_access($node, $op, $account ) {
  462. if ($op == 'create') {
  463. if (!user_access('create chado_pub content', $account)) {
  464. return FALSE;
  465. }
  466. return TRUE;
  467. }
  468. if ($op == 'update') {
  469. if (!user_access('edit chado_pub content', $account)) {
  470. return FALSE;
  471. }
  472. }
  473. if ($op == 'delete') {
  474. if (!user_access('delete chado_pub content', $account)) {
  475. return FALSE;
  476. }
  477. }
  478. if ($op == 'view') {
  479. if (!user_access('access chado_pub content', $account)) {
  480. return FALSE;
  481. }
  482. }
  483. return NULL;
  484. }
  485. /**
  486. * Implementation of tripal_pub_insert().
  487. *
  488. * This function inserts user entered information pertaining to the Publication instance into the
  489. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  490. *
  491. * @parm $node
  492. * Then node which contains the information stored within the node-ID
  493. *
  494. *
  495. */
  496. function chado_pub_insert($node) {
  497. $title = trim($node->pubtitle);
  498. $pyear = trim($node->pyear);
  499. $uniquename = trim($node->uniquename);
  500. $is_obsolete = $node->is_obsolete;
  501. $type_id = $node->type_id;
  502. // we need an array suitable for the tripal_pub_create_citation() function
  503. // to automatically generate a citation if a uniquename doesn't already exist
  504. $pub_arr = array();
  505. // if there is an pub_id in the $node object then this must be a sync so
  506. // we can skip adding the pub as it is already there, although
  507. // we do need to proceed with the rest of the insert
  508. if (!property_exists($node, 'pub_id')) {
  509. $properties = array(); // stores all of the properties we need to add
  510. $cross_refs = array(); // stores any cross references for this publication
  511. // get the properties from the form
  512. $properties = chado_node_properties_form_retreive($node);
  513. // get the list of properties for easy lookup (without doing lots of database queries
  514. $properties_list = array();
  515. $sql = "
  516. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  517. FROM {cvtermpath} CVTP
  518. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  519. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  520. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  521. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  522. NOT CVTS.is_obsolete = 1
  523. ORDER BY CVTS.name ASC
  524. ";
  525. $prop_types = chado_query($sql);
  526. while ($prop = $prop_types->fetchObject()) {
  527. $properties_list[$prop->cvterm_id] = $prop->name;
  528. // The 'Citation' term is special because it serves
  529. // both as a property and as the uniquename for the
  530. // pub and we want it stored in both the pub table and the pubprop table
  531. if ($prop->name == 'Citation') {
  532. $citation_id = $prop->cvterm_id;
  533. if (!empty($node->uniquename)) {
  534. $properties[$citation_id] = $node->uniquename;
  535. }
  536. }
  537. }
  538. // iterate through all of the properties and remove those that really are
  539. // part of the pub table fields
  540. $volume = '';
  541. $volumetitle = '';
  542. $issue = '';
  543. $pages = '';
  544. $publisher = '';
  545. $series_name = '';
  546. $pubplace = '';
  547. $miniref = '';
  548. $cross_refs = array();
  549. foreach ($properties as $type_id => $element) {
  550. $value = trim($element[0]);
  551. $name = $properties_list[$type_id];
  552. // populate our $pub_array for building a citation
  553. $pub_arr[$name] = $value;
  554. // remove properties that are stored in the pub table
  555. if ($name == "Volume") {
  556. $volume = $value;
  557. unset($properties[$type_id]);
  558. }
  559. elseif ($name == "Volume Title") {
  560. $volumetitle = $value;
  561. unset($properties[$type_id]);
  562. }
  563. elseif ($name == "Issue") {
  564. $issue = $value;
  565. unset($properties[$type_id]);
  566. }
  567. elseif ($name == "Pages") {
  568. $pages = $value;
  569. unset($properties[$type_id]);
  570. }
  571. elseif ($name == "Publisher") {
  572. $publisher = $value;
  573. unset($properties[$type_id]);
  574. }
  575. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  576. $series_name = $value;
  577. unset($properties[$type_id]);
  578. }
  579. elseif ($name == "Journal Country" or $name == "Published Location") {
  580. $pubplace = $value;
  581. // allow this property to go into the pubprop table so we don't loose info
  582. // so don't unset it. But it will also go into the pub.pubplace field
  583. }
  584. elseif ($name == "Publication Dbxref") {
  585. // we will add the cross-references to the pub_dbxref table
  586. // but we also want to keep the property in the pubprop table so don't unset it
  587. $cross_refs[] = $value;
  588. }
  589. }
  590. // generate a citation for this pub if one doesn't already exist
  591. if (!$node->uniquename and !array_key_exists($citation_id, $properties)) {
  592. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  593. $pub_arr['Title'] = $node->pubtitle;
  594. $pub_arr['Publication Type'][0] = $pub_type->name;
  595. $pub_arr['Year'] = $node->pyear;
  596. $node->uniquename = tripal_pub_create_citation($pub_arr);
  597. $properties[$citation_id][0] = $node->uniquename;
  598. }
  599. // insert the pub record
  600. $values = array(
  601. 'title' => $node->pubtitle,
  602. 'series_name' => substr($series_name, 0, 255),
  603. 'type_id' => $node->type_id,
  604. 'pyear' => $node->pyear,
  605. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  606. 'uniquename' => $node->uniquename,
  607. 'volumetitle' => $volumetitle,
  608. 'volume' => $volume,
  609. 'issue' => $issue,
  610. 'pages' => $pages,
  611. 'miniref' => substr($miniref, 0, 255),
  612. 'publisher' => substr($publisher, 0, 255),
  613. 'pubplace' => substr($pubplace, 0, 255),
  614. );
  615. $pub = tripal_core_chado_insert('pub', $values);
  616. if (!$pub) {
  617. drupal_set_message("Error inserting publication", "error");
  618. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  619. return;
  620. }
  621. $pub_id = $pub['pub_id'];
  622. // now add in the properties
  623. // Only adds in those not used in the pub record
  624. $details = array(
  625. 'property_table' => 'pubprop',
  626. 'base_table' => 'pub',
  627. 'foreignkey_name' => 'pub_id',
  628. 'foreignkey_value' => $pub_id
  629. );
  630. chado_node_properties_form_update_properties($node, $details, $properties);
  631. // * Relationships Form *
  632. $details = array(
  633. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  634. 'foreignkey_value' => $pub_id // value of the pub_id key
  635. );
  636. chado_node_relationships_form_update_relationships($node, $details);
  637. // add in any database cross-references
  638. foreach ($cross_refs as $index => $ref) {
  639. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  640. if (!$pub_dbxref) {
  641. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  642. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  643. array('%ref' => $ref), WATCHDOG_ERROR);
  644. }
  645. }
  646. }
  647. else {
  648. $pub_id = $node->pub_id;
  649. }
  650. // Make sure the entry for this pub doesn't already exist in the
  651. // chado_pub table if it doesn't exist then we want to add it.
  652. $check_org_id = chado_get_id_for_node('pub', $node->nid);
  653. if (!$check_org_id) {
  654. $record = new stdClass();
  655. $record->nid = $node->nid;
  656. $record->vid = $node->vid;
  657. $record->pub_id = $pub_id;
  658. drupal_write_record('chado_pub', $record);
  659. }
  660. }
  661. /*
  662. *
  663. * Implements hook_update
  664. *
  665. * The purpose of the function is to allow the module to take action when an edited node is being
  666. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  667. * As well, the database will be changed, so the user changed information will be saved to the database.
  668. *
  669. * @param $node
  670. * The node being updated
  671. *
  672. * @ingroup tripal_pub
  673. */
  674. function chado_pub_update($node) {
  675. $title = trim($node->pubtitle);
  676. $pyear = trim($node->pyear);
  677. $uniquename = trim($node->uniquename);
  678. $is_obsolete = $node->is_obsolete;
  679. $type_id = $node->type_id;
  680. // we need an array suitable for the tripal_pub_create_citation() function
  681. // to automatically generate a citation if a uniquename doesn't already exist
  682. $pub_arr = array();
  683. // get the publication ID for this publication
  684. $pub_id = chado_get_id_for_node('pub', $node->nid) ;
  685. $properties = array(); // stores all of the properties we need to add
  686. $cross_refs = array(); // stores any cross references for this publication
  687. // get the properties from the form
  688. $properties = chado_node_properties_form_retreive($node);
  689. // get the list of properties for easy lookup (without doing lots of database queries
  690. $properties_list = array();
  691. $sql = "
  692. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  693. FROM {cvtermpath} CVTP
  694. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  695. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  696. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  697. WHERE CV.name = 'tripal_pub' and
  698. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  699. NOT CVTS.is_obsolete = 1
  700. ORDER BY CVTS.name ASC
  701. ";
  702. $prop_types = chado_query($sql);
  703. while ($prop = $prop_types->fetchObject()) {
  704. $properties_list[$prop->cvterm_id] = $prop->name;
  705. // The 'Citation' term is special because it serves
  706. // both as a property and as the uniquename for the
  707. // pub and we want it stored in both the pub table and the pubprop table
  708. if ($prop->name == 'Citation') {
  709. $citation_id = $prop->cvterm_id;
  710. if (!empty($node->uniquename)) {
  711. $properties[$citation_id][0] = $node->uniquename;
  712. }
  713. }
  714. }
  715. // iterate through all of the properties and remove those that really are
  716. // part of the pub table fields
  717. $volume = '';
  718. $volumetitle = '';
  719. $issue = '';
  720. $pages = '';
  721. $publisher = '';
  722. $series_name = '';
  723. $pubplace = '';
  724. $miniref = '';
  725. $cross_refs = array();
  726. foreach ($properties as $type_id => $element) {
  727. foreach ($element as $index => $value) {
  728. $name = $properties_list[$type_id];
  729. // populate our $pub_array for building a citation
  730. $pub_arr[$name] = $value;
  731. // remove properties that are stored in the pub table
  732. if ($name == "Volume") {
  733. $volume = $value;
  734. unset($properties[$type_id]);
  735. }
  736. elseif ($name == "Volume Title") {
  737. $volumetitle = $value;
  738. unset($properties[$type_id]);
  739. }
  740. elseif ($name == "Issue") {
  741. $issue = $value;
  742. unset($properties[$type_id]);
  743. }
  744. elseif ($name == "Pages") {
  745. $pages = $value;
  746. unset($properties[$type_id]);
  747. }
  748. elseif ($name == "Publisher") {
  749. $publisher = $value;
  750. unset($properties[$type_id]);
  751. }
  752. elseif ($name == "Journal Name" or $name == "Conference Name") {
  753. $series_name = $value;
  754. unset($properties[$type_id]);
  755. }
  756. elseif ($name == "Journal Country" or $name == "Published Location") {
  757. $pubplace = $value;
  758. // allow this property to go into the pubprop table so we don't loose info
  759. // so don't unset it. But it will also go into the pub.pubplace field
  760. }
  761. elseif ($name == "Publication Dbxref") {
  762. // we will add the cross-references to the pub_dbxref table
  763. // but we also want to keep the property in the pubprop table so don't unset it
  764. $cross_refs[] = $value;
  765. }
  766. }
  767. }
  768. // generate a citation for this pub if one doesn't already exist
  769. if (!$node->uniquename) {
  770. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  771. $pub_arr['Title'] = $node->pubtitle;
  772. $pub_arr['Publication Type'][0] = $pub_type->name;
  773. $pub_arr['Year'] = $node->pyear;
  774. $node->uniquename = tripal_pub_create_citation($pub_arr);
  775. $properties[$citation_id][0] = $node->uniquename;
  776. }
  777. // update the pub record
  778. $match = array(
  779. 'pub_id' => $pub_id,
  780. );
  781. $values = array(
  782. 'title' => $node->pubtitle,
  783. 'type_id' => $node->type_id,
  784. 'pyear' => $node->pyear,
  785. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  786. 'uniquename' => $node->uniquename,
  787. 'series_name' => substr($series_name, 0, 255),
  788. 'volumetitle' => $volumetitle,
  789. 'volume' => $volume,
  790. 'issue' => $issue,
  791. 'pages' => $pages,
  792. 'miniref' => substr($miniref, 0, 255),
  793. 'publisher' => substr($publisher, 0, 255),
  794. 'pubplace' => substr($pubplace, 0, 255),
  795. );
  796. $status = tripal_core_chado_update('pub', $match, $values);
  797. if (!$status) {
  798. drupal_set_message("Error updating publication", "error");
  799. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  800. return;
  801. }
  802. // now add in the properties by first removing any the publication
  803. // already has and adding the ones we have
  804. $details = array(
  805. 'property_table' => 'pubprop',
  806. 'base_table' => 'pub',
  807. 'foreignkey_name' => 'pub_id',
  808. 'foreignkey_value' => $pub_id
  809. );
  810. chado_node_properties_form_update_properties($node, $details, $properties);
  811. // * Relationships Form *
  812. $details = array(
  813. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  814. 'foreignkey_value' => $pub_id // value of the pub_id key
  815. );
  816. chado_node_relationships_form_update_relationships($node, $details);
  817. // add in any database cross-references after first removing
  818. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  819. foreach ($cross_refs as $index => $ref) {
  820. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  821. if (!$pub_dbxref) {
  822. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  823. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  824. array('%ref' => $ref), WATCHDOG_ERROR);
  825. }
  826. }
  827. }
  828. /**
  829. * Implementation of tripal_pub_load().
  830. *
  831. *
  832. * @param $node
  833. * The node that is to be accessed from the database
  834. *
  835. * @return $node
  836. * The node with the information to be loaded into the database
  837. *
  838. */
  839. function chado_pub_load($nodes) {
  840. foreach ($nodes as $nid => $node) {
  841. // find the pub and add in the details
  842. $pub_id = chado_get_id_for_node('pub', $nid);
  843. // get the pub
  844. $values = array('pub_id' => $pub_id);
  845. $pub = tripal_core_generate_chado_var('pub', $values);
  846. // expand the 'text' fields as those aren't included by default
  847. // and they really shouldn't be so large to cause problems
  848. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  849. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  850. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  851. // set the URL path
  852. $nodes[$nid]->path = "pub/$pub_id";
  853. $nodes[$nid]->pub = $pub;
  854. }
  855. }
  856. /**
  857. * Implementation of tripal_pub_delete().
  858. *
  859. * This function takes a node and if the delete button has been chosen by the user, the publication
  860. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  861. * the 'chado_pub' table.
  862. *
  863. * @parm $node
  864. * Then node which contains the information stored within the node-ID
  865. *
  866. */
  867. function chado_pub_delete(&$node) {
  868. $pub_id = chado_get_id_for_node('pub', $node->nid);
  869. // if we don't have a pub id for this node then this isn't a node of
  870. // type chado_pub or the entry in the chado_pub table was lost.
  871. if (!$pub_id) {
  872. return;
  873. }
  874. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  875. // drupal database
  876. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  877. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  878. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  879. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  880. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  881. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  882. // Remove data from pub and pubprop tables of chado database as well
  883. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  884. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  885. }
  886. /**
  887. *
  888. * @ingroup tripal_feature
  889. */
  890. function tripal_pub_node_view($node, $view_mode, $langcode) {
  891. switch ($node->type) {
  892. case 'chado_pub':
  893. // Show feature browser and counts
  894. if ($view_mode == 'full') {
  895. $node->content['tripal_pub_authors'] = array(
  896. '#markup' => theme('tripal_pub_authors', array('node' => $node)),
  897. '#tripal_toc_id' => 'authors',
  898. '#tripal_toc_title' => 'Author Details',
  899. );
  900. $node->content['tripal_pub_base'] = array(
  901. '#markup' => theme('tripal_pub_base', array('node' => $node)),
  902. '#tripal_toc_id' => 'base',
  903. '#tripal_toc_title' => 'Overview',
  904. '#weight' => -100,
  905. );
  906. $node->content['tripal_pub_featuremaps'] = array(
  907. '#markup' => theme('tripal_pub_featuremaps', array('node' => $node)),
  908. '#tripal_toc_id' => 'featuremaps',
  909. '#tripal_toc_title' => 'Maps',
  910. );
  911. $node->content['tripal_pub_features'] = array(
  912. '#markup' => theme('tripal_pub_features', array('node' => $node)),
  913. '#tripal_toc_id' => 'features',
  914. '#tripal_toc_title' => 'Features',
  915. );
  916. $node->content['tripal_pub_libraries'] = array(
  917. '#markup' => theme('tripal_pub_libraries', array('node' => $node)),
  918. '#tripal_toc_id' => 'libraries',
  919. '#tripal_toc_title' => 'Libraries',
  920. );
  921. $node->content['tripal_pub_projects'] = array(
  922. '#markup' => theme('tripal_pub_projects', array('node' => $node)),
  923. '#tripal_toc_id' => 'projects',
  924. '#tripal_toc_title' => 'Projects',
  925. );
  926. $node->content['tripal_pub_properties'] = array(
  927. '#markup' => theme('tripal_pub_properties', array('node' => $node)),
  928. '#tripal_toc_id' => 'properties',
  929. '#tripal_toc_title' => 'Properties',
  930. );
  931. $node->content['tripal_pub_references'] = array(
  932. '#markup' => theme('tripal_pub_references', array('node' => $node)),
  933. '#tripal_toc_id' => 'references',
  934. '#tripal_toc_title' => 'Cross References',
  935. );
  936. $node->content['tripal_pub_relationships'] = array(
  937. '#markup' => theme('tripal_pub_relationships', array('node' => $node)),
  938. '#tripal_toc_id' => 'relationships',
  939. '#tripal_toc_title' => 'Relationships',
  940. );
  941. $node->content['tripal_pub_stocks'] = array(
  942. '#markup' => theme('tripal_pub_stocks', array('node' => $node)),
  943. '#tripal_toc_id' => 'stocks',
  944. '#tripal_toc_title' => 'Stocks',
  945. );
  946. }
  947. if ($view_mode == 'teaser') {
  948. $node->content['tripal_pub_teaser'] = array(
  949. '#markup' => theme('tripal_pub_teaser', array('node' => $node)),
  950. );
  951. }
  952. break;
  953. }
  954. }
  955. /**
  956. *
  957. * @param $node
  958. */
  959. function tripal_pub_node_insert($node) {
  960. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  961. // where [pub id] is the Chado publication ID. This will allow for easy linking
  962. // into the publication without needing to know the node. Of course if you know the
  963. // node that will still work too (e.g. http://[base url]/node/[node id]
  964. // so the nodeapi function ensures that the URL path is set after insert or update
  965. // of the node and when the node is loaded if it hasn't yet been set.
  966. if ($node->type == 'chado_pub') {
  967. $pub_id = chado_get_id_for_node('pub', $node->nid);
  968. tripal_pub_set_pub_url($node, $pub_id);
  969. }
  970. }
  971. /**
  972. *
  973. * @param $node
  974. * @param $types
  975. */
  976. function tripal_pub_node_load($nodes, $types) {
  977. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  978. // where [pub id] is the Chado publication ID. This will allow for easy linking
  979. // into the publication without needing to know the node. Of course if you know the
  980. // node that will still work too (e.g. http://[base url]/node/[node id]
  981. // so the nodeapi function ensures that the URL path is set after insert or update
  982. // of the node and when the node is loaded if it hasn't yet been set.
  983. if (count(array_intersect(array('chado_pub'), $types))) {
  984. foreach ($nodes as $nid => $node) {
  985. if ($node->type == 'chado_pub' and !property_exists($node, 'path')) {
  986. $pub_id = chado_get_id_for_node('pub', $node->nid);
  987. $path = tripal_pub_set_pub_url($node, $pub_id);
  988. }
  989. }
  990. }
  991. }
  992. /**
  993. *
  994. * @param $node
  995. */
  996. function tripal_pub_node_update($node) {
  997. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  998. // where [pub id] is the Chado publication ID. This will allow for easy linking
  999. // into the publication without needing to know the node. Of course if you know the
  1000. // node that will still work too (e.g. http://[base url]/node/[node id]
  1001. // so the nodeapi function ensures that the URL path is set after insert or update
  1002. // of the node and when the node is loaded if it hasn't yet been set.
  1003. if ($node->type == 'chado_pub') {
  1004. $pub_id = chado_get_id_for_node('pub', $node->nid);
  1005. tripal_pub_set_pub_url($node, $pub_id);
  1006. }
  1007. }