tripal_pub.chado_node.inc 44 KB

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