tripal_pub.chado_node.inc 45 KB

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