Tripal Plant PopGen Submit
page_3.php
Go to the documentation of this file.
1 <?php
2 
8 require_once 'page_3_ajax.php';
9 require_once 'page_3_helper.php';
10 
24 function tpps_page_3_create_form(array &$form, array &$form_state) {
25 
26  if (isset($form_state['saved_values'][TPPS_PAGE_3])) {
27  $values = $form_state['saved_values'][TPPS_PAGE_3];
28  }
29  else {
30  $values = array();
31  }
32 
33  if ($form_state['saved_values'][TPPS_PAGE_2]['study_type'] != 1) {
34  tpps_study_location($form, $form_state);
35  }
36 
37  $form['existing_trees'] = array(
38  '#type' => 'checkbox',
39  '#title' => t('These plants may have been studied in the past'),
40  '#description' => t('If this box is checked, TPPS will try to find plants with matching ids around the same location as the ones you are providing. If it finds them successfully, it will mark them as the same plant in the database.'),
41  );
42 
43  if (tpps_access('administer tpps module')) {
44  $form['skip_validation'] = array(
45  '#type' => 'checkbox',
46  '#title' => t('Skip location validation (ignore location information)'),
47  );
48  }
49 
50  $form['tree-accession'] = array(
51  '#type' => 'fieldset',
52  '#title' => t('Plant Accession Information'),
53  '#tree' => TRUE,
54  '#prefix' => '<div id="tpps_accession">',
55  '#suffix' => '</div>',
56  );
57 
58  $species_number = $form_state['stats']['species_count'];
59 
60  if ($species_number > 1) {
61  // Create the single/multiple file checkbox.
62  $form['tree-accession']['check'] = array(
63  '#type' => 'checkbox',
64  '#title' => t('I would like to upload a separate plant accession file for each species.'),
65  '#ajax' => array(
66  'wrapper' => 'tpps_accession',
67  'callback' => 'tpps_accession_multi_file',
68  ),
69  );
70  }
71 
72  $file_description = "Please upload a spreadsheet file containing plant population data. When your file is uploaded, you will be shown a table with your column header names, several drop-downs, and the first few rows of your file. You will be asked to define the data type for each column, using the drop-downs provided to you. If a column data type does not fit any of the options in the drop-down menu, you may omit that drop-down menu. Your file must contain columns with information about at least the Plant Identifier and the Location of the plant (either gps coordinates or country/state).";
73  $file_upload_location = 'public://' . variable_get('tpps_accession_files_dir', 'tpps_accession');
74 
75  if ($form_state['saved_values'][TPPS_PAGE_2]['study_type'] == '4') {
76  $file_description .= ' Location columns should describe the location of the source plant for the Common Garden.';
77  }
78 
79  if ($species_number > 1) {
80  $file_description .= " If you are uploading a single file with multiple species, your file must also specify the genus and species of each plant.";
81  }
82 
83  $image_path = drupal_get_path('module', 'tpps') . '/images/';
84  $file_description .= "Please find an example of an accession file below.<figure><img src=\"/{$image_path}accession_example.png\"><figcaption>Example Accession File</figcaption></figure>";
85 
86  $check = tpps_get_ajax_value($form_state, array('tree-accession', 'check'), NULL);
87 
88  for ($i = 1; $i <= $species_number; $i++) {
89  $name = $form_state['saved_values'][TPPS_PAGE_1]['organism']["$i"]['name'];
90 
91  $column_options = array(
92  '0' => 'N/A',
93  '1' => 'Plant Identifier',
94  '2' => 'Country',
95  '3' => 'State',
96  '4' => 'Latitude',
97  '5' => 'Longitude',
98  '8' => 'County',
99  '9' => 'District',
100  '12' => 'Population Group',
101  '13' => 'Clone Number',
102  );
103 
104  $title = t("@name Accession File: *", array('@name' => $name)) . "<br>$file_description";
105  if ($species_number > 1 and !$check) {
106  $title = t('Plant Accession File: *') . "<br>$file_description";
107  $column_options['6'] = 'Genus';
108  $column_options['7'] = 'Species';
109  $column_options['10'] = 'Genus + Species';
110  }
111 
112  if ($form_state['saved_values'][TPPS_PAGE_2]['study_type'] != '1') {
113  $column_options['11'] = 'Source Plant Identifier';
114  }
115 
116  $form['tree-accession']["species-$i"] = array(
117  '#type' => 'fieldset',
118  '#collapsible' => TRUE,
119  '#states' => ($i > 1) ? array(
120  'visible' => array(
121  ':input[name="tree-accession[check]"]' => array('checked' => TRUE),
122  ),
123  ) : NULL,
124  );
125 
126  $form['tree-accession']["species-$i"]['file'] = array(
127  '#type' => 'managed_file',
128  '#title' => $title,
129  '#upload_location' => $file_upload_location,
130  '#upload_validators' => array(
131  'file_validate_extensions' => array('txt csv xlsx'),
132  ),
133  '#field_prefix' => '<span style="width: 100%;display: block;text-align: right;padding-right: 2%;">Allowed file extensions: txt csv xlsx</span>',
134  '#suffix' => '<style>figure {}</style>',
135  'empty' => array(
136  '#default_value' => isset($values['tree-accession']["species-$i"]['file']['empty']) ? $values['tree-accession']["species-$i"]['file']['empty'] : 'NA',
137  ),
138  'columns' => array(
139  '#description' => t('Please define which columns hold the required data: Plant Identifier and Location. If your plants are located based on a population group, you can provide the population group column and a mapping of population group to location below.'),
140  ),
141  'no-header' => array(),
142  'empty' => array(
143  '#default_value' => isset($values['tree-accession']["species-$i"]['file']['empty']) ? $values['tree-accession']["species-$i"]['file']['empty'] : 'NA',
144  ),
145  'columns-options' => array(
146  '#type' => 'hidden',
147  '#value' => $column_options,
148  ),
149  );
150 
151  $form['tree-accession']["species-$i"]['coord-format'] = array(
152  '#type' => 'select',
153  '#title' => t('Coordinate Projection'),
154  '#options' => array(
155  'WGS 84',
156  'NAD 83',
157  'ETRS 89',
158  'Other Coordinate Projection',
159  'My file does not use coordinates for plant locations',
160  ),
161  '#states' => $form['tree-accession']["species-$i"]['#states'] ?? NULL,
162  '#prefix' => "<div id=\"population-mapping-species-$i\">",
163  );
164 
165  $cols = tpps_get_ajax_value($form_state, array(
166  'tree-accession',
167  "species-$i",
168  'file',
169  'columns',
170  ), NULL, 'file');
171 
172  $fid = tpps_get_ajax_value($form_state, array(
173  'tree-accession',
174  "species-$i",
175  'file',
176  ), NULL);
177 
178  $skip = tpps_get_ajax_value($form_state, array(
179  'skip_validation',
180  ), NULL);
181 
182  if (!empty($fid) and empty($skip)) {
183  $wrapper_id = "{$fid}_map_wrapper";
184  $button_id = "{$fid}_map_button";
185  $form['tree-accession']["species-$i"]['coord-format']['#suffix'] = "<div id=\"$wrapper_id\"></div>"
186  . "<input id=\"$button_id\" type=\"button\" value=\"Click here to view plants on map!\" class=\"btn btn-primary map-button\"></input>";
187  $no_header = tpps_get_ajax_value($form_state, array(
188  'tree-accession',
189  "species-$i",
190  'file',
191  'no_header',
192  ), NULL, 'file');
193 
194  $id_col = $lat_col = $long_col = NULL;
195  foreach ($cols as $key => $col) {
196  if ($key[0] != '#') {
197  if ((is_array($col) and $col['#value'] == '1') or (!is_array($col) and $col == '1')) {
198  $id_col = $key;
199  }
200  if ((is_array($col) and $col['#value'] == '4') or (!is_array($col) and $col == '4')) {
201  $lat_col = $key;
202  }
203  if ((is_array($col) and $col['#value'] == '5') or (!is_array($col) and $col == '5')) {
204  $long_col = $key;
205  }
206  }
207  }
208 
209  drupal_add_js(array(
210  'tpps' => array(
211  'accession_files' => array(
212  $fid => array(
213  'no_header' => $no_header,
214  'id_col' => $id_col,
215  'lat_col' => $lat_col,
216  'long_col' => $long_col,
217  'fid' => $fid,
218  ),
219  ),
220  ),
221  ), 'setting');
222 
223  drupal_add_js(array(
224  'tpps' => array(
225  'map_buttons' => array(
226  $fid => array(
227  'wrapper' => $wrapper_id,
228  'button' => $button_id,
229  'fid' => $fid,
230  ),
231  ),
232  ),
233  ), 'setting');
234  }
235 
236  $form['tree-accession']["species-$i"]['pop-group'] = array(
237  '#type' => 'hidden',
238  '#title' => 'Population group mapping',
239  '#suffix' => '</div>',
240  '#tree' => TRUE,
241  );
242 
243  $pop_group_show = FALSE;
244  $found_lat = FALSE;
245  $found_lng = FALSE;
246 
247  if (!empty($cols)) {
248  foreach ($cols as $col_name => $data) {
249  if ($col_name[0] == '#') {
250  continue;
251  }
252  $val = $data;
253  $fid = $form_state['saved_values'][TPPS_PAGE_3]['tree-accession']["species-$i"]['file'] ?? NULL;
254  if (!empty($data['#value'])) {
255  $fid = $form_state['complete form']['tree-accession']["species-$i"]['file']['#value']['fid'];
256  $val = $data['#value'];
257  }
258  switch ($val) {
259  case '4':
260  $found_lat = TRUE;
261  break;
262 
263  case '5':
264  $found_lng = TRUE;
265  break;
266 
267  case '12':
268  $pop_group_show = TRUE;
269  $pop_col = $col_name;
270  break;
271 
272  default:
273  break;
274  }
275  }
276 
277  if ($pop_group_show and !empty($fid) and ($file = file_load($fid))) {
278  $form['tree-accession']["species-$i"]['pop-group']['#type'] = 'fieldset';
279  $pop_groups = array();
280  $options = array(
281  'columns' => array(
282  $pop_col,
283  ),
284  'pop_groups' => &$pop_groups,
285  );
286  tpps_file_iterator($fid, 'tpps_accession_pop_groups', $options);
287  foreach ($pop_groups as $pop_group) {
288  $form['tree-accession']["species-$i"]['pop-group'][$pop_group] = array(
289  '#type' => 'textfield',
290  '#title' => "Location for $name plants from group $pop_group:",
291  '#description' => t('The location for this population. This should be GPS coordinates if possbile, otherwise this can be the name of a location.'),
292  );
293  }
294  }
295 
296  if ($found_lat and $found_lng) {
297  unset($form['tree-accession']["species-$i"]['pop-group']['#suffix']);
298  $form['tree-accession']["species-$i"]['exact_coords'] = array(
299  '#type' => 'checkbox',
300  '#title' => t('The provided GPS coordinates are exact'),
301  '#default_value' => $form_state['saved_values'][TPPS_PAGE_3]['tree-accession']["species-$i"]['exact_coords'] ?? TRUE,
302  );
303 
304  $form['tree-accession']["species-$i"]['coord_precision'] = array(
305  '#type' => 'textfield',
306  '#title' => t('Coordinates accuracy:'),
307  '#description' => t('The precision of the provided coordinates. For example, if a plant could be up to 10m awa from the provided coordinates, then the accuracy would be "10m".'),
308  '#suffix' => '</div>',
309  '#states' => array(
310  'visible' => array(
311  ":input[name=\"tree-accession[species-$i][exact_coords]\"]" => array('checked' => FALSE),
312  ),
313  ),
314  );
315  }
316  }
317  }
318 
319  $map_api_key = variable_get('tpps_maps_api_key', NULL);
320  if (!empty($map_api_key)) {
321  $form['tree-accession']['#suffix'] .= '
322  <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
323  <script src="https://maps.googleapis.com/maps/api/js?key=' . $map_api_key . '&callback=initMap"
324  async defer></script>
325  <style>
326  #map_wrapper {
327  height: 450px;
328  }
329  </style>';
330  }
331 
332  $form['Back'] = array(
333  '#type' => 'submit',
334  '#value' => t('Back'),
335  '#prefix' => '<div class="input-description">* : Required Field</div>',
336  );
337 
338  $form['Save'] = array(
339  '#type' => 'submit',
340  '#value' => t('Save'),
341  );
342 
343  $form['Next'] = array(
344  '#type' => 'submit',
345  '#value' => t('Next'),
346  );
347 }
const TPPS_PAGE_1
Definition: tpps.module:12
tpps_get_ajax_value(array &$state, array $parents, $default=NULL, $file_name="")
Definition: form_utils.inc:236
tpps_page_3_create_form(array &$form, array &$form_state)
Definition: page_3.php:24
tpps_study_location(array &$form, array &$form_state)
tpps_access()
Definition: tpps.module:341
const TPPS_PAGE_3
Definition: tpps.module:14
const TPPS_PAGE_2
Definition: tpps.module:13
tpps_file_iterator($fid, $function, array &$options=array())