tripal_pub.chado_node.inc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. ddl($properties, 'clean properties');
  492. // get the list of properties for easy lookup (without doing lots of database queries
  493. $properties_list = array();
  494. $sql = "
  495. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  496. FROM {cvtermpath} CVTP
  497. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  498. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  499. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  500. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  501. NOT CVTS.is_obsolete = 1
  502. ORDER BY CVTS.name ASC
  503. ";
  504. $prop_types = chado_query($sql);
  505. while ($prop = $prop_types->fetchObject()) {
  506. $properties_list[$prop->cvterm_id] = $prop->name;
  507. // The 'Citation' term is special because it serves
  508. // both as a property and as the uniquename for the
  509. // pub and we want it stored in both the pub table and the pubprop table
  510. if ($prop->name == 'Citation') {
  511. $citation_id = $prop->cvterm_id;
  512. if (!empty($node->uniquename)) {
  513. $properties[$citation_id] = $node->uniquename;
  514. }
  515. }
  516. }
  517. // iterate through all of the properties and remove those that really are
  518. // part of the pub table fields
  519. $volume = '';
  520. $volumetitle = '';
  521. $issue = '';
  522. $pages = '';
  523. $publisher = '';
  524. $series_name = '';
  525. $pubplace = '';
  526. $miniref = '';
  527. $cross_refs = array();
  528. foreach ($properties as $type_id => $element) {
  529. $value = trim($element[0]);
  530. $name = $properties_list[$type_id];
  531. // populate our $pub_array for building a citation
  532. $pub_arr[$name] = $value;
  533. // remove properties that are stored in the pub table
  534. if ($name == "Volume") {
  535. $volume = $value;
  536. unset($properties[$type_id]);
  537. }
  538. elseif ($name == "Volume Title") {
  539. $volumetitle = $value;
  540. unset($properties[$type_id]);
  541. }
  542. elseif ($name == "Issue") {
  543. $issue = $value;
  544. unset($properties[$type_id]);
  545. }
  546. elseif ($name == "Pages") {
  547. $pages = $value;
  548. unset($properties[$type_id]);
  549. }
  550. elseif ($name == "Publisher") {
  551. $publisher = $value;
  552. unset($properties[$type_id]);
  553. }
  554. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  555. $series_name = $value;
  556. unset($properties[$type_id]);
  557. }
  558. elseif ($name == "Journal Country" or $name == "Published Location") {
  559. $pubplace = $value;
  560. // allow this property to go into the pubprop table so we don't loose info
  561. // so don't unset it. But it will also go into the pub.pubplace field
  562. }
  563. elseif ($name == "Publication Dbxref") {
  564. // we will add the cross-references to the pub_dbxref table
  565. // but we also want to keep the property in the pubprop table so don't unset it
  566. $cross_refs[] = $value;
  567. }
  568. }
  569. // generate a citation for this pub if one doesn't already exist
  570. if (!$node->uniquename and !array_key_exists($citation_id, $properties)) {
  571. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  572. $pub_arr['Title'] = $node->pubtitle;
  573. $pub_arr['Publication Type'][0] = $pub_type->name;
  574. $pub_arr['Year'] = $node->pyear;
  575. $node->uniquename = tripal_pub_create_citation($pub_arr);
  576. $properties[$citation_id][0] = $node->uniquename;
  577. }
  578. // insert the pub record
  579. $values = array(
  580. 'title' => $node->pubtitle,
  581. 'series_name' => substr($series_name, 0, 255),
  582. 'type_id' => $node->type_id,
  583. 'pyear' => $node->pyear,
  584. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  585. 'uniquename' => $node->uniquename,
  586. 'volumetitle' => $volumetitle,
  587. 'volume' => $volume,
  588. 'issue' => $issue,
  589. 'pages' => $pages,
  590. 'miniref' => substr($miniref, 0, 255),
  591. 'publisher' => substr($publisher, 0, 255),
  592. 'pubplace' => substr($pubplace, 0, 255),
  593. );
  594. $pub = tripal_core_chado_insert('pub', $values);
  595. if (!$pub) {
  596. drupal_set_message("Error inserting publication", "error");
  597. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  598. return;
  599. }
  600. $pub_id = $pub['pub_id'];
  601. // now add in the properties
  602. // Only adds in those not used in the pub record
  603. ddl($properties, 'properties before update');
  604. $details = array(
  605. 'property_table' => 'pubprop',
  606. 'base_table' => 'pub',
  607. 'foreignkey_name' => 'pub_id',
  608. 'foreignkey_value' => $pub_id
  609. );
  610. chado_node_properties_form_update_properties($node, $details, $properties);
  611. // add in any database cross-references
  612. foreach ($cross_refs as $index => $ref) {
  613. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  614. if (!$pub_dbxref) {
  615. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  616. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  617. array('%ref' => $ref), WATCHDOG_ERROR);
  618. }
  619. }
  620. }
  621. else {
  622. $pub_id = $node->pub_id;
  623. }
  624. // Make sure the entry for this pub doesn't already exist in the
  625. // chado_pub table if it doesn't exist then we want to add it.
  626. $check_org_id = chado_get_id_for_node('pub', $node->nid);
  627. if (!$check_org_id) {
  628. $record = new stdClass();
  629. $record->nid = $node->nid;
  630. $record->vid = $node->vid;
  631. $record->pub_id = $pub_id;
  632. drupal_write_record('chado_pub', $record);
  633. }
  634. }
  635. /*
  636. *
  637. * Implements hook_update
  638. *
  639. * The purpose of the function is to allow the module to take action when an edited node is being
  640. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  641. * As well, the database will be changed, so the user changed information will be saved to the database.
  642. *
  643. * @param $node
  644. * The node being updated
  645. *
  646. * @ingroup tripal_pub
  647. */
  648. function chado_pub_update($node) {
  649. $title = trim($node->pubtitle);
  650. $pyear = trim($node->pyear);
  651. $uniquename = trim($node->uniquename);
  652. $is_obsolete = $node->is_obsolete;
  653. $type_id = $node->type_id;
  654. // we need an array suitable for the tripal_pub_create_citation() function
  655. // to automatically generate a citation if a uniquename doesn't already exist
  656. $pub_arr = array();
  657. // get the publication ID for this publication
  658. $pub_id = chado_get_id_for_node('pub', $node->nid) ;
  659. $properties = array(); // stores all of the properties we need to add
  660. $cross_refs = array(); // stores any cross references for this publication
  661. // get the properties from the form
  662. $properties = chado_node_properties_form_retreive($node);
  663. // get the list of properties for easy lookup (without doing lots of database queries
  664. $properties_list = array();
  665. $sql = "
  666. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  667. FROM {cvtermpath} CVTP
  668. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  669. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  670. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  671. WHERE CV.name = 'tripal_pub' and
  672. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  673. NOT CVTS.is_obsolete = 1
  674. ORDER BY CVTS.name ASC
  675. ";
  676. $prop_types = chado_query($sql);
  677. while ($prop = $prop_types->fetchObject()) {
  678. $properties_list[$prop->cvterm_id] = $prop->name;
  679. // The 'Citation' term is special because it serves
  680. // both as a property and as the uniquename for the
  681. // pub and we want it stored in both the pub table and the pubprop table
  682. if ($prop->name == 'Citation') {
  683. $citation_id = $prop->cvterm_id;
  684. if (!empty($node->uniquename)) {
  685. $properties[$citation_id][0] = $node->uniquename;
  686. }
  687. }
  688. }
  689. // iterate through all of the properties and remove those that really are
  690. // part of the pub table fields
  691. $volume = '';
  692. $volumetitle = '';
  693. $issue = '';
  694. $pages = '';
  695. $publisher = '';
  696. $series_name = '';
  697. $pubplace = '';
  698. $miniref = '';
  699. $cross_refs = array();
  700. foreach ($properties as $type_id => $element) {
  701. foreach ($element as $index => $value) {
  702. $name = $properties_list[$type_id];
  703. // populate our $pub_array for building a citation
  704. $pub_arr[$name] = $value;
  705. // remove properties that are stored in the pub table
  706. if ($name == "Volume") {
  707. $volume = $value;
  708. unset($properties[$type_id]);
  709. }
  710. elseif ($name == "Volume Title") {
  711. $volumetitle = $value;
  712. unset($properties[$type_id]);
  713. }
  714. elseif ($name == "Issue") {
  715. $issue = $value;
  716. unset($properties[$type_id]);
  717. }
  718. elseif ($name == "Pages") {
  719. $pages = $value;
  720. unset($properties[$type_id]);
  721. }
  722. elseif ($name == "Publisher") {
  723. $publisher = $value;
  724. unset($properties[$type_id]);
  725. }
  726. elseif ($name == "Journal Name" or $name == "Conference Name") {
  727. $series_name = $value;
  728. unset($properties[$type_id]);
  729. }
  730. elseif ($name == "Journal Country" or $name == "Published Location") {
  731. $pubplace = $value;
  732. // allow this property to go into the pubprop table so we don't loose info
  733. // so don't unset it. But it will also go into the pub.pubplace field
  734. }
  735. elseif ($name == "Publication Dbxref") {
  736. // we will add the cross-references to the pub_dbxref table
  737. // but we also want to keep the property in the pubprop table so don't unset it
  738. $cross_refs[] = $value;
  739. }
  740. }
  741. }
  742. // generate a citation for this pub if one doesn't already exist
  743. if (!$node->uniquename) {
  744. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  745. $pub_arr['Title'] = $node->pubtitle;
  746. $pub_arr['Publication Type'][0] = $pub_type->name;
  747. $pub_arr['Year'] = $node->pyear;
  748. $node->uniquename = tripal_pub_create_citation($pub_arr);
  749. $properties[$citation_id][0] = $node->uniquename;
  750. }
  751. // update the pub record
  752. $match = array(
  753. 'pub_id' => $pub_id,
  754. );
  755. $values = array(
  756. 'title' => $node->pubtitle,
  757. 'type_id' => $node->type_id,
  758. 'pyear' => $node->pyear,
  759. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  760. 'uniquename' => $node->uniquename,
  761. 'series_name' => substr($series_name, 0, 255),
  762. 'volumetitle' => $volumetitle,
  763. 'volume' => $volume,
  764. 'issue' => $issue,
  765. 'pages' => $pages,
  766. 'miniref' => substr($miniref, 0, 255),
  767. 'publisher' => substr($publisher, 0, 255),
  768. 'pubplace' => substr($pubplace, 0, 255),
  769. );
  770. $status = tripal_core_chado_update('pub', $match, $values);
  771. if (!$status) {
  772. drupal_set_message("Error updating publication", "error");
  773. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  774. return;
  775. }
  776. // now add in the properties by first removing any the publication
  777. // already has and adding the ones we have
  778. $details = array(
  779. 'property_table' => 'pubprop',
  780. 'base_table' => 'pub',
  781. 'foreignkey_name' => 'pub_id',
  782. 'foreignkey_value' => $pub_id
  783. );
  784. chado_node_properties_form_update_properties($node, $details, $properties);
  785. // add in any database cross-references after first removing
  786. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  787. foreach ($cross_refs as $index => $ref) {
  788. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  789. if (!$pub_dbxref) {
  790. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  791. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  792. array('%ref' => $ref), WATCHDOG_ERROR);
  793. }
  794. }
  795. }
  796. /**
  797. * Implementation of tripal_pub_load().
  798. *
  799. *
  800. * @param $node
  801. * The node that is to be accessed from the database
  802. *
  803. * @return $node
  804. * The node with the information to be loaded into the database
  805. *
  806. */
  807. function chado_pub_load($nodes) {
  808. foreach ($nodes as $nid => $node) {
  809. // find the pub and add in the details
  810. $pub_id = chado_get_id_for_node('pub', $nid);
  811. // get the pub
  812. $values = array('pub_id' => $pub_id);
  813. $pub = tripal_core_generate_chado_var('pub', $values);
  814. // expand the 'text' fields as those aren't included by default
  815. // and they really shouldn't be so large to cause problems
  816. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  817. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  818. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  819. // set the URL path
  820. $nodes[$nid]->path = "pub/$pub_id";
  821. $nodes[$nid]->pub = $pub;
  822. }
  823. }
  824. /**
  825. * Implementation of tripal_pub_delete().
  826. *
  827. * This function takes a node and if the delete button has been chosen by the user, the publication
  828. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  829. * the 'chado_pub' table.
  830. *
  831. * @parm $node
  832. * Then node which contains the information stored within the node-ID
  833. *
  834. */
  835. function chado_pub_delete(&$node) {
  836. $pub_id = chado_get_id_for_node('pub', $node->nid);
  837. // if we don't have a pub id for this node then this isn't a node of
  838. // type chado_pub or the entry in the chado_pub table was lost.
  839. if (!$pub_id) {
  840. return;
  841. }
  842. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  843. // drupal database
  844. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  845. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  846. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  847. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  848. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  849. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  850. // Remove data from pub and pubprop tables of chado database as well
  851. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  852. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  853. }
  854. /**
  855. *
  856. * @ingroup tripal_feature
  857. */
  858. function tripal_pub_node_view($node, $view_mode, $langcode) {
  859. switch ($node->type) {
  860. case 'chado_pub':
  861. // Show feature browser and counts
  862. if ($view_mode == 'full') {
  863. $node->content['tripal_pub_authors'] = array(
  864. '#value' => theme('tripal_pub_authors', array('node' => $node)),
  865. );
  866. $node->content['tripal_pub_base'] = array(
  867. '#value' => theme('tripal_pub_base', array('node' => $node)),
  868. );
  869. $node->content['tripal_pub_featuremaps'] = array(
  870. '#value' => theme('tripal_pub_featuremaps', array('node' => $node)),
  871. );
  872. $node->content['tripal_pub_features'] = array(
  873. '#value' => theme('tripal_pub_features', array('node' => $node)),
  874. );
  875. $node->content['tripal_pub_libraries'] = array(
  876. '#value' => theme('tripal_pub_libraries', array('node' => $node)),
  877. );
  878. $node->content['tripal_pub_projects'] = array(
  879. '#value' => theme('tripal_pub_projects', array('node' => $node)),
  880. );
  881. $node->content['tripal_pub_properties'] = array(
  882. '#value' => theme('tripal_pub_properties', array('node' => $node)),
  883. );
  884. $node->content['tripal_pub_references'] = array(
  885. '#value' => theme('tripal_pub_references', array('node' => $node)),
  886. );
  887. $node->content['tripal_pub_relationships'] = array(
  888. '#value' => theme('tripal_pub_relationships', array('node' => $node)),
  889. );
  890. $node->content['tripal_pub_stocks'] = array(
  891. '#value' => theme('tripal_pub_stocks', array('node' => $node)),
  892. );
  893. }
  894. if ($view_mode == 'teaser') {
  895. $node->content['tripal_pub_teaser'] = array(
  896. '#value' => theme('tripal_pub_teaser', array('node' => $node)),
  897. );
  898. }
  899. break;
  900. }
  901. }
  902. /**
  903. *
  904. * @param $node
  905. */
  906. function tripal_pub_node_insert($node) {
  907. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  908. // where [pub id] is the Chado publication ID. This will allow for easy linking
  909. // into the publication without needing to know the node. Of course if you know the
  910. // node that will still work too (e.g. http://[base url]/node/[node id]
  911. // so the nodeapi function ensures that the URL path is set after insert or update
  912. // of the node and when the node is loaded if it hasn't yet been set.
  913. if ($node->type == 'chado_pub') {
  914. $pub_id = chado_get_id_for_node('pub', $node->nid);
  915. tripal_pub_set_pub_url($node, $pub_id);
  916. }
  917. }
  918. /**
  919. *
  920. * @param $node
  921. * @param $types
  922. */
  923. function tripal_pub_node_load($nodes, $types) {
  924. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  925. // where [pub id] is the Chado publication ID. This will allow for easy linking
  926. // into the publication without needing to know the node. Of course if you know the
  927. // node that will still work too (e.g. http://[base url]/node/[node id]
  928. // so the nodeapi function ensures that the URL path is set after insert or update
  929. // of the node and when the node is loaded if it hasn't yet been set.
  930. if (count(array_intersect(array('chado_pub'), $types))) {
  931. foreach ($nodes as $nid => $node) {
  932. if ($node->type == 'chado_pub' and !property_exists($node, 'path')) {
  933. $pub_id = chado_get_id_for_node('pub', $node->nid);
  934. $path = tripal_pub_set_pub_url($node, $pub_id);
  935. }
  936. }
  937. }
  938. }
  939. /**
  940. *
  941. * @param $node
  942. */
  943. function tripal_pub_node_update($node) {
  944. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  945. // where [pub id] is the Chado publication ID. This will allow for easy linking
  946. // into the publication without needing to know the node. Of course if you know the
  947. // node that will still work too (e.g. http://[base url]/node/[node id]
  948. // so the nodeapi function ensures that the URL path is set after insert or update
  949. // of the node and when the node is loaded if it hasn't yet been set.
  950. if ($node->type == 'chado_pub') {
  951. $pub_id = chado_get_id_for_node('pub', $node->nid);
  952. tripal_pub_set_pub_url($node, $pub_id);
  953. }
  954. }