tripal_pub.chado_node.inc 36 KB

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