tripal_pub.chado_node.inc 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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 = chado_expand_var($pub, 'field', 'pub.title');
  62. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  63. $pub = chado_expand_var($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) and isset($form_state['values']['pubtitle'])) {
  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_add_node_form_properties($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 = chado_select_record('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 = chado_select_record('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_add_node_form_relationships($form, $form_state, $details);
  266. // ADDITIONAL DBXREFS FORM
  267. //---------------------------------------------
  268. $details = array(
  269. 'linking_table' => 'pub_dbxref', // the name of the _dbxref table
  270. 'base_foreign_key' => 'pub_id', // the name of the key in your base chado table
  271. 'base_key_value' => $pub_id // the value of pub_id for this record
  272. );
  273. // Adds the form elements to your current form
  274. chado_add_node_form_dbxrefs($form, $form_state, $details);
  275. return $form;
  276. }
  277. /*
  278. *
  279. */
  280. function chado_pub_validate($node, $form, &$form_state) {
  281. // get the submitted values
  282. $title = trim($node->pubtitle);
  283. $pyear = trim($node->pyear);
  284. $uniquename = trim($node->uniquename);
  285. $is_obsolete = $node->is_obsolete;
  286. $type_id = $node->type_id;
  287. // if this is a delete then don't validate
  288. if($node->op == 'Delete') {
  289. return;
  290. }
  291. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  292. // need to validate during syncing so just skip it.
  293. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  294. return;
  295. }
  296. $pub = array();
  297. // make sure the year is four digits
  298. if(!preg_match('/^\d{4}$/', $pyear)){
  299. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  300. return;
  301. }
  302. // get the type of publication
  303. $values = array('cvterm_id' => $type_id);
  304. $options = array('statement_name' => 'sel_pub_ty');
  305. $cvterm = chado_select_record('cvterm', array('name'), $values, $options);
  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. foreach ($node as $element => $value) {
  314. // if this is an existing property (either previously in the database or
  315. // added via AHAH/AJAX callback)
  316. if (preg_match('/^prop_value-(\d+)-(\d+)$/', $element, $matches)) {
  317. $prop_type_id = $matches[1];
  318. $prop_type = tripal_cv_get_cvterm_by_id($prop_type_id);
  319. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  320. $series_name = $value;
  321. }
  322. if($prop_type->name == 'Citation') {
  323. $uniquename = $value;
  324. }
  325. $pub[$prop_type->name] = $value;
  326. }
  327. // if this is a new property (added by this submit of the form)
  328. elseif ($element == 'new_id') {
  329. $prop_type = tripal_cv_get_cvterm_by_id($value);
  330. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  331. $series_name = $node->new_value;
  332. }
  333. if($prop_type->name == 'Citation') {
  334. $uniquename = $node->new_value;
  335. }
  336. $pub[$prop_type->name] = $node->new_value;
  337. }
  338. }
  339. // if the citation is missing then try to generate one
  340. if (!$uniquename) {
  341. $pub['Title'] = $title;
  342. $pub['Publication Type'][0] = $cvterm[0]->name;
  343. $pub['Year'] = $pyear;
  344. $uniquename = tripal_pub_create_citation($pub);
  345. if (!$uniquename) {
  346. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  347. }
  348. }
  349. $skip_duplicate_check = 0;
  350. // if this publication is a Patent then skip the validation below. Patents can have the title
  351. // name and year but be different
  352. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  353. $skip_duplicate_check = 1;
  354. }
  355. // Validating for an update
  356. if (!is_null($node->nid)) {
  357. $pub_id = $node->pub_id;
  358. // first get the original title, type and year before it was changed
  359. $values = array('pub_id' => $pub_id);
  360. $columns = array('title', 'pyear', 'type_id', 'series_name');
  361. $options = array('statement_name' => 'sel_pub_id');
  362. $pub = chado_select_record('pub', $columns, $values, $options);
  363. // if the title, type, year or series_name have changed then check the pub
  364. // to see if it is a duplicate of another
  365. if((strcmp(strtolower($pub[0]->title), strtolower($title)) == 0) and
  366. (strcmp(strtolower($pub[0]->series_name), strtolower($series_name)) == 0) and
  367. ($pub[0]->type_id == $type_id) and
  368. ($pub[0]->pyear == $pyear)) {
  369. $skip_duplicate_check = 1;
  370. }
  371. // check to see if a duplicate publication already exists
  372. if (!$skip_duplicate_check) {
  373. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id);
  374. }
  375. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  376. }
  377. // Validating for an insert
  378. else {
  379. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm);
  380. chado_pub_validate_check_uniquename($uniquename);
  381. }
  382. }
  383. /**
  384. *
  385. * @param unknown $uniquename
  386. */
  387. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  388. $results = tripal_pub_get_pub_by_uniquename($uniquename);
  389. // make sure we don't capture our pub_id in the list (remove it)
  390. foreach ($results as $index => $found_pub_id) {
  391. if($found_pub_id == $pub_id){
  392. unset($results[$index]);
  393. }
  394. }
  395. if (count($results) > 0) {
  396. $message = t('A publication with this unique citation already exists.');
  397. form_set_error('uniquename', $message);
  398. }
  399. }
  400. /**
  401. *
  402. */
  403. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  404. // make sure the publication is unique using the prefereed import duplication check
  405. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  406. switch ($import_dups_check) {
  407. case 'title_year':
  408. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, NULL);
  409. // make sure we don't capture our pub_id in the list (remove it)
  410. foreach ($results as $index => $found_pub_id) {
  411. if($found_pub_id == $pub_id){
  412. unset($results[$index]);
  413. }
  414. }
  415. if (count($results) > 0) {
  416. $message = t('A publication with this title and publication year, already exists.');
  417. form_set_error('pyear', $message);
  418. }
  419. break;
  420. case 'title_year_type':
  421. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, $cvterm[0]->name, $pyear, NULL);
  422. // make sure we don't capture our pub_id in the list (remove it)
  423. foreach ($results as $index => $found_pub_id) {
  424. if($found_pub_id == $pub_id){
  425. unset($results[$index]);
  426. }
  427. }
  428. if (count($results) > 0) {
  429. $message = t('A publication with this title, type and publication year, already exists.');
  430. form_set_error('pyear', $message);
  431. }
  432. break;
  433. case 'title_year_media':
  434. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, $series_name);
  435. // make sure we don't capture our pub_id in the list (remove it)
  436. foreach ($results as $index => $found_pub_id) {
  437. if($found_pub_id == $pub_id){
  438. unset($results[$index]);
  439. }
  440. }
  441. if (count($results) > 0) {
  442. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year, already exists.');
  443. form_set_error('pyear', $message);
  444. }
  445. break;
  446. }
  447. }
  448. /**
  449. * Implement hook_access().
  450. *
  451. * This hook allows node modules to limit access to the node types they define.
  452. *
  453. * @param $node
  454. * The node on which the operation is to be performed, or, if it does not yet exist, the
  455. * type of node to be created
  456. *
  457. * @param $op
  458. * The operation to be performed
  459. *
  460. * @param $account
  461. * A user object representing the user for whom the operation is to be performed
  462. *
  463. * @return
  464. * If the permission for the specified operation is not set then return FALSE. If the
  465. * permission is set then return NULL as this allows other modules to disable
  466. * access. The only exception is when the $op == 'create'. We will always
  467. * return TRUE if the permission is set.
  468. *
  469. */
  470. function chado_pub_node_access($node, $op, $account ) {
  471. if ($op == 'create') {
  472. if (!user_access('create chado_pub content', $account)) {
  473. return FALSE;
  474. }
  475. return TRUE;
  476. }
  477. if ($op == 'update') {
  478. if (!user_access('edit chado_pub content', $account)) {
  479. return FALSE;
  480. }
  481. }
  482. if ($op == 'delete') {
  483. if (!user_access('delete chado_pub content', $account)) {
  484. return FALSE;
  485. }
  486. }
  487. if ($op == 'view') {
  488. if (!user_access('access chado_pub content', $account)) {
  489. return FALSE;
  490. }
  491. }
  492. return NULL;
  493. }
  494. /**
  495. * Implementation of tripal_pub_insert().
  496. *
  497. * This function inserts user entered information pertaining to the Publication instance into the
  498. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  499. *
  500. * @parm $node
  501. * Then node which contains the information stored within the node-ID
  502. *
  503. *
  504. */
  505. function chado_pub_insert($node) {
  506. $title = trim($node->pubtitle);
  507. $pyear = trim($node->pyear);
  508. $uniquename = trim($node->uniquename);
  509. $is_obsolete = $node->is_obsolete;
  510. $type_id = $node->type_id;
  511. // we need an array suitable for the tripal_pub_create_citation() function
  512. // to automatically generate a citation if a uniquename doesn't already exist
  513. $pub_arr = array();
  514. // if there is an pub_id in the $node object then this must be a sync so
  515. // we can skip adding the pub as it is already there, although
  516. // we do need to proceed with the rest of the insert
  517. if (!property_exists($node, 'pub_id')) {
  518. $properties = array(); // stores all of the properties we need to add
  519. $cross_refs = array(); // stores any cross references for this publication
  520. // get the properties from the form
  521. $properties = chado_retrieve_node_form_properties($node);
  522. // get the list of properties for easy lookup (without doing lots of database queries
  523. $properties_list = array();
  524. $sql = "
  525. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  526. FROM {cvtermpath} CVTP
  527. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  528. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  529. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  530. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  531. NOT CVTS.is_obsolete = 1
  532. ORDER BY CVTS.name ASC
  533. ";
  534. $prop_types = chado_query($sql);
  535. while ($prop = $prop_types->fetchObject()) {
  536. $properties_list[$prop->cvterm_id] = $prop->name;
  537. // The 'Citation' term is special because it serves
  538. // both as a property and as the uniquename for the
  539. // pub and we want it stored in both the pub table and the pubprop table
  540. if ($prop->name == 'Citation') {
  541. $citation_id = $prop->cvterm_id;
  542. if (!empty($node->uniquename)) {
  543. $properties[$citation_id] = $node->uniquename;
  544. }
  545. }
  546. }
  547. // iterate through all of the properties and remove those that really are
  548. // part of the pub table fields
  549. $volume = '';
  550. $volumetitle = '';
  551. $issue = '';
  552. $pages = '';
  553. $publisher = '';
  554. $series_name = '';
  555. $pubplace = '';
  556. $miniref = '';
  557. $cross_refs = array();
  558. foreach ($properties as $type_id => $element) {
  559. $value = trim($element[0]);
  560. $name = $properties_list[$type_id];
  561. // populate our $pub_array for building a citation
  562. $pub_arr[$name] = $value;
  563. // remove properties that are stored in the pub table
  564. if ($name == "Volume") {
  565. $volume = $value;
  566. unset($properties[$type_id]);
  567. }
  568. elseif ($name == "Volume Title") {
  569. $volumetitle = $value;
  570. unset($properties[$type_id]);
  571. }
  572. elseif ($name == "Issue") {
  573. $issue = $value;
  574. unset($properties[$type_id]);
  575. }
  576. elseif ($name == "Pages") {
  577. $pages = $value;
  578. unset($properties[$type_id]);
  579. }
  580. elseif ($name == "Publisher") {
  581. $publisher = $value;
  582. unset($properties[$type_id]);
  583. }
  584. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  585. $series_name = $value;
  586. unset($properties[$type_id]);
  587. }
  588. elseif ($name == "Journal Country" or $name == "Published Location") {
  589. $pubplace = $value;
  590. // allow this property to go into the pubprop table so we don't loose info
  591. // so don't unset it. But it will also go into the pub.pubplace field
  592. }
  593. elseif ($name == "Publication Dbxref") {
  594. // we will add the cross-references to the pub_dbxref table
  595. // but we also want to keep the property in the pubprop table so don't unset it
  596. $cross_refs[] = $value;
  597. }
  598. }
  599. // generate a citation for this pub if one doesn't already exist
  600. if (!$node->uniquename and !array_key_exists($citation_id, $properties)) {
  601. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  602. $pub_arr['Title'] = $node->pubtitle;
  603. $pub_arr['Publication Type'][0] = $pub_type->name;
  604. $pub_arr['Year'] = $node->pyear;
  605. $node->uniquename = tripal_pub_create_citation($pub_arr);
  606. $properties[$citation_id][0] = $node->uniquename;
  607. }
  608. // insert the pub record
  609. $values = array(
  610. 'title' => $node->pubtitle,
  611. 'series_name' => substr($series_name, 0, 255),
  612. 'type_id' => $node->type_id,
  613. 'pyear' => $node->pyear,
  614. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  615. 'uniquename' => $node->uniquename,
  616. 'volumetitle' => $volumetitle,
  617. 'volume' => $volume,
  618. 'issue' => $issue,
  619. 'pages' => $pages,
  620. 'miniref' => substr($miniref, 0, 255),
  621. 'publisher' => substr($publisher, 0, 255),
  622. 'pubplace' => substr($pubplace, 0, 255),
  623. );
  624. $pub = chado_insert_record('pub', $values);
  625. if (!$pub) {
  626. drupal_set_message("Error inserting publication", "error");
  627. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  628. return;
  629. }
  630. $pub_id = $pub['pub_id'];
  631. // now add in the properties
  632. // Only adds in those not used in the pub record
  633. $details = array(
  634. 'property_table' => 'pubprop',
  635. 'base_table' => 'pub',
  636. 'foreignkey_name' => 'pub_id',
  637. 'foreignkey_value' => $pub_id
  638. );
  639. chado_update_node_form_properties($node, $details, $properties);
  640. // * Relationships Form *
  641. $details = array(
  642. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  643. 'foreignkey_value' => $pub_id // value of the pub_id key
  644. );
  645. chado_update_node_form_relationships($node, $details);
  646. // add in any database cross-references
  647. foreach ($cross_refs as $index => $ref) {
  648. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  649. if (!$pub_dbxref) {
  650. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  651. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  652. array('%ref' => $ref), WATCHDOG_ERROR);
  653. }
  654. }
  655. // * Additional DBxrefs Form *
  656. $details = array(
  657. 'linking_table' => 'pub_dbxref', // the name of your _dbxref table
  658. 'foreignkey_name' => 'pub_id', // the name of the key in your base table
  659. 'foreignkey_value' => $pub_id // the value of the pub_id key
  660. );
  661. chado_update_node_form_dbxrefs($node, $details);
  662. }
  663. else {
  664. $pub_id = $node->pub_id;
  665. }
  666. // Make sure the entry for this pub doesn't already exist in the
  667. // chado_pub table if it doesn't exist then we want to add it.
  668. $check_org_id = chado_get_id_from_nid('pub', $node->nid);
  669. if (!$check_org_id) {
  670. $record = new stdClass();
  671. $record->nid = $node->nid;
  672. $record->vid = $node->vid;
  673. $record->pub_id = $pub_id;
  674. drupal_write_record('chado_pub', $record);
  675. }
  676. }
  677. /*
  678. *
  679. * Implements hook_update
  680. *
  681. * The purpose of the function is to allow the module to take action when an edited node is being
  682. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  683. * As well, the database will be changed, so the user changed information will be saved to the database.
  684. *
  685. * @param $node
  686. * The node being updated
  687. *
  688. * @ingroup tripal_pub
  689. */
  690. function chado_pub_update($node) {
  691. $title = trim($node->pubtitle);
  692. $pyear = trim($node->pyear);
  693. $uniquename = trim($node->uniquename);
  694. $is_obsolete = $node->is_obsolete;
  695. $type_id = $node->type_id;
  696. // we need an array suitable for the tripal_pub_create_citation() function
  697. // to automatically generate a citation if a uniquename doesn't already exist
  698. $pub_arr = array();
  699. // get the publication ID for this publication
  700. $pub_id = chado_get_id_from_nid('pub', $node->nid) ;
  701. $properties = array(); // stores all of the properties we need to add
  702. $cross_refs = array(); // stores any cross references for this publication
  703. // get the properties from the form
  704. $properties = chado_retrieve_node_form_properties($node);
  705. // get the list of properties for easy lookup (without doing lots of database queries
  706. $properties_list = array();
  707. $sql = "
  708. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  709. FROM {cvtermpath} CVTP
  710. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  711. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  712. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  713. WHERE CV.name = 'tripal_pub' and
  714. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  715. NOT CVTS.is_obsolete = 1
  716. ORDER BY CVTS.name ASC
  717. ";
  718. $prop_types = chado_query($sql);
  719. while ($prop = $prop_types->fetchObject()) {
  720. $properties_list[$prop->cvterm_id] = $prop->name;
  721. // The 'Citation' term is special because it serves
  722. // both as a property and as the uniquename for the
  723. // pub and we want it stored in both the pub table and the pubprop table
  724. if ($prop->name == 'Citation') {
  725. $citation_id = $prop->cvterm_id;
  726. if (!empty($node->uniquename)) {
  727. $properties[$citation_id][0] = $node->uniquename;
  728. }
  729. }
  730. }
  731. // iterate through all of the properties and remove those that really are
  732. // part of the pub table fields
  733. $volume = '';
  734. $volumetitle = '';
  735. $issue = '';
  736. $pages = '';
  737. $publisher = '';
  738. $series_name = '';
  739. $pubplace = '';
  740. $miniref = '';
  741. $cross_refs = array();
  742. foreach ($properties as $type_id => $element) {
  743. foreach ($element as $index => $value) {
  744. $name = $properties_list[$type_id];
  745. // populate our $pub_array for building a citation
  746. $pub_arr[$name] = $value;
  747. // remove properties that are stored in the pub table
  748. if ($name == "Volume") {
  749. $volume = $value;
  750. unset($properties[$type_id]);
  751. }
  752. elseif ($name == "Volume Title") {
  753. $volumetitle = $value;
  754. unset($properties[$type_id]);
  755. }
  756. elseif ($name == "Issue") {
  757. $issue = $value;
  758. unset($properties[$type_id]);
  759. }
  760. elseif ($name == "Pages") {
  761. $pages = $value;
  762. unset($properties[$type_id]);
  763. }
  764. elseif ($name == "Publisher") {
  765. $publisher = $value;
  766. unset($properties[$type_id]);
  767. }
  768. elseif ($name == "Journal Name" or $name == "Conference Name") {
  769. $series_name = $value;
  770. unset($properties[$type_id]);
  771. }
  772. elseif ($name == "Journal Country" or $name == "Published Location") {
  773. $pubplace = $value;
  774. // allow this property to go into the pubprop table so we don't loose info
  775. // so don't unset it. But it will also go into the pub.pubplace field
  776. }
  777. elseif ($name == "Publication Dbxref") {
  778. // we will add the cross-references to the pub_dbxref table
  779. // but we also want to keep the property in the pubprop table so don't unset it
  780. $cross_refs[] = $value;
  781. }
  782. }
  783. }
  784. // generate a citation for this pub if one doesn't already exist
  785. if (!$node->uniquename) {
  786. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  787. $pub_arr['Title'] = $node->pubtitle;
  788. $pub_arr['Publication Type'][0] = $pub_type->name;
  789. $pub_arr['Year'] = $node->pyear;
  790. $node->uniquename = tripal_pub_create_citation($pub_arr);
  791. $properties[$citation_id][0] = $node->uniquename;
  792. }
  793. // update the pub record
  794. $match = array(
  795. 'pub_id' => $pub_id,
  796. );
  797. $values = array(
  798. 'title' => $node->pubtitle,
  799. 'type_id' => $node->type_id,
  800. 'pyear' => $node->pyear,
  801. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  802. 'uniquename' => $node->uniquename,
  803. 'series_name' => substr($series_name, 0, 255),
  804. 'volumetitle' => $volumetitle,
  805. 'volume' => $volume,
  806. 'issue' => $issue,
  807. 'pages' => $pages,
  808. 'miniref' => substr($miniref, 0, 255),
  809. 'publisher' => substr($publisher, 0, 255),
  810. 'pubplace' => substr($pubplace, 0, 255),
  811. );
  812. $status = chado_update_record('pub', $match, $values);
  813. if (!$status) {
  814. drupal_set_message("Error updating publication", "error");
  815. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  816. return;
  817. }
  818. // now add in the properties by first removing any the publication
  819. // already has and adding the ones we have
  820. $details = array(
  821. 'property_table' => 'pubprop',
  822. 'base_table' => 'pub',
  823. 'foreignkey_name' => 'pub_id',
  824. 'foreignkey_value' => $pub_id
  825. );
  826. chado_update_node_form_properties($node, $details, $properties);
  827. // * Relationships Form *
  828. $details = array(
  829. 'relationship_table' => 'pub_relationship', // name of the _relationship table
  830. 'foreignkey_value' => $pub_id // value of the pub_id key
  831. );
  832. chado_update_node_form_relationships($node, $details);
  833. // add in any database cross-references after first removing
  834. chado_delete_record('pub_dbxref', array('pub_id' => $pub_id));
  835. foreach ($cross_refs as $index => $ref) {
  836. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  837. if (!$pub_dbxref) {
  838. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  839. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  840. array('%ref' => $ref), WATCHDOG_ERROR);
  841. }
  842. }
  843. // * Additional DBxrefs Form *
  844. $details = array(
  845. 'linking_table' => 'pub_dbxref', // the name of your _dbxref table
  846. 'foreignkey_name' => 'pub_id', // the name of the key in your base table
  847. 'foreignkey_value' => $pub_id // the value of the pub_id key
  848. );
  849. chado_update_node_form_dbxrefs($node, $details);
  850. }
  851. /**
  852. * Implementation of tripal_pub_load().
  853. *
  854. *
  855. * @param $node
  856. * The node that is to be accessed from the database
  857. *
  858. * @return $node
  859. * The node with the information to be loaded into the database
  860. *
  861. */
  862. function chado_pub_load($nodes) {
  863. foreach ($nodes as $nid => $node) {
  864. // find the pub and add in the details
  865. $pub_id = chado_get_id_from_nid('pub', $nid);
  866. // get the pub
  867. $values = array('pub_id' => $pub_id);
  868. $pub = chado_generate_var('pub', $values);
  869. // expand the 'text' fields as those aren't included by default
  870. // and they really shouldn't be so large to cause problems
  871. $pub = chado_expand_var($pub, 'field', 'pub.title');
  872. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  873. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  874. // set the URL path
  875. $nodes[$nid]->path = "pub/$pub_id";
  876. $nodes[$nid]->pub = $pub;
  877. }
  878. }
  879. /**
  880. * Implementation of tripal_pub_delete().
  881. *
  882. * This function takes a node and if the delete button has been chosen by the user, the publication
  883. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  884. * the 'chado_pub' table.
  885. *
  886. * @parm $node
  887. * Then node which contains the information stored within the node-ID
  888. *
  889. */
  890. function chado_pub_delete(&$node) {
  891. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  892. // if we don't have a pub id for this node then this isn't a node of
  893. // type chado_pub or the entry in the chado_pub table was lost.
  894. if (!$pub_id) {
  895. return;
  896. }
  897. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  898. // drupal database
  899. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  900. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  901. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  902. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  903. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  904. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  905. // Remove data from pub and pubprop tables of chado database as well
  906. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  907. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  908. }
  909. /**
  910. *
  911. * @ingroup tripal_feature
  912. */
  913. function tripal_pub_node_view($node, $view_mode, $langcode) {
  914. switch ($node->type) {
  915. case 'chado_pub':
  916. // Show feature browser and counts
  917. if ($view_mode == 'full') {
  918. $node->content['tripal_pub_authors'] = array(
  919. '#markup' => theme('tripal_pub_authors', array('node' => $node)),
  920. '#tripal_toc_id' => 'authors',
  921. '#tripal_toc_title' => 'Author Details',
  922. );
  923. $node->content['tripal_pub_base'] = array(
  924. '#markup' => theme('tripal_pub_base', array('node' => $node)),
  925. '#tripal_toc_id' => 'base',
  926. '#tripal_toc_title' => 'Overview',
  927. '#weight' => -100,
  928. );
  929. $node->content['tripal_pub_featuremaps'] = array(
  930. '#markup' => theme('tripal_pub_featuremaps', array('node' => $node)),
  931. '#tripal_toc_id' => 'featuremaps',
  932. '#tripal_toc_title' => 'Maps',
  933. );
  934. $node->content['tripal_pub_features'] = array(
  935. '#markup' => theme('tripal_pub_features', array('node' => $node)),
  936. '#tripal_toc_id' => 'features',
  937. '#tripal_toc_title' => 'Features',
  938. );
  939. $node->content['tripal_pub_libraries'] = array(
  940. '#markup' => theme('tripal_pub_libraries', array('node' => $node)),
  941. '#tripal_toc_id' => 'libraries',
  942. '#tripal_toc_title' => 'Libraries',
  943. );
  944. $node->content['tripal_pub_projects'] = array(
  945. '#markup' => theme('tripal_pub_projects', array('node' => $node)),
  946. '#tripal_toc_id' => 'projects',
  947. '#tripal_toc_title' => 'Projects',
  948. );
  949. $node->content['tripal_pub_properties'] = array(
  950. '#markup' => theme('tripal_pub_properties', array('node' => $node)),
  951. '#tripal_toc_id' => 'properties',
  952. '#tripal_toc_title' => 'Properties',
  953. );
  954. $node->content['tripal_pub_references'] = array(
  955. '#markup' => theme('tripal_pub_references', array('node' => $node)),
  956. '#tripal_toc_id' => 'references',
  957. '#tripal_toc_title' => 'Cross References',
  958. );
  959. $node->content['tripal_pub_relationships'] = array(
  960. '#markup' => theme('tripal_pub_relationships', array('node' => $node)),
  961. '#tripal_toc_id' => 'relationships',
  962. '#tripal_toc_title' => 'Relationships',
  963. );
  964. $node->content['tripal_pub_stocks'] = array(
  965. '#markup' => theme('tripal_pub_stocks', array('node' => $node)),
  966. '#tripal_toc_id' => 'stocks',
  967. '#tripal_toc_title' => 'Stocks',
  968. );
  969. }
  970. if ($view_mode == 'teaser') {
  971. $node->content['tripal_pub_teaser'] = array(
  972. '#markup' => theme('tripal_pub_teaser', array('node' => $node)),
  973. );
  974. }
  975. break;
  976. }
  977. }
  978. /**
  979. *
  980. * @param $node
  981. */
  982. function tripal_pub_node_insert($node) {
  983. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  984. // where [pub id] is the Chado publication ID. This will allow for easy linking
  985. // into the publication without needing to know the node. Of course if you know the
  986. // node that will still work too (e.g. http://[base url]/node/[node id]
  987. // so the nodeapi function ensures that the URL path is set after insert or update
  988. // of the node and when the node is loaded if it hasn't yet been set.
  989. if ($node->type == 'chado_pub') {
  990. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  991. tripal_pub_set_pub_url($node, $pub_id);
  992. }
  993. }
  994. /**
  995. *
  996. * @param $node
  997. * @param $types
  998. */
  999. function tripal_pub_node_load($nodes, $types) {
  1000. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  1001. // where [pub id] is the Chado publication ID. This will allow for easy linking
  1002. // into the publication without needing to know the node. Of course if you know the
  1003. // node that will still work too (e.g. http://[base url]/node/[node id]
  1004. // so the nodeapi function ensures that the URL path is set after insert or update
  1005. // of the node and when the node is loaded if it hasn't yet been set.
  1006. if (count(array_intersect(array('chado_pub'), $types))) {
  1007. foreach ($nodes as $nid => $node) {
  1008. if ($node->type == 'chado_pub' and !property_exists($node, 'path')) {
  1009. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1010. $path = tripal_pub_set_pub_url($node, $pub_id);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. /**
  1016. *
  1017. * @param $node
  1018. */
  1019. function tripal_pub_node_update($node) {
  1020. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  1021. // where [pub id] is the Chado publication ID. This will allow for easy linking
  1022. // into the publication without needing to know the node. Of course if you know the
  1023. // node that will still work too (e.g. http://[base url]/node/[node id]
  1024. // so the nodeapi function ensures that the URL path is set after insert or update
  1025. // of the node and when the node is loaded if it hasn't yet been set.
  1026. if ($node->type == 'chado_pub') {
  1027. $pub_id = chado_get_id_from_nid('pub', $node->nid);
  1028. tripal_pub_set_pub_url($node, $pub_id);
  1029. }
  1030. }