tripal_pub.chado_node.inc 44 KB

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