tripal_pub.chado_node.inc 44 KB

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