tripal_pub.chado_node.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. <?php
  2. /**
  3. * This is the chado_pub node form callback. The arguments
  4. * are out of order from a typical form because it's a defined callback
  5. */
  6. function chado_pub_form($node, $form_state) {
  7. $form = array();
  8. // Default values can come in the following ways:
  9. //
  10. // 1) as elements of the $node object. This occurs when editing an existing pub
  11. // 2) in the $form_state['values'] array which occurs on a failed validation or
  12. // ajax callbacks from non submit form elements
  13. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  14. // form elements and the form is being rebuilt
  15. //
  16. // set form field defaults
  17. $pub_id = null;
  18. $title = '';
  19. $pyear = '';
  20. $uniquename = '';
  21. $type_id = '';
  22. $is_obsolete = '';
  23. // some of the fields in the pub table should show up in the properties
  24. // form elements to make the form more seemless. We will add them
  25. // to this array.
  26. $more_props = array();
  27. // if we are editing an existing node then the pub is already part of the node
  28. if (property_exists($node, 'pub')) {
  29. $pub = $node->pub;
  30. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  31. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  32. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  33. $pub_id = $pub->pub_id;
  34. $title = $pub->title;
  35. $pyear = $pub->pyear;
  36. $uniquename = $pub->uniquename;
  37. $type_id = $pub->type_id->cvterm_id;
  38. $is_obsolete = $pub->is_obsolete;
  39. // if the obsolete value is set by the database then it is in the form of
  40. // 't' or 'f', we need to convert to 1 or 0
  41. $is_obsolete = $is_obsolete == 't' ? 1 : $is_obsolete;
  42. $is_obsolete = $is_obsolete == 'f' ? 0 : $is_obsolete;
  43. // set the organism_id in the form
  44. $form['pub_id'] = array(
  45. '#type' => 'value',
  46. '#value' => $pub->pub_id,
  47. );
  48. // get fields from the pub table and convert them to properties. We will add these to the $more_props
  49. // array which gets passed in to the tripal_core_properties_form() API call further down
  50. if ($pub->volumetitle) {
  51. $cvterm = tripal_cv_get_cvterm_by_name('Volume Title', NULL, 'tripal_pub');
  52. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volumetitle);
  53. }
  54. if ($pub->volume) {
  55. $cvterm = tripal_cv_get_cvterm_by_name('Volume', NULL, 'tripal_pub');
  56. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->volume);
  57. }
  58. if ($pub->series_name) {
  59. switch ($pub->type_id->name) {
  60. case 'Journal Article':
  61. $cvterm = tripal_cv_get_cvterm_by_name('Journal Name', NULL, 'tripal_pub');
  62. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  63. break;
  64. case 'Conference Proceedings':
  65. $cvterm = tripal_cv_get_cvterm_by_name('Conference Name', NULL, 'tripal_pub');
  66. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  67. break;
  68. default:
  69. $cvterm = tripal_cv_get_cvterm_by_name('Series Name', NULL, 'tripal_pub');
  70. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->series_name);
  71. }
  72. }
  73. if ($pub->issue) {
  74. $cvterm = tripal_cv_get_cvterm_by_name('Issue', NULL, 'tripal_pub');
  75. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->issue);
  76. }
  77. if ($pub->pages) {
  78. $cvterm = tripal_cv_get_cvterm_by_name('Pages', NULL, 'tripal_pub');
  79. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  80. }
  81. if ($pub->miniref) {
  82. // not sure what to do with this one
  83. }
  84. if ($pub->publisher) {
  85. $cvterm = tripal_cv_get_cvterm_by_name('Publisher', NULL, 'tripal_pub');
  86. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->publisher);
  87. }
  88. if ($pub->pubplace) {
  89. $cvterm = tripal_cv_get_cvterm_by_name('Published Location', NULL, 'tripal_pub');
  90. $more_props[] = array('cvterm' => $cvterm, 'value' => $pub->pages);
  91. }
  92. }
  93. // if we are re constructing the form from a failed validation or ajax callback
  94. // then use the $form_state['values'] values
  95. if (array_key_exists('values', $form_state)) {
  96. $title = $form_state['values']['pubtitle'];
  97. $pyear = $form_state['values']['pyear'];
  98. $uniquename = $form_state['values']['uniquename'];
  99. $type_id = $form_state['values']['type_id'];
  100. $is_obsolete = $form_state['values']['is_obsolete'];
  101. }
  102. // if we are re building the form from after submission (from ajax call) then
  103. // the values are in the $form_state['input'] array
  104. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  105. $title = $form_state['input']['pubtitle'];
  106. $uniquename = $form_state['input']['uniquename'];
  107. $type_id = $form_state['input']['type_id'];
  108. $is_obsolete = $form_state['input']['is_obsolete'];
  109. }
  110. // a drupal title can only be 255 characters, but the Chado title can be much longer.
  111. // we use the publication title as the drupal title, but we'll need to truncate it.
  112. $form['title'] = array(
  113. '#type' => 'hidden',
  114. '#value' => substr($title, 0, 255),
  115. );
  116. $form['pubtitle'] = array(
  117. '#type' => 'textarea',
  118. '#title' => t('Publication Title'),
  119. '#default_value' => $title,
  120. '#required' => TRUE,
  121. );
  122. // get the list of publication types. In the Tripal publication
  123. // ontologies these are all grouped under the term 'Publication Type'
  124. // we want the default to be 'Journal Article'
  125. $sql = "
  126. SELECT
  127. CVTS.cvterm_id, CVTS.name
  128. FROM {cvtermpath} CVTP
  129. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  130. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  131. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  132. WHERE
  133. CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
  134. NOT CVTS.is_obsolete = 1
  135. ORDER BY CVTS.name ASC
  136. ";
  137. $results = chado_query($sql);
  138. $pub_types = array();
  139. while ($pub_type = $results->fetchObject()) {
  140. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  141. // if we don't have a default type then set the default to be 'Journal Article'
  142. if (strcmp($pub_type->name,"Journal Article") == 0 and !$type_id) {
  143. $type_id = $pub_type->cvterm_id;
  144. }
  145. }
  146. $form['type_id'] = array(
  147. '#type' => 'select',
  148. '#title' => t('Publication Type'),
  149. '#options' => $pub_types,
  150. '#required' => TRUE,
  151. '#default_value' => $type_id,
  152. );
  153. $form['pyear'] = array(
  154. '#type' => 'textfield',
  155. '#title' => t('Publication Year'),
  156. '#default_value' => $pyear,
  157. '#required' => TRUE,
  158. '#size' => 5,
  159. '#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.'),
  160. );
  161. $form['uniquename'] = array(
  162. '#type' => 'textarea',
  163. '#title' => t('Citation'),
  164. '#default_value' => $uniquename,
  165. '#description' => t('All publications must have a unique citation.
  166. <b>Please enter the full citation for this publication or leave blank and one will be generated
  167. automatically if possible</b>. For PubMed style citations list
  168. the last name of the author followed by initials. Each author should be separated by a comma. Next comes
  169. 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.
  170. 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
  171. suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
  172. );
  173. $form['is_obsolete'] = array(
  174. '#type' => 'checkbox',
  175. '#title' => t('Is Obsolete? (Check for Yes)'),
  176. '#default_value' => $is_obsolete,
  177. );
  178. // get publication properties list
  179. $properties_select = array();
  180. $properties_select[] = 'Select a Property';
  181. $sql = "
  182. SELECT
  183. DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  184. FROM {cvtermpath} CVTP
  185. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  186. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  187. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  188. WHERE CV.name = 'tripal_pub' and
  189. (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
  190. NOT CVTS.is_obsolete = 1
  191. ORDER BY CVTS.name ASC
  192. ";
  193. $prop_types = chado_query($sql);
  194. while ($prop = $prop_types->fetchObject()) {
  195. // add all properties except the Citation. That property is set via the uniquename field
  196. if ($prop->name != 'Citation') {
  197. $properties[$prop->cvterm_id] = $prop->name;
  198. }
  199. }
  200. // add in the properties fields. The 'Citation' term is special because it serves
  201. // both as a property and as the uniquename for the publiation table so we exclude it
  202. // as it shouldn't be selected as a property
  203. $exclude = array("Citation");
  204. $instructions = '';
  205. tripal_core_properties_form($form, $form_state, 'pubprop', 'pub_id', 'tripal_pub',
  206. $properties, $pub_id, $exclude, $more_props, $instructions, 'Properties');
  207. return $form;
  208. }
  209. /*
  210. *
  211. */
  212. function chado_pub_validate($node, $form, &$form_state) {
  213. // get the submitted values
  214. $title = trim($node->pubtitle);
  215. $pyear = trim($node->pyear);
  216. $uniquename = trim($node->uniquename);
  217. $is_obsolete = $node->is_obsolete;
  218. $type_id = $node->type_id;
  219. // if this is a delete then don't validate
  220. if($node->op == 'Delete') {
  221. return;
  222. }
  223. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  224. // need to validate during syncing so just skip it.
  225. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  226. return;
  227. }
  228. $pub = array();
  229. // make sure the year is four digits
  230. if(!preg_match('/^\d{4}$/', $pyear)){
  231. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  232. return;
  233. }
  234. // get the type of publication
  235. $values = array('cvterm_id' => $type_id);
  236. $options = array('statement_name' => 'sel_pub_ty');
  237. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
  238. if (count($cvterm) == 0) {
  239. $message = t('Invalid publication type.');
  240. form_set_error('type_id', $message);
  241. return;
  242. }
  243. // get the media name looking at the properties
  244. $series_name = '';
  245. foreach ($node as $element => $value) {
  246. // if this is an existing property (either previously in the database or
  247. // added via AHAH/AJAX callback)
  248. if (preg_match('/^prop_value-(\d+)-(\d+)$/', $element, $matches)) {
  249. $prop_type_id = $matches[1];
  250. $prop_type = tripal_cv_get_cvterm_by_id($prop_type_id);
  251. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  252. $series_name = $value;
  253. }
  254. if($prop_type->name == 'Citation') {
  255. $uniquename = $value;
  256. }
  257. $pub[$prop_type->name] = $value;
  258. }
  259. // if this is a new property (added by this submit of the form)
  260. elseif ($element == 'new_id') {
  261. $prop_type = tripal_cv_get_cvterm_by_id($value);
  262. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  263. $series_name = $node->new_value;
  264. }
  265. if($prop_type->name == 'Citation') {
  266. $uniquename = $node->new_value;
  267. }
  268. $pub[$prop_type->name] = $node->new_value;
  269. }
  270. }
  271. // if the citation is missing then try to generate one
  272. if (!$uniquename) {
  273. $pub['Title'] = $title;
  274. $pub['Publication Type'][0] = $cvterm[0]->name;
  275. $pub['Year'] = $pyear;
  276. $uniquename = tripal_pub_create_citation($pub);
  277. if (!$uniquename) {
  278. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  279. }
  280. }
  281. $skip_duplicate_check = 0;
  282. // if this publication is a Patent then skip the validation below. Patents can have the title
  283. // name and year but be different
  284. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  285. $skip_duplicate_check = 1;
  286. }
  287. // Validating for an update
  288. if (!is_null($node->nid)) {
  289. $pub_id = $node->pub_id;
  290. // first get the original title, type and year before it was changed
  291. $values = array('pub_id' => $pub_id);
  292. $columns = array('title', 'pyear', 'type_id', 'series_name');
  293. $options = array('statement_name' => 'sel_pub_id');
  294. $pub = tripal_core_chado_select('pub', $columns, $values, $options);
  295. // if the title, type, year or series_name have changed then check the pub
  296. // to see if it is a duplicate of another
  297. if((strcmp(strtolower($pub[0]->title), strtolower($title)) == 0) and
  298. (strcmp(strtolower($pub[0]->series_name), strtolower($series_name)) == 0) and
  299. ($pub[0]->type_id == $type_id) and
  300. ($pub[0]->pyear == $pyear)) {
  301. $skip_duplicate_check = 1;
  302. }
  303. // check to see if a duplicate publication already exists
  304. if (!$skip_duplicate_check) {
  305. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id);
  306. }
  307. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  308. }
  309. // Validating for an insert
  310. else {
  311. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm);
  312. chado_pub_validate_check_uniquename($uniquename);
  313. }
  314. }
  315. /**
  316. *
  317. * @param unknown $uniquename
  318. */
  319. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  320. $results = tripal_pub_get_pub_by_uniquename($uniquename);
  321. // make sure we don't capture our pub_id in the list (remove it)
  322. foreach ($results as $index => $found_pub_id) {
  323. if($found_pub_id == $pub_id){
  324. unset($results[$index]);
  325. }
  326. }
  327. if (count($results) > 0) {
  328. $message = t('A publication with this unique citation already exists.');
  329. form_set_error('uniquename', $message);
  330. }
  331. }
  332. /**
  333. *
  334. */
  335. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  336. // make sure the publication is unique using the prefereed import duplication check
  337. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  338. switch ($import_dups_check) {
  339. case 'title_year':
  340. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, NULL);
  341. // make sure we don't capture our pub_id in the list (remove it)
  342. foreach ($results as $index => $found_pub_id) {
  343. if($found_pub_id == $pub_id){
  344. unset($results[$index]);
  345. }
  346. }
  347. if (count($results) > 0) {
  348. $message = t('A publication with this title and publication year, already exists.');
  349. form_set_error('pyear', $message);
  350. }
  351. break;
  352. case 'title_year_type':
  353. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, $cvterm[0]->name, $pyear, NULL);
  354. // make sure we don't capture our pub_id in the list (remove it)
  355. foreach ($results as $index => $found_pub_id) {
  356. if($found_pub_id == $pub_id){
  357. unset($results[$index]);
  358. }
  359. }
  360. if (count($results) > 0) {
  361. $message = t('A publication with this title, type and publication year, already exists.');
  362. form_set_error('pyear', $message);
  363. }
  364. break;
  365. case 'title_year_media':
  366. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, $series_name);
  367. // make sure we don't capture our pub_id in the list (remove it)
  368. foreach ($results as $index => $found_pub_id) {
  369. if($found_pub_id == $pub_id){
  370. unset($results[$index]);
  371. }
  372. }
  373. if (count($results) > 0) {
  374. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year, already exists.');
  375. form_set_error('pyear', $message);
  376. }
  377. break;
  378. }
  379. }
  380. /**
  381. * Implement hook_access().
  382. *
  383. * This hook allows node modules to limit access to the node types they define.
  384. *
  385. * @param $node
  386. * The node on which the operation is to be performed, or, if it does not yet exist, the
  387. * type of node to be created
  388. *
  389. * @param $op
  390. * The operation to be performed
  391. *
  392. * @param $account
  393. * A user object representing the user for whom the operation is to be performed
  394. *
  395. * @return
  396. * If the permission for the specified operation is not set then return FALSE. If the
  397. * permission is set then return NULL as this allows other modules to disable
  398. * access. The only exception is when the $op == 'create'. We will always
  399. * return TRUE if the permission is set.
  400. *
  401. */
  402. function chado_pub_node_access($node, $op, $account ) {
  403. if ($op == 'create') {
  404. if (!user_access('create chado_pub content', $account)) {
  405. return FALSE;
  406. }
  407. return TRUE;
  408. }
  409. if ($op == 'update') {
  410. if (!user_access('edit chado_pub content', $account)) {
  411. return FALSE;
  412. }
  413. }
  414. if ($op == 'delete') {
  415. if (!user_access('delete chado_pub content', $account)) {
  416. return FALSE;
  417. }
  418. }
  419. if ($op == 'view') {
  420. if (!user_access('access chado_pub content', $account)) {
  421. return FALSE;
  422. }
  423. }
  424. return NULL;
  425. }
  426. /**
  427. * Implementation of tripal_pub_insert().
  428. *
  429. * This function inserts user entered information pertaining to the Publication instance into the
  430. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  431. *
  432. * @parm $node
  433. * Then node which contains the information stored within the node-ID
  434. *
  435. *
  436. */
  437. function chado_pub_insert($node) {
  438. $title = trim($node->pubtitle);
  439. $pyear = trim($node->pyear);
  440. $uniquename = trim($node->uniquename);
  441. $is_obsolete = $node->is_obsolete;
  442. $type_id = $node->type_id;
  443. // we need an array suitable for the tripal_pub_create_citation() function
  444. // to automatically generate a citation if a uniquename doesn't already exist
  445. $pub_arr = array();
  446. // if there is an pub_id in the $node object then this must be a sync so
  447. // we can skip adding the pub as it is already there, although
  448. // we do need to proceed with the rest of the insert
  449. if (!property_exists($node, 'pub_id')) {
  450. $properties = array(); // stores all of the properties we need to add
  451. $cross_refs = array(); // stores any cross references for this publication
  452. // get the properties from the form
  453. $properties = tripal_core_properties_form_retreive($node, 'tripal_pub');
  454. // get the list of properties for easy lookup (without doing lots of database queries
  455. $properties_list = array();
  456. $sql = "
  457. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  458. FROM {cvtermpath} CVTP
  459. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  460. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  461. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  462. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  463. NOT CVTS.is_obsolete = 1
  464. ORDER BY CVTS.name ASC
  465. ";
  466. $prop_types = chado_query($sql);
  467. while ($prop = $prop_types->fetchObject()) {
  468. $properties_list[$prop->cvterm_id] = $prop->name;
  469. // The 'Citation' term is special because it serves
  470. // both as a property and as the uniquename for the
  471. // pub and we want it stored in both the pub table and the pubprop table
  472. if ($prop->name == 'Citation') {
  473. $properties[$prop->name][0] = $node->uniquename;
  474. }
  475. }
  476. // iterate through all of the properties and remove those that really are
  477. // part of the pub table fields
  478. $volume = '';
  479. $volumetitle = '';
  480. $issue = '';
  481. $pages = '';
  482. $publisher = '';
  483. $series_name = '';
  484. $pubplace = '';
  485. $miniref = '';
  486. $cross_refs = array();
  487. foreach ($properties as $name => $element) {
  488. $value = trim($element[0]);
  489. // populate our $pub_array for building a citation
  490. $pub_arr[$name] = $value;
  491. // remove properties that are stored in the pub table
  492. if ($name == "Volume") {
  493. $volume = $value;
  494. unset($properties[$name]);
  495. }
  496. elseif ($name == "Volume Title") {
  497. $volumetitle = $value;
  498. unset($properties[$name]);
  499. }
  500. elseif ($name == "Issue") {
  501. $issue = $value;
  502. unset($properties[$name]);
  503. }
  504. elseif ($name == "Pages") {
  505. $pages = $value;
  506. unset($properties[$name]);
  507. }
  508. elseif ($name == "Publisher") {
  509. $publisher = $value;
  510. unset($properties[$name]);
  511. }
  512. elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
  513. $series_name = $value;
  514. unset($properties[$name]);
  515. }
  516. elseif ($name == "Journal Country" or $name == "Published Location") {
  517. $pubplace = $value;
  518. // allow this property to go into the pubprop table so we don't loose info
  519. // so don't unset it. But it will also go into the pub.pubplace field
  520. }
  521. elseif ($name == "Publication Dbxref") {
  522. // we will add the cross-references to the pub_dbxref table
  523. // but we also want to keep the property in the pubprop table so don't unset it
  524. $cross_refs[] = $value;
  525. }
  526. }
  527. // generate a citation for this pub if one doesn't already exist
  528. if (!$node->uniquename and array_key_exists('Citation', $properties)) {
  529. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  530. $pub_arr['Title'] = $node->pubtitle;
  531. $pub_arr['Publication Type'][0] = $pub_type->name;
  532. $pub_arr['Year'] = $node->pyear;
  533. $node->uniquename = tripal_pub_create_citation($pub_arr);
  534. $properties['Citation'][0] = $node->uniquename;
  535. }
  536. // insert the pub record
  537. $values = array(
  538. 'title' => $node->pubtitle,
  539. 'series_name' => substr($series_name, 0, 255),
  540. 'type_id' => $node->type_id,
  541. 'pyear' => $node->pyear,
  542. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  543. 'uniquename' => $node->uniquename,
  544. 'volumetitle' => $volumetitle,
  545. 'volume' => $volume,
  546. 'issue' => $issue,
  547. 'pages' => $pages,
  548. 'miniref' => substr($miniref, 0, 255),
  549. 'publisher' => substr($publisher, 0, 255),
  550. 'pubplace' => substr($pubplace, 0, 255),
  551. );
  552. $pub = tripal_core_chado_insert('pub', $values);
  553. if (!$pub) {
  554. drupal_set_message("Error inserting publication", "error");
  555. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  556. return;
  557. }
  558. $pub_id = $pub['pub_id'];
  559. // now add in the properties
  560. foreach ($properties as $property => $elements) {
  561. foreach ($elements as $rank => $value) {
  562. $status = tripal_pub_insert_property($pub['pub_id'], $property, $value, FALSE);
  563. if (!$status) {
  564. drupal_set_message("Error cannot add property: $property", "error");
  565. watchdog('tripal_pub', "Error cannot add property: %prop",
  566. array('%property' => $property), WATCHDOG_ERROR);
  567. }
  568. }
  569. }
  570. // add in any database cross-references
  571. foreach ($cross_refs as $index => $ref) {
  572. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  573. if (!$pub_dbxref) {
  574. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  575. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  576. array('%ref' => $ref), WATCHDOG_ERROR);
  577. }
  578. }
  579. }
  580. else {
  581. $pub_id = $node->pub_id;
  582. }
  583. // Make sure the entry for this pub doesn't already exist in the
  584. // chado_pub table if it doesn't exist then we want to add it.
  585. $check_org_id = chado_get_id_for_node('pub', $node->nid);
  586. if (!$check_org_id) {
  587. $record = new stdClass();
  588. $record->nid = $node->nid;
  589. $record->vid = $node->vid;
  590. $record->pub_id = $pub_id;
  591. drupal_write_record('chado_pub', $record);
  592. }
  593. }
  594. /*
  595. *
  596. * Implements hook_update
  597. *
  598. * The purpose of the function is to allow the module to take action when an edited node is being
  599. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  600. * As well, the database will be changed, so the user changed information will be saved to the database.
  601. *
  602. * @param $node
  603. * The node being updated
  604. *
  605. * @ingroup tripal_pub
  606. */
  607. function chado_pub_update($node) {
  608. $title = trim($node->pubtitle);
  609. $pyear = trim($node->pyear);
  610. $uniquename = trim($node->uniquename);
  611. $is_obsolete = $node->is_obsolete;
  612. $type_id = $node->type_id;
  613. // we need an array suitable for the tripal_pub_create_citation() function
  614. // to automatically generate a citation if a uniquename doesn't already exist
  615. $pub_arr = array();
  616. // get the publication ID for this publication
  617. $pub_id = chado_get_id_for_node('pub', $node->nid) ;
  618. $properties = array(); // stores all of the properties we need to add
  619. $cross_refs = array(); // stores any cross references for this publication
  620. // get the properties from the form
  621. $properties = tripal_core_properties_form_retreive($node, 'tripal_pub');
  622. // get the list of properties for easy lookup (without doing lots of database queries
  623. $properties_list = array();
  624. $sql = "
  625. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  626. FROM {cvtermpath} CVTP
  627. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  628. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  629. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  630. WHERE CV.name = 'tripal_pub' and
  631. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  632. NOT CVTS.is_obsolete = 1
  633. ORDER BY CVTS.name ASC
  634. ";
  635. $prop_types = chado_query($sql);
  636. while ($prop = $prop_types->fetchObject()) {
  637. $properties_list[$prop->cvterm_id] = $prop->name;
  638. // The 'Citation' term is special because it serves
  639. // both as a property and as the uniquename for the
  640. // pub and we want it stored in both the pub table and the pubprop table
  641. if ($prop->name == 'Citation') {
  642. $properties[$prop->name][0] = $node->uniquename;
  643. }
  644. }
  645. // iterate through all of the properties and remove those that really are
  646. // part of the pub table fields
  647. $volume = '';
  648. $volumetitle = '';
  649. $issue = '';
  650. $pages = '';
  651. $publisher = '';
  652. $series_name = '';
  653. $pubplace = '';
  654. $miniref = '';
  655. $cross_refs = array();
  656. foreach ($properties as $name => $element) {
  657. foreach ($element as $index => $value) {
  658. // populate our $pub_array for building a citation
  659. $pub_arr[$name] = $value;
  660. // remove properties that are stored in the pub table
  661. if ($name == "Volume") {
  662. $volume = $value;
  663. unset($properties[$name]);
  664. }
  665. elseif ($name == "Volume Title") {
  666. $volumetitle = $value;
  667. unset($properties[$name]);
  668. }
  669. elseif ($name == "Issue") {
  670. $issue = $value;
  671. unset($properties[$name]);
  672. }
  673. elseif ($name == "Pages") {
  674. $pages = $value;
  675. unset($properties[$name]);
  676. }
  677. elseif ($name == "Publisher") {
  678. $publisher = $value;
  679. unset($properties[$name]);
  680. }
  681. elseif ($name == "Journal Name" or $name == "Conference Name") {
  682. $series_name = $value;
  683. unset($properties[$name]);
  684. }
  685. elseif ($name == "Journal Country" or $name == "Published Location") {
  686. $pubplace = $value;
  687. // allow this property to go into the pubprop table so we don't loose info
  688. // so don't unset it. But it will also go into the pub.pubplace field
  689. }
  690. elseif ($name == "Publication Dbxref") {
  691. // we will add the cross-references to the pub_dbxref table
  692. // but we also want to keep the property in the pubprop table so don't unset it
  693. $cross_refs[] = $value;
  694. }
  695. }
  696. }
  697. // generate a citation for this pub if one doesn't already exist
  698. if (!$node->uniquename) {
  699. $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
  700. $pub_arr['Title'] = $node->pubtitle;
  701. $pub_arr['Publication Type'][0] = $pub_type->name;
  702. $pub_arr['Year'] = $node->pyear;
  703. $node->uniquename = tripal_pub_create_citation($pub_arr);
  704. $properties['Citation'][0] = $node->uniquename;
  705. }
  706. // update the pub record
  707. $match = array(
  708. 'pub_id' => $pub_id,
  709. );
  710. $values = array(
  711. 'title' => $node->pubtitle,
  712. 'type_id' => $node->type_id,
  713. 'pyear' => $node->pyear,
  714. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  715. 'uniquename' => $node->uniquename,
  716. 'series_name' => substr($series_name, 0, 255),
  717. 'volumetitle' => $volumetitle,
  718. 'volume' => $volume,
  719. 'issue' => $issue,
  720. 'pages' => $pages,
  721. 'miniref' => substr($miniref, 0, 255),
  722. 'publisher' => substr($publisher, 0, 255),
  723. 'pubplace' => substr($pubplace, 0, 255),
  724. );
  725. $status = tripal_core_chado_update('pub', $match, $values);
  726. if (!$status) {
  727. drupal_set_message("Error updating publication", "error");
  728. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  729. return;
  730. }
  731. // now add in the properties by first removing any the publication
  732. // already has and adding the ones we have
  733. tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  734. foreach ($properties as $property => $elements) {
  735. foreach ($elements as $rank => $value) {
  736. $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
  737. if (!$status) {
  738. drupal_set_message("Error cannot add property: '$property'", "error");
  739. watchdog('tripal_pub', "Error cannot add property: '%prop'",
  740. array('%prop' => $property), WATCHDOG_ERROR);
  741. }
  742. }
  743. }
  744. // add in any database cross-references after first removing
  745. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  746. foreach ($cross_refs as $index => $ref) {
  747. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  748. if (!$pub_dbxref) {
  749. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  750. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  751. array('%ref' => $ref), WATCHDOG_ERROR);
  752. }
  753. }
  754. }
  755. /**
  756. * Implementation of tripal_pub_load().
  757. *
  758. *
  759. * @param $node
  760. * The node that is to be accessed from the database
  761. *
  762. * @return $node
  763. * The node with the information to be loaded into the database
  764. *
  765. */
  766. function chado_pub_load($nodes) {
  767. foreach ($nodes as $nid => $node) {
  768. // find the pub and add in the details
  769. $pub_id = chado_get_id_for_node('pub', $nid);
  770. // get the pub
  771. $values = array('pub_id' => $pub_id);
  772. $pub = tripal_core_generate_chado_var('pub', $values);
  773. // expand the 'text' fields as those aren't included by default
  774. // and they really shouldn't be so large to cause problems
  775. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  776. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  777. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  778. // set the URL path
  779. $nodes[$nid]->path = "pub/$pub_id";
  780. $nodes[$nid]->pub = $pub;
  781. }
  782. }
  783. /**
  784. * Implementation of tripal_pub_delete().
  785. *
  786. * This function takes a node and if the delete button has been chosen by the user, the publication
  787. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  788. * the 'chado_pub' table.
  789. *
  790. * @parm $node
  791. * Then node which contains the information stored within the node-ID
  792. *
  793. */
  794. function chado_pub_delete(&$node) {
  795. $pub_id = chado_get_id_for_node('pub', $node->nid);
  796. // if we don't have a pub id for this node then this isn't a node of
  797. // type chado_pub or the entry in the chado_pub table was lost.
  798. if (!$pub_id) {
  799. return;
  800. }
  801. // Remove data from {chado_pub}, {node} and {node_revision} tables of
  802. // drupal database
  803. $sql_del = "DELETE FROM {chado_pub} WHERE nid = :nid AND vid = :vid";
  804. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  805. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  806. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  807. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  808. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  809. // Remove data from pub and pubprop tables of chado database as well
  810. chado_query("DELETE FROM {pubprop} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  811. chado_query("DELETE FROM {pub} WHERE pub_id = :pub_id", array(':pub_id' => $pub_id));
  812. }