tripal_pub.chado_node.inc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. <?php
  2. /**
  3. * Implementation of hook_tripal_pub_node_info().
  4. *
  5. * This node_info, is a simple node that describes the functionallity of the module.
  6. *
  7. */
  8. function tripal_pub_node_info() {
  9. return array(
  10. 'chado_pub' => array(
  11. 'name' => t('Publication'),
  12. 'base' => 'chado_pub',
  13. 'description' => t('A publication from the Chado database'),
  14. 'title_label' => t('Article Title'),
  15. 'body_label' => t('Abstract'),
  16. 'has_title' => TRUE,
  17. 'has_body' => FALSE,
  18. 'chado_node_api' => array(
  19. 'base_table' => 'pub',
  20. 'hook_prefix' => 'chado_pub',
  21. 'record_type_title' => array(
  22. 'singular' => t('Publication'),
  23. 'plural' => t('Publications')
  24. ),
  25. 'sync_filters' => array(
  26. 'type_id' => FALSE,
  27. 'organism_id' => FALSE
  28. ),
  29. ),
  30. ),
  31. );
  32. }
  33. /**
  34. * This is the chado_pub node form callback. The arguments
  35. * are out of order from a typical form because it's a defined callback
  36. */
  37. function chado_pub_form($node, $form_state) {
  38. $form = array();
  39. // Default values can come in the following ways:
  40. //
  41. // 1) as elements of the $node object. This occurs when editing an existing pub
  42. // 2) in the $form_state['values'] array which occurs on a failed validation or
  43. // ajax callbacks from non submit form elements
  44. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  45. // form elements and the form is being rebuilt
  46. //
  47. // set form field defaults
  48. $pub_id = null;
  49. $title = '';
  50. $pyear = '';
  51. $uniquename = '';
  52. $type_id = '';
  53. $is_obsolete = '';
  54. // some of the fields in the pub table should show up in the properties
  55. // form elements to make the form more seemless. We will add them
  56. // to this array.
  57. $more_props = array();
  58. // if we are editing an existing node then the pub is already part of the node
  59. if (property_exists($node, 'pub')) {
  60. $pub = $node->pub;
  61. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  62. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  63. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  64. $pub_id = $pub->pub_id;
  65. $title = $pub->title;
  66. $pyear = $pub->pyear;
  67. $uniquename = $pub->uniquename;
  68. $type_id = $pub->type_id->cvterm_id;
  69. $is_obsolete = $pub->is_obsolete;
  70. // if the obsolete value is set by the database then it is in the form of
  71. // 't' or 'f', we need to convert to 1 or 0
  72. $is_obsolete = $is_obsolete == 't' ? 1 : $is_obsolete;
  73. $is_obsolete = $is_obsolete == 'f' ? 0 : $is_obsolete;
  74. // set the organism_id in the form
  75. $form['pub_id'] = array(
  76. '#type' => 'value',
  77. '#value' => $pub->pub_id,
  78. );
  79. // get fields from the pub table and convert them to properties. We will add these to the $more_props
  80. // array which gets passed in to the tripal_core_properties_form() API call further down
  81. if ($pub->volumetitle) {
  82. $cvterm = tripal_cv_get_cvterm_by_name('Volume Title', NULL, 'tripal_pub');
  83. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volumetitle);
  84. }
  85. if ($pub->volume) {
  86. $cvterm = tripal_cv_get_cvterm_by_name('Volume', NULL, 'tripal_pub');
  87. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volume);
  88. }
  89. if ($pub->series_name) {
  90. switch ($pub->type_id->name) {
  91. case 'Journal Article':
  92. $cvterm = tripal_cv_get_cvterm_by_name('Journal Name', NULL, 'tripal_pub');
  93. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  94. break;
  95. case 'Conference Proceedings':
  96. $cvterm = tripal_cv_get_cvterm_by_name('Conference Name', NULL, 'tripal_pub');
  97. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  98. break;
  99. default:
  100. $cvterm = tripal_cv_get_cvterm_by_name('Series Name', NULL, 'tripal_pub');
  101. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  102. }
  103. }
  104. if ($pub->issue) {
  105. $cvterm = tripal_cv_get_cvterm_by_name('Issue', NULL, 'tripal_pub');
  106. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->issue);
  107. }
  108. if ($pub->pages) {
  109. $cvterm = tripal_cv_get_cvterm_by_name('Pages', NULL, 'tripal_pub');
  110. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  111. }
  112. if ($pub->miniref) {
  113. // not sure what to do with this one
  114. }
  115. if ($pub->publisher) {
  116. $cvterm = tripal_cv_get_cvterm_by_name('Publisher', NULL, 'tripal_pub');
  117. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->publisher);
  118. }
  119. if ($pub->pubplace) {
  120. $cvterm = tripal_cv_get_cvterm_by_name('Published Location', NULL, 'tripal_pub');
  121. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  122. }
  123. }
  124. // if we are re constructing the form from a failed validation or ajax callback
  125. // then use the $form_state['values'] values
  126. if (array_key_exists('values', $form_state)) {
  127. $title = $form_state['values']['pubtitle'];
  128. $pyear = $form_state['values']['pyear'];
  129. $uniquename = $form_state['values']['uniquename'];
  130. $type_id = $form_state['values']['type_id'];
  131. $is_obsolete = $form_state['values']['is_obsolete'];
  132. }
  133. // if we are re building the form from after submission (from ajax call) then
  134. // the values are in the $form_state['input'] array
  135. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  136. $title = $form_state['input']['pubtitle'];
  137. $uniquename = $form_state['input']['uniquename'];
  138. $type_id = $form_state['input']['type_id'];
  139. $is_obsolete = $form_state['input']['is_obsolete'];
  140. }
  141. // a drupal title can only be 255 characters, but the Chado title can be much longer.
  142. // we use the publication title as the drupal title, but we'll need to truncate it.
  143. $form['title'] = array(
  144. '#type' => 'hidden',
  145. '#value' => substr($title, 0, 255),
  146. );
  147. $form['pubtitle'] = array(
  148. '#type' => 'textarea',
  149. '#title' => t('Publication Title'),
  150. '#default_value' => $title,
  151. '#required' => TRUE,
  152. );
  153. // get the list of publication types. In the Tripal publication
  154. // ontologies these are all grouped under the term 'Publication Type'
  155. // we want the default to be 'Journal Article'
  156. $sql = "
  157. SELECT
  158. CVTS.cvterm_id, CVTS.name
  159. FROM {cvtermpath} CVTP
  160. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  161. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  162. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  163. WHERE
  164. CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
  165. NOT CVTS.is_obsolete = 1
  166. ORDER BY CVTS.name ASC
  167. ";
  168. $results = chado_query($sql);
  169. $pub_types = array();
  170. while ($pub_type = $results->fetchObject()) {
  171. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  172. // if we don't have a default type then set the default to be 'Journal Article'
  173. if (strcmp($pub_type->name,"Journal Article") == 0 and !$type_id) {
  174. $type_id = $pub_type->cvterm_id;
  175. }
  176. }
  177. $form['type_id'] = array(
  178. '#type' => 'select',
  179. '#title' => t('Publication Type'),
  180. '#options' => $pub_types,
  181. '#required' => TRUE,
  182. '#default_value' => $type_id,
  183. );
  184. $form['pyear'] = array(
  185. '#type' => 'textfield',
  186. '#title' => t('Publication Year'),
  187. '#default_value' => $pyear,
  188. '#required' => TRUE,
  189. '#size' => 5,
  190. '#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.'),
  191. );
  192. $form['uniquename'] = array(
  193. '#type' => 'textarea',
  194. '#title' => t('Citation'),
  195. '#default_value' => $uniquename,
  196. '#description' => t('All publications must have a unique citation.
  197. <b>Please enter the full citation for this publication or leave blank and one will be generated
  198. automatically if possible</b>. For PubMed style citations list
  199. the last name of the author followed by initials. Each author should be separated by a comma. Next comes
  200. 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.
  201. 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
  202. suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
  203. );
  204. $form['is_obsolete'] = array(
  205. '#type' => 'checkbox',
  206. '#title' => t('Is Obsolete? (Check for Yes)'),
  207. '#default_value' => $is_obsolete,
  208. );
  209. // Properties Form
  210. // ----------------------------------
  211. // D7 @TODO: Properties API doesn't handle exclude
  212. $exclude = array("Citation");
  213. $details = array(
  214. 'property_table' => 'pubprop',
  215. 'base_foreign_key' => 'pub_id',
  216. 'base_key_value' => $pub_id,
  217. 'cv_name' => 'tripal_pub'
  218. );
  219. chado_node_properties_form($form, $form_state, $details);
  220. // get publication properties list
  221. /**
  222. $properties_select = array();
  223. $properties_select[] = 'Select a Property';
  224. $sql = "
  225. SELECT
  226. DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  227. FROM {cvtermpath} CVTP
  228. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  229. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  230. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  231. WHERE CV.name = 'tripal_pub' and
  232. (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
  233. NOT CVTS.is_obsolete = 1
  234. ORDER BY CVTS.name ASC
  235. ";
  236. $prop_types = chado_query($sql);
  237. while ($prop = $prop_types->fetchObject()) {
  238. // add all properties except the Citation. That property is set via the uniquename field
  239. if ($prop->name != 'Citation') {
  240. $properties[$prop->cvterm_id] = $prop->name;
  241. }
  242. }
  243. // add in the properties fields. The 'Citation' term is special because it serves
  244. // both as a property and as the uniquename for the publiation table so we exclude it
  245. // as it shouldn't be selected as a property
  246. $exclude = array("Citation");
  247. $instructions = '';
  248. tripal_core_properties_form($form, $form_state, 'pubprop', 'pub_id', 'tripal_pub',
  249. $properties, $pub_id, $exclude, $more_props, $instructions, 'Properties');
  250. */
  251. return $form;
  252. }
  253. /*
  254. *
  255. */
  256. function chado_pub_validate($node, $form, &$form_state) {
  257. // get the submitted values
  258. $title = trim($node->pubtitle);
  259. $pyear = trim($node->pyear);
  260. $uniquename = trim($node->uniquename);
  261. $is_obsolete = $node->is_obsolete;
  262. $type_id = $node->type_id;
  263. // if this is a delete then don't validate
  264. if($node->op == 'Delete') {
  265. return;
  266. }
  267. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  268. // need to validate during syncing so just skip it.
  269. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  270. return;
  271. }
  272. $pub = array();
  273. // make sure the year is four digits
  274. if(!preg_match('/^\d{4}$/', $pyear)){
  275. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  276. return;
  277. }
  278. // get the type of publication
  279. $values = array('cvterm_id' => $type_id);
  280. $options = array('statement_name' => 'sel_pub_ty');
  281. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
  282. if (count($cvterm) == 0) {
  283. $message = t('Invalid publication type.');
  284. form_set_error('type_id', $message);
  285. return;
  286. }
  287. // get the media name looking at the properties
  288. $series_name = '';
  289. foreach ($node as $element => $value) {
  290. // if this is an existing property (either previously in the database or
  291. // added via AHAH/AJAX callback)
  292. if (preg_match('/^prop_value-(\d+)-(\d+)$/', $element, $matches)) {
  293. $prop_type_id = $matches[1];
  294. $prop_type = tripal_cv_get_cvterm_by_id($prop_type_id);
  295. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  296. $series_name = $value;
  297. }
  298. if($prop_type->name == 'Citation') {
  299. $uniquename = $value;
  300. }
  301. $pub[$prop_type->name] = $value;
  302. }
  303. // if this is a new property (added by this submit of the form)
  304. elseif ($element == 'new_id') {
  305. $prop_type = tripal_cv_get_cvterm_by_id($value);
  306. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  307. $series_name = $node->new_value;
  308. }
  309. if($prop_type->name == 'Citation') {
  310. $uniquename = $node->new_value;
  311. }
  312. $pub[$prop_type->name] = $node->new_value;
  313. }
  314. }
  315. // if the citation is missing then try to generate one
  316. if (!$uniquename) {
  317. $pub['Title'] = $title;
  318. $pub['Publication Type'][0] = $cvterm[0]->name;
  319. $pub['Year'] = $pyear;
  320. $uniquename = tripal_pub_create_citation($pub);
  321. if (!$uniquename) {
  322. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  323. }
  324. }
  325. $skip_duplicate_check = 0;
  326. // if this publication is a Patent then skip the validation below. Patents can have the title
  327. // name and year but be different
  328. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  329. $skip_duplicate_check = 1;
  330. }
  331. // Validating for an update
  332. if (!is_null($node->nid)) {
  333. $pub_id = $node->pub_id;
  334. // first get the original title, type and year before it was changed
  335. $values = array('pub_id' => $pub_id);
  336. $columns = array('title', 'pyear', 'type_id', 'series_name');
  337. $options = array('statement_name' => 'sel_pub_id');
  338. $pub = tripal_core_chado_select('pub', $columns, $values, $options);
  339. // if the title, type, year or series_name have changed then check the pub
  340. // to see if it is a duplicate of another
  341. if((strcmp(strtolower($pub[0]->title), strtolower($title)) == 0) and
  342. (strcmp(strtolower($pub[0]->series_name), strtolower($series_name)) == 0) and
  343. ($pub[0]->type_id == $type_id) and
  344. ($pub[0]->pyear == $pyear)) {
  345. $skip_duplicate_check = 1;
  346. }
  347. // check to see if a duplicate publication already exists
  348. if (!$skip_duplicate_check) {
  349. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id);
  350. }
  351. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  352. }
  353. // Validating for an insert
  354. else {
  355. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm);
  356. chado_pub_validate_check_uniquename($uniquename);
  357. }
  358. }
  359. /**
  360. *
  361. * @param unknown $uniquename
  362. */
  363. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  364. $results = tripal_pub_get_pub_by_uniquename($uniquename);
  365. // make sure we don't capture our pub_id in the list (remove it)
  366. foreach ($results as $index => $found_pub_id) {
  367. if($found_pub_id == $pub_id){
  368. unset($results[$index]);
  369. }
  370. }
  371. if (count($results) > 0) {
  372. $message = t('A publication with this unique citation already exists.');
  373. form_set_error('uniquename', $message);
  374. }
  375. }
  376. /**
  377. *
  378. */
  379. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  380. // make sure the publication is unique using the prefereed import duplication check
  381. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  382. switch ($import_dups_check) {
  383. case 'title_year':
  384. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, NULL);
  385. // make sure we don't capture our pub_id in the list (remove it)
  386. foreach ($results as $index => $found_pub_id) {
  387. if($found_pub_id == $pub_id){
  388. unset($results[$index]);
  389. }
  390. }
  391. if (count($results) > 0) {
  392. $message = t('A publication with this title and publication year, already exists.');
  393. form_set_error('pyear', $message);
  394. }
  395. break;
  396. case 'title_year_type':
  397. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, $cvterm[0]->name, $pyear, NULL);
  398. // make sure we don't capture our pub_id in the list (remove it)
  399. foreach ($results as $index => $found_pub_id) {
  400. if($found_pub_id == $pub_id){
  401. unset($results[$index]);
  402. }
  403. }
  404. if (count($results) > 0) {
  405. $message = t('A publication with this title, type and publication year, already exists.');
  406. form_set_error('pyear', $message);
  407. }
  408. break;
  409. case 'title_year_media':
  410. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, $series_name);
  411. // make sure we don't capture our pub_id in the list (remove it)
  412. foreach ($results as $index => $found_pub_id) {
  413. if($found_pub_id == $pub_id){
  414. unset($results[$index]);
  415. }
  416. }
  417. if (count($results) > 0) {
  418. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year, already exists.');
  419. form_set_error('pyear', $message);
  420. }
  421. break;
  422. }
  423. }
  424. /**
  425. * Implement hook_access().
  426. *
  427. * This hook allows node modules to limit access to the node types they define.
  428. *
  429. * @param $node
  430. * The node on which the operation is to be performed, or, if it does not yet exist, the
  431. * type of node to be created
  432. *
  433. * @param $op
  434. * The operation to be performed
  435. *
  436. * @param $account
  437. * A user object representing the user for whom the operation is to be performed
  438. *
  439. * @return
  440. * If the permission for the specified operation is not set then return FALSE. If the
  441. * permission is set then return NULL as this allows other modules to disable
  442. * access. The only exception is when the $op == 'create'. We will always
  443. * return TRUE if the permission is set.
  444. *
  445. */
  446. function chado_pub_node_access($node, $op, $account ) {
  447. if ($op == 'create') {
  448. if (!user_access('create chado_pub content', $account)) {
  449. return FALSE;
  450. }
  451. return TRUE;
  452. }
  453. if ($op == 'update') {
  454. if (!user_access('edit chado_pub content', $account)) {
  455. return FALSE;
  456. }
  457. }
  458. if ($op == 'delete') {
  459. if (!user_access('delete chado_pub content', $account)) {
  460. return FALSE;
  461. }
  462. }
  463. if ($op == 'view') {
  464. if (!user_access('access chado_pub content', $account)) {
  465. return FALSE;
  466. }
  467. }
  468. return NULL;
  469. }
  470. /**
  471. * Implementation of tripal_pub_insert().
  472. *
  473. * This function inserts user entered information pertaining to the Publication instance into the
  474. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  475. *
  476. * @parm $node
  477. * Then node which contains the information stored within the node-ID
  478. *
  479. *
  480. */
  481. function chado_pub_insert($node) {
  482. $title = trim($node->pubtitle);
  483. $pyear = trim($node->pyear);
  484. $uniquename = trim($node->uniquename);
  485. $is_obsolete = $node->is_obsolete;
  486. $type_id = $node->type_id;
  487. // we need an array suitable for the tripal_pub_create_citation() function
  488. // to automatically generate a citation if a uniquename doesn't already exist
  489. $pub_arr = array();
  490. // if there is an pub_id in the $node object then this must be a sync so
  491. // we can skip adding the pub as it is already there, although
  492. // we do need to proceed with the rest of the insert
  493. if (!property_exists($node, 'pub_id')) {
  494. $properties = array(); // stores all of the properties we need to add
  495. $cross_refs = array(); // stores any cross references for this publication
  496. // get the properties from the form
  497. $properties = chado_node_properties_form_retreive($node);
  498. // get the list of properties for easy lookup (without doing lots of database queries
  499. $properties_list = array();
  500. $sql = "
  501. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  502. FROM {cvtermpath} CVTP
  503. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  504. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  505. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  506. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  507. NOT CVTS.is_obsolete = 1
  508. ORDER BY CVTS.name ASC
  509. ";
  510. $prop_types = chado_query($sql);
  511. while ($prop = $prop_types->fetchObject()) {
  512. $properties_list[$prop->cvterm_id] = $prop->name;
  513. // The 'Citation' term is special because it serves
  514. // both as a property and as the uniquename for the
  515. // pub and we want it stored in both the pub table and the pubprop table
  516. if ($prop->name == 'Citation') {
  517. $properties[$prop->name][0] = $node->uniquename;
  518. }
  519. }
  520. // iterate through all of the properties and remove those that really are
  521. // part of the pub table fields
  522. $volume = '';
  523. $volumetitle = '';
  524. $issue = '';
  525. $pages = '';
  526. $publisher = '';
  527. $series_name = '';
  528. $pubplace = '';
  529. $miniref = '';
  530. $cross_refs = array();
  531. foreach ($properties as $name => $element) {
  532. $value = trim($element[0]);
  533. // populate our $pub_array for building a citation
  534. $pub_arr[$name] = $value;
  535. // remove properties that are stored in the pub table
  536. if ($name == "Volume") {
  537. $volume = $value;
  538. unset($properties[$name]);
  539. }
  540. elseif ($name == "Volume Title") {
  541. $volumetitle = $value;
  542. unset($properties[$name]);
  543. }
  544. elseif ($name == "Issue") {
  545. $issue = $value;
  546. unset($properties[$name]);
  547. }
  548. elseif ($name == "Pages") {
  549. $pages = $value;
  550. unset($properties[$name]);
  551. }
  552. elseif ($name == "Publisher") {
  553. $publisher = $value;
  554. unset($properties[$name]);
  555. }
  556. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  557. $series_name = $value;
  558. unset($properties[$name]);
  559. }
  560. elseif ($name == "Journal Country" or $name == "Published Location") {
  561. $pubplace = $value;
  562. // allow this property to go into the pubprop table so we don't loose info
  563. // so don't unset it. But it will also go into the pub.pubplace field
  564. }
  565. elseif ($name == "Publication Dbxref") {
  566. // we will add the cross-references to the pub_dbxref table
  567. // but we also want to keep the property in the pubprop table so don't unset it
  568. $cross_refs[] = $value;
  569. }
  570. }
  571. // generate a citation for this pub if one doesn't already exist
  572. if (!$node->uniquename and array_key_exists('Citation', $properties)) {
  573. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  574. $pub_arr['Title'] = $node->pubtitle;
  575. $pub_arr['Publication Type'][0] = $pub_type->name;
  576. $pub_arr['Year'] = $node->pyear;
  577. $node->uniquename = tripal_pub_create_citation($pub_arr);
  578. $properties['Citation'][0] = $node->uniquename;
  579. }
  580. // insert the pub record
  581. $values = array(
  582. 'title' => $node->pubtitle,
  583. 'series_name' => substr($series_name, 0, 255),
  584. 'type_id' => $node->type_id,
  585. 'pyear' => $node->pyear,
  586. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  587. 'uniquename' => $node->uniquename,
  588. 'volumetitle' => $volumetitle,
  589. 'volume' => $volume,
  590. 'issue' => $issue,
  591. 'pages' => $pages,
  592. 'miniref' => substr($miniref, 0, 255),
  593. 'publisher' => substr($publisher, 0, 255),
  594. 'pubplace' => substr($pubplace, 0, 255),
  595. );
  596. $pub = tripal_core_chado_insert('pub', $values);
  597. if (!$pub) {
  598. drupal_set_message("Error inserting publication", "error");
  599. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  600. return;
  601. }
  602. $pub_id = $pub['pub_id'];
  603. // now add in the properties
  604. // Only adds in those not used in the pub record
  605. chado_node_properties_form_update_properties($node, $details, $properties);
  606. // add in any database cross-references
  607. foreach ($cross_refs as $index => $ref) {
  608. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  609. if (!$pub_dbxref) {
  610. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  611. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  612. array('%ref' => $ref), WATCHDOG_ERROR);
  613. }
  614. }
  615. }
  616. else {
  617. $pub_id = $node->pub_id;
  618. }
  619. // Make sure the entry for this pub doesn't already exist in the
  620. // chado_pub table if it doesn't exist then we want to add it.
  621. $check_org_id = chado_get_id_for_node('pub', $node->nid);
  622. if (!$check_org_id) {
  623. $record = new stdClass();
  624. $record->nid = $node->nid;
  625. $record->vid = $node->vid;
  626. $record->pub_id = $pub_id;
  627. drupal_write_record('chado_pub', $record);
  628. }
  629. }
  630. /*
  631. *
  632. * Implements hook_update
  633. *
  634. * The purpose of the function is to allow the module to take action when an edited node is being
  635. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  636. * As well, the database will be changed, so the user changed information will be saved to the database.
  637. *
  638. * @param $node
  639. * The node being updated
  640. *
  641. * @ingroup tripal_pub
  642. */
  643. function chado_pub_update($node) {
  644. $title = trim($node->pubtitle);
  645. $pyear = trim($node->pyear);
  646. $uniquename = trim($node->uniquename);
  647. $is_obsolete = $node->is_obsolete;
  648. $type_id = $node->type_id;
  649. // we need an array suitable for the tripal_pub_create_citation() function
  650. // to automatically generate a citation if a uniquename doesn't already exist
  651. $pub_arr = array();
  652. // get the publication ID for this publication
  653. $pub_id = chado_get_id_for_node('pub', $node->nid) ;
  654. $properties = array(); // stores all of the properties we need to add
  655. $cross_refs = array(); // stores any cross references for this publication
  656. // get the properties from the form
  657. $properties = chado_node_properties_form_retreive($node);
  658. // get the list of properties for easy lookup (without doing lots of database queries
  659. $properties_list = array();
  660. $sql = "
  661. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  662. FROM {cvtermpath} CVTP
  663. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  664. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  665. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  666. WHERE CV.name = 'tripal_pub' and
  667. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  668. NOT CVTS.is_obsolete = 1
  669. ORDER BY CVTS.name ASC
  670. ";
  671. $prop_types = chado_query($sql);
  672. while ($prop = $prop_types->fetchObject()) {
  673. $properties_list[$prop->cvterm_id] = $prop->name;
  674. // The 'Citation' term is special because it serves
  675. // both as a property and as the uniquename for the
  676. // pub and we want it stored in both the pub table and the pubprop table
  677. if ($prop->name == 'Citation') {
  678. $properties[$prop->name][0] = $node->uniquename;
  679. }
  680. }
  681. // iterate through all of the properties and remove those that really are
  682. // part of the pub table fields
  683. $volume = '';
  684. $volumetitle = '';
  685. $issue = '';
  686. $pages = '';
  687. $publisher = '';
  688. $series_name = '';
  689. $pubplace = '';
  690. $miniref = '';
  691. $cross_refs = array();
  692. foreach ($properties as $name => $element) {
  693. foreach ($element as $index => $value) {
  694. // populate our $pub_array for building a citation
  695. $pub_arr[$name] = $value;
  696. // remove properties that are stored in the pub table
  697. if ($name == "Volume") {
  698. $volume = $value;
  699. unset($properties[$name]);
  700. }
  701. elseif ($name == "Volume Title") {
  702. $volumetitle = $value;
  703. unset($properties[$name]);
  704. }
  705. elseif ($name == "Issue") {
  706. $issue = $value;
  707. unset($properties[$name]);
  708. }
  709. elseif ($name == "Pages") {
  710. $pages = $value;
  711. unset($properties[$name]);
  712. }
  713. elseif ($name == "Publisher") {
  714. $publisher = $value;
  715. unset($properties[$name]);
  716. }
  717. elseif ($name == "Journal Name" or $name == "Conference Name") {
  718. $series_name = $value;
  719. unset($properties[$name]);
  720. }
  721. elseif ($name == "Journal Country" or $name == "Published Location") {
  722. $pubplace = $value;
  723. // allow this property to go into the pubprop table so we don't loose info
  724. // so don't unset it. But it will also go into the pub.pubplace field
  725. }
  726. elseif ($name == "Publication Dbxref") {
  727. // we will add the cross-references to the pub_dbxref table
  728. // but we also want to keep the property in the pubprop table so don't unset it
  729. $cross_refs[] = $value;
  730. }
  731. }
  732. }
  733. // generate a citation for this pub if one doesn't already exist
  734. if (!$node->uniquename) {
  735. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  736. $pub_arr['Title'] = $node->pubtitle;
  737. $pub_arr['Publication Type'][0] = $pub_type->name;
  738. $pub_arr['Year'] = $node->pyear;
  739. $node->uniquename = tripal_pub_create_citation($pub_arr);
  740. $properties['Citation'][0] = $node->uniquename;
  741. }
  742. // update the pub record
  743. $match = array(
  744. 'pub_id' => $pub_id,
  745. );
  746. $values = array(
  747. 'title' => $node->pubtitle,
  748. 'type_id' => $node->type_id,
  749. 'pyear' => $node->pyear,
  750. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  751. 'uniquename' => $node->uniquename,
  752. 'series_name' => substr($series_name, 0, 255),
  753. 'volumetitle' => $volumetitle,
  754. 'volume' => $volume,
  755. 'issue' => $issue,
  756. 'pages' => $pages,
  757. 'miniref' => substr($miniref, 0, 255),
  758. 'publisher' => substr($publisher, 0, 255),
  759. 'pubplace' => substr($pubplace, 0, 255),
  760. );
  761. $status = tripal_core_chado_update('pub', $match, $values);
  762. if (!$status) {
  763. drupal_set_message("Error updating publication", "error");
  764. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  765. return;
  766. }
  767. // now add in the properties by first removing any the publication
  768. // already has and adding the ones we have
  769. $details = array(
  770. 'property_table' => 'pubprop',
  771. 'base_table' => 'pub',
  772. 'foreignkey_name' => 'pub_id',
  773. 'foreignkey_value' => $pub_id
  774. );
  775. chado_node_properties_form_update_properties($node, $details, $$properties);
  776. // add in any database cross-references after first removing
  777. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  778. foreach ($cross_refs as $index => $ref) {
  779. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  780. if (!$pub_dbxref) {
  781. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  782. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  783. array('%ref' => $ref), WATCHDOG_ERROR);
  784. }
  785. }
  786. }
  787. /**
  788. * Implementation of tripal_pub_load().
  789. *
  790. *
  791. * @param $node
  792. * The node that is to be accessed from the database
  793. *
  794. * @return $node
  795. * The node with the information to be loaded into the database
  796. *
  797. */
  798. function chado_pub_load($nodes) {
  799. foreach ($nodes as $nid => $node) {
  800. // find the pub and add in the details
  801. $pub_id = chado_get_id_for_node('pub', $nid);
  802. // get the pub
  803. $values = array('pub_id' => $pub_id);
  804. $pub = tripal_core_generate_chado_var('pub', $values);
  805. // expand the 'text' fields as those aren't included by default
  806. // and they really shouldn't be so large to cause problems
  807. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  808. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  809. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  810. // set the URL path
  811. $nodes[$nid]->path = "pub/$pub_id";
  812. $nodes[$nid]->pub = $pub;
  813. }
  814. }
  815. /**
  816. * Implementation of tripal_pub_delete().
  817. *
  818. * This function takes a node and if the delete button has been chosen by the user, the publication
  819. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  820. * the 'chado_pub' table.
  821. *
  822. * @parm $node
  823. * Then node which contains the information stored within the node-ID
  824. *
  825. */
  826. function chado_pub_delete(&$node) {
  827. $pub_id = chado_get_id_for_node('pub', $node->nid);
  828. // if we don't have a pub id for this node then this isn't a node of
  829. // type chado_pub or the entry in the chado_pub table was lost.
  830. if (!$pub_id) {
  831. return;
  832. }
  833. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  834. // drupal database
  835. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  836. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  837. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  838. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  839. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  840. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  841. // Remove data from pub and pubprop tables of chado database as well
  842. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  843. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  844. }
  845. /**
  846. *
  847. * @ingroup tripal_feature
  848. */
  849. function tripal_pub_node_view($node, $view_mode, $langcode) {
  850. switch ($node->type) {
  851. case 'chado_pub':
  852. // Show feature browser and counts
  853. if ($view_mode == 'full') {
  854. $node->content['tripal_pub_authors'] = array(
  855. '#value' => theme('tripal_pub_authors', array('node' => $node)),
  856. );
  857. $node->content['tripal_pub_base'] = array(
  858. '#value' => theme('tripal_pub_base', array('node' => $node)),
  859. );
  860. $node->content['tripal_pub_featuremaps'] = array(
  861. '#value' => theme('tripal_pub_featuremaps', array('node' => $node)),
  862. );
  863. $node->content['tripal_pub_features'] = array(
  864. '#value' => theme('tripal_pub_features', array('node' => $node)),
  865. );
  866. $node->content['tripal_pub_libraries'] = array(
  867. '#value' => theme('tripal_pub_libraries', array('node' => $node)),
  868. );
  869. $node->content['tripal_pub_projects'] = array(
  870. '#value' => theme('tripal_pub_projects', array('node' => $node)),
  871. );
  872. $node->content['tripal_pub_properties'] = array(
  873. '#value' => theme('tripal_pub_properties', array('node' => $node)),
  874. );
  875. $node->content['tripal_pub_references'] = array(
  876. '#value' => theme('tripal_pub_references', array('node' => $node)),
  877. );
  878. $node->content['tripal_pub_relationships'] = array(
  879. '#value' => theme('tripal_pub_relationships', array('node' => $node)),
  880. );
  881. $node->content['tripal_pub_stocks'] = array(
  882. '#value' => theme('tripal_pub_stocks', array('node' => $node)),
  883. );
  884. }
  885. if ($view_mode == 'teaser') {
  886. $node->content['tripal_pub_teaser'] = array(
  887. '#value' => theme('tripal_pub_teaser', array('node' => $node)),
  888. );
  889. }
  890. break;
  891. }
  892. }
  893. /**
  894. *
  895. * @param $node
  896. */
  897. function tripal_pub_node_insert($node) {
  898. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  899. // where [pub id] is the Chado publication ID. This will allow for easy linking
  900. // into the publication without needing to know the node. Of course if you know the
  901. // node that will still work too (e.g. http://[base url]/node/[node id]
  902. // so the nodeapi function ensures that the URL path is set after insert or update
  903. // of the node and when the node is loaded if it hasn't yet been set.
  904. if ($node->type == 'chado_pub') {
  905. $pub_id = chado_get_id_for_node('pub', $node->nid);
  906. tripal_pub_set_pub_url($node, $pub_id);
  907. }
  908. }
  909. /**
  910. *
  911. * @param $node
  912. * @param $types
  913. */
  914. function tripal_pub_node_load($nodes, $types) {
  915. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  916. // where [pub id] is the Chado publication ID. This will allow for easy linking
  917. // into the publication without needing to know the node. Of course if you know the
  918. // node that will still work too (e.g. http://[base url]/node/[node id]
  919. // so the nodeapi function ensures that the URL path is set after insert or update
  920. // of the node and when the node is loaded if it hasn't yet been set.
  921. if (count(array_intersect(array('chado_pub'), $types))) {
  922. foreach ($nodes as $nid => $node) {
  923. if ($node->type == 'chado_pub' and !property_exists($node, 'path')) {
  924. $pub_id = chado_get_id_for_node('pub', $node->nid);
  925. $path = tripal_pub_set_pub_url($node, $pub_id);
  926. }
  927. }
  928. }
  929. }
  930. /**
  931. *
  932. * @param $node
  933. */
  934. function tripal_pub_node_update($node) {
  935. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  936. // where [pub id] is the Chado publication ID. This will allow for easy linking
  937. // into the publication without needing to know the node. Of course if you know the
  938. // node that will still work too (e.g. http://[base url]/node/[node id]
  939. // so the nodeapi function ensures that the URL path is set after insert or update
  940. // of the node and when the node is loaded if it hasn't yet been set.
  941. if ($node->type == 'chado_pub') {
  942. $pub_id = chado_get_id_for_node('pub', $node->nid);
  943. tripal_pub_set_pub_url($node, $pub_id);
  944. }
  945. }