pub_form.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. $volumetitle = '';
  20. $volume = '';
  21. $series_name = '';
  22. $issue = '';
  23. $pyear = '';
  24. $pages = '';
  25. $miniref = '';
  26. $uniquename = '';
  27. $type_id = '';
  28. $is_obsolete = '';
  29. $publisher = '';
  30. $pubplace = '';
  31. // if we are editing an existing node then the pub is already part of the node
  32. if (property_exists($node, 'pub')) {
  33. $pub = $node->pub;
  34. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  35. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  36. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  37. $pub_id = $pub->pub_id;
  38. $title = $node->title;
  39. $volumetitle = $node->volumetitle;
  40. $volume = $node->volume;
  41. $series_name = $node->series_name;
  42. $issue = $node->issue;
  43. $pyear = $node->pyear;
  44. $pages = $node->pages;
  45. $miniref = $node->miniref;
  46. $uniquename = $node->uniquename;
  47. $type_id = $node->type_id;
  48. $is_obsolete = $node->is_obsolete;
  49. $publisher = $node->publisher;
  50. $pubplace = $node->pubplace;
  51. // if the obsolete value is set by the database then it is in the form of
  52. // 't' or 'f', we need to convert to 1 or 0
  53. $is_obsolete = $is_obsolete == 't' ? 1 : $is_obsolete;
  54. $is_obsolete = $is_obsolete == 'f' ? 0 : $is_obsolete;
  55. // set the organism_id in the form
  56. $form['pub_id'] = array(
  57. '#type' => 'value',
  58. '#value' => $pub->pub_id,
  59. );
  60. }
  61. // if we are re constructing the form from a failed validation or ajax callback
  62. // then use the $form_state['values'] values
  63. if (array_key_exists('values', $form_state)) {
  64. $title = $form_state['values']['pubtitle'];
  65. $volumetitle = $form_state['values']['volumetitle'];
  66. $volume = $form_state['values']['volume'];
  67. $series_name = $form_state['values']['series_name'];
  68. $issue = $form_state['values']['issue'];
  69. $pyear = $form_state['values']['pyear'];
  70. $pages = $form_state['values']['pages'];
  71. $miniref = $form_state['values']['miniref'];
  72. $uniquename = $form_state['values']['uniquename'];
  73. $type_id = $form_state['values']['type_id'];
  74. $is_obsolete = $form_state['values']['is_obsolete'];
  75. $publisher = $form_state['values']['publisher'];
  76. $pubplace = $form_state['values']['pubplace'];
  77. }
  78. // if we are re building the form from after submission (from ajax call) then
  79. // the values are in the $form_state['input'] array
  80. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  81. $title = $form_state['input']['pubtitle'];
  82. $volumetitle = $form_state['input']['volumetitle'];
  83. $volume = $form_state['input']['volume'];
  84. $series_name = $form_state['input']['series_name'];
  85. $issue = $form_state['input']['issue'];
  86. $pyear = $form_state['input']['pyear'];
  87. $pages = $form_state['input']['pages'];
  88. $miniref = $form_state['input']['miniref'];
  89. $uniquename = $form_state['input']['uniquename'];
  90. $type_id = $form_state['input']['type_id'];
  91. $is_obsolete = $form_state['input']['is_obsolete'];
  92. $publisher = $form_state['input']['publisher'];
  93. $pubplace = $form_state['input']['pubplace'];
  94. }
  95. // a drupal title can only be 255 characters, but the Chado title can be much longer.
  96. // we use the publication title as the drupal title, but we'll need to truncate it.
  97. $form['title'] = array(
  98. '#type' => 'hidden',
  99. '#value' => substr($title, 0, 255),
  100. );
  101. $form['pubtitle'] = array(
  102. '#type' => 'textarea',
  103. '#title' => t('Publication Title'),
  104. '#default_value' => $title,
  105. '#required' => TRUE,
  106. );
  107. // get the list of publication types. In the Tripal publication
  108. // ontologies these are all grouped under the term 'Publication Type'
  109. // we want the default to be 'Journal Article'
  110. $sql = "
  111. SELECT
  112. CVTS.cvterm_id, CVTS.name
  113. FROM {cvtermpath} CVTP
  114. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  115. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  116. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  117. WHERE
  118. CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
  119. NOT CVTS.is_obsolete = 1
  120. ORDER BY CVTS.name ASC
  121. ";
  122. $results = chado_query($sql);
  123. $pub_types = array();
  124. while ($pub_type = $results->fetchObject()) {
  125. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  126. // if we don't have a default type then set the default to be 'Journal Article'
  127. if (strcmp($pub_type->name,"Journal Article") == 0 and !$type_id) {
  128. $type_id = $pub_type->cvterm_id;
  129. }
  130. }
  131. $form['type_id'] = array(
  132. '#type' => 'select',
  133. '#title' => t('Publication Type'),
  134. '#options' => $pub_types,
  135. '#required' => TRUE,
  136. '#default_value' => $type_id,
  137. );
  138. $form['pyear'] = array(
  139. '#type' => 'textfield',
  140. '#title' => t('Publication Year'),
  141. '#default_value' => $pyear,
  142. '#required' => TRUE,
  143. '#size' => 5,
  144. '#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.'),
  145. );
  146. $form['uniquename'] = array(
  147. '#type' => 'textarea',
  148. '#title' => t('Citation'),
  149. '#default_value' => $uniquename,
  150. '#description' => t('All publications must have a unique citation.
  151. <b>Please enter the full citation for this publication or leave blank and one will be generated
  152. automatically if possible</b>. For PubMed style citations list
  153. the last name of the author followed by initials. Each author should be separated by a comma. Next comes
  154. 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.
  155. 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
  156. suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52.</pre>'),
  157. );
  158. $form['is_obsolete'] = array(
  159. '#type' => 'checkbox',
  160. '#title' => t('Is Obsolete? (Check for Yes)'),
  161. '#default_value' => $is_obsolete,
  162. );
  163. // get publication properties list
  164. $properties_select = array();
  165. $properties_select[] = 'Select a Property';
  166. $sql = "
  167. SELECT
  168. DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  169. FROM {cvtermpath} CVTP
  170. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  171. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  172. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  173. WHERE CV.name = 'tripal_pub' and
  174. (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
  175. NOT CVTS.is_obsolete = 1
  176. ORDER BY CVTS.name ASC
  177. ";
  178. $prop_types = chado_query($sql);
  179. while ($prop = $prop_types->fetchObject()) {
  180. // add all properties except the Citation. That property is set via the uniquename field
  181. if ($prop->name != 'Citation') {
  182. $properties[$prop->cvterm_id] = $prop->name;
  183. }
  184. }
  185. // add in the properties fields. The 'Citation' term is special because it serves
  186. // both as a property and as the uniquename for the publiation table so we exclude it
  187. // as it shouldn't be selected as a property
  188. $exclude = array("Citation");
  189. $instructions = '';
  190. tripal_core_properties_form($form, $form_state, 'pubprop', 'pub_id', 'tripal_pub',
  191. $properties, $pub_id, $exclude, $instructions);
  192. return $form;
  193. }
  194. /*
  195. *
  196. */
  197. function chado_pub_validate($node, $form, &$form_state) {
  198. // get the submitted values
  199. $title = trim($node->pubtitle);
  200. $pyear = trim($node->pyear);
  201. $uniquename = trim($node->uniquename);
  202. $is_obsolete = $node->is_obsolete;
  203. $type_id = $node->type_id;
  204. // if this is a delete then don't validate
  205. if($node->op == 'Delete') {
  206. return;
  207. }
  208. // we are syncing if we do not have a node ID but we do have a pub_id. We don't
  209. // need to validate during syncing so just skip it.
  210. if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
  211. return;
  212. }
  213. $pub = array();
  214. // make sure the year is four digits
  215. if(!preg_match('/^\d{4}$/', $pyear)){
  216. form_set_error('pyear', t('The publication year should be a 4 digit year.'));
  217. return;
  218. }
  219. // get the type of publication
  220. $values = array('cvterm_id' => $type_id);
  221. $options = array('statement_name' => 'sel_pub_ty');
  222. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values, $options);
  223. if (count($cvterm) == 0) {
  224. $message = t('Invalid publication type.');
  225. form_set_error('type_id', $message);
  226. return;
  227. }
  228. // get the media name looking at the properties
  229. $series_name = '';
  230. foreach ($node as $element => $value) {
  231. // if this is an existing property (either previously in the database or
  232. // added via AHAH/AJAX callback)
  233. if (preg_match('/^prop_value-(\d+)-(\d+)$/', $element, $matches)) {
  234. $prop_type_id = $matches[1];
  235. $prop_type = tripal_cv_get_cvterm_by_id($prop_type_id);
  236. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  237. $series_name = $value;
  238. }
  239. if($prop_type->name == 'Citation') {
  240. $uniquename = $value;
  241. }
  242. $pub[$prop_type->name] = $value;
  243. }
  244. // if this is a new property (added by this submit of the form)
  245. elseif ($element == 'new_id') {
  246. $prop_type = tripal_cv_get_cvterm_by_id($value);
  247. if($prop_type->name == 'Conference Name' or $prop_type->name == 'Journal Name') {
  248. $series_name = $node->new_value;
  249. }
  250. if($prop_type->name == 'Citation') {
  251. $uniquename = $node->new_value;
  252. }
  253. $pub[$prop_type->name] = $node->new_value;
  254. }
  255. }
  256. // if the citation is missing then try to generate one
  257. if (!$uniquename) {
  258. $pub['Title'] = $title;
  259. $pub['Publication Type'][0] = $cvterm[0]->name;
  260. $pub['Year'] = $pyear;
  261. $uniquename = tripal_pub_create_citation($pub);
  262. if (!$uniquename) {
  263. form_set_error('uniquename', "Cannot automatically generate a citation for this publication type. Please add one manually.");
  264. }
  265. }
  266. $skip_duplicate_check = 0;
  267. // if this publication is a Patent then skip the validation below. Patents can have the title
  268. // name and year but be different
  269. if (strcmp($cvterm[0]->name,'Patent') == 0) {
  270. $skip_duplicate_check = 1;
  271. }
  272. // Validating for an update
  273. if (!is_null($node->nid)) {
  274. $pub_id = $node->pub_id;
  275. // first get the original title, type and year before it was changed
  276. $values = array('pub_id' => $pub_id);
  277. $columns = array('title', 'pyear', 'type_id', 'series_name');
  278. $options = array('statement_name' => 'sel_pub_id');
  279. $pub = tripal_core_chado_select('pub', $columns, $values, $options);
  280. // if the title, type, year or series_name have changed then check the pub
  281. // to see if it is a duplicate of another
  282. if((strcmp(strtolower($pub[0]->title), strtolower($title)) == 0) and
  283. (strcmp(strtolower($pub[0]->series_name), strtolower($series_name)) == 0) and
  284. ($pub[0]->type_id == $type_id) and
  285. ($pub[0]->year == $pyear)) {
  286. $skip_duplicate_check = 1;
  287. }
  288. // check to see if a duplicate publication already exists
  289. if (!$skip_duplicate_check) {
  290. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id);
  291. }
  292. chado_pub_validate_check_uniquename($uniquename, $pub_id);
  293. }
  294. // Validating for an insert
  295. else {
  296. chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm);
  297. chado_pub_validate_check_uniquename($uniquename);
  298. }
  299. }
  300. /**
  301. *
  302. * @param unknown $uniquename
  303. */
  304. function chado_pub_validate_check_uniquename($uniquename, $pub_id = NULL) {
  305. $results = tripal_pub_get_pub_by_uniquename($uniquename);
  306. // make sure we don't capture our pub_id in the list (remove it)
  307. foreach ($results as $index => $found_pub_id) {
  308. if($found_pub_id == $pub_id){
  309. unset($results[$index]);
  310. }
  311. }
  312. if (count($results) > 0) {
  313. $message = t('A publication with this unique citation already exists.');
  314. form_set_error('uniquename', $message);
  315. }
  316. }
  317. /**
  318. *
  319. */
  320. function chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm, $pub_id = NULL) {
  321. // make sure the publication is unique using the prefereed import duplication check
  322. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  323. switch ($import_dups_check) {
  324. case 'title_year':
  325. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, NULL);
  326. // make sure we don't capture our pub_id in the list (remove it)
  327. foreach ($results as $index => $found_pub_id) {
  328. if($found_pub_id == $pub_id){
  329. unset($results[$index]);
  330. }
  331. }
  332. if (count($results) > 0) {
  333. $message = t('A publication with this title and publication year, already exists.');
  334. form_set_error('pyear', $message);
  335. }
  336. break;
  337. case 'title_year_type':
  338. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, $cvterm[0]->name, $pyear, NULL);
  339. // make sure we don't capture our pub_id in the list (remove it)
  340. foreach ($results as $index => $found_pub_id) {
  341. if($found_pub_id == $pub_id){
  342. unset($results[$index]);
  343. }
  344. }
  345. if (count($results) > 0) {
  346. $message = t('A publication with this title, type and publication year, already exists.');
  347. form_set_error('pyear', $message);
  348. }
  349. break;
  350. case 'title_year_media':
  351. $results = tripal_pub_get_pubs_by_title_type_pyear_series($title, NULL, $pyear, $series_name);
  352. // make sure we don't capture our pub_id in the list (remove it)
  353. foreach ($results as $index => $found_pub_id) {
  354. if($found_pub_id == $pub_id){
  355. unset($results[$index]);
  356. }
  357. }
  358. if (count($results) > 0) {
  359. $message = t('A publication with this title, media name (e.g. Journal Name) and publication year, already exists.');
  360. form_set_error('pyear', $message);
  361. }
  362. break;
  363. }
  364. }