pub_form.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. }