tripal_pub.chado_node.inc 37 KB

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