tripal_pub.chado_node.inc 44 KB

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