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. // get publication properties list
  210. $properties_select = array();
  211. $properties_select[] = 'Select a Property';
  212. $sql = "
  213. SELECT
  214. DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  215. FROM {cvtermpath} CVTP
  216. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  217. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  218. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  219. WHERE CV.name = 'tripal_pub' and
  220. (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
  221. NOT CVTS.is_obsolete = 1
  222. ORDER BY CVTS.name ASC
  223. ";
  224. $prop_types = chado_query($sql);
  225. while ($prop = $prop_types->fetchObject()) {
  226. // add all properties except the Citation. That property is set via the uniquename field
  227. if ($prop->name != 'Citation') {
  228. $properties[$prop->cvterm_id] = $prop->name;
  229. }
  230. }
  231. // add in the properties fields. The 'Citation' term is special because it serves
  232. // both as a property and as the uniquename for the publiation table so we exclude it
  233. // as it shouldn't be selected as a property
  234. $exclude = array("Citation");
  235. $instructions = '';
  236. tripal_core_properties_form($form, $form_state, 'pubprop', 'pub_id', 'tripal_pub',
  237. $properties, $pub_id, $exclude, $more_props, $instructions, 'Properties');
  238. return $form;
  239. }
  240. /*
  241. *
  242. */
  243. function chado_pub_validate($node, $form, &$form_state) {
  244. // get the submitted values
  245. $title = trim($node->pubtitle);
  246. $pyear = trim($node->pyear);
  247. $uniquename = trim($node->uniquename);
  248. $is_obsolete = $node->is_obsolete;
  249. $type_id = $node->type_id;
  250. // if this is a delete then don't validate
  251. if($node->op == 'Delete') {
  252. return;
  253. }
  254. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  255. // need to validate during syncing so just skip it.
  256. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  257. return;
  258. }
  259. $pub = array();
  260. // make sure the year is four digits
  261. if(!preg_match('/^\d{4}$/', $pyear)){
  262. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  263. return;
  264. }
  265. // get the type of publication
  266. $values = array('cvterm_id' => $type_id);
  267. $options = array('statement_name' => 'sel_pub_ty');
  268. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
  269. if (count($cvterm) == 0) {
  270. $message = t('Invalid publication type.');
  271. form_set_error('type_id', $message);
  272. return;
  273. }
  274. // get the media name looking at the properties
  275. $series_name = '';
  276. foreach ($node as $element => $value) {
  277. // if this is an existing property (either previously in the database or
  278. // added via AHAH/AJAX callback)
  279. if (preg_match('/^prop_value-(\d+)-(\d+)$/', $element, $matches)) {
  280. $prop_type_id = $matches[1];
  281. $prop_type = tripal_cv_get_cvterm_by_id($prop_type_id);
  282. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  283. $series_name = $value;
  284. }
  285. if($prop_type->name == 'Citation') {
  286. $uniquename = $value;
  287. }
  288. $pub[$prop_type->name] = $value;
  289. }
  290. // if this is a new property (added by this submit of the form)
  291. elseif ($element == 'new_id') {
  292. $prop_type = tripal_cv_get_cvterm_by_id($value);
  293. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  294. $series_name = $node->new_value;
  295. }
  296. if($prop_type->name == 'Citation') {
  297. $uniquename = $node->new_value;
  298. }
  299. $pub[$prop_type->name] = $node->new_value;
  300. }
  301. }
  302. // if the citation is missing then try to generate one
  303. if (!$uniquename) {
  304. $pub['Title'] = $title;
  305. $pub['Publication Type'][0] = $cvterm[0]->name;
  306. $pub['Year'] = $pyear;
  307. $uniquename = tripal_pub_create_citation($pub);
  308. if (!$uniquename) {
  309. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  310. }
  311. }
  312. $skip_duplicate_check = 0;
  313. // if this publication is a Patent then skip the validation below. Patents can have the title
  314. // name and year but be different
  315. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  316. $skip_duplicate_check = 1;
  317. }
  318. // Validating for an update
  319. if (!is_null($node->nid)) {
  320. $pub_id = $node->pub_id;
  321. // first get the original title, type and year before it was changed
  322. $values = array('pub_id' => $pub_id);
  323. $columns = array('title', 'pyear', 'type_id', 'series_name');
  324. $options = array('statement_name' => 'sel_pub_id');
  325. $pub = tripal_core_chado_select('pub', $columns, $values, $options);
  326. // if the title, type, year or series_name have changed then check the pub
  327. // to see if it is a duplicate of another
  328. if((strcmp(strtolower($pub[0]->title), strtolower($title)) == 0) and
  329. (strcmp(strtolower($pub[0]->series_name), strtolower($series_name)) == 0) and
  330. ($pub[0]->type_id == $type_id) and
  331. ($pub[0]->pyear == $pyear)) {
  332. $skip_duplicate_check = 1;
  333. }
  334. // check to see if a duplicate publication already exists
  335. if (!$skip_duplicate_check) {
  336. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id);
  337. }
  338. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  339. }
  340. // Validating for an insert
  341. else {
  342. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm);
  343. chado_pub_validate_check_uniquename($uniquename);
  344. }
  345. }
  346. /**
  347. *
  348. * @param unknown $uniquename
  349. */
  350. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  351. $results = tripal_pub_get_pub_by_uniquename($uniquename);
  352. // make sure we don't capture our pub_id in the list (remove it)
  353. foreach ($results as $index => $found_pub_id) {
  354. if($found_pub_id == $pub_id){
  355. unset($results[$index]);
  356. }
  357. }
  358. if (count($results) > 0) {
  359. $message = t('A publication with this unique citation already exists.');
  360. form_set_error('uniquename', $message);
  361. }
  362. }
  363. /**
  364. *
  365. */
  366. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  367. // make sure the publication is unique using the prefereed import duplication check
  368. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  369. switch ($import_dups_check) {
  370. case 'title_year':
  371. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, NULL);
  372. // make sure we don't capture our pub_id in the list (remove it)
  373. foreach ($results as $index => $found_pub_id) {
  374. if($found_pub_id == $pub_id){
  375. unset($results[$index]);
  376. }
  377. }
  378. if (count($results) > 0) {
  379. $message = t('A publication with this title and publication year, already exists.');
  380. form_set_error('pyear', $message);
  381. }
  382. break;
  383. case 'title_year_type':
  384. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, $cvterm[0]->name, $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, type and publication year, already exists.');
  393. form_set_error('pyear', $message);
  394. }
  395. break;
  396. case 'title_year_media':
  397. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, $series_name);
  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, media name (e.g. Journal Name) and publication year, already exists.');
  406. form_set_error('pyear', $message);
  407. }
  408. break;
  409. }
  410. }
  411. /**
  412. * Implement hook_access().
  413. *
  414. * This hook allows node modules to limit access to the node types they define.
  415. *
  416. * @param $node
  417. * The node on which the operation is to be performed, or, if it does not yet exist, the
  418. * type of node to be created
  419. *
  420. * @param $op
  421. * The operation to be performed
  422. *
  423. * @param $account
  424. * A user object representing the user for whom the operation is to be performed
  425. *
  426. * @return
  427. * If the permission for the specified operation is not set then return FALSE. If the
  428. * permission is set then return NULL as this allows other modules to disable
  429. * access. The only exception is when the $op == 'create'. We will always
  430. * return TRUE if the permission is set.
  431. *
  432. */
  433. function chado_pub_node_access($node, $op, $account ) {
  434. if ($op == 'create') {
  435. if (!user_access('create chado_pub content', $account)) {
  436. return FALSE;
  437. }
  438. return TRUE;
  439. }
  440. if ($op == 'update') {
  441. if (!user_access('edit chado_pub content', $account)) {
  442. return FALSE;
  443. }
  444. }
  445. if ($op == 'delete') {
  446. if (!user_access('delete chado_pub content', $account)) {
  447. return FALSE;
  448. }
  449. }
  450. if ($op == 'view') {
  451. if (!user_access('access chado_pub content', $account)) {
  452. return FALSE;
  453. }
  454. }
  455. return NULL;
  456. }
  457. /**
  458. * Implementation of tripal_pub_insert().
  459. *
  460. * This function inserts user entered information pertaining to the Publication instance into the
  461. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  462. *
  463. * @parm $node
  464. * Then node which contains the information stored within the node-ID
  465. *
  466. *
  467. */
  468. function chado_pub_insert($node) {
  469. $title = trim($node->pubtitle);
  470. $pyear = trim($node->pyear);
  471. $uniquename = trim($node->uniquename);
  472. $is_obsolete = $node->is_obsolete;
  473. $type_id = $node->type_id;
  474. // we need an array suitable for the tripal_pub_create_citation() function
  475. // to automatically generate a citation if a uniquename doesn't already exist
  476. $pub_arr = array();
  477. // if there is an pub_id in the $node object then this must be a sync so
  478. // we can skip adding the pub as it is already there, although
  479. // we do need to proceed with the rest of the insert
  480. if (!property_exists($node, 'pub_id')) {
  481. $properties = array(); // stores all of the properties we need to add
  482. $cross_refs = array(); // stores any cross references for this publication
  483. // get the properties from the form
  484. $properties = tripal_core_properties_form_retreive($node, 'tripal_pub');
  485. // get the list of properties for easy lookup (without doing lots of database queries
  486. $properties_list = array();
  487. $sql = "
  488. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  489. FROM {cvtermpath} CVTP
  490. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  491. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  492. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  493. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  494. NOT CVTS.is_obsolete = 1
  495. ORDER BY CVTS.name ASC
  496. ";
  497. $prop_types = chado_query($sql);
  498. while ($prop = $prop_types->fetchObject()) {
  499. $properties_list[$prop->cvterm_id] = $prop->name;
  500. // The 'Citation' term is special because it serves
  501. // both as a property and as the uniquename for the
  502. // pub and we want it stored in both the pub table and the pubprop table
  503. if ($prop->name == 'Citation') {
  504. $properties[$prop->name][0] = $node->uniquename;
  505. }
  506. }
  507. // iterate through all of the properties and remove those that really are
  508. // part of the pub table fields
  509. $volume = '';
  510. $volumetitle = '';
  511. $issue = '';
  512. $pages = '';
  513. $publisher = '';
  514. $series_name = '';
  515. $pubplace = '';
  516. $miniref = '';
  517. $cross_refs = array();
  518. foreach ($properties as $name => $element) {
  519. $value = trim($element[0]);
  520. // populate our $pub_array for building a citation
  521. $pub_arr[$name] = $value;
  522. // remove properties that are stored in the pub table
  523. if ($name == "Volume") {
  524. $volume = $value;
  525. unset($properties[$name]);
  526. }
  527. elseif ($name == "Volume Title") {
  528. $volumetitle = $value;
  529. unset($properties[$name]);
  530. }
  531. elseif ($name == "Issue") {
  532. $issue = $value;
  533. unset($properties[$name]);
  534. }
  535. elseif ($name == "Pages") {
  536. $pages = $value;
  537. unset($properties[$name]);
  538. }
  539. elseif ($name == "Publisher") {
  540. $publisher = $value;
  541. unset($properties[$name]);
  542. }
  543. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  544. $series_name = $value;
  545. unset($properties[$name]);
  546. }
  547. elseif ($name == "Journal Country" or $name == "Published Location") {
  548. $pubplace = $value;
  549. // allow this property to go into the pubprop table so we don't loose info
  550. // so don't unset it. But it will also go into the pub.pubplace field
  551. }
  552. elseif ($name == "Publication Dbxref") {
  553. // we will add the cross-references to the pub_dbxref table
  554. // but we also want to keep the property in the pubprop table so don't unset it
  555. $cross_refs[] = $value;
  556. }
  557. }
  558. // generate a citation for this pub if one doesn't already exist
  559. if (!$node->uniquename and array_key_exists('Citation', $properties)) {
  560. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  561. $pub_arr['Title'] = $node->pubtitle;
  562. $pub_arr['Publication Type'][0] = $pub_type->name;
  563. $pub_arr['Year'] = $node->pyear;
  564. $node->uniquename = tripal_pub_create_citation($pub_arr);
  565. $properties['Citation'][0] = $node->uniquename;
  566. }
  567. // insert the pub record
  568. $values = array(
  569. 'title' => $node->pubtitle,
  570. 'series_name' => substr($series_name, 0, 255),
  571. 'type_id' => $node->type_id,
  572. 'pyear' => $node->pyear,
  573. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  574. 'uniquename' => $node->uniquename,
  575. 'volumetitle' => $volumetitle,
  576. 'volume' => $volume,
  577. 'issue' => $issue,
  578. 'pages' => $pages,
  579. 'miniref' => substr($miniref, 0, 255),
  580. 'publisher' => substr($publisher, 0, 255),
  581. 'pubplace' => substr($pubplace, 0, 255),
  582. );
  583. $pub = tripal_core_chado_insert('pub', $values);
  584. if (!$pub) {
  585. drupal_set_message("Error inserting publication", "error");
  586. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  587. return;
  588. }
  589. $pub_id = $pub['pub_id'];
  590. // now add in the properties
  591. foreach ($properties as $property => $elements) {
  592. foreach ($elements as $rank => $value) {
  593. $status = tripal_pub_insert_property($pub['pub_id'], $property, $value, FALSE);
  594. if (!$status) {
  595. drupal_set_message("Error cannot add property: $property", "error");
  596. watchdog('tripal_pub', "Error cannot add property: %prop",
  597. array('%property' => $property), WATCHDOG_ERROR);
  598. }
  599. }
  600. }
  601. // add in any database cross-references
  602. foreach ($cross_refs as $index => $ref) {
  603. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  604. if (!$pub_dbxref) {
  605. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  606. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  607. array('%ref' => $ref), WATCHDOG_ERROR);
  608. }
  609. }
  610. }
  611. else {
  612. $pub_id = $node->pub_id;
  613. }
  614. // Make sure the entry for this pub doesn't already exist in the
  615. // chado_pub table if it doesn't exist then we want to add it.
  616. $check_org_id = chado_get_id_for_node('pub', $node->nid);
  617. if (!$check_org_id) {
  618. $record = new stdClass();
  619. $record->nid = $node->nid;
  620. $record->vid = $node->vid;
  621. $record->pub_id = $pub_id;
  622. drupal_write_record('chado_pub', $record);
  623. }
  624. }
  625. /*
  626. *
  627. * Implements hook_update
  628. *
  629. * The purpose of the function is to allow the module to take action when an edited node is being
  630. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  631. * As well, the database will be changed, so the user changed information will be saved to the database.
  632. *
  633. * @param $node
  634. * The node being updated
  635. *
  636. * @ingroup tripal_pub
  637. */
  638. function chado_pub_update($node) {
  639. $title = trim($node->pubtitle);
  640. $pyear = trim($node->pyear);
  641. $uniquename = trim($node->uniquename);
  642. $is_obsolete = $node->is_obsolete;
  643. $type_id = $node->type_id;
  644. // we need an array suitable for the tripal_pub_create_citation() function
  645. // to automatically generate a citation if a uniquename doesn't already exist
  646. $pub_arr = array();
  647. // get the publication ID for this publication
  648. $pub_id = chado_get_id_for_node('pub', $node->nid) ;
  649. $properties = array(); // stores all of the properties we need to add
  650. $cross_refs = array(); // stores any cross references for this publication
  651. // get the properties from the form
  652. $properties = tripal_core_properties_form_retreive($node, 'tripal_pub');
  653. // get the list of properties for easy lookup (without doing lots of database queries
  654. $properties_list = array();
  655. $sql = "
  656. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  657. FROM {cvtermpath} CVTP
  658. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  659. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  660. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  661. WHERE CV.name = 'tripal_pub' and
  662. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  663. NOT CVTS.is_obsolete = 1
  664. ORDER BY CVTS.name ASC
  665. ";
  666. $prop_types = chado_query($sql);
  667. while ($prop = $prop_types->fetchObject()) {
  668. $properties_list[$prop->cvterm_id] = $prop->name;
  669. // The 'Citation' term is special because it serves
  670. // both as a property and as the uniquename for the
  671. // pub and we want it stored in both the pub table and the pubprop table
  672. if ($prop->name == 'Citation') {
  673. $properties[$prop->name][0] = $node->uniquename;
  674. }
  675. }
  676. // iterate through all of the properties and remove those that really are
  677. // part of the pub table fields
  678. $volume = '';
  679. $volumetitle = '';
  680. $issue = '';
  681. $pages = '';
  682. $publisher = '';
  683. $series_name = '';
  684. $pubplace = '';
  685. $miniref = '';
  686. $cross_refs = array();
  687. foreach ($properties as $name => $element) {
  688. foreach ($element as $index => $value) {
  689. // populate our $pub_array for building a citation
  690. $pub_arr[$name] = $value;
  691. // remove properties that are stored in the pub table
  692. if ($name == "Volume") {
  693. $volume = $value;
  694. unset($properties[$name]);
  695. }
  696. elseif ($name == "Volume Title") {
  697. $volumetitle = $value;
  698. unset($properties[$name]);
  699. }
  700. elseif ($name == "Issue") {
  701. $issue = $value;
  702. unset($properties[$name]);
  703. }
  704. elseif ($name == "Pages") {
  705. $pages = $value;
  706. unset($properties[$name]);
  707. }
  708. elseif ($name == "Publisher") {
  709. $publisher = $value;
  710. unset($properties[$name]);
  711. }
  712. elseif ($name == "Journal Name" or $name == "Conference Name") {
  713. $series_name = $value;
  714. unset($properties[$name]);
  715. }
  716. elseif ($name == "Journal Country" or $name == "Published Location") {
  717. $pubplace = $value;
  718. // allow this property to go into the pubprop table so we don't loose info
  719. // so don't unset it. But it will also go into the pub.pubplace field
  720. }
  721. elseif ($name == "Publication Dbxref") {
  722. // we will add the cross-references to the pub_dbxref table
  723. // but we also want to keep the property in the pubprop table so don't unset it
  724. $cross_refs[] = $value;
  725. }
  726. }
  727. }
  728. // generate a citation for this pub if one doesn't already exist
  729. if (!$node->uniquename) {
  730. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  731. $pub_arr['Title'] = $node->pubtitle;
  732. $pub_arr['Publication Type'][0] = $pub_type->name;
  733. $pub_arr['Year'] = $node->pyear;
  734. $node->uniquename = tripal_pub_create_citation($pub_arr);
  735. $properties['Citation'][0] = $node->uniquename;
  736. }
  737. // update the pub record
  738. $match = array(
  739. 'pub_id' => $pub_id,
  740. );
  741. $values = array(
  742. 'title' => $node->pubtitle,
  743. 'type_id' => $node->type_id,
  744. 'pyear' => $node->pyear,
  745. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  746. 'uniquename' => $node->uniquename,
  747. 'series_name' => substr($series_name, 0, 255),
  748. 'volumetitle' => $volumetitle,
  749. 'volume' => $volume,
  750. 'issue' => $issue,
  751. 'pages' => $pages,
  752. 'miniref' => substr($miniref, 0, 255),
  753. 'publisher' => substr($publisher, 0, 255),
  754. 'pubplace' => substr($pubplace, 0, 255),
  755. );
  756. $status = tripal_core_chado_update('pub', $match, $values);
  757. if (!$status) {
  758. drupal_set_message("Error updating publication", "error");
  759. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  760. return;
  761. }
  762. // now add in the properties by first removing any the publication
  763. // already has and adding the ones we have
  764. tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  765. foreach ($properties as $property => $elements) {
  766. foreach ($elements as $rank => $value) {
  767. $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
  768. if (!$status) {
  769. drupal_set_message("Error cannot add property: '$property'", "error");
  770. watchdog('tripal_pub', "Error cannot add property: '%prop'",
  771. array('%prop' => $property), WATCHDOG_ERROR);
  772. }
  773. }
  774. }
  775. // add in any database cross-references after first removing
  776. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  777. foreach ($cross_refs as $index => $ref) {
  778. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  779. if (!$pub_dbxref) {
  780. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  781. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  782. array('%ref' => $ref), WATCHDOG_ERROR);
  783. }
  784. }
  785. }
  786. /**
  787. * Implementation of tripal_pub_load().
  788. *
  789. *
  790. * @param $node
  791. * The node that is to be accessed from the database
  792. *
  793. * @return $node
  794. * The node with the information to be loaded into the database
  795. *
  796. */
  797. function chado_pub_load($nodes) {
  798. foreach ($nodes as $nid => $node) {
  799. // find the pub and add in the details
  800. $pub_id = chado_get_id_for_node('pub', $nid);
  801. // get the pub
  802. $values = array('pub_id' => $pub_id);
  803. $pub = tripal_core_generate_chado_var('pub', $values);
  804. // expand the 'text' fields as those aren't included by default
  805. // and they really shouldn't be so large to cause problems
  806. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  807. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  808. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  809. // set the URL path
  810. $nodes[$nid]->path = "pub/$pub_id";
  811. $nodes[$nid]->pub = $pub;
  812. }
  813. }
  814. /**
  815. * Implementation of tripal_pub_delete().
  816. *
  817. * This function takes a node and if the delete button has been chosen by the user, the publication
  818. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  819. * the 'chado_pub' table.
  820. *
  821. * @parm $node
  822. * Then node which contains the information stored within the node-ID
  823. *
  824. */
  825. function chado_pub_delete(&$node) {
  826. $pub_id = chado_get_id_for_node('pub', $node->nid);
  827. // if we don't have a pub id for this node then this isn't a node of
  828. // type chado_pub or the entry in the chado_pub table was lost.
  829. if (!$pub_id) {
  830. return;
  831. }
  832. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  833. // drupal database
  834. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  835. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  836. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  837. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  838. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  839. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  840. // Remove data from pub and pubprop tables of chado database as well
  841. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  842. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  843. }
  844. /**
  845. *
  846. * @ingroup tripal_feature
  847. */
  848. function tripal_pub_node_view($node, $view_mode, $langcode) {
  849. switch ($node->type) {
  850. case 'chado_pub':
  851. // Show feature browser and counts
  852. if ($view_mode == 'full') {
  853. $node->content['tripal_pub_authors'] = array(
  854. '#value' => theme('tripal_pub_authors', array('node' => $node)),
  855. );
  856. $node->content['tripal_pub_base'] = array(
  857. '#value' => theme('tripal_pub_base', array('node' => $node)),
  858. );
  859. $node->content['tripal_pub_featuremaps'] = array(
  860. '#value' => theme('tripal_pub_featuremaps', array('node' => $node)),
  861. );
  862. $node->content['tripal_pub_features'] = array(
  863. '#value' => theme('tripal_pub_features', array('node' => $node)),
  864. );
  865. $node->content['tripal_pub_libraries'] = array(
  866. '#value' => theme('tripal_pub_libraries', array('node' => $node)),
  867. );
  868. $node->content['tripal_pub_projects'] = array(
  869. '#value' => theme('tripal_pub_projects', array('node' => $node)),
  870. );
  871. $node->content['tripal_pub_properties'] = array(
  872. '#value' => theme('tripal_pub_properties', array('node' => $node)),
  873. );
  874. $node->content['tripal_pub_references'] = array(
  875. '#value' => theme('tripal_pub_references', array('node' => $node)),
  876. );
  877. $node->content['tripal_pub_relationships'] = array(
  878. '#value' => theme('tripal_pub_relationships', array('node' => $node)),
  879. );
  880. $node->content['tripal_pub_stocks'] = array(
  881. '#value' => theme('tripal_pub_stocks', array('node' => $node)),
  882. );
  883. }
  884. if ($view_mode == 'teaser') {
  885. $node->content['tripal_pub_teaser'] = array(
  886. '#value' => theme('tripal_pub_teaser', array('node' => $node)),
  887. );
  888. }
  889. break;
  890. }
  891. }
  892. /**
  893. *
  894. * @param $node
  895. */
  896. function tripal_pub_node_insert($node) {
  897. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  898. // where [pub id] is the Chado publication ID. This will allow for easy linking
  899. // into the publication without needing to know the node. Of course if you know the
  900. // node that will still work too (e.g. http://[base url]/node/[node id]
  901. // so the nodeapi function ensures that the URL path is set after insert or update
  902. // of the node and when the node is loaded if it hasn't yet been set.
  903. if ($node->type == 'chado_pub') {
  904. $pub_id = chado_get_id_for_node('pub', $node->nid);
  905. tripal_pub_set_pub_url($node, $pub_id);
  906. }
  907. }
  908. /**
  909. *
  910. * @param $node
  911. * @param $types
  912. */
  913. function tripal_pub_node_load($nodes, $types) {
  914. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  915. // where [pub id] is the Chado publication ID. This will allow for easy linking
  916. // into the publication without needing to know the node. Of course if you know the
  917. // node that will still work too (e.g. http://[base url]/node/[node id]
  918. // so the nodeapi function ensures that the URL path is set after insert or update
  919. // of the node and when the node is loaded if it hasn't yet been set.
  920. if (count(array_intersect(array('chado_pub'), $types))) {
  921. foreach ($nodes as $nid => $node) {
  922. if ($node->type == 'chado_pub' and !property_exists($node, 'path')) {
  923. $pub_id = chado_get_id_for_node('pub', $node->nid);
  924. $path = tripal_pub_set_pub_url($node, $pub_id);
  925. }
  926. }
  927. }
  928. }
  929. /**
  930. *
  931. * @param $node
  932. */
  933. function tripal_pub_node_update($node) {
  934. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  935. // where [pub id] is the Chado publication ID. This will allow for easy linking
  936. // into the publication without needing to know the node. Of course if you know the
  937. // node that will still work too (e.g. http://[base url]/node/[node id]
  938. // so the nodeapi function ensures that the URL path is set after insert or update
  939. // of the node and when the node is loaded if it hasn't yet been set.
  940. if ($node->type == 'chado_pub') {
  941. $pub_id = chado_get_id_for_node('pub', $node->nid);
  942. tripal_pub_set_pub_url($node, $pub_id);
  943. }
  944. }