tripal_pub.chado_node.inc 41 KB

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