tripal_pub.chado_node.inc 44 KB

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