pub_form.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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_node_form($form_state, $node) {
  7. tripal_core_ahah_init_form();
  8. $form = array();
  9. dpm($form_state);
  10. $type = node_get_types('type', $node);
  11. $pub = $node->pub;
  12. $pub_id = $pub->pub_id;
  13. $d_title = $form_state['values']['title'] ? $form_state['values']['title'] : $pub->title;
  14. $d_uniquename = $form_state['values']['uniquename'] ? $form_state['values']['uniquename'] : $pub->uniquename;
  15. $d_type_id = $form_state['values']['type_id'] ? $form_state['values']['type_id'] : $pub->type_id->cvterm_id;
  16. $d_volume = $form_state['values']['volume'] ? $form_state['values']['volume'] : $pub->volume;
  17. $d_volumetitle = $form_state['values']['volumetitle'] ? $form_state['values']['volumetitle'] : $pub->volumetitle;
  18. $d_series_name = $form_state['values']['series_name'] ? $form_state['values']['series_name'] : $pub->series_name;
  19. $d_issue = $form_state['values']['issue'] ? $form_state['values']['issue'] : $pub->issue;
  20. $d_pyear = $form_state['values']['pyear'] ? $form_state['values']['pyear'] : $pub->pyear;
  21. $d_pages = $form_state['values']['pages'] ? $form_state['values']['pages'] : $pub->pages;
  22. $d_miniref = $form_state['values']['miniref'] ? $form_state['values']['miniref'] : $pub->miniref;
  23. $d_publisher = $form_state['values']['publisher'] ? $form_state['values']['publisher'] : $pub->publisher;
  24. $d_pubplace = $form_state['values']['pubplace'] ? $form_state['values']['pubplace'] : $pub->pubplace;
  25. $d_is_obsolete = $form_state['values']['is_obsolete'] ? $form_state['values']['is_obsolete'] : $pub->is_obsolete;
  26. // on AHAH callbacks we want to keep a list of all the properties that have been removed
  27. // we'll store this info in a hidden field and retrieve it here
  28. $d_removed = $form_state['values']['removed'];
  29. // get the defaults first from the database and then from the form_state
  30. $default_type = $pub->type_id->cvterm_id;
  31. $form['pub_id'] = array(
  32. '#type' => 'hidden',
  33. '#value' => $pub_id,
  34. );
  35. // get the list of publication types. In the Tripal publication
  36. // ontologies these are all grouped under the term 'Publication Type'
  37. // we want the default to be 'Journal Article'
  38. $sql = "
  39. SELECT CVTS.cvterm_id, CVTS.name
  40. FROM {cvtermpath} CVTP
  41. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  42. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  43. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  44. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Type'
  45. ORDER BY CVTS.name ASC
  46. ";
  47. $results = chado_query($sql);
  48. $pub_types = array();
  49. while ($pub_type = db_fetch_object($results)) {
  50. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  51. // if we don't have a default type then set the default to be 'Journal Article'
  52. if (strcmp($pub_type->name,"Journal Article") == 0 and !$d_type_id) {
  53. $d_type_id = $pub_type->cvterm_id;
  54. }
  55. }
  56. $form['type_id'] = array(
  57. '#type' => 'select',
  58. '#title' => t('Publication Type'),
  59. '#options' => $pub_types,
  60. '#required' => TRUE,
  61. '#default_value' => $d_type_id,
  62. );
  63. // Article Title.
  64. $form['title'] = array(
  65. '#type' => 'textfield',
  66. '#title' => check_plain($type->title_label),
  67. '#default_value' => $d_title,
  68. '#required' => TRUE,
  69. );
  70. $form['series_name'] = array(
  71. '#type' => 'textfield',
  72. '#title' => t('Series Name (e.g. Journal Name)'),
  73. '#description' => t('Full name of (journal) series.'),
  74. '#default_value' => $d_series_name,
  75. '#required' => TRUE,
  76. );
  77. $form['pyear'] = array(
  78. '#type' => 'textfield',
  79. '#title' => t('Publication Year'),
  80. '#default_value' => $d_pyear,
  81. '#required' => TRUE,
  82. );
  83. $form['uniquename'] = array(
  84. '#type' => 'textarea',
  85. '#title' => t('Citation'),
  86. '#default_value' => $d_uniquename,
  87. '#description' => t('All publications must have a unique citation. Please enter the full citation for this publication.
  88. For PubMed style citations list
  89. the last name of the author followed by initials. Each author should be separated by a comma. Next comes
  90. the title, followed by the series title (e.g. journal name), publication date (3 character Month, day, 4
  91. digit year), volume, issue and page numbers. You may also use HTML to provide a link in the citation.
  92. 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
  93. suffer taxonomic influence?</a> J Ethnopharmacol. 2013 Apr 19; 146(3):842-52. PubMed PMID: 23462414</pre>'),
  94. '#required' => TRUE,
  95. );
  96. // get publication properties list and create the array that will be used for selecting a property type
  97. $sql = "
  98. SELECT CVTS.cvterm_id, CVTS.name
  99. FROM {cvtermpath} CVTP
  100. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  101. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  102. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  103. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details'
  104. ORDER BY CVTS.name ASC
  105. ";
  106. $prop_types = chado_query($sql);
  107. $num_properties = 0;
  108. $d_properties = array();
  109. $properties = array();
  110. $removed = array();
  111. $properties[] = 'Select a Property';
  112. while ($prop = db_fetch_object($prop_types)) {
  113. $properties[$prop->cvterm_id] = $prop->name;
  114. // skip any properties that the user requested to delete through a previous
  115. // AHAH callback or through the current AHAH callback
  116. if($d_removed[$prop->cvterm_id . '-0']) {
  117. continue;
  118. }
  119. if($form_state['post']['remove-' . $prop->cvterm_id . '-0']) {
  120. $d_removed[$prop->cvterm_id . '-0'] = 1;
  121. continue;
  122. }
  123. // if any of the properties match the fields in the pub table then we want to include those
  124. // automatically
  125. if($prop->name == 'Volume' and $d_volume) {
  126. $d_properties[$prop->cvterm_id][0]['name'] = $prop->name;
  127. $d_properties[$prop->cvterm_id][0]['id'] = $prop->cvterm_id;
  128. $d_properties[$prop->cvterm_id][0]['value'] = $d_volume;
  129. $num_properties++;
  130. }
  131. if($prop->name == 'Volume Title' and $d_volumetitle) {
  132. $d_properties[$prop->cvterm_id][0]['name'] = $prop->name;
  133. $d_properties[$prop->cvterm_id][0]['id'] = $prop->cvterm_id;
  134. $d_properties[$prop->cvterm_id][0]['value'] = $d_volumetitle;
  135. $num_properties++;
  136. }
  137. if($prop->name == 'Issue' and $d_issue) {
  138. $d_properties[$prop->cvterm_id][0]['name'] = $prop->name;
  139. $d_properties[$prop->cvterm_id][0]['id'] = $prop->cvterm_id;
  140. $d_properties[$prop->cvterm_id][0]['value'] = $d_issue;
  141. $num_properties++;
  142. }
  143. if($prop->name == 'Pages' and $d_pages) {
  144. $d_properties[$prop->cvterm_id][0]['name'] = $prop->name;
  145. $d_properties[$prop->cvterm_id][0]['id'] = $prop->cvterm_id;
  146. $d_properties[$prop->cvterm_id][0]['value'] = $d_pages;
  147. $num_properties++;
  148. }
  149. }
  150. // get the properties for this publication
  151. if($pub_id) {
  152. $sql = "
  153. SELECT CVT.cvterm_id, CVT.name, PP.value, PP.rank
  154. FROM {pubprop} PP
  155. INNER JOIN {cvterm} CVT on CVT.cvterm_id = PP.type_id
  156. WHERE PP.pub_id = %d
  157. ORDER BY CVT.name, PP.rank
  158. ";
  159. $pub_props = chado_query($sql, $pub_id);
  160. while ($prop = db_fetch_object($pub_props)) {
  161. // skip properties that were handled above
  162. if($prop->name == "Volume" or $prop->name == "Volume Title" or
  163. $prop->name == "Issue" or $prop->name == "Pages" or
  164. $prop->name == "Citation") {
  165. continue;
  166. }
  167. // skip any properties that the user requested to delete through a previous
  168. // AHAH callback or through the current AHAH callback
  169. if($d_removed[$prop->cvterm_id . '-' . $prop->rank]) {
  170. continue;
  171. }
  172. if($form_state['post']['remove-' . $prop->cvterm_id . '-' . $prop->rank]) {
  173. $d_removed[$prop->cvterm_id . '-0'] = 1;
  174. continue;
  175. }
  176. // add new properties that weren't handled yet
  177. if(array_key_exists($prop->cvterm_id, $properties)) {
  178. $d_properties[$prop->cvterm_id][$prop->rank]['name'] = $prop->name;
  179. $d_properties[$prop->cvterm_id][$prop->rank]['id'] = $prop->cvterm_id;
  180. $d_properties[$prop->cvterm_id][$prop->rank]['value'] = $prop->value;
  181. $num_properties++;
  182. }
  183. }
  184. }
  185. $form['removed'] = array(
  186. '#type' => 'hidden',
  187. '#value' => $d_removed,
  188. );
  189. // build the fields for the properties
  190. $i = 0;
  191. foreach ($d_properties as $type_id => $ranks) {
  192. foreach ($ranks as $rank => $d_property) {
  193. $form['properties'][$type_id][$rank]["prop_id-$type_id-$rank"] = array(
  194. '#type' => 'select',
  195. '#options' => $properties,
  196. '#default_value' => $d_property['id']
  197. );
  198. $rows = 2;
  199. if (preg_match('/Abstract/', $d_property['name'])) {
  200. $rows = 10;
  201. }
  202. $form['properties'][$type_id][$rank]["prop_value-$type_id-$rank"] = array(
  203. '#type' => 'textarea',
  204. '#default_value' => $d_property['value'],
  205. '#cols' => 20,
  206. '#rows' => $rows
  207. );
  208. $form['properties'][$type_id][$rank]["remove-$type_id-$rank"] = array(
  209. '#type' => 'image_button',
  210. '#value' => t('Remove'),
  211. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  212. '#ahah' => array(
  213. 'path' => "tripal_pub/properties/minus/$type_id/$rank",
  214. 'wrapper' => 'chado-pub-details',
  215. 'event' => 'click',
  216. 'method' => 'replace',
  217. ),
  218. '#attributes' => array('onClick' => 'return false;'),
  219. );
  220. $i++;
  221. }
  222. }
  223. // add one more blank field
  224. $form['properties']['new_id'] = array(
  225. '#type' => 'select',
  226. '#options' => $properties,
  227. );
  228. $form['properties']['new_value'] = array(
  229. '#type' => 'textarea',
  230. '#default_value' => '',
  231. '#cols' => 5,
  232. '#rows' => $rows
  233. );
  234. $form['properties']["add"] = array(
  235. '#type' => 'image_button',
  236. '#value' => t('Add'),
  237. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  238. '#ahah' => array(
  239. 'path' => "tripal_pub/properties/add",
  240. 'wrapper' => 'chado-pub-details',
  241. 'event' => 'click',
  242. 'method' => 'replace',
  243. ),
  244. '#attributes' => array('onClick' => 'return false;'),
  245. );
  246. /*
  247. $form['volume'] = array(
  248. '#type' => 'textfield',
  249. '#title' => t('Volume'),
  250. '#default_value' => $d_volume
  251. );
  252. $form['issue'] = array(
  253. '#type' => 'textfield',
  254. '#title' => t('Issue'),
  255. '#default_value' => $d_issue
  256. );
  257. $form['pages'] = array(
  258. '#type' => 'textfield',
  259. '#title' => t('Pages'),
  260. '#description' => t('Page number range[s], e.g. 457--459, viii + 664pp, lv--lvii.'),
  261. '#default_value' => $d_pages
  262. );
  263. $form['volumetitle'] = array(
  264. '#type' => 'textfield',
  265. '#title' => t('Volume Title'),
  266. '#description' => t('Title of part if one of a series.'),
  267. '#default_value' => $d_volumetitle
  268. );
  269. $form['miniref'] = array(
  270. '#type' => 'textfield',
  271. '#title' => t('Mini-Ref'),
  272. '#required' => FALSE,
  273. '#default_value' => $d_miniref
  274. );
  275. $form['publisher'] = array(
  276. '#type' => 'textfield',
  277. '#title' => t('Publisher Name'),
  278. '#required' => FALSE,
  279. '#default_value' => $d_publisher
  280. );
  281. $form['pubplace'] = array(
  282. '#type' => 'textfield',
  283. '#title' => t('Place of Publication'),
  284. '#required' => FALSE,
  285. '#default_value' => $d_pubplace
  286. );
  287. */
  288. $form['is_obsolete'] = array(
  289. '#type' => 'checkbox',
  290. '#title' => t('Is Obsolete? (Check for Yes)'),
  291. '#required' => TRUE,
  292. '#default_value' => $d_isobsolete
  293. );
  294. return $form;
  295. }
  296. /*
  297. *
  298. */
  299. function theme_chado_pub_node_form($form) {
  300. $rows = array();
  301. if ($form['properties']) {
  302. foreach ($form['properties'] as $i => $ranks) {
  303. if (is_numeric($i)) {
  304. foreach ($ranks as $rank => $elements) {
  305. if (is_numeric($rank)) {
  306. $rows[] = array(
  307. array('data' => drupal_render($elements["prop_id-$i-$rank"]), 'width' => '20%'),
  308. drupal_render($elements["prop_value-$i-$rank"]),
  309. array('data' => drupal_render($elements["remove-$i-$rank"]), 'width' => '5%'),
  310. );
  311. }
  312. }
  313. }
  314. }
  315. $rows[] = array(
  316. array('data' => drupal_render($form['properties']['new_id']), 'width' => '20%'),
  317. drupal_render($form['properties']['new_value']),
  318. array('data' => drupal_render($form['properties']['add']), 'width' => '5%'),
  319. );
  320. }
  321. $headers = array('Property Type','Value', '');
  322. $markup = '<div id="chado-pub-details">';
  323. $markup .= drupal_render($form['pub_id']);
  324. $markup .= drupal_render($form['title']);
  325. $markup .= drupal_render($form['type_id']);
  326. $markup .= drupal_render($form['series_name']);
  327. $markup .= drupal_render($form['pyear']);
  328. $markup .= drupal_render($form['uniquename']);
  329. $markup .= "<b>Include Additional Details</b>";
  330. $markup .= theme('table', $headers, $rows);
  331. $markup .= "</div>";
  332. $form['properties'] = array(
  333. '#type' => 'markup',
  334. '#value' => $markup,
  335. );
  336. return drupal_render($form);
  337. }
  338. /*
  339. *
  340. */
  341. function tripal_pub_property_add() {
  342. $status = TRUE;
  343. // prepare and render the form
  344. $form = tripal_core_ahah_prepare_form();
  345. $data = theme('chado_pub_node_form', $form);
  346. // bind javascript events to the new objects that will be returned
  347. // so that AHAH enabled elements will work.
  348. $settings = tripal_core_ahah_bind_events();
  349. // return the updated JSON
  350. drupal_json(
  351. array(
  352. 'status' => $status,
  353. 'data' => $data,
  354. 'settings' => $settings,
  355. )
  356. );
  357. }
  358. /*
  359. *
  360. */
  361. function tripal_pub_property_delete() {
  362. $status = TRUE;
  363. // prepare and render the form
  364. $form = tripal_core_ahah_prepare_form();
  365. $data = theme('chado_pub_node_form', $form);
  366. // bind javascript events to the new objects that will be returned
  367. // so that AHAH enabled elements will work.
  368. $settings = tripal_core_ahah_bind_events();
  369. // return the updated JSON
  370. drupal_json(
  371. array(
  372. 'status' => $status,
  373. 'data' => $data,
  374. 'settings' => $settings,
  375. )
  376. );
  377. }