tripal_pub.chado_node.inc 43 KB

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