tripal_pub.chado_node.inc 42 KB

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