tripal_pub.chado_node.inc 45 KB

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