tripal_pub.chado_node.inc 41 KB

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