pub_form.inc 13 KB

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