tripal_pub.chado_node.inc 44 KB

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